Java Code Examples for org.bukkit.inventory.InventoryHolder#getInventory()

The following examples show how to use org.bukkit.inventory.InventoryHolder#getInventory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LWC.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Merge inventories into one
 *
 * @param blocks
 * @return
 */
public ItemStack[] mergeInventories(List<Block> blocks) {
    ItemStack[] stacks = new ItemStack[54];
    int index = 0;

    try {
        for (Block block : blocks) {
            if (!(block.getState() instanceof InventoryHolder)) {
                continue;
            }

            InventoryHolder holder = (InventoryHolder) block.getState();
            Inventory inventory = holder.getInventory();

            // Add all the items from this inventory
            for (ItemStack stack : inventory.getContents()) {
                stacks[index] = stack;
                index++;
            }
        }
    } catch (Exception e) {
        return mergeInventories(blocks);
    }

    return stacks;
}
 
Example 2
Source File: Shop.java    From ShopChest with MIT License 5 votes vote down vote up
/**
 * Runs everything that needs to be called synchronously in order 
 * to prepare creating the hologram.
 */
private PreCreateResult preCreateHologram() {
    plugin.debug("Creating hologram (#" + id + ")");

    InventoryHolder ih = getInventoryHolder();

    if (ih == null) return null;

    Chest[] chests = new Chest[2];
    BlockFace face;

    if (ih instanceof DoubleChest) {
        DoubleChest dc = (DoubleChest) ih;
        Chest r = (Chest) dc.getRightSide();
        Chest l = (Chest) dc.getLeftSide();

        chests[0] = r;
        chests[1] = l;
    } else {
        chests[0] = (Chest) ih;
    }

    if (Utils.getMajorVersion() < 13) {
        face = ((org.bukkit.material.Directional) chests[0].getData()).getFacing();
    } else {
        face = ((Directional) chests[0].getBlockData()).getFacing();
    }

    return new PreCreateResult(ih.getInventory(), chests, face);
}
 
Example 3
Source File: LWC.java    From Modern-LWC with MIT License 4 votes vote down vote up
/**
 * Deposit items into an inventory chest Works with double chests.
 *
 * @param block
 * @param itemStack
 * @return remaining items (if any)
 */
@SuppressWarnings("deprecation")
public Map<Integer, ItemStack> depositItems(Block block, ItemStack itemStack) {
    BlockState blockState;

    if ((blockState = block.getState()) != null && (blockState instanceof InventoryHolder)) {
        Block doubleChestBlock = null;
        InventoryHolder holder = (InventoryHolder) blockState;

        if (DoubleChestMatcher.PROTECTABLES_CHESTS.contains(block.getType())) {
            doubleChestBlock = findAdjacentDoubleChest(block);
        } else if (block.getType() == Material.FURNACE) {
            Inventory inventory = holder.getInventory();

            if (inventory.getItem(0) != null && inventory.getItem(1) != null) {
                if (inventory.getItem(0).getType() == itemStack.getType()
                        && inventory.getItem(0).getData().getData() == itemStack.getData().getData()
                        && inventory.getItem(0)
                        .getMaxStackSize() >= (inventory.getItem(0).getAmount() + itemStack.getAmount())) {
                    // ItemStack fits on Slot 0
                } else if (inventory.getItem(1).getType() == itemStack.getType()
                        && inventory.getItem(1).getData().getData() == itemStack.getData().getData()
                        && inventory.getItem(1)
                        .getMaxStackSize() >= (inventory.getItem(1).getAmount() + itemStack.getAmount())) {
                    // ItemStack fits on Slot 1
                } else {
                    return null;
                }
            }
        }

        if (itemStack.getAmount() <= 0) {
            return new HashMap<Integer, ItemStack>();
        }

        Map<Integer, ItemStack> remaining = holder.getInventory().addItem(itemStack);

        // we have remainders, deal with it
        if (remaining.size() > 0) {
            int key = remaining.keySet().iterator().next();
            ItemStack remainingItemStack = remaining.get(key);

            // is it a double chest ?????
            if (doubleChestBlock != null) {
                InventoryHolder holder2 = (InventoryHolder) doubleChestBlock.getState();
                remaining = holder2.getInventory().addItem(remainingItemStack);
            }

            // recheck remaining in the event of double chest being used
            if (remaining.size() > 0) {
                return remaining;
            }
        }
    }

    return new HashMap<Integer, ItemStack>();
}