2019-11-04 19:08:49 +01:00
|
|
|
package de.ellpeck.naturesaura.items.tools;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.Helper;
|
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
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;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-05 23:32:31 +01:00
|
|
|
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;
|
|
|
|
import net.minecraft.world.level.block.BushBlock;
|
|
|
|
import net.minecraft.world.phys.BlockHitResult;
|
2019-11-04 19:08:49 +01:00
|
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
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) {
|
|
|
|
super(material, speed, 0, new Properties().tab(NaturesAura.CREATIVE_TAB));
|
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);
|
2021-12-05 23:32:31 +01:00
|
|
|
success |= super.useOn(newContext) == InteractionResult.SUCCESS;
|
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) {
|
2020-05-13 15:52:46 +02:00
|
|
|
if (stack.getItem() == ModItems.SKY_HOE) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!(player.level.getBlockState(pos).getBlock() instanceof BushBlock))
|
2020-05-13 15:52:46 +02:00
|
|
|
return false;
|
2021-12-04 15:40:09 +01: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);
|
|
|
|
var otherState = player.level.getBlockState(offset);
|
2020-05-13 15:52:46 +02:00
|
|
|
if (otherState.getBlock() instanceof BushBlock)
|
2021-12-04 15:40:09 +01:00
|
|
|
player.level.destroyBlock(offset, true);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
|
2020-05-13 15:52:46 +02:00
|
|
|
return Helper.makeRechargeProvider(stack, true);
|
2019-11-04 19:08:49 +01:00
|
|
|
}
|
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
|
|
|
}
|