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

117 lines
5.3 KiB
Java
Raw Normal View History

2018-10-19 18:32:20 +02:00
package de.ellpeck.naturesaura.items.tools;
2018-10-20 21:19:08 +02:00
import de.ellpeck.naturesaura.Helper;
2019-11-04 19:08:49 +01:00
import de.ellpeck.naturesaura.NaturesAura;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.data.ItemModelGenerator;
2018-10-20 00:54:33 +02:00
import de.ellpeck.naturesaura.items.ModItems;
2020-01-29 00:40:28 +01:00
import de.ellpeck.naturesaura.reg.ICustomItemModel;
2018-10-19 18:32:20 +02:00
import de.ellpeck.naturesaura.reg.IModItem;
2020-01-22 01:32:26 +01:00
import de.ellpeck.naturesaura.reg.ModRegistry;
2021-12-04 19:17:21 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-04 19:17:21 +01:00
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ShovelItem;
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.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
2018-10-20 21:19:08 +02:00
import net.minecraftforge.common.capabilities.ICapabilityProvider;
2018-10-19 18:32:20 +02:00
2018-10-20 21:19:08 +02:00
import javax.annotation.Nullable;
2020-01-29 00:40:28 +01:00
public class ItemShovel extends ShovelItem implements IModItem, ICustomItemModel {
2021-12-04 19:17:21 +01:00
2018-10-19 18:32:20 +02:00
private final String baseName;
2021-12-04 19:17:21 +01:00
public ItemShovel(String baseName, Tier material, float damage, float speed) {
super(material, damage, speed, new Properties().tab(NaturesAura.CREATIVE_TAB));
2018-10-19 18:32:20 +02:00
this.baseName = baseName;
2020-01-22 01:32:26 +01:00
ModRegistry.add(this);
2018-10-19 18:32:20 +02:00
}
2018-10-20 00:54:33 +02:00
@Override
2021-12-04 19:17:21 +01:00
public InteractionResult useOn(UseOnContext context) {
2021-12-04 15:40:09 +01:00
Level level = context.getLevel();
Player player = context.getPlayer();
2021-12-04 19:17:21 +01:00
ItemStack stack = player.getItemInHand(context.getHand());
BlockPos pos = context.getClickedPos();
2021-12-04 15:40:09 +01:00
BlockState state = level.getBlockState(pos);
2020-01-23 19:20:47 +01:00
if (this == ModItems.INFUSED_IRON_SHOVEL) {
int damage = 0;
2020-10-13 23:37:58 +02:00
if (state.getBlock() == Blocks.DIRT || state.getBlock() == Blocks.MYCELIUM) {
2021-12-04 19:17:21 +01:00
if (level.getBlockState(pos.above()).getMaterial() == Material.AIR) {
level.setBlockAndUpdate(pos, Blocks.GRASS_BLOCK.defaultBlockState());
damage = 5;
}
} else {
2021-12-04 19:17:21 +01:00
int range = player.isCrouching() ? 0 : 1;
for (int x = -range; x <= range; x++) {
for (int y = -range; y <= range; y++) {
2021-12-04 19:17:21 +01:00
BlockPos actualPos = pos.offset(x, 0, y);
Direction facing = context.getClickedFace();
if (player.mayUseItemAt(actualPos.relative(facing), facing, stack)) {
2019-10-20 22:30:49 +02:00
if (facing != Direction.DOWN
2021-12-04 19:17:21 +01:00
&& level.getBlockState(actualPos.above()).getMaterial() == Material.AIR
2021-12-04 15:40:09 +01:00
&& level.getBlockState(actualPos).getBlock() == Blocks.GRASS_BLOCK) {
2021-12-04 19:17:21 +01:00
if (!level.isClientSide)
level.setBlock(actualPos, Blocks.DIRT_PATH.defaultBlockState(), 11);
damage = 1;
2018-10-20 00:54:33 +02:00
}
}
}
}
}
if (damage > 0) {
2021-12-04 19:17:21 +01:00
level.playSound(player, pos, SoundEvents.SHOVEL_FLATTEN, SoundSource.BLOCKS, 1.0F, 1.0F);
stack.hurtAndBreak(damage, player, p -> p.broadcastBreakEvent(context.getHand()));
2021-12-04 15:40:09 +01:00
return InteractionResult.SUCCESS;
2018-10-20 00:54:33 +02:00
}
2020-05-13 17:57:31 +02:00
} else if (this == ModItems.SKY_SHOVEL) {
if (this.getDestroySpeed(stack, state) <= 1)
2021-12-04 19:17:21 +01:00
return super.useOn(context);
InteractionHand otherHand = context.getHand() == InteractionHand.MAIN_HAND ? InteractionHand.OFF_HAND : InteractionHand.MAIN_HAND;
ItemStack other = player.getItemInHand(otherHand);
2020-05-13 17:57:31 +02:00
if (other.isEmpty() || !(other.getItem() instanceof BlockItem))
2021-12-04 19:17:21 +01:00
return super.useOn(context);
2021-12-04 15:40:09 +01:00
level.removeBlock(pos, false);
BlockEntity tile = state.hasBlockEntity() ? level.getBlockEntity(pos) : null;
2021-12-04 19:17:21 +01:00
Block.dropResources(state, level, pos, tile, null, ItemStack.EMPTY);
UseOnContext newContext = new UseOnContext(player, otherHand, new BlockHitResult(context.getClickLocation(), context.getClickedFace(), context.getClickedPos(), context.isInside()));
other.useOn(newContext);
stack.hurtAndBreak(1, player, p -> p.broadcastBreakEvent(context.getHand()));
2021-12-04 15:40:09 +01:00
return InteractionResult.SUCCESS;
2018-10-20 00:54:33 +02:00
}
2021-12-04 15:40:09 +01:00
return InteractionResult.PASS;
2018-10-20 00:54:33 +02:00
}
2018-10-19 18:32:20 +02:00
@Override
public String getBaseName() {
return this.baseName;
}
2018-10-20 21:19:08 +02:00
@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);
2018-10-20 21:19:08 +02: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());
}
2018-10-19 18:32:20 +02:00
}