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:
parent
ef4460a8a7
commit
b4dd5d1a67
|
@ -40,57 +40,39 @@ public class ItemChestChanger extends Item
|
||||||
{
|
{
|
||||||
if (world.isRemote) return false;
|
if (world.isRemote) return false;
|
||||||
TileEntity te = world.getTileEntity(pos);
|
TileEntity te = world.getTileEntity(pos);
|
||||||
TileEntityIronChest newchest;
|
TileEntityIronChest newchest = new TileEntityIronChest();
|
||||||
if (te != null && te instanceof TileEntityIronChest)
|
ItemStack[] chestContents = new ItemStack[27];
|
||||||
{
|
if (te != null) {
|
||||||
TileEntityIronChest ironchest = (TileEntityIronChest) te;
|
if (te instanceof TileEntityIronChest) {
|
||||||
newchest = ironchest.applyUpgradeItem(this);
|
chestContents = ((TileEntityIronChest) te).chestContents;
|
||||||
if (newchest == null)
|
newchest = IronChestType.makeEntity(this.getTargetChestOrdinal(this.type.ordinal()));
|
||||||
{
|
if (newchest == null) return false;
|
||||||
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)
|
|
||||||
{
|
te.updateContainingBlockInfo();
|
||||||
TileEntityChest tec = (TileEntityChest) te;
|
if (te instanceof TileEntityChest) ((TileEntityChest) te).checkForAdjacentChests();
|
||||||
if (tec.numPlayersUsing > 0)
|
|
||||||
{
|
world.removeTileEntity(pos);
|
||||||
return false;
|
world.setBlockToAir(pos);
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
world.setTileEntity(pos, newchest);
|
world.setTileEntity(pos, newchest);
|
||||||
world.setBlockState(pos, IronChest.ironChestBlock.getStateFromMeta(newchest.getType().ordinal()), 3);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue