All inventory stuff in org.bukkit.craftbukkit moved to org.bukkit.craftbukkit.inventory
This commit is contained in:
@@ -0,0 +1,239 @@
|
||||
package org.bukkit.craftbukkit.inventory;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.minecraft.server.IInventory;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.Material;
|
||||
|
||||
public class CraftInventory implements org.bukkit.inventory.Inventory {
|
||||
protected IInventory inventory;
|
||||
|
||||
public CraftInventory(IInventory inventory) {
|
||||
this.inventory = inventory;
|
||||
}
|
||||
|
||||
public IInventory getInventory() {
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return getInventory().h_();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return getInventory().b();
|
||||
}
|
||||
|
||||
public CraftItemStack getItem(int index) {
|
||||
return new CraftItemStack(getInventory().a(index));
|
||||
}
|
||||
|
||||
public CraftItemStack[] getContents() {
|
||||
CraftItemStack[] items = new CraftItemStack[getSize()];
|
||||
net.minecraft.server.ItemStack[] mcItems = getInventory().getContents();
|
||||
|
||||
for (int i = 0; i < mcItems.length; i++ ) {
|
||||
items[i] = new CraftItemStack(mcItems[i]);
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItem(int index, ItemStack item) {
|
||||
getInventory().a( index, new net.minecraft.server.ItemStack( item.getTypeId(), item.getAmount(), 0));
|
||||
}
|
||||
|
||||
public boolean contains(int materialId) {
|
||||
for (ItemStack item: getContents()) {
|
||||
if (item.getTypeId() == materialId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Material material) {
|
||||
return contains(material.getId());
|
||||
}
|
||||
|
||||
public boolean contains(ItemStack item) {
|
||||
for (ItemStack i: getContents()) {
|
||||
if (item.equals(i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public HashMap<Integer, ItemStack> all(int materialId) {
|
||||
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
|
||||
|
||||
ItemStack[] inventory = getContents();
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
ItemStack item = inventory[i];
|
||||
if (item.getTypeId() == materialId) {
|
||||
slots.put( i, item );
|
||||
}
|
||||
}
|
||||
return slots;
|
||||
}
|
||||
|
||||
public HashMap<Integer, ItemStack> all(Material material) {
|
||||
return all(material.getId());
|
||||
}
|
||||
|
||||
public HashMap<Integer, ItemStack> all(ItemStack item) {
|
||||
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
|
||||
|
||||
ItemStack[] inventory = getContents();
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
if (item.equals(inventory[i])) {
|
||||
slots.put( i, item );
|
||||
}
|
||||
}
|
||||
return slots;
|
||||
}
|
||||
|
||||
public int first(int materialId) {
|
||||
ItemStack[] inventory = getContents();
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
if (inventory[i].getTypeId() == materialId) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int first(Material material) {
|
||||
return first(material.getId());
|
||||
}
|
||||
|
||||
public int first(ItemStack item) {
|
||||
ItemStack[] inventory = getContents();
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
if (item.equals(inventory[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int firstEmpty() {
|
||||
return first(Material.AIR);
|
||||
}
|
||||
|
||||
public int firstPartial(int materialId) {
|
||||
ItemStack[] inventory = getContents();
|
||||
for (int i = 0; i < inventory.length; i++) {
|
||||
ItemStack item = inventory[i];
|
||||
if (item != null && item.getTypeId() == materialId && item.getAmount() < item.getMaxStackSize()) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int firstPartial(Material material) {
|
||||
return firstPartial(material.getId());
|
||||
}
|
||||
|
||||
public int firstPartial(ItemStack item) {
|
||||
return firstPartial(item.getTypeId());
|
||||
}
|
||||
|
||||
public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
|
||||
HashMap<Integer,ItemStack> leftover = new HashMap<Integer,ItemStack>();
|
||||
|
||||
/* TODO: some optimization
|
||||
* - Create a 'firstPartial' with a 'fromIndex'
|
||||
* - Record the lastPartial per Material
|
||||
* - Cache firstEmpty result
|
||||
*/
|
||||
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
ItemStack item = items[i];
|
||||
while (true) {
|
||||
// Do we already have a stack of it?
|
||||
int firstPartial = firstPartial( item.getTypeId() );
|
||||
|
||||
// Drat! no partial stack
|
||||
if (firstPartial == -1) {
|
||||
// Find a free spot!
|
||||
int firstFree = firstEmpty();
|
||||
|
||||
if (firstFree == -1) {
|
||||
// No space at all!
|
||||
leftover.put(i, item);
|
||||
break;
|
||||
} else {
|
||||
// More than a single stack!
|
||||
if (item.getAmount() > getMaxItemStack()) {
|
||||
setItem( firstFree, new ItemStack(item.getTypeId(), getMaxItemStack()));
|
||||
item.setAmount(item.getAmount() - getMaxItemStack());
|
||||
} else {
|
||||
// Just store it
|
||||
setItem( firstFree, item );
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// So, apparently it might only partially fit, well lets do just that
|
||||
ItemStack partialItem = getItem(firstPartial);
|
||||
|
||||
int amount = item.getAmount();
|
||||
int partialAmount = partialItem.getAmount();
|
||||
int maxAmount = partialItem.getMaxStackSize();
|
||||
|
||||
// Check if it fully fits
|
||||
if (amount + partialAmount <= maxAmount) {
|
||||
partialItem.setAmount( amount + partialAmount );
|
||||
break;
|
||||
}
|
||||
|
||||
// It fits partially
|
||||
partialItem.setAmount( maxAmount );
|
||||
item.setAmount( amount + partialAmount - maxAmount );
|
||||
}
|
||||
}
|
||||
}
|
||||
return leftover;
|
||||
}
|
||||
|
||||
private int getMaxItemStack() {
|
||||
return getInventory().c();
|
||||
}
|
||||
|
||||
public void remove(int materialId) {
|
||||
ItemStack[] items = getContents();
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
if (items[i].getTypeId() == materialId) {
|
||||
clear(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(Material material) {
|
||||
remove(material.getId());
|
||||
}
|
||||
|
||||
public void remove(ItemStack item) {
|
||||
ItemStack[] items = getContents();
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
if (items[i].equals(item)) {
|
||||
clear(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void clear(int index) {
|
||||
setItem(index, null);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
for (int i = 0; i < getSize(); i++) {
|
||||
clear(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.bukkit.craftbukkit.inventory;
|
||||
|
||||
import net.minecraft.server.InventoryPlayer;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
|
||||
public class CraftInventoryPlayer extends CraftInventory implements PlayerInventory {
|
||||
public CraftInventoryPlayer(net.minecraft.server.InventoryPlayer inventory) {
|
||||
super(inventory);
|
||||
}
|
||||
|
||||
public InventoryPlayer getInventory() {
|
||||
return (InventoryPlayer) inventory;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return super.getSize() - 4;
|
||||
}
|
||||
|
||||
public CraftItemStack getItemInHand() {
|
||||
return new CraftItemStack( getInventory().e() );
|
||||
}
|
||||
|
||||
public CraftItemStack getHelmet() {
|
||||
return getItem( getSize() + 0 );
|
||||
}
|
||||
|
||||
public CraftItemStack getChestplate() {
|
||||
return getItem( getSize() + 1 );
|
||||
}
|
||||
|
||||
public CraftItemStack getLeggings() {
|
||||
return getItem( getSize() + 2 );
|
||||
}
|
||||
|
||||
public CraftItemStack getBoots() {
|
||||
return getItem( getSize() + 3 );
|
||||
}
|
||||
|
||||
public void setHelmet(ItemStack helmet) {
|
||||
setItem( getSize() + 0, helmet );
|
||||
}
|
||||
|
||||
public void setChestplate(ItemStack chestplate) {
|
||||
setItem( getSize() + 1, chestplate );
|
||||
}
|
||||
|
||||
public void setLeggings(ItemStack leggings) {
|
||||
setItem( getSize() + 2, leggings );
|
||||
}
|
||||
|
||||
public void setBoots(ItemStack boots) {
|
||||
setItem( getSize() + 3, boots );
|
||||
}
|
||||
|
||||
public ItemStack[] getArmorContents() {
|
||||
net.minecraft.server.ItemStack[] mcItems = getInventory().getArmorContents();
|
||||
ItemStack[] ret = new ItemStack[mcItems.length];
|
||||
|
||||
for (int i = 0; i < mcItems.length; i++ ) {
|
||||
ret[i] = new CraftItemStack(mcItems[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.bukkit.craftbukkit.inventory;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.Material;
|
||||
|
||||
public class CraftItemStack extends ItemStack {
|
||||
protected net.minecraft.server.ItemStack item;
|
||||
|
||||
public CraftItemStack(net.minecraft.server.ItemStack item) {
|
||||
super(item != null ? item.c : 0, item != null ? item.a : 0);
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
/*
|
||||
* Unsure if we have to sync before each of these calls the values in 'item'
|
||||
* are all public.
|
||||
*/
|
||||
|
||||
@Override
|
||||
public Material getType() {
|
||||
super.setTypeId(item != null ? item.c : 0); // sync, needed?
|
||||
return super.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTypeId() {
|
||||
super.setTypeId(item != null ? item.c : 0); // sync, needed?
|
||||
return item != null ? item.c : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTypeId(int type) {
|
||||
if (type == 0) {
|
||||
super.setTypeId(0);
|
||||
super.setAmount(0);
|
||||
item = null;
|
||||
} else {
|
||||
if (item == null) {
|
||||
item = new net.minecraft.server.ItemStack(type, 1, 0);
|
||||
super.setAmount(1);
|
||||
} else {
|
||||
item.c = type;
|
||||
super.setTypeId(item.c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAmount() {
|
||||
super.setAmount(item != null ? item.a : 0); // sync, needed?
|
||||
return (item != null ? item.a : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAmount(int amount) {
|
||||
if (amount == 0) {
|
||||
super.setTypeId(0);
|
||||
super.setAmount(0);
|
||||
item = null;
|
||||
} else {
|
||||
super.setAmount(amount);
|
||||
item.a = amount;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDamage(final byte damage) {
|
||||
// Ignore damage if item is null
|
||||
if (item != null) {
|
||||
super.setDamage(damage);
|
||||
item.d = damage;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getDamage() {
|
||||
if (item != null) {
|
||||
super.setDamage((byte) item.d); // sync, needed?
|
||||
return (byte) item.d;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxStackSize() {
|
||||
return item.a().b();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.bukkit.craftbukkit.inventory;
|
||||
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import net.minecraft.server.Slot;
|
||||
|
||||
public class CraftSlot implements org.bukkit.inventory.Slot {
|
||||
private final Slot slot;
|
||||
|
||||
public CraftSlot(Slot slot) {
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
public Inventory getInventory() {
|
||||
return new CraftInventory( slot.b );
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return slot.a;
|
||||
}
|
||||
|
||||
public ItemStack getItem() {
|
||||
return new CraftItemStack( slot.c() );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user