Updated inventories to a has-a relationship.

Added PlayerInventory interface with playeronly methods.
Add equals to ItemStack.

By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
Bukkit/Spigot 2011-01-09 18:21:31 +01:00
parent f81c44fbba
commit f95c139dd6
6 changed files with 237 additions and 50 deletions

View file

@ -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 );
*/
}

View file

@ -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<Integer, ItemStack> addItem(ItemStack... items);
/**
* Get all ItemStacks from the inventory
*
* @return All the ItemStacks from all slots
*/
public Collection<ItemStack> 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<Slot> all(int materialId); public Collection<Slot>
* all(Material material); public Collection<Slot> 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<Integer,ItemStack> 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<Integer,ItemStack> 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<Integer,ItemStack> 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();
}

View file

@ -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();
}
}

View file

@ -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<ItemStack> 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();
}

View file

@ -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();
}

View file

@ -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();
}