mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-02-12 04:20:41 +00:00
Cleaned InventoryWorkaround
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1590 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
parent
4bbc2aa8d5
commit
198607d425
1 changed files with 24 additions and 23 deletions
|
@ -1,6 +1,7 @@
|
||||||
package com.earth2me.essentials;
|
package com.earth2me.essentials;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Item;
|
import org.bukkit.entity.Item;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
|
@ -18,17 +19,17 @@ public final class InventoryWorkaround
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int first(Inventory ci, ItemStack item, boolean forceDurability, boolean forceAmount)
|
public static int first(final Inventory inventory, final ItemStack item, final boolean forceDurability, final boolean forceAmount)
|
||||||
{
|
{
|
||||||
return next(ci, item, 0, forceDurability, forceAmount);
|
return next(inventory, item, 0, forceDurability, forceAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int next(Inventory ci, ItemStack item, int start, boolean forceDurability, boolean forceAmount)
|
public static int next(final Inventory cinventory, final ItemStack item, final int start, final boolean forceDurability, final boolean forceAmount)
|
||||||
{
|
{
|
||||||
ItemStack[] inventory = ci.getContents();
|
final ItemStack[] inventory = cinventory.getContents();
|
||||||
for (int i = start; i < inventory.length; i++)
|
for (int i = start; i < inventory.length; i++)
|
||||||
{
|
{
|
||||||
ItemStack cItem = inventory[i];
|
final ItemStack cItem = inventory[i];
|
||||||
if (cItem == null)
|
if (cItem == null)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
@ -41,15 +42,15 @@ public final class InventoryWorkaround
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashMap<Integer, ItemStack> removeItem(Inventory ci, boolean forceDurability, ItemStack... items)
|
public static Map<Integer, ItemStack> removeItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items)
|
||||||
{
|
{
|
||||||
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
||||||
|
|
||||||
// TODO: optimization
|
// TODO: optimization
|
||||||
|
|
||||||
for (int i = 0; i < items.length; i++)
|
for (int i = 0; i < items.length; i++)
|
||||||
{
|
{
|
||||||
ItemStack item = items[i];
|
final ItemStack item = items[i];
|
||||||
if (item == null)
|
if (item == null)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
@ -66,7 +67,7 @@ public final class InventoryWorkaround
|
||||||
}
|
}
|
||||||
|
|
||||||
// get first Item, ignore the amount
|
// get first Item, ignore the amount
|
||||||
int first = first(ci, item, forceDurability, false);
|
final int first = first(cinventory, item, forceDurability, false);
|
||||||
|
|
||||||
// Drat! we don't have this type in the inventory
|
// Drat! we don't have this type in the inventory
|
||||||
if (first == -1)
|
if (first == -1)
|
||||||
|
@ -77,20 +78,20 @@ public final class InventoryWorkaround
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ItemStack itemStack = ci.getItem(first);
|
final ItemStack itemStack = cinventory.getItem(first);
|
||||||
int amount = itemStack.getAmount();
|
final int amount = itemStack.getAmount();
|
||||||
|
|
||||||
if (amount <= toDelete)
|
if (amount <= toDelete)
|
||||||
{
|
{
|
||||||
toDelete -= amount;
|
toDelete -= amount;
|
||||||
// clear the slot, all used up
|
// clear the slot, all used up
|
||||||
ci.clear(first);
|
cinventory.clear(first);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// split the stack and store
|
// split the stack and store
|
||||||
itemStack.setAmount(amount - toDelete);
|
itemStack.setAmount(amount - toDelete);
|
||||||
ci.setItem(first, itemStack);
|
cinventory.setItem(first, itemStack);
|
||||||
toDelete = 0;
|
toDelete = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,9 +100,9 @@ public final class InventoryWorkaround
|
||||||
return leftover;
|
return leftover;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean containsItem(Inventory ci, boolean forceDurability, ItemStack... items)
|
public static boolean containsItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items)
|
||||||
{
|
{
|
||||||
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
||||||
|
|
||||||
// TODO: optimization
|
// TODO: optimization
|
||||||
|
|
||||||
|
@ -131,7 +132,7 @@ public final class InventoryWorkaround
|
||||||
|
|
||||||
for (int i = 0; i < combined.length; i++)
|
for (int i = 0; i < combined.length; i++)
|
||||||
{
|
{
|
||||||
ItemStack item = combined[i];
|
final ItemStack item = combined[i];
|
||||||
if (item == null)
|
if (item == null)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
@ -147,7 +148,7 @@ public final class InventoryWorkaround
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int slot = next(ci, item, position, forceDurability, false);
|
final int slot = next(cinventory, item, position, forceDurability, false);
|
||||||
|
|
||||||
// Drat! we don't have this type in the inventory
|
// Drat! we don't have this type in the inventory
|
||||||
if (slot == -1)
|
if (slot == -1)
|
||||||
|
@ -157,8 +158,8 @@ public final class InventoryWorkaround
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ItemStack itemStack = ci.getItem(slot);
|
final ItemStack itemStack = cinventory.getItem(slot);
|
||||||
int amount = itemStack.getAmount();
|
final int amount = itemStack.getAmount();
|
||||||
|
|
||||||
if (amount <= mustHave)
|
if (amount <= mustHave)
|
||||||
{
|
{
|
||||||
|
@ -175,11 +176,11 @@ public final class InventoryWorkaround
|
||||||
return leftover.isEmpty();
|
return leftover.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item[] dropItem(Location loc, ItemStack itm)
|
public static Item[] dropItem(final Location loc, final ItemStack itm)
|
||||||
{
|
{
|
||||||
int maxStackSize = itm.getType().getMaxStackSize();
|
final int maxStackSize = itm.getType().getMaxStackSize();
|
||||||
int stacks = itm.getAmount() / maxStackSize;
|
final int stacks = itm.getAmount() / maxStackSize;
|
||||||
int leftover = itm.getAmount() % maxStackSize;
|
final int leftover = itm.getAmount() % maxStackSize;
|
||||||
Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)];
|
Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)];
|
||||||
for (int i = 0; i < stacks; i++) {
|
for (int i = 0; i < stacks; i++) {
|
||||||
itemStacks[i] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), maxStackSize, itm.getDurability()));
|
itemStacks[i] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), maxStackSize, itm.getDurability()));
|
||||||
|
|
Loading…
Reference in a new issue