Inventories
Inventory interface usage examples
Adding Items
The addItem method allows you to add one or more items to the inventory. It returns a map of slots and items that couldn't be added, indicating which items failed to be placed due to inventory constraints.
// Example: Adding items to the inventory
ItemStack item1 = new ItemStack(VanillaMaterial.DIAMOND.getMaterial(), 10);
ItemStack item2 = new ItemStack(VanillaMaterial.IRON_INGOT.getMaterial(), 20);
HashMap<Integer, ItemStack> failedItems = inventory.addItem(item1, item2);
if (!failedItems.isEmpty()) {
Cotton.getLogger().info("Some items couldn't be added to the inventory:");
failedItems.forEach((slot, item) -> {
Cotton.getLogger().info("Slot: " + slot + ", Item: " + item);
});
}Checking for Items
You can check if the inventory contains a specific item or material using the contains methods.
// Example: Checking if the inventory contains a material with a minimum amount
boolean containsMaterial = inventory.contains(VanillaMaterial.IRON_INGOT.getMaterial(), 10);
Cotton.getLogger().info("Inventory contains at least 10 iron ingots: " + containsMaterial);Retrieving Items
The first methods allow you to find the first occurrence of an item or material in the inventory. You can also find the first empty slot using firstEmpty.
Removing Items
The remove methods allow you to remove specific items or materials from the inventory.
Clearing Inventory
You can clear the entire inventory or specific slots using the clear methods.
Setting Contents
The setContents method allows you to set the inventory's contents to a specific array of items.
Querying Inventory Information
You can get various information about the inventory, such as its size, type, contents, and more.
By utilizing these methods, you can effectively manage and manipulate the contents of an inventory in various scenarios, ensuring smooth and efficient item handling within your application.
Last updated