From 94071d05bb0d12913bb0a9c0ecab81ab63a4e112 Mon Sep 17 00:00:00 2001 From: mDiyo Date: Thu, 21 Aug 2014 07:12:33 -0800 Subject: [PATCH] Pattern chests keep inventory on harvest --- .../plugins/waila/WailaRegistrar.java | 2 +- .../tools/blocks/ToolStationBlock.java | 93 ++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/main/java/tconstruct/plugins/waila/WailaRegistrar.java b/src/main/java/tconstruct/plugins/waila/WailaRegistrar.java index 167a491e..7e1f6cc7 100644 --- a/src/main/java/tconstruct/plugins/waila/WailaRegistrar.java +++ b/src/main/java/tconstruct/plugins/waila/WailaRegistrar.java @@ -37,7 +37,7 @@ public class WailaRegistrar public static String fluidNameHelper (FluidStack f) { - return f.getFluid().getLocalizedName(f); + return f.getFluid().getLocalizedName(); } } diff --git a/src/main/java/tconstruct/tools/blocks/ToolStationBlock.java b/src/main/java/tconstruct/tools/blocks/ToolStationBlock.java index defb23cc..7ab73a3d 100644 --- a/src/main/java/tconstruct/tools/blocks/ToolStationBlock.java +++ b/src/main/java/tconstruct/tools/blocks/ToolStationBlock.java @@ -7,9 +7,11 @@ import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.IIcon; @@ -197,7 +199,7 @@ public class ToolStationBlock extends InventoryBlock } } - @Override + /*@Override public void onBlockPlacedBy (World world, int x, int y, int z, EntityLivingBase par5EntityLiving, ItemStack par6ItemStack) { if (PHConstruct.freePatterns) @@ -214,11 +216,98 @@ public class ToolStationBlock extends InventoryBlock } } super.onBlockPlacedBy(world, x, y, z, par5EntityLiving, par6ItemStack); - } + }*/ @Override public String getTextureDomain (int textureNameIndex) { return "tinker"; } + + /* Keep pattern chest inventory */ + @Override + public void breakBlock (World par1World, int x, int y, int z, Block blockID, int meta) + { + if (meta < 5 || meta > 9) + super.breakBlock(par1World, x, y, z, blockID, meta); + else + { + par1World.removeTileEntity(x, y, z); + } + } + + @Override + public boolean removedByPlayer (World world, EntityPlayer player, int x, int y, int z, boolean willHarvest) + { + player.addExhaustion(0.025F); + + if (!world.isRemote && world.getGameRules().getGameRuleBooleanValue("doTileDrops")) + { + int meta = world.getBlockMetadata(x, y, z); + if (meta >= 5 && meta <= 9) + { + ItemStack chest = new ItemStack(this, 1, 5); + NBTTagCompound inventory = new NBTTagCompound(); + PatternChestLogic logic = (PatternChestLogic) world.getTileEntity(x, y, z); + logic.writeInventoryToNBT(inventory); + NBTTagCompound baseTag = new NBTTagCompound(); + baseTag.setTag("Inventory", inventory); + chest.setTagCompound(baseTag); + + //Spawn item + if (!player.capabilities.isCreativeMode || player.isSneaking()) + { + float f = 0.7F; + double d0 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D; + double d1 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D; + double d2 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D; + EntityItem entityitem = new EntityItem(world, (double) x + d0, (double) y + d1, (double) z + d2, chest); + entityitem.delayBeforeCanPickup = 10; + world.spawnEntityInWorld(entityitem); + } + } + } + return world.setBlockToAir(x, y, z); + } + + @Override + public void harvestBlock (World world, EntityPlayer player, int x, int y, int z, int meta) + { + if (meta < 5 || meta > 9) + super.harvestBlock(world, player, x, y, z, meta); + //Do nothing + } + + @Override + public void onBlockPlacedBy (World world, int x, int y, int z, EntityLivingBase living, ItemStack stack) + { + boolean keptInventory = false; + if (stack.hasTagCompound()) + { + NBTTagCompound inventory = stack.getTagCompound().getCompoundTag("Inventory"); + if (inventory != null) + { + PatternChestLogic logic = (PatternChestLogic) world.getTileEntity(x, y, z); + logic.readInventoryFromNBT(inventory); + logic.xCoord = x; + logic.yCoord = y; + logic.zCoord = z; + keptInventory = true; + } + } + if (!keptInventory && PHConstruct.freePatterns) + { + int meta = world.getBlockMetadata(x, y, z); + if (meta == 5) + { + PatternChestLogic logic = (PatternChestLogic) world.getTileEntity(x, y, z); + for (int i = 1; i <= 13; i++) + { + logic.setInventorySlotContents(i - 1, new ItemStack(TinkerTools.woodPattern, 1, i)); + } + logic.setInventorySlotContents(13, new ItemStack(TinkerTools.woodPattern, 1, 22)); + } + } + super.onBlockPlacedBy(world, x, y, z, living, stack); + } } \ No newline at end of file