mirror of
https://github.com/PaperMC/Paper.git
synced 2025-03-22 15:05:35 +01:00
Added getContents() to IInventory and implemented it.
Implemented Inventory and PlayerInventory and updated StorageMinecart and Slot. Added getMaxStackSize to CraftItemStack. By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
parent
846d1800c5
commit
76e05aa578
6 changed files with 309 additions and 35 deletions
|
@ -2,22 +2,18 @@
|
||||||
package org.bukkit.craftbukkit;
|
package org.bukkit.craftbukkit;
|
||||||
|
|
||||||
import net.minecraft.server.EntityPlayer;
|
import net.minecraft.server.EntityPlayer;
|
||||||
import net.minecraft.server.InventoryPlayer;
|
|
||||||
import org.bukkit.HumanEntity;
|
import org.bukkit.HumanEntity;
|
||||||
import org.bukkit.ItemStack;
|
import org.bukkit.ItemStack;
|
||||||
|
import org.bukkit.PlayerInventory;
|
||||||
|
|
||||||
public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
|
public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
|
||||||
private EntityPlayer entity;
|
private EntityPlayer entity;
|
||||||
|
private CraftInventoryPlayer inventory;
|
||||||
|
|
||||||
public CraftHumanEntity(final CraftServer server, final EntityPlayer entity) {
|
public CraftHumanEntity(final CraftServer server, final EntityPlayer entity) {
|
||||||
super(server, entity);
|
super(server, entity);
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
}
|
this.inventory = new CraftInventoryPlayer( entity.an );
|
||||||
|
|
||||||
public ItemStack getSelectedItem() {
|
|
||||||
// TODO: Implement inventories
|
|
||||||
final InventoryPlayer inventory = entity.an;
|
|
||||||
return new ItemStack(inventory.e().c, inventory.e().a);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -34,6 +30,14 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PlayerInventory getInventory() {
|
||||||
|
return inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getItemInHand() {
|
||||||
|
return getInventory().getItemInHand();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "CraftHumanEntity{" + "id=" + getEntityID() + "name=" + getName() + '}';
|
return "CraftHumanEntity{" + "id=" + getEntityID() + "name=" + getName() + '}';
|
||||||
|
|
|
@ -0,0 +1,195 @@
|
||||||
|
package org.bukkit.craftbukkit;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import net.minecraft.server.IInventory;
|
||||||
|
|
||||||
|
import org.bukkit.ItemStack;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
|
||||||
|
public class CraftInventory implements org.bukkit.Inventory {
|
||||||
|
protected IInventory inventory;
|
||||||
|
|
||||||
|
public CraftInventory(IInventory inventory) {
|
||||||
|
this.inventory = inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IInventory getInventory() {
|
||||||
|
return inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return getInventory().a();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return getInventory().b();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getItem(int index) {
|
||||||
|
return new CraftItemStack(getInventory().a(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack[] getContents() {
|
||||||
|
ItemStack[] items = new ItemStack[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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
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++) {
|
||||||
|
if (inventory[i].getAmount() <= inventory[i].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);
|
||||||
|
} else {
|
||||||
|
// Just store it
|
||||||
|
setItem( firstFree, item );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package org.bukkit.craftbukkit;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import net.minecraft.server.InventoryPlayer;
|
||||||
|
|
||||||
|
import org.bukkit.ItemStack;
|
||||||
|
import org.bukkit.PlayerInventory;
|
||||||
|
|
||||||
|
public class CraftInventoryPlayer extends CraftInventory implements PlayerInventory {
|
||||||
|
public CraftInventoryPlayer(net.minecraft.server.InventoryPlayer inventory) {
|
||||||
|
super(inventory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InventoryPlayer getInventory() {
|
||||||
|
return (InventoryPlayer) inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<ItemStack> getArmorContents() {
|
||||||
|
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
|
||||||
|
for (net.minecraft.server.ItemStack item : getInventory().getArmorContents()) {
|
||||||
|
ItemStack i = null;
|
||||||
|
if (item != null) {
|
||||||
|
i = new CraftItemStack(item);
|
||||||
|
}
|
||||||
|
items.add(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getItemInHand() {
|
||||||
|
return new CraftItemStack( getInventory().e() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getHelmet() {
|
||||||
|
return getItem( getSize() - 4 );
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getChestplate() {
|
||||||
|
return getItem( getSize() - 3 );
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getLeggings() {
|
||||||
|
return getItem( getSize() - 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getBoots() {
|
||||||
|
return getItem( getSize() - 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHelmet(ItemStack helmet) {
|
||||||
|
setItem( getSize() - 4, helmet );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChestplate(ItemStack chestplate) {
|
||||||
|
setItem( getSize() - 3, chestplate );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLeggings(ItemStack leggings) {
|
||||||
|
setItem( getSize() - 2, leggings );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBoots(ItemStack boots) {
|
||||||
|
setItem( getSize() - 1, boots );
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ public class CraftItemStack extends ItemStack {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Unsure if we have to syn before each of these calls the values in 'item'
|
* Unsure if we have to sync before each of these calls the values in 'item'
|
||||||
* are all public.
|
* are all public.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -82,4 +82,8 @@ public class CraftItemStack extends ItemStack {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxStackSize() {
|
||||||
|
return item.a().b();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package org.bukkit.craftbukkit;
|
||||||
|
|
||||||
|
import org.bukkit.Inventory;
|
||||||
|
import org.bukkit.ItemStack;
|
||||||
|
import net.minecraft.server.Slot;
|
||||||
|
|
||||||
|
public class CraftSlot implements org.bukkit.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() );
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,8 @@
|
||||||
package org.bukkit.craftbukkit;
|
package org.bukkit.craftbukkit;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import net.minecraft.server.EntityMinecart;
|
import net.minecraft.server.EntityMinecart;
|
||||||
|
|
||||||
import org.bukkit.ItemStack;
|
import org.bukkit.Inventory;
|
||||||
import org.bukkit.StorageMinecart;
|
import org.bukkit.StorageMinecart;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,32 +11,14 @@ import org.bukkit.StorageMinecart;
|
||||||
* @author sk89q
|
* @author sk89q
|
||||||
*/
|
*/
|
||||||
public class CraftStorageMinecart extends CraftMinecart implements StorageMinecart {
|
public class CraftStorageMinecart extends CraftMinecart implements StorageMinecart {
|
||||||
|
private CraftInventory inventory;
|
||||||
|
|
||||||
public CraftStorageMinecart(CraftServer server, EntityMinecart entity) {
|
public CraftStorageMinecart(CraftServer server, EntityMinecart entity) {
|
||||||
super(server, entity);
|
super(server, entity);
|
||||||
|
inventory = new CraftInventory( entity );
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public Inventory getInventory() {
|
||||||
return minecart.c();
|
return inventory;
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return minecart.b();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStack getItem(int index) {
|
|
||||||
return new CraftItemStack(minecart.a(index));
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ItemStack> getContents() {
|
|
||||||
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
|
|
||||||
for (net.minecraft.server.ItemStack item: minecart.getContents()) {
|
|
||||||
ItemStack i = null;
|
|
||||||
if (item != null) {
|
|
||||||
i = new CraftItemStack( item );
|
|
||||||
}
|
|
||||||
items.add(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
return items;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue