public static ItemStack[] getContainerContents(InventoryHolder container) {
//If it isn't a chest, there is no issue!
if (!(container instanceof Chest)) return container.getInventory().getContents();
Chest chest = (Chest)container;
Chest second = null;
//Iterate through nearby blocks to find any other chests
if (chest.getBlock().getRelative(BlockFace.NORTH).getType() == Material.CHEST)
second = (Chest) chest.getBlock().getRelative(BlockFace.NORTH).getState();
else if (chest.getBlock().getRelative(BlockFace.SOUTH).getType() == Material.CHEST)
second = (Chest) chest.getBlock().getRelative(BlockFace.SOUTH).getState();
else if (chest.getBlock().getRelative(BlockFace.EAST).getType() == Material.CHEST)
second = (Chest) chest.getBlock().getRelative(BlockFace.EAST).getState();
else if (chest.getBlock().getRelative(BlockFace.WEST).getType() == Material.CHEST)
second = (Chest) chest.getBlock().getRelative(BlockFace.WEST).getState();
//If we can't find a second chest, just return this one
if (second == null) {
return chest.getInventory().getContents();
}
else {
//I think it would be good, to consistently return same chest
//contents, regardless of what
//block was clicked on. That means, we must determine, which part
//of chest comes first, and which second.
//I choose the one, which has lower X coordinate. If they are same,
//than it's the one with lower Z coordinate.
//I believe it can be easily checked with this trick:
ItemStack[] result = new ItemStack[54];
ItemStack[] firstHalf;
ItemStack[] secondHalf;
if ((chest.getX() + chest.getZ()) < (second.getX() + second.getZ())) {
firstHalf = chest.getInventory().getContents();
secondHalf = second.getInventory().getContents();
} else {
firstHalf = second.getInventory().getContents();
secondHalf = chest.getInventory().getContents();
}
//Merge them
for (int i = 0; i < 27; i++) {