Fix chests not keeping content on update

I'm not sure how much of this code is actually needed but after changing many parts it seems to make the chests keep their contents. Also made it so instead of setting the stack size to 0, if the player is in creative it remains the same, if not it subtracts 1 from the stack size (incase somehow they get a stack of more than 1) All upgrades appear to work and all contents seem to remain in chest. This fixes #31.1
This commit is contained in:
Mitchell 2015-01-28 19:19:38 -04:00
parent ef4460a8a7
commit b4dd5d1a67
1 changed files with 36 additions and 54 deletions

View File

@ -40,57 +40,39 @@ public class ItemChestChanger extends Item
{
if (world.isRemote) return false;
TileEntity te = world.getTileEntity(pos);
TileEntityIronChest newchest;
if (te != null && te instanceof TileEntityIronChest)
{
TileEntityIronChest ironchest = (TileEntityIronChest) te;
newchest = ironchest.applyUpgradeItem(this);
if (newchest == null)
{
return false;
TileEntityIronChest newchest = new TileEntityIronChest();
ItemStack[] chestContents = new ItemStack[27];
if (te != null) {
if (te instanceof TileEntityIronChest) {
chestContents = ((TileEntityIronChest) te).chestContents;
newchest = IronChestType.makeEntity(this.getTargetChestOrdinal(this.type.ordinal()));
if (newchest == null) return false;
} else if (te instanceof TileEntityChest) {
if (((TileEntityChest) te).numPlayersUsing > 0) return false;
if (!getType().canUpgrade(IronChestType.WOOD)) return false;
chestContents = new ItemStack[((TileEntityChest) te).getSizeInventory()];
for (int i = 0; i < chestContents.length; i ++) chestContents[i] = ((TileEntityChest) te).getStackInSlot(i);
newchest = IronChestType.makeEntity(IronChestType.IRON.ordinal());
}
}
else if (te != null && te instanceof TileEntityChest)
{
TileEntityChest tec = (TileEntityChest) te;
if (tec.numPlayersUsing > 0)
{
return false;
}
if (!getType().canUpgrade(IronChestType.WOOD))
{
return false;
}
// Force old TE out of the world so that adjacent chests can update
newchest = IronChestType.makeEntity(getTargetChestOrdinal(IronChestType.WOOD.ordinal()));
int newSize = newchest.chestContents.length;
ItemStack[] chestContents = ObfuscationReflectionHelper.getPrivateValue(TileEntityChest.class, tec, 0);
System.arraycopy(chestContents, 0, newchest.chestContents, 0, Math.min(newSize, chestContents.length));
BlockIronChest block = IronChest.ironChestBlock;
block.dropContent(newSize, tec, world, tec.getPos());
newchest.setFacing((byte) tec.getBlockMetadata());
newchest.sortTopStacks();
for (int i = 0; i < Math.min(newSize, chestContents.length); i++)
{
chestContents[i] = null;
}
// Clear the old block out
world.setBlockState(pos, Blocks.air.getDefaultState(), 3);
// Force the Chest TE to reset it's knowledge of neighbouring blocks
tec.updateContainingBlockInfo();
// Force the Chest TE to update any neighbours so they update next
// tick
tec.checkForAdjacentChests();
// And put in our block instead
world.setBlockState(pos, block.getStateFromMeta(newchest.getType().ordinal()), 3);
}
else
{
return false;
}
te.updateContainingBlockInfo();
if (te instanceof TileEntityChest) ((TileEntityChest) te).checkForAdjacentChests();
world.removeTileEntity(pos);
world.setBlockToAir(pos);
world.setTileEntity(pos, newchest);
world.setBlockState(pos, IronChest.ironChestBlock.getStateFromMeta(newchest.getType().ordinal()), 3);
stack.stackSize = 0;
world.markBlockForUpdate(pos);
TileEntity te2 = world.getTileEntity(pos);
if (te2 instanceof TileEntityIronChest) {
((TileEntityIronChest) te2).setContents(chestContents);
}
stack.stackSize = player.capabilities.isCreativeMode ? stack.stackSize : stack.stackSize - 1;
return true;
}