Fixed blocks not keeping data on placement

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
CraftBukkit/Spigot 2011-01-14 19:44:11 +00:00
parent c49063334a
commit 5899ec007c

View file

@ -1,208 +1,208 @@
package org.bukkit.craftbukkit; package org.bukkit.craftbukkit;
import java.util.HashMap; import java.util.HashMap;
import net.minecraft.server.IInventory; import net.minecraft.server.IInventory;
import org.bukkit.ItemStack; import org.bukkit.ItemStack;
import org.bukkit.Material; import org.bukkit.Material;
public class CraftInventory implements org.bukkit.Inventory { public class CraftInventory implements org.bukkit.Inventory {
protected IInventory inventory; protected IInventory inventory;
public CraftInventory(IInventory inventory) { public CraftInventory(IInventory inventory) {
this.inventory = inventory; this.inventory = inventory;
} }
public IInventory getInventory() { public IInventory getInventory() {
return inventory; return inventory;
} }
public int getSize() { public int getSize() {
return getInventory().h_(); return getInventory().h_();
} }
public String getName() { public String getName() {
return getInventory().b(); return getInventory().b();
} }
public CraftItemStack getItem(int index) { public CraftItemStack getItem(int index) {
return new CraftItemStack(getInventory().a(index)); return new CraftItemStack(getInventory().a(index));
} }
public CraftItemStack[] getContents() { public CraftItemStack[] getContents() {
CraftItemStack[] items = new CraftItemStack[getSize()]; CraftItemStack[] items = new CraftItemStack[getSize()];
net.minecraft.server.ItemStack[] mcItems = getInventory().getContents(); net.minecraft.server.ItemStack[] mcItems = getInventory().getContents();
for (int i = 0; i < mcItems.length; i++ ) { for (int i = 0; i < mcItems.length; i++ ) {
items[i] = new CraftItemStack(mcItems[i]); items[i] = new CraftItemStack(mcItems[i]);
} }
return items; return items;
} }
public void setItem(int index, ItemStack item) { public void setItem(int index, ItemStack item) {
getInventory().a( index, new net.minecraft.server.ItemStack( item.getTypeID(), item.getAmount(), 0)); getInventory().a( index, new net.minecraft.server.ItemStack( item.getTypeID(), item.getAmount(), 0));
} }
public boolean contains(int materialId) { public boolean contains(int materialId) {
for (ItemStack item: getContents()) { for (ItemStack item: getContents()) {
if (item.getTypeID() == materialId) { if (item.getTypeID() == materialId) {
return true; return true;
} }
} }
return false; return false;
} }
public boolean contains(Material material) { public boolean contains(Material material) {
return contains(material.getID()); return contains(material.getID());
} }
public boolean contains(ItemStack item) { public boolean contains(ItemStack item) {
for (ItemStack i: getContents()) { for (ItemStack i: getContents()) {
if (item.equals(i)) { if (item.equals(i)) {
return true; return true;
} }
} }
return false; return false;
} }
public HashMap<Integer, ItemStack> all(int materialId) { public HashMap<Integer, ItemStack> all(int materialId) {
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>(); HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
ItemStack[] inventory = getContents(); ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) { for (int i = 0; i < inventory.length; i++) {
ItemStack item = inventory[i]; ItemStack item = inventory[i];
if (item.getTypeID() == materialId) { if (item.getTypeID() == materialId) {
slots.put( i, item ); slots.put( i, item );
} }
} }
return slots; return slots;
} }
public HashMap<Integer, ItemStack> all(Material material) { public HashMap<Integer, ItemStack> all(Material material) {
return all(material.getID()); return all(material.getID());
} }
public HashMap<Integer, ItemStack> all(ItemStack item) { public HashMap<Integer, ItemStack> all(ItemStack item) {
HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>(); HashMap<Integer, ItemStack> slots = new HashMap<Integer, ItemStack>();
ItemStack[] inventory = getContents(); ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) { for (int i = 0; i < inventory.length; i++) {
if (item.equals(inventory[i])) { if (item.equals(inventory[i])) {
slots.put( i, item ); slots.put( i, item );
} }
} }
return slots; return slots;
} }
public int first(int materialId) { public int first(int materialId) {
ItemStack[] inventory = getContents(); ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) { for (int i = 0; i < inventory.length; i++) {
if (inventory[i].getTypeID() == materialId) { if (inventory[i].getTypeID() == materialId) {
return i; return i;
} }
} }
return -1; return -1;
} }
public int first(Material material) { public int first(Material material) {
return first(material.getID()); return first(material.getID());
} }
public int first(ItemStack item) { public int first(ItemStack item) {
ItemStack[] inventory = getContents(); ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) { for (int i = 0; i < inventory.length; i++) {
if (item.equals(inventory[i])) { if (item.equals(inventory[i])) {
return i; return i;
} }
} }
return -1; return -1;
} }
public int firstEmpty() { public int firstEmpty() {
return first(Material.AIR); return first(Material.AIR);
} }
public int firstPartial(int materialId) { public int firstPartial(int materialId) {
ItemStack[] inventory = getContents(); ItemStack[] inventory = getContents();
for (int i = 0; i < inventory.length; i++) { for (int i = 0; i < inventory.length; i++) {
ItemStack item = inventory[i]; ItemStack item = inventory[i];
if (item != null && item.getTypeID() == materialId && item.getAmount() < item.getMaxStackSize()) { if (item != null && item.getTypeID() == materialId && item.getAmount() < item.getMaxStackSize()) {
return i; return i;
} }
} }
return -1; return -1;
} }
public int firstPartial(Material material) { public int firstPartial(Material material) {
return firstPartial(material.getID()); return firstPartial(material.getID());
} }
public int firstPartial(ItemStack item) { public int firstPartial(ItemStack item) {
return firstPartial(item.getTypeID()); return firstPartial(item.getTypeID());
} }
public HashMap<Integer, ItemStack> addItem(ItemStack... items) { public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
HashMap<Integer,ItemStack> leftover = new HashMap<Integer,ItemStack>(); HashMap<Integer,ItemStack> leftover = new HashMap<Integer,ItemStack>();
/* TODO: some optimization /* TODO: some optimization
* - Create a 'firstPartial' with a 'fromIndex' * - Create a 'firstPartial' with a 'fromIndex'
* - Record the lastPartial per Material * - Record the lastPartial per Material
* - Cache firstEmpty result * - Cache firstEmpty result
*/ */
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
ItemStack item = items[i]; ItemStack item = items[i];
while (true) { while (true) {
// Do we already have a stack of it? // Do we already have a stack of it?
int firstPartial = firstPartial( item.getTypeID() ); int firstPartial = firstPartial( item.getTypeID() );
// Drat! no partial stack // Drat! no partial stack
if (firstPartial == -1) { if (firstPartial == -1) {
// Find a free spot! // Find a free spot!
int firstFree = firstEmpty(); int firstFree = firstEmpty();
if (firstFree == -1) { if (firstFree == -1) {
// No space at all! // No space at all!
leftover.put(i, item); leftover.put(i, item);
break; break;
} else { } else {
// More than a single stack! // More than a single stack!
if (item.getAmount() > getMaxItemStack()) { if (item.getAmount() > getMaxItemStack()) {
setItem( firstFree, new ItemStack(item.getTypeID(), getMaxItemStack())); setItem( firstFree, new ItemStack(item.getTypeID(), getMaxItemStack()));
item.setAmount(item.getAmount() - getMaxItemStack()); item.setAmount(item.getAmount() - getMaxItemStack());
} else { } else {
// Just store it // Just store it
setItem( firstFree, item ); setItem( firstFree, item );
break; break;
} }
} }
} else { } else {
// So, apparently it might only partially fit, well lets do just that // So, apparently it might only partially fit, well lets do just that
ItemStack partialItem = getItem(firstPartial); ItemStack partialItem = getItem(firstPartial);
int amount = item.getAmount(); int amount = item.getAmount();
int partialAmount = partialItem.getAmount(); int partialAmount = partialItem.getAmount();
int maxAmount = partialItem.getMaxStackSize(); int maxAmount = partialItem.getMaxStackSize();
// Check if it fully fits // Check if it fully fits
if (amount + partialAmount <= maxAmount) { if (amount + partialAmount <= maxAmount) {
partialItem.setAmount( amount + partialAmount ); partialItem.setAmount( amount + partialAmount );
break; break;
} }
// It fits partially // It fits partially
partialItem.setAmount( maxAmount ); partialItem.setAmount( maxAmount );
item.setAmount( amount + partialAmount - maxAmount ); item.setAmount( amount + partialAmount - maxAmount );
} }
} }
} }
return leftover; return leftover;
} }
private int getMaxItemStack() { private int getMaxItemStack() {
return getInventory().c(); return getInventory().c();
} }
} }