mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-02-12 04:20:41 +00:00
Use the correct maxStackSize, when adding things to the inventory.
This commit is contained in:
parent
246383804a
commit
46cba7a9a7
1 changed files with 126 additions and 5 deletions
|
@ -3,6 +3,7 @@ package com.earth2me.essentials;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
||||||
import org.bukkit.entity.Item;
|
import org.bukkit.entity.Item;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
@ -42,6 +43,124 @@ public final class InventoryWorkaround
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability)
|
||||||
|
{
|
||||||
|
if (item == null)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
final ItemStack[] inventory = cinventory.getContents();
|
||||||
|
for (int i = 0; i < inventory.length; i++)
|
||||||
|
{
|
||||||
|
final ItemStack cItem = inventory[i];
|
||||||
|
if (cItem == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (item.getTypeId() == cItem.getTypeId() && cItem.getAmount() < cItem.getType().getMaxStackSize() && (!forceDurability || cItem.getDurability() == item.getDurability()))
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Integer, ItemStack> addItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items)
|
||||||
|
{
|
||||||
|
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
||||||
|
|
||||||
|
/* TODO: some optimization
|
||||||
|
* - Create a 'firstPartial' with a 'fromIndex'
|
||||||
|
* - Record the lastPartial per Material
|
||||||
|
* - Cache firstEmpty result
|
||||||
|
*/
|
||||||
|
|
||||||
|
// combine items
|
||||||
|
|
||||||
|
ItemStack[] combined = new ItemStack[items.length];
|
||||||
|
for (int i = 0; i < items.length; i++)
|
||||||
|
{
|
||||||
|
if (items[i] == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < combined.length; j++)
|
||||||
|
{
|
||||||
|
if (combined[j] == null)
|
||||||
|
{
|
||||||
|
combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()))
|
||||||
|
{
|
||||||
|
combined[j].setAmount(combined[j].getAmount() + items[i].getAmount());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < items.length; i++)
|
||||||
|
{
|
||||||
|
final ItemStack item = items[i];
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
// Do we already have a stack of it?
|
||||||
|
final int firstPartial = firstPartial(cinventory, item, forceDurability);
|
||||||
|
|
||||||
|
// Drat! no partial stack
|
||||||
|
if (firstPartial == -1)
|
||||||
|
{
|
||||||
|
// Find a free spot!
|
||||||
|
final int firstFree = cinventory.firstEmpty();
|
||||||
|
|
||||||
|
if (firstFree == -1)
|
||||||
|
{
|
||||||
|
// No space at all!
|
||||||
|
leftover.put(i, item);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// More than a single stack!
|
||||||
|
if (item.getAmount() > item.getType().getMaxStackSize())
|
||||||
|
{
|
||||||
|
cinventory.setItem(firstFree, new CraftItemStack(item.getTypeId(), item.getType().getMaxStackSize(), item.getDurability()));
|
||||||
|
item.setAmount(item.getAmount() - item.getType().getMaxStackSize());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Just store it
|
||||||
|
cinventory.setItem(firstFree, item);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// So, apparently it might only partially fit, well lets do just that
|
||||||
|
final ItemStack partialItem = cinventory.getItem(firstPartial);
|
||||||
|
|
||||||
|
final int amount = item.getAmount();
|
||||||
|
final int partialAmount = partialItem.getAmount();
|
||||||
|
final int maxAmount = partialItem.getType().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;
|
||||||
|
}
|
||||||
|
|
||||||
public static Map<Integer, ItemStack> removeItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items)
|
public static Map<Integer, ItemStack> removeItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items)
|
||||||
{
|
{
|
||||||
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
||||||
|
@ -182,10 +301,12 @@ public final class InventoryWorkaround
|
||||||
final int stacks = itm.getAmount() / maxStackSize;
|
final int stacks = itm.getAmount() / maxStackSize;
|
||||||
final 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()));
|
||||||
}
|
}
|
||||||
if (leftover > 0) {
|
if (leftover > 0)
|
||||||
|
{
|
||||||
itemStacks[stacks] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), leftover, itm.getDurability()));
|
itemStacks[stacks] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), leftover, itm.getDurability()));
|
||||||
}
|
}
|
||||||
return itemStacks;
|
return itemStacks;
|
||||||
|
|
Loading…
Reference in a new issue