/** * 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(); } }
/** * Adds a position to be used a source. * * @param pos * @return */ public void addSingleSourcePosition(Vector pos) { int x = pos.getBlockX(); int y = pos.getBlockY(); int z = pos.getBlockZ(); if (CraftBook.getBlockID(pos) == BlockType.CHEST) { ComplexBlock complexBlock = etc.getServer().getComplexBlock(x, y, z); if (complexBlock instanceof Chest) { Chest chest = (Chest) complexBlock; Item[] itemArray = chest.getContents(); boolean occupied = false; // Got to make sure that at least one slot is occupied for (int i = 0; itemArray.length > i; i++) { if (itemArray[i] != null) { // Found an item if (itemArray[i].getAmount() > 0) { occupied = true; break; } } } if (occupied) { chests.add(new ComparableComplexBlock<Chest>(pos.toBlockVector(), chest)); } } } }
/** * 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(); } }