summaryrefslogtreecommitdiffstats
path: root/src/main/java/org
diff options
context:
space:
mode:
authorDinnerbone <dinnerbone@dinnerbone.com>2011-01-14 19:44:11 +0000
committerDinnerbone <dinnerbone@dinnerbone.com>2011-01-14 19:44:11 +0000
commit202e44ab1a0b8365d4580183eef8fa0c336f5db1 (patch)
tree156228a74e72f61091113e4122044c577234b75b /src/main/java/org
parentfe4d5db0e0382faa10e95abb0c4d47a0823deefe (diff)
downloadcraftbukkit-202e44ab1a0b8365d4580183eef8fa0c336f5db1.tar
craftbukkit-202e44ab1a0b8365d4580183eef8fa0c336f5db1.tar.gz
craftbukkit-202e44ab1a0b8365d4580183eef8fa0c336f5db1.tar.lz
craftbukkit-202e44ab1a0b8365d4580183eef8fa0c336f5db1.tar.xz
craftbukkit-202e44ab1a0b8365d4580183eef8fa0c336f5db1.zip
Fixed blocks not keeping data on placement
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftInventory.java416
1 files changed, 208 insertions, 208 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftInventory.java b/src/main/java/org/bukkit/craftbukkit/CraftInventory.java
index b0461d80..7f70f770 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftInventory.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftInventory.java
@@ -1,208 +1,208 @@
-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().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();
- }
-
-}
+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().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();
+ }
+
+}