From df64bc81977779bfe52e0325a5299083ba18123b Mon Sep 17 00:00:00 2001 From: Bernhard Bonigl Date: Sun, 16 Nov 2014 23:13:39 +0100 Subject: [PATCH] Slab Pattern Chests now also keep their inventory! --- .../tconstruct/tools/blocks/CraftingSlab.java | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/main/java/tconstruct/tools/blocks/CraftingSlab.java b/src/main/java/tconstruct/tools/blocks/CraftingSlab.java index 5089df1585f..59dc3d59651 100644 --- a/src/main/java/tconstruct/tools/blocks/CraftingSlab.java +++ b/src/main/java/tconstruct/tools/blocks/CraftingSlab.java @@ -7,8 +7,10 @@ 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.*; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.*; import net.minecraft.world.World; @@ -132,7 +134,22 @@ public void getSubBlocks (Item b, CreativeTabs tab, List list) @Override public void onBlockPlacedBy (World world, int x, int y, int z, EntityLivingBase entityliving, ItemStack stack) { - if (PHConstruct.freePatterns) + boolean keptInventory = false; + if (stack.hasTagCompound()) + { + NBTTagCompound inventory = stack.getTagCompound().getCompoundTag("Inventory"); + TileEntity te = world.getTileEntity(x, y, z); + if (inventory != null && te instanceof PatternChestLogic) + { + PatternChestLogic logic = (PatternChestLogic) te; + 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 == 4) @@ -169,4 +186,52 @@ public TileEntity createNewTileEntity (World var1, int metadata) return null; } } + + /* Keep pattern chest inventory */ + @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 == 4) + { + ItemStack chest = new ItemStack(this, 1, 4); + 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); + + // remove content. This is necessary because otherwise the patterns would also spill into the world + // we don't want to prevent that since that's the intended behaviour for explosions. + for(int i = 0; i < logic.getSizeInventory(); i++) + logic.setInventorySlotContents(i, null); + + //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 != 4) + super.harvestBlock(world, player, x, y, z, meta); + //Do nothing + } }