2016-02-26 22:46:55 +01:00
|
|
|
package techreborn.items.tools;
|
|
|
|
|
|
|
|
import ic2.api.tile.IWrenchable;
|
2016-03-13 16:37:55 +01:00
|
|
|
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;
|
|
|
|
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.util.EnumFacing;
|
|
|
|
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-02-26 22:46:55 +01:00
|
|
|
|
2016-02-27 10:15:14 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-02-26 22:46:55 +01:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
2016-03-07 21:33:08 +01:00
|
|
|
* Created by modmuss50 on 26/02/2016.
|
2016-02-26 22:46:55 +01:00
|
|
|
*/
|
2016-02-28 01:49:50 +01:00
|
|
|
public class ItemWrench extends ItemTR implements ITexturedItem {
|
2016-02-26 22:46:55 +01:00
|
|
|
|
|
|
|
public ItemWrench() {
|
|
|
|
setCreativeTab(TechRebornCreativeTabMisc.instance);
|
|
|
|
setUnlocalizedName("techreborn.wrench");
|
2016-03-05 23:56:52 +01:00
|
|
|
setMaxStackSize(1);
|
2016-02-26 22:46:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 19:38:41 +01:00
|
|
|
|
2016-02-26 22:46:55 +01:00
|
|
|
@Override
|
2016-03-13 19:38:41 +01:00
|
|
|
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
|
2016-02-26 22:46:55 +01:00
|
|
|
if(world.isAirBlock(pos) || !player.isSneaking()){
|
2016-03-13 19:38:41 +01:00
|
|
|
return EnumActionResult.FAIL;
|
2016-02-26 22:46:55 +01:00
|
|
|
}
|
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
|
|
|
if(tile == null){
|
2016-03-13 19:38:41 +01:00
|
|
|
return EnumActionResult.FAIL;
|
2016-02-26 22:46:55 +01:00
|
|
|
}
|
2016-03-06 16:02:17 +01:00
|
|
|
if(!(tile instanceof IInventory)){
|
2016-03-13 19:38:41 +01:00
|
|
|
return EnumActionResult.FAIL;
|
2016-03-06 16:02:17 +01:00
|
|
|
}
|
2016-02-27 10:15:14 +01:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:46:55 +01:00
|
|
|
if(tile instanceof IWrenchable){
|
|
|
|
if(((IWrenchable) tile).wrenchCanRemove(player)){
|
|
|
|
ItemStack itemStack = ((IWrenchable) tile).getWrenchDrop(player);
|
|
|
|
if(itemStack == null){
|
2016-03-13 19:38:41 +01:00
|
|
|
return EnumActionResult.FAIL;
|
2016-02-26 22:46:55 +01:00
|
|
|
}
|
2016-02-27 10:15:14 +01:00
|
|
|
items.add(itemStack);
|
|
|
|
}
|
|
|
|
for(ItemStack itemStack : items){
|
2016-02-26 22:46:55 +01:00
|
|
|
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-13 18:38:12 +01:00
|
|
|
//TODO 1.9 sounds
|
|
|
|
//world.playSoundAtEntity(player, "techreborn:block_dismantle", 0.8F, 1F);
|
2016-02-27 10:15:14 +01:00
|
|
|
if(!world.isRemote){
|
|
|
|
world.setBlockState(pos, Blocks.air.getDefaultState(), 2);
|
|
|
|
}
|
2016-03-13 19:38:41 +01:00
|
|
|
return EnumActionResult.SUCCESS;
|
2016-02-26 22:46:55 +01:00
|
|
|
}
|
2016-03-13 19:38:41 +01:00
|
|
|
return EnumActionResult.FAIL;
|
2016-02-26 22:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getTextureName(int damage) {
|
|
|
|
return "techreborn:items/tool/wrench";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxMeta() {
|
|
|
|
return 1;
|
|
|
|
}
|
2016-03-14 14:28:33 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public ModelResourceLocation getModel(ItemStack stack, EntityPlayer player, int useRemaining) {
|
|
|
|
return new ModelResourceLocation(ModInfo.MOD_ID + ":" + getUnlocalizedName(stack).substring(5), "inventory");
|
|
|
|
}
|
2016-02-26 22:46:55 +01:00
|
|
|
}
|