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

98 lines
3.9 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;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
2019-11-04 19:08:49 +01:00
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.IItemTier;
2018-10-20 00:54:33 +02:00
import net.minecraft.item.ItemStack;
2019-11-04 19:08:49 +01:00
import net.minecraft.item.ItemUseContext;
import net.minecraft.item.ShovelItem;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
2018-10-20 00:54:33 +02:00
import net.minecraft.util.SoundCategory;
2019-11-04 19:08:49 +01:00
import net.minecraft.util.SoundEvents;
2018-10-20 00:54:33 +02:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
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 {
2018-10-19 18:32:20 +02:00
private final String baseName;
2020-01-26 01:41:49 +01:00
public ItemShovel(String baseName, IItemTier material, float damage, float speed) {
2019-11-04 19:08:49 +01:00
super(material, damage, speed, new Properties().group(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
2019-11-04 19:08:49 +01:00
public ActionResultType onItemUse(ItemUseContext context) {
2020-01-23 19:20:47 +01:00
if (this == ModItems.INFUSED_IRON_SHOVEL) {
2019-11-04 19:08:49 +01:00
PlayerEntity player = context.getPlayer();
World world = context.getWorld();
BlockPos pos = context.getPos();
ItemStack stack = player.getHeldItem(context.getHand());
BlockState state = world.getBlockState(pos);
int damage = 0;
2019-11-04 19:08:49 +01:00
if (state.getBlock() == Blocks.DIRT) {
if (world.getBlockState(pos.up()).getMaterial() == Material.AIR) {
world.setBlockState(pos, Blocks.GRASS.getDefaultState());
damage = 5;
}
} else {
2020-01-28 18:08:56 +01:00
int range = player.isShiftKeyDown() ? 0 : 1;
for (int x = -range; x <= range; x++) {
for (int y = -range; y <= range; y++) {
BlockPos actualPos = pos.add(x, 0, y);
2019-11-04 19:08:49 +01:00
Direction facing = context.getFace();
if (player.canPlayerEdit(actualPos.offset(facing), facing, stack)) {
2019-10-20 22:30:49 +02:00
if (facing != Direction.DOWN
2019-11-04 19:08:49 +01:00
&& world.getBlockState(actualPos.up()).getMaterial() == Material.AIR
&& world.getBlockState(actualPos).getBlock() == Blocks.GRASS_BLOCK) {
2019-11-04 19:08:49 +01:00
if (!world.isRemote) {
world.setBlockState(actualPos, Blocks.GRASS_PATH.getDefaultState(), 11);
}
damage = 1;
2018-10-20 00:54:33 +02:00
}
}
}
}
}
if (damage > 0) {
2019-11-04 19:08:49 +01:00
world.playSound(player, pos, SoundEvents.ITEM_SHOVEL_FLATTEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
stack.damageItem(damage, player, playerEntity -> playerEntity.sendBreakAnimation(context.getHand()));
2019-10-20 22:30:49 +02:00
return ActionResultType.SUCCESS;
2018-10-20 00:54:33 +02:00
}
}
2019-10-20 22:30:49 +02:00
return ActionResultType.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
2019-10-20 22:30:49 +02:00
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT 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
}