NaturesAura/src/main/java/de/ellpeck/naturesaura/items/tools/ItemHoe.java

115 lines
5 KiB
Java
Raw Normal View History

2019-11-04 19:08:49 +01:00
package de.ellpeck.naturesaura.items.tools;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.ItemModelGenerator;
2019-11-04 19:08:49 +01:00
import de.ellpeck.naturesaura.items.ModItems;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.reg.ICustomItemModel;
2019-11-04 19:08:49 +01:00
import de.ellpeck.naturesaura.reg.IModItem;
2020-01-22 01:32:26 +01:00
import de.ellpeck.naturesaura.reg.ModRegistry;
2021-12-05 23:32:31 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.HoeItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.context.UseOnContext;
2023-02-21 12:09:31 +01:00
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
2021-12-05 23:32:31 +01:00
import net.minecraft.world.level.block.BushBlock;
2023-02-21 12:09:31 +01:00
import net.minecraft.world.level.block.LeavesBlock;
2021-12-05 23:32:31 +01:00
import net.minecraft.world.phys.BlockHitResult;
2019-11-04 19:08:49 +01:00
2020-01-29 00:40:28 +01:00
public class ItemHoe extends HoeItem implements IModItem, ICustomItemModel {
2019-11-04 19:08:49 +01:00
private final String baseName;
2021-12-05 23:32:31 +01:00
public ItemHoe(String baseName, Tier material, int speed) {
2023-07-08 12:32:27 +02:00
super(material, speed, 0, new Properties());
2019-11-04 19:08:49 +01:00
this.baseName = baseName;
2022-06-27 15:24:04 +02:00
ModRegistry.ALL_ITEMS.add(this);
2019-11-04 19:08:49 +01:00
}
@Override
2021-12-05 23:32:31 +01:00
public InteractionResult useOn(UseOnContext context) {
2020-05-13 15:52:46 +02:00
if (this == ModItems.INFUSED_IRON_HOE) {
2021-12-15 16:30:22 +01:00
var level = context.getLevel();
var result = super.useOn(context);
2021-12-05 23:32:31 +01:00
if (!level.isClientSide && result.consumesAction()) {
2021-12-15 16:30:22 +01:00
var seed = ItemStack.EMPTY;
var random = level.getRandom();
var pos = context.getClickedPos();
2020-05-13 15:52:46 +02:00
if (random.nextInt(5) == 0) {
seed = new ItemStack(Items.WHEAT_SEEDS);
} else if (random.nextInt(10) == 0) {
2021-12-15 16:30:22 +01:00
var rand = random.nextInt(3);
2020-05-13 15:52:46 +02:00
if (rand == 0) {
seed = new ItemStack(Items.MELON_SEEDS);
} else if (rand == 1) {
seed = new ItemStack(Items.PUMPKIN_SEEDS);
} else if (rand == 2) {
seed = new ItemStack(Items.BEETROOT_SEEDS);
}
}
if (!seed.isEmpty()) {
2021-12-15 16:30:22 +01:00
var item = new ItemEntity(level, pos.getX() + random.nextFloat(), pos.getY() + 1F, pos.getZ() + random.nextFloat(), seed);
2021-12-05 23:32:31 +01:00
level.addFreshEntity(item);
2020-05-13 15:52:46 +02:00
}
}
return result;
} else if (this == ModItems.SKY_HOE) {
2021-12-15 16:30:22 +01:00
var success = false;
for (var x = -1; x <= 1; x++) {
for (var z = -1; z <= 1; z++) {
var offset = context.getClickedPos().offset(x, 0, z);
var newResult = new BlockHitResult(context.getClickLocation(), context.getClickedFace(), offset, context.isInside());
var newContext = new UseOnContext(context.getPlayer(), context.getHand(), newResult);
2023-02-21 12:19:23 +01:00
success |= super.useOn(newContext).consumesAction();
2019-11-04 19:08:49 +01:00
}
}
2021-12-04 15:40:09 +01:00
return success ? InteractionResult.SUCCESS : InteractionResult.PASS;
2020-05-13 15:52:46 +02:00
}
2021-12-05 23:32:31 +01:00
return super.useOn(context);
2020-05-13 15:52:46 +02:00
}
2019-11-04 19:08:49 +01:00
2020-05-13 15:52:46 +02:00
@Override
2021-12-04 15:40:09 +01:00
public boolean onBlockStartBreak(ItemStack stack, BlockPos pos, Player player) {
2023-02-21 12:09:31 +01:00
if (!player.isShiftKeyDown() && (stack.getItem() == ModItems.SKY_HOE || stack.getItem() == ModItems.DEPTH_HOE)) {
2023-07-08 12:32:27 +02:00
var block = player.level().getBlockState(pos).getBlock();
2023-02-21 12:09:31 +01:00
if (!(block instanceof BushBlock) && (stack.getItem() != ModItems.DEPTH_HOE || !(block instanceof LeavesBlock)))
2020-05-13 15:52:46 +02:00
return false;
2023-07-08 12:32:27 +02:00
if (!player.level().isClientSide) {
2021-12-15 16:30:22 +01:00
var range = 3;
for (var x = -range; x <= range; x++) {
for (var y = -range; y <= range; y++) {
for (var z = -range; z <= range; z++) {
2020-05-13 15:52:46 +02:00
if (x == 0 && y == 0 && z == 0)
continue;
2021-12-15 16:30:22 +01:00
var offset = pos.offset(x, y, z);
2023-07-08 12:32:27 +02:00
var offState = player.level().getBlockState(offset);
2023-02-21 12:09:31 +01:00
if (offState.getBlock() instanceof BushBlock || stack.getItem() == ModItems.DEPTH_HOE && offState.getBlock() instanceof LeavesBlock) {
2023-07-08 12:32:27 +02:00
var entity = offState.hasBlockEntity() ? player.level().getBlockEntity(offset) : null;
Block.dropResources(offState, player.level(), offset, entity, null, stack);
2023-07-08 12:32:27 +02:00
player.level().setBlock(offset, Blocks.AIR.defaultBlockState(), 3);
2023-02-21 12:09:31 +01:00
}
2020-05-13 15:52:46 +02:00
}
}
}
2019-11-04 19:08:49 +01:00
}
}
2020-05-13 15:52:46 +02:00
return false;
2019-11-04 19:08:49 +01:00
}
@Override
public String getBaseName() {
return this.baseName;
}
2020-01-29 00:40:28 +01:00
@Override
public void generateCustomItemModel(ItemModelGenerator generator) {
generator.withExistingParent(this.getBaseName(), "item/handheld").texture("layer0", "item/" + this.getBaseName());
}
2019-11-04 19:08:49 +01:00
}