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

123 lines
5 KiB
Java
Raw Normal View History

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.Level;
import net.minecraft.world.level.block.BushBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
2019-11-04 19:08:49 +01:00
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import javax.annotation.Nullable;
import java.util.Random;
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;
2020-01-22 01:32:26 +01:00
ModRegistry.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-04 15:40:09 +01:00
Level level = context.getLevel();
2021-12-05 23:32:31 +01:00
InteractionResult result = super.useOn(context);
if (!level.isClientSide && result.consumesAction()) {
2020-05-13 15:52:46 +02:00
ItemStack seed = ItemStack.EMPTY;
2021-12-04 15:40:09 +01:00
Random random = level.getRandom();
2021-12-05 23:32:31 +01:00
BlockPos 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) {
int rand = random.nextInt(3);
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-04 15:40:09 +01:00
ItemEntity 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) {
boolean success = false;
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
2021-12-05 23:32:31 +01:00
BlockPos offset = context.getClickedPos().offset(x, 0, z);
BlockHitResult newResult = new BlockHitResult(context.getClickLocation(), context.getClickedFace(), offset, context.isInside());
UseOnContext newContext = new UseOnContext(context.getPlayer(), context.getHand(), newResult);
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) {
2020-05-13 15:52:46 +02:00
int range = 3;
for (int x = -range; x <= range; x++) {
for (int y = -range; y <= range; y++) {
for (int z = -range; z <= range; z++) {
if (x == 0 && y == 0 && z == 0)
continue;
2021-12-05 23:32:31 +01:00
BlockPos offset = pos.offset(x, y, z);
2021-12-04 15:40:09 +01:00
BlockState 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
}