Skip to content

Commit

Permalink
Slab Pattern Chests now also keep their inventory!
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Nov 16, 2014
1 parent 6b6928e commit df64bc8
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/main/java/tconstruct/tools/blocks/CraftingSlab.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}

0 comments on commit df64bc8

Please sign in to comment.