From f95c139dd64e692d622ec09364956f6a77947702 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sun, 9 Jan 2011 18:21:31 +0100 Subject: [PATCH] Updated inventories to a has-a relationship. Added PlayerInventory interface with playeronly methods. Add equals to ItemStack. By: Erik Broes --- .../src/main/java/org/bukkit/HumanEntity.java | 32 ++++- .../src/main/java/org/bukkit/Inventory.java | 124 ++++++++++++++---- .../src/main/java/org/bukkit/ItemStack.java | 19 +++ .../main/java/org/bukkit/PlayerInventory.java | 82 ++++++++++++ paper-api/src/main/java/org/bukkit/Slot.java | 22 +--- .../main/java/org/bukkit/StorageMinecart.java | 8 +- 6 files changed, 237 insertions(+), 50 deletions(-) create mode 100644 paper-api/src/main/java/org/bukkit/PlayerInventory.java diff --git a/paper-api/src/main/java/org/bukkit/HumanEntity.java b/paper-api/src/main/java/org/bukkit/HumanEntity.java index 0cc7c1f51c..04b391a8dc 100644 --- a/paper-api/src/main/java/org/bukkit/HumanEntity.java +++ b/paper-api/src/main/java/org/bukkit/HumanEntity.java @@ -13,10 +13,34 @@ public interface HumanEntity extends LivingEntity { public String getName(); /** - * Gets the item this entity has currently selected, which will be shown in - * their hand + * Get the player's inventory. * - * @return ItemStack containing details on the item this entity has selected + * @return The inventory of the player, this also contains the armor slots. */ - public ItemStack getSelectedItem(); + public PlayerInventory getInventory(); + + /** + * Returns the ItemStack currently in your hand, can be empty. + * + * @return The ItemStack of the item you are currently holding. + */ + public ItemStack getItemInHand(); + + + /** TODO: This probably won't work ;( + * Sets the item to the given ItemStack, this will replace whatever the + * user was holding. + * + * @param item The ItemStack which will end up in the hand + * @return + * + public void setItemInHand( ItemStack item ); + + ** + * Changes the item in hand to another of your 'action slots'. + * + * @param index The new index to use, only valid ones are 0-8. + * + public void selectItemInHand( int index ); + */ } diff --git a/paper-api/src/main/java/org/bukkit/Inventory.java b/paper-api/src/main/java/org/bukkit/Inventory.java index 7378903e57..a3233694df 100644 --- a/paper-api/src/main/java/org/bukkit/Inventory.java +++ b/paper-api/src/main/java/org/bukkit/Inventory.java @@ -1,6 +1,6 @@ package org.bukkit; -import java.util.Collection; +import java.util.HashMap; /** * Interface to the various inventories @@ -20,21 +20,6 @@ public interface Inventory { */ public String getName(); - /** - * TODO Set the name of the inventory - * - * @param name The new name of the inventory - public void setName(String name); - */ - - /** TODO: Appears minecraft has different ideas for slots! - * Get the slot at a specific index of an inventory - * - * @param index The index of the slot to get - * @return The Slot found at the index - public Slot getSlot(int index); - */ - /** * Get the ItemStack found in the slot at the given index * @@ -43,23 +28,108 @@ public interface Inventory { */ public ItemStack getItem(int index); + /** + * Stores the ItemStack at the given index + * + * @param index The index where to put the ItemStack + * @param item The ItemStack to set + */ + public void setItem(int index, ItemStack item); + + /** + * Stores the given ItemStacks in the inventory + * + * @param items The ItemStacks to add + * @return + */ + public HashMap addItem(ItemStack... items); + /** * Get all ItemStacks from the inventory * * @return All the ItemStacks from all slots */ - public Collection getContents(); + public ItemStack[] getContents(); - /* - * TODO public boolean contains(int materialId); public boolean - * contains(Material material); public boolean contains(ItemStack item); + /** + * Check if the inventory contains any ItemStacks with the given materialId * - * public Collection all(int materialId); public Collection - * all(Material material); public Collection all(ItemStack item); - * - * public Slot first(int materialId); public Slot first(Material material); - * public Slot first(ItemStack item); - * - * public int firstEmptyIndex(); + * @param materialId The materialId to check for + * @return If any ItemStacks were found */ + public boolean contains(int materialId); + + /** + * Check if the inventory contains any ItemStacks with the given material + * + * @param material The material to check for + * @return If any ItemStacks were found + */ + public boolean contains(Material material); + + /** + * Check if the inventory contains any ItemStacks matching the given ItemStack + * This will only match if both the type and the amount of the stack match + * + * @param item The ItemStack to match against + * @return If any matching ItemStacks were found + */ + public boolean contains(ItemStack item); + + /** + * Find all slots in the inventory containing any ItemStacks with the given materialId + * + * @param materialId The materialId to look for + * @return The Slots found. + */ + public HashMap all(int materialId); + + /** + * Find all slots in the inventory containing any ItemStacks with the given material + * + * @param materialId The material to look for + * @return The Slots found. + */ + public HashMap all(Material material); + + /** + * Find all slots in the inventory containing any ItemStacks with the given ItemStack + * This will only match slots if both the type and the amount of the stack match + * + * @param item The ItemStack to match against + * @return The Slots found. + */ + public HashMap all(ItemStack item); + + /** + * Find the first slot in the inventory containing an ItemStack with the given materialId + * + * @param materialId The materialId to look for + * @return The Slot found. + */ + public int first(int materialId); + + /** + * Find the first slot in the inventory containing an ItemStack with the given material + * + * @param materialId The material to look for + * @return The Slot found. + */ + public int first(Material material); + + /** + * Find the first slot in the inventory containing an ItemStack with the given stack + * This will only match a slot if both the type and the amount of the stack match + * + * @param item The ItemStack to match against + * @return The Slot found. + */ + public int first(ItemStack item); + + /** + * Find the first empty Slot. + * + * @return The first empty Slot found. + */ + public int firstEmpty(); } \ No newline at end of file diff --git a/paper-api/src/main/java/org/bukkit/ItemStack.java b/paper-api/src/main/java/org/bukkit/ItemStack.java index f235062134..e90a5fe5d9 100644 --- a/paper-api/src/main/java/org/bukkit/ItemStack.java +++ b/paper-api/src/main/java/org/bukkit/ItemStack.java @@ -115,4 +115,23 @@ public class ItemStack { public byte getDamage() { return damage; } + + /** + * Get the maximum stacksize for the material hold in this ItemStack + * Returns -1 if it has no idea. + * + * @return The maximum you can stack this material to. + */ + public int getMaxStackSize() { + return -1; + } + + @Override + public boolean equals(Object object) { + return false; + } + + public boolean equals(ItemStack item) { + return item.getAmount() == getAmount() && item.getTypeID() == getTypeID(); + } } diff --git a/paper-api/src/main/java/org/bukkit/PlayerInventory.java b/paper-api/src/main/java/org/bukkit/PlayerInventory.java new file mode 100644 index 0000000000..419eb07bcd --- /dev/null +++ b/paper-api/src/main/java/org/bukkit/PlayerInventory.java @@ -0,0 +1,82 @@ +package org.bukkit; + +import java.util.ArrayList; + +/** + * Includes interface to the 4 armor slots + */ +public interface PlayerInventory extends Inventory { + /** + * Get all ItemStacks from the armor slots + * + * @return All the ItemStacks from the armor slots + */ + public ArrayList getArmorContents(); + + /** + * Return the ItemStack from the helmet slot + * + * @return The ItemStack in the helmet slot + */ + public ItemStack getHelmet(); + + /** + * Return the ItemStack from the chestplate slot + * + * @return The ItemStack in the chestplate slot + */ + public ItemStack getChestplate(); + + /** + * Return the ItemStack from the leg slot + * + * @return The ItemStack in the leg slot + */ + public ItemStack getLeggings(); + + /** + * Return the ItemStack from the boots slot + * + * @return The ItemStack in the boots slot + */ + public ItemStack getBoots(); + + /** + * Put the given ItemStack into the helmet slot + * This does not check if the ItemStack is a helmet + * + * @param helmet The ItemStack to use as helmet + */ + public void setHelmet(ItemStack helmet); + + /** + * Put the given ItemStack into the chestplate slot + * This does not check if the ItemStack is a chestplate + * + * @param chestplate The ItemStack to use as chestplate + */ + public void setChestplate(ItemStack chestplate); + + /** + * Put the given ItemStack into the leg slot + * This does not check if the ItemStack is a pair of leggings + * + * @param leggings The ItemStack to use as leggings + */ + public void setLeggings(ItemStack leggings); + + /** + * Put the given ItemStack into the boots slot + * This does not check if the ItemStack is a boots + * + * @param boots The ItemStack to use as boots + */ + public void setBoots(ItemStack boots); + + /** + * Returns the ItemStack currently hold + * + * @return The currently holded ItemStack + */ + public ItemStack getItemInHand(); +} \ No newline at end of file diff --git a/paper-api/src/main/java/org/bukkit/Slot.java b/paper-api/src/main/java/org/bukkit/Slot.java index 3ead03998f..008c464fcc 100644 --- a/paper-api/src/main/java/org/bukkit/Slot.java +++ b/paper-api/src/main/java/org/bukkit/Slot.java @@ -3,39 +3,25 @@ package org.bukkit; /** * Represents a slot in an inventory */ -public class Slot { - private Inventory inventory; - private int index; - - public Slot(Inventory inventory, int index) { - this.inventory = inventory; - this.index = index; - } - +public interface Slot { /** * Gets the inventory this slot belongs to * * @return The inventory */ - public Inventory getInventory() { - return inventory; - } + public Inventory getInventory(); /** * Get the index this slot belongs to * * @return Index of the slot */ - public int getIndex() { - return index; - } + public int getIndex(); /** * Get the item from the slot. * * @return ItemStack in the slot. */ - public ItemStack getItem() { - return inventory.getItem(index); - } + public ItemStack getItem(); } diff --git a/paper-api/src/main/java/org/bukkit/StorageMinecart.java b/paper-api/src/main/java/org/bukkit/StorageMinecart.java index bd7b1b02e0..7ec1b5ac62 100644 --- a/paper-api/src/main/java/org/bukkit/StorageMinecart.java +++ b/paper-api/src/main/java/org/bukkit/StorageMinecart.java @@ -5,5 +5,11 @@ package org.bukkit; * * @author sk89q */ -public interface StorageMinecart extends Minecart, Inventory { +public interface StorageMinecart extends Minecart { + /** + * Return the inventory object for this StorageMinecart. + * + * @return The inventory for this Minecart + */ + public Inventory getInventory(); }