TechReborn/src/main/java/techreborn/items/tools/ItemWrench.java

161 lines
4.3 KiB
Java
Raw Normal View History

2016-02-26 22:46:55 +01:00
package techreborn.items.tools;
2016-03-25 10:47:34 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import me.modmuss50.jsonDestroyer.api.ITexturedItem;
2016-02-27 10:15:14 +01:00
import net.minecraft.block.BlockDynamicLiquid;
import net.minecraft.block.BlockStaticLiquid;
2016-03-14 14:28:33 +01:00
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
2016-02-26 22:46:55 +01:00
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
2016-02-27 10:15:14 +01:00
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemBlock;
2016-02-26 22:46:55 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
2016-03-13 19:38:41 +01:00
import net.minecraft.util.EnumActionResult;
2016-03-15 09:13:05 +01:00
import net.minecraft.util.EnumFacing;
2016-03-13 19:38:41 +01:00
import net.minecraft.util.EnumHand;
2016-03-13 17:08:30 +01:00
import net.minecraft.util.math.BlockPos;
2016-02-26 22:46:55 +01:00
import net.minecraft.world.World;
2016-02-27 10:15:14 +01:00
import net.minecraftforge.fluids.BlockFluidBase;
2016-03-14 14:28:33 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-02-26 22:46:55 +01:00
import techreborn.client.TechRebornCreativeTabMisc;
2016-02-28 01:49:50 +01:00
import techreborn.items.ItemTR;
2016-03-14 14:28:33 +01:00
import techreborn.lib.ModInfo;
2016-03-25 10:47:34 +01:00
import ic2.api.tile.IWrenchable;
2016-02-26 22:46:55 +01:00
/**
2016-03-07 21:33:08 +01:00
* Created by modmuss50 on 26/02/2016.
2016-02-26 22:46:55 +01:00
*/
2016-03-25 10:47:34 +01:00
public class ItemWrench extends ItemTR implements ITexturedItem
{
public ItemWrench()
{
setCreativeTab(TechRebornCreativeTabMisc.instance);
setUnlocalizedName("techreborn.wrench");
setMaxStackSize(1);
}
@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand,
EnumFacing facing, float hitX, float hitY, float hitZ)
{
if (world.isAirBlock(pos) || !player.isSneaking())
{
return EnumActionResult.FAIL;
}
TileEntity tile = world.getTileEntity(pos);
if (tile == null)
{
return EnumActionResult.FAIL;
}
if (!(tile instanceof IInventory))
{
return EnumActionResult.FAIL;
}
List<ItemStack> items = new ArrayList<ItemStack>();
IInventory inventory = (IInventory) tile;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
ItemStack itemStack = inventory.getStackInSlot(i);
if (itemStack == null)
{
continue;
}
if (itemStack != null && itemStack.stackSize > 0)
{
if (itemStack.getItem() instanceof ItemBlock)
{
if (((ItemBlock) itemStack.getItem()).block instanceof BlockFluidBase
|| ((ItemBlock) itemStack.getItem()).block instanceof BlockStaticLiquid
|| ((ItemBlock) itemStack.getItem()).block instanceof BlockDynamicLiquid)
{
continue;
}
}
}
items.add(itemStack.copy());
}
if (tile instanceof IWrenchable)
{
if (((IWrenchable) tile).wrenchCanRemove(player))
{
ItemStack itemStack = ((IWrenchable) tile).getWrenchDrop(player);
if (itemStack == null)
{
return EnumActionResult.FAIL;
}
items.add(itemStack);
}
for (ItemStack itemStack : items)
{
Random rand = new Random();
float dX = rand.nextFloat() * 0.8F + 0.1F;
float dY = rand.nextFloat() * 0.8F + 0.1F;
float dZ = rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityItem = new EntityItem(world, pos.getX() + dX, pos.getY() + dY, pos.getZ() + dZ,
itemStack.copy());
if (itemStack.hasTagCompound())
{
entityItem.getEntityItem().setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());
}
float factor = 0.05F;
entityItem.motionX = rand.nextGaussian() * factor;
entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
entityItem.motionZ = rand.nextGaussian() * factor;
if (!world.isRemote)
{
world.spawnEntityInWorld(entityItem);
}
}
// TODO 1.9 sounds
// world.playSoundAtEntity(player, "techreborn:block_dismantle",
// 0.8F, 1F);
if (!world.isRemote)
{
world.setBlockState(pos, Blocks.air.getDefaultState(), 2);
}
return EnumActionResult.SUCCESS;
}
return EnumActionResult.FAIL;
}
@Override
public String getTextureName(int damage)
{
return "techreborn:items/tool/wrench";
}
@Override
public int getMaxMeta()
{
return 1;
}
@Override
@SideOnly(Side.CLIENT)
public ModelResourceLocation getModel(ItemStack stack, EntityPlayer player, int useRemaining)
{
return new ModelResourceLocation(ModInfo.MOD_ID + ":" + getUnlocalizedName(stack).substring(5), "inventory");
}
@SideOnly(Side.CLIENT)
public boolean isFull3D()
{
return true;
}
2016-02-26 22:46:55 +01:00
}