All inventory stuff in org.bukkit moved to org.bukkit.inventory

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot
2011-01-15 21:27:40 +00:00
parent e2fd2dfd00
commit 1de48a9184
12 changed files with 15 additions and 12 deletions

View File

@@ -0,0 +1,170 @@
package org.bukkit.inventory;
import java.util.HashMap;
import org.bukkit.Material;
/**
* Interface to the various inventories
*/
public interface Inventory {
/**
* Returns the size of the inventory
*
* @return The inventory size
*/
public int getSize();
/**
* Return the name of the inventory
*
* @return The inventory name
*/
public String getName();
/**
* Get the ItemStack found in the slot at the given index
*
* @param index The index of the Slot's ItemStack to return
* @return The ItemStack in the slot
*/
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 ItemStack[] getContents();
/**
* Check if the inventory contains any ItemStacks with the given materialId
*
* @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();
/**
* Remove all stacks in the inventory matching the given materialId.
*
* @param materialId The material to remove
*/
public void remove(int materialId);
/**
* Remove all stacks in the inventory matching the given material.
*
* @param material The material to remove
*/
public void remove(Material material);
/**
* Remove all stacks in the inventory matching 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
*/
public void remove(ItemStack item);
/**
* Clear out a particular slot in the index
*
* @param index The index to empty.
*/
public void clear(int index);
/**
* Clear out the whole index
*/
public void clear();
}

View File

@@ -0,0 +1,197 @@
package org.bukkit.inventory;
import org.bukkit.Material;
import org.bukkit.material.MaterialData;
/**
* Represents a stack of items
*/
public class ItemStack {
private int type;
private int amount = 0;
private MaterialData data = null;
private byte damage = 0;
public ItemStack(final int type) {
this(type, 0);
}
public ItemStack(final Material type) {
this(type, 0);
}
public ItemStack(final int type, final int amount) {
this(type, amount, (byte) 0);
}
public ItemStack(final Material type, final int amount) {
this(type.getId(), amount);
}
public ItemStack(final int type, final int amount, final byte damage) {
this(type, amount, damage, (byte) 0);
}
public ItemStack(final Material type, final int amount, final byte damage) {
this(type.getId(), amount, damage);
}
public ItemStack(final int type, final int amount, final byte damage, final byte data) {
this.type = type;
this.amount = amount;
this.damage = damage;
createData(data);
}
public ItemStack(final Material type, final int amount, final byte damage, final byte data) {
this(type.getId(), amount, damage, data);
}
/**
* Gets the type of this item
*
* @return Type of the items in this stack
*/
public Material getType() {
return Material.getMaterial(type);
}
/**
* Sets the type of this item<br />
* <br />
* Note that in doing so you will reset the MaterialData for this stack
*
* @param type New type to set the items in this stack to
*/
public void setType(Material type) {
setTypeId(type.getId());
}
/**
* Gets the type id of this item
*
* @return Type Id of the items in this stack
*/
public int getTypeId() {
return type;
}
/**
* Sets the type id of this item<br />
* <br />
* Note that in doing so you will reset the MaterialData for this stack
*
* @param type New type id to set the items in this stack to
*/
public void setTypeId(int type) {
this.type = type;
createData((byte)0);
}
/**
* Gets the amount of items in this stack
*
* @return Amount of items in this stick
*/
public int getAmount() {
return amount;
}
/**
* Sets the amount of items in this stack
*
* @param amount New amount of items in this stack
*/
public void setAmount(int amount) {
this.amount = amount;
}
/**
* Gets the MaterialData for this stack of items
*
* @return MaterialData for this item
*/
public MaterialData getData() {
return data;
}
/**
* Sets the MaterialData for this stack of items
*
* @param amount New MaterialData for this item
*/
public void setData(MaterialData data) {
Material mat = getType();
if ((mat == null) || (mat.getData() == null)) {
this.data = data;
} else {
if ((data.getClass() == mat.getData()) || (data.getClass() == MaterialData.class)) {
this.data = data;
} else {
throw new IllegalArgumentException("Provided data is not of type "
+ mat.getData().getName() + ", found " + data.getClass().getName());
}
}
}
/**
* Sets the damage of this item<br /><br />
*
* 0x00 represents an item which cannot be damaged<br />
* 0x01 represents an item at maximum health<br />
* 0x32 represents an item with no health left
*
* @param damage Damage of this item
*/
public void setDamage(final byte damage) {
this.damage = damage;
}
/**
* Gets the damage of this item<br /><br />
*
* 0x00 represents an item which cannot be damaged<br />
* 0x01 represents an item at maximum health<br />
* 0x32 represents an item with no health left
*
* @return Damage of this item
*/
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;
}
private void createData(final byte data) {
Material mat = Material.getMaterial(type);
if (mat == null) {
this.data = new MaterialData(type, data);
} else {
this.data = mat.getNewData(data);
}
}
@Override
public String toString() {
return "ItemStack{"+getType().name()+" x "+getAmount()+"}";
}
@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,80 @@
package org.bukkit.inventory;
/**
* 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 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

@@ -0,0 +1,27 @@
package org.bukkit.inventory;
/**
* Represents a slot in an inventory
*/
public interface Slot {
/**
* Gets the inventory this slot belongs to
*
* @return The inventory
*/
public Inventory getInventory();
/**
* Get the index this slot belongs to
*
* @return Index of the slot
*/
public int getIndex();
/**
* Get the item from the slot.
*
* @return ItemStack in the slot.
*/
public ItemStack getItem();
}