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

@ -38,60 +38,42 @@ public class ItemChestChanger extends Item
@Override @Override
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ) public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{ {
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;
else if (te != null && te instanceof TileEntityChest) chestContents = new ItemStack[((TileEntityChest) te).getSizeInventory()];
{ for (int i = 0; i < chestContents.length; i ++) chestContents[i] = ((TileEntityChest) te).getStackInSlot(i);
TileEntityChest tec = (TileEntityChest) te; newchest = IronChestType.makeEntity(IronChestType.IRON.ordinal());
if (tec.numPlayersUsing > 0) }
{ }
return false;
} te.updateContainingBlockInfo();
if (!getType().canUpgrade(IronChestType.WOOD)) if (te instanceof TileEntityChest) ((TileEntityChest) te).checkForAdjacentChests();
{
return false; world.removeTileEntity(pos);
} world.setBlockToAir(pos);
// Force old TE out of the world so that adjacent chests can update
newchest = IronChestType.makeEntity(getTargetChestOrdinal(IronChestType.WOOD.ordinal())); world.setTileEntity(pos, newchest);
int newSize = newchest.chestContents.length; world.setBlockState(pos, IronChest.ironChestBlock.getStateFromMeta(newchest.getType().ordinal()), 3);
ItemStack[] chestContents = ObfuscationReflectionHelper.getPrivateValue(TileEntityChest.class, tec, 0);
System.arraycopy(chestContents, 0, newchest.chestContents, 0, Math.min(newSize, chestContents.length)); world.markBlockForUpdate(pos);
BlockIronChest block = IronChest.ironChestBlock;
block.dropContent(newSize, tec, world, tec.getPos()); TileEntity te2 = world.getTileEntity(pos);
newchest.setFacing((byte) tec.getBlockMetadata()); if (te2 instanceof TileEntityIronChest) {
newchest.sortTopStacks(); ((TileEntityIronChest) te2).setContents(chestContents);
for (int i = 0; i < Math.min(newSize, chestContents.length); i++) }
{
chestContents[i] = null; stack.stackSize = player.capabilities.isCreativeMode ? stack.stackSize : stack.stackSize - 1;
} return true;
// 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.setBlockState(pos, IronChest.ironChestBlock.getStateFromMeta(newchest.getType().ordinal()), 3);
stack.stackSize = 0;
return true;
} }
public int getTargetChestOrdinal(int sourceOrdinal) public int getTargetChestOrdinal(int sourceOrdinal)