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

195 lines
5.4 KiB
Java
Raw Normal View History

2016-02-26 22:46:55 +01:00
package techreborn.items.tools;
2016-03-27 21:10:49 +02:00
import ic2.api.tile.IWrenchable;
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-03-14 14:28:33 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-03-27 19:38:43 +02:00
import reborncore.common.tile.TileMachineBase;
2016-03-28 14:35:59 +02:00
import techreborn.blocks.fluid.BlockFluidBase;
2016-03-27 19:38:43 +02:00
import techreborn.blocks.storage.BlockBatBox;
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;
import techreborn.tiles.storage.TileEnergyStorage;
2016-03-28 14:35:59 +02:00
2016-03-27 21:10:49 +02:00
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
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);
}
2016-03-28 14:35:59 +02:00
@Override public EnumActionResult onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos,
EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand)
{
2016-03-27 19:38:43 +02:00
if (world.isAirBlock(pos))
2016-03-25 10:47:34 +01:00
{
return EnumActionResult.FAIL;
}
TileEntity tile = world.getTileEntity(pos);
if (tile == null)
{
return EnumActionResult.FAIL;
}
2016-03-27 19:38:43 +02:00
2016-04-23 14:32:03 +02:00
if (!player.isSneaking() && !player.worldObj.isRemote)
2016-03-28 14:35:59 +02:00
{
2016-04-23 14:32:03 +02:00
if (tile instanceof TileMachineBase)
2016-03-28 14:35:59 +02:00
{
if (side != EnumFacing.DOWN && side != EnumFacing.UP)
{
2016-03-27 19:38:43 +02:00
((TileMachineBase) tile).setFacing(side);
return EnumActionResult.SUCCESS;
}
}
}
return super.onItemUseFirst(stack, player, world, pos, side, hitX, hitY, hitZ, hand);
}
2016-03-28 14:35:59 +02:00
@Override public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos,
EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
2016-03-27 19:38:43 +02:00
{
2016-03-28 14:35:59 +02:00
2016-03-27 19:38:43 +02:00
if (world.isAirBlock(pos))
2016-03-25 10:47:34 +01:00
{
return EnumActionResult.FAIL;
}
2016-03-27 19:38:43 +02:00
TileEntity tile = world.getTileEntity(pos);
if (tile == null)
2016-03-25 10:47:34 +01:00
{
2016-03-27 19:38:43 +02:00
return EnumActionResult.FAIL;
}
2016-03-28 14:35:59 +02:00
if (!world.isRemote)
2016-03-27 19:38:43 +02:00
{
2016-03-28 14:35:59 +02:00
if (player.isSneaking())
2016-03-25 10:47:34 +01:00
{
List<ItemStack> items = new ArrayList<>();
2016-03-28 14:35:59 +02:00
if (tile instanceof IInventory)
2016-03-25 10:47:34 +01:00
{
2016-03-28 14:35:59 +02:00
IInventory inventory = (IInventory) tile;
for (int i = 0; i < inventory.getSizeInventory(); i++)
2016-03-27 19:38:43 +02:00
{
2016-03-28 14:35:59 +02:00
ItemStack itemStack = inventory.getStackInSlot(i);
if (itemStack != null)
2016-03-27 19:38:43 +02:00
{
2016-03-28 14:35:59 +02:00
if (itemStack.stackSize > 0)
2016-03-27 19:38:43 +02:00
{
2016-03-28 14:35:59 +02:00
if (itemStack.getItem() instanceof ItemBlock)
if (!(((ItemBlock) itemStack.getItem()).block instanceof BlockFluidBase) || !(((ItemBlock) itemStack.getItem()).block instanceof BlockStaticLiquid)
|| !(((ItemBlock) itemStack.getItem()).block instanceof BlockDynamicLiquid))
{
items.add(itemStack.copy());
}
2016-03-27 19:38:43 +02:00
}
}
}
2016-03-28 14:35:59 +02:00
if (tile instanceof IWrenchable)
2016-03-27 19:38:43 +02:00
{
2016-03-28 14:35:59 +02:00
if (((IWrenchable) tile).wrenchCanRemove(player))
{
ItemStack itemStack = ((IWrenchable) tile).getWrenchDrop(player);
if (itemStack == null)
{
return EnumActionResult.FAIL;
}
items.add(itemStack);
}
if (!items.isEmpty())
2016-03-27 19:38:43 +02:00
{
2016-03-28 14:35:59 +02:00
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);
}
}
2016-03-27 19:38:43 +02:00
}
2016-03-28 14:35:59 +02:00
// TODO 1.9 sounds
// world.playSoundAtEntity(player, "techreborn:block_dismantle",
// 0.8F, 1F);
2016-03-27 19:38:43 +02:00
if (!world.isRemote)
{
2016-03-28 14:35:59 +02:00
world.setBlockState(pos, Blocks.air.getDefaultState(), 2);
2016-03-27 19:38:43 +02:00
}
2016-03-28 14:35:59 +02:00
return EnumActionResult.SUCCESS;
2016-03-27 19:38:43 +02:00
}
2016-03-25 10:47:34 +01:00
}
}
2016-03-28 14:35:59 +02:00
return EnumActionResult.FAIL;
}else{
return EnumActionResult.FAIL;
2016-03-25 10:47:34 +01:00
}
}
2016-03-28 14:35:59 +02:00
@Override public String getTextureName(int damage)
2016-03-25 10:47:34 +01:00
{
return "techreborn:items/tool/wrench";
}
2016-03-28 14:35:59 +02:00
@Override public int getMaxMeta()
2016-03-25 10:47:34 +01:00
{
return 1;
}
2016-03-28 14:35:59 +02:00
@Override @SideOnly(Side.CLIENT) public ModelResourceLocation getModel(ItemStack stack, EntityPlayer player,
int useRemaining)
2016-03-25 10:47:34 +01:00
{
return new ModelResourceLocation(ModInfo.MOD_ID + ":" + getUnlocalizedName(stack).substring(5), "inventory");
}
2016-03-28 14:35:59 +02:00
@SideOnly(Side.CLIENT) public boolean isFull3D()
2016-03-25 10:47:34 +01:00
{
return true;
}
2016-02-26 22:46:55 +01:00
}