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

111 lines
4 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-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;
import net.minecraft.block.state.IBlockState;
2018-10-20 00:54:33 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
2018-10-19 18:32:20 +02:00
import net.minecraft.item.ItemSpade;
2018-10-20 00:54:33 +02:00
import net.minecraft.item.ItemStack;
2018-10-20 21:19:08 +02:00
import net.minecraft.nbt.NBTTagCompound;
2018-10-20 00:54:33 +02:00
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
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;
2018-10-19 18:32:20 +02:00
public class ItemShovelNA extends ItemSpade implements IModItem, IModelProvider {
private final String baseName;
public ItemShovelNA(String baseName, ToolMaterial material) {
super(material);
this.baseName = baseName;
ModRegistry.addItemOrBlock(this);
}
2018-10-20 00:54:33 +02:00
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (this == ModItems.INFUSED_SHOVEL) {
ItemStack stack = player.getHeldItem(hand);
IBlockState 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)) {
if (facing != EnumFacing.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);
return EnumActionResult.SUCCESS;
2018-10-20 00:54:33 +02:00
}
}
return EnumActionResult.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 boolean shouldAddCreative() {
return true;
}
@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
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
if (this == ModItems.INFUSED_SHOVEL)
return Helper.makeRechargeProvider(stack);
else return null;
}
2018-10-19 18:32:20 +02:00
}