/** * Gets a block. * * @param pos * @param id * @return * @throws OutOfBlocksException */ public void fetchBlock(int id) throws BlockSourceException { try { for (ComparableComplexBlock<Chest> c : chests) { Chest chest = c.getChest(); Item[] itemArray = chest.getContents(); // Find the item for (int i = 0; itemArray.length > i; i++) { if (itemArray[i] != null) { // Found an item if (itemArray[i].getItemId() == id && itemArray[i].getAmount() >= 1) { int newAmount = itemArray[i].getAmount() - 1; if (newAmount > 0) { itemArray[i].setAmount(newAmount); } else { itemArray[i] = null; } chest.setContents(itemArray); return; } } } } throw new OutOfBlocksException(id); } finally { flushChanges(); } }
/** * Stores a block. * * @param pos * @param id * @return * @throws OutOfSpaceException */ public void storeBlock(int id) throws BlockSourceException { try { for (ComparableComplexBlock<Chest> c : chests) { Chest chest = c.getChest(); Item[] itemArray = chest.getContents(); int emptySlot = -1; // Find an existing slot to put it into for (int i = 0; itemArray.length > i; i++) { if (itemArray[i] != null) { // Found an item if (itemArray[i].getItemId() == id && itemArray[i].getAmount() < 64) { int newAmount = itemArray[i].getAmount() + 1; itemArray[i].setAmount(newAmount); chest.setContents(itemArray); return; } } else { emptySlot = i; } } // Didn't find an existing stack, so let's create a new one if (emptySlot != -1) { itemArray[emptySlot] = new Item(id, 1); chest.setContents(itemArray); return; } } throw new OutOfSpaceException(id); } finally { flushChanges(); } }