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

107 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;
2018-10-20 00:54:33 +02:00
import de.ellpeck.naturesaura.items.ModItems;
2018-11-29 17:58:47 +01:00
import de.ellpeck.naturesaura.reg.ICreativeItem;
2018-10-19 18:32:20 +02:00
import de.ellpeck.naturesaura.reg.IModItem;
import de.ellpeck.naturesaura.reg.IModelProvider;
import de.ellpeck.naturesaura.reg.ModRegistry;
import net.minecraft.block.BlockDirt;
2018-10-20 00:54:33 +02:00
import net.minecraft.block.material.Material;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.block.Blocks;
import net.minecraft.util.SoundEvents;
import net.minecraft.item.ShovelItem;
2018-10-20 00:54:33 +02:00
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
2018-10-20 00:54:33 +02:00
import net.minecraft.util.SoundCategory;
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
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
2018-10-20 21:19:08 +02:00
import javax.annotation.Nullable;
2019-10-20 22:30:49 +02:00
public class ItemShovelNA extends ShovelItem implements IModItem, ICreativeItem, IModelProvider {
2018-10-19 18:32:20 +02:00
private final String baseName;
public ItemShovelNA(String baseName, ToolMaterial material) {
super(material);
this.baseName = baseName;
2018-11-29 17:58:47 +01:00
ModRegistry.add(this);
2018-10-19 18:32:20 +02:00
}
2018-10-20 00:54:33 +02:00
@Override
2019-10-20 22:30:49 +02:00
public ActionResultType onItemUse(PlayerEntity player, World worldIn, BlockPos pos, Hand hand, Direction facing, float hitX, float hitY, float hitZ) {
2018-10-20 00:54:33 +02:00
if (this == ModItems.INFUSED_SHOVEL) {
ItemStack stack = player.getHeldItem(hand);
2019-10-20 22:30:49 +02:00
BlockState state = worldIn.getBlockState(pos);
int damage = 0;
if (state.getBlock() instanceof BlockDirt) {
if (worldIn.getBlockState(pos.up()).getMaterial() == Material.AIR) {
worldIn.setBlockState(pos, Blocks.GRASS.getDefaultState());
damage = 5;
}
} else {
int range = player.isSneaking() ? 0 : 1;
for (int x = -range; x <= range; x++) {
for (int y = -range; y <= range; y++) {
BlockPos actualPos = pos.add(x, 0, y);
if (player.canPlayerEdit(actualPos.offset(facing), facing, stack)) {
2019-10-20 22:30:49 +02:00
if (facing != Direction.DOWN
&& worldIn.getBlockState(actualPos.up()).getMaterial() == Material.AIR
&& worldIn.getBlockState(actualPos).getBlock() == Blocks.GRASS) {
if (!worldIn.isRemote) {
worldIn.setBlockState(actualPos, Blocks.GRASS_PATH.getDefaultState(), 11);
}
damage = 1;
2018-10-20 00:54:33 +02:00
}
}
}
}
}
if (damage > 0) {
2018-10-20 00:54:33 +02:00
worldIn.playSound(player, pos, SoundEvents.ITEM_SHOVEL_FLATTEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
stack.damageItem(damage, player);
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;
}
@Override
public void onPreInit(FMLPreInitializationEvent event) {
}
@Override
public void onInit(FMLInitializationEvent event) {
}
@Override
public void onPostInit(FMLPostInitializationEvent event) {
}
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) {
2018-10-20 21:19:08 +02:00
if (this == ModItems.INFUSED_SHOVEL)
2018-12-01 18:56:05 +01:00
return Helper.makeRechargeProvider(stack, true);
2018-10-20 21:19:08 +02:00
else return null;
}
2018-10-19 18:32:20 +02:00
}