mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-28 05:25:42 +01:00
Clarify functionality in CraftInventory. Fixes BUKKIT-3097
Changes some NPEs to IllegalArgumentExceptions for exception consistency. Contains(ItemStack, int) correctly calculates number of ItemStacks. Adds a containsAtLeast(ItemStack, int) for finding a combined amount of a single similar ItemStack. Makes some utility methods private to prevent ambiguity in use.
This commit is contained in:
parent
78f48258d8
commit
d66d3674ea
@ -16,6 +16,7 @@ import net.minecraft.server.TileEntityBrewingStand;
|
|||||||
import net.minecraft.server.TileEntityDispenser;
|
import net.minecraft.server.TileEntityDispenser;
|
||||||
import net.minecraft.server.TileEntityFurnace;
|
import net.minecraft.server.TileEntityFurnace;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.event.inventory.InventoryType;
|
import org.bukkit.event.inventory.InventoryType;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
@ -88,6 +89,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(Material material) {
|
public boolean contains(Material material) {
|
||||||
|
Validate.notNull(material, "Material cannot be null");
|
||||||
return contains(material.getId());
|
return contains(material.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,16 +106,21 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(int materialId, int amount) {
|
public boolean contains(int materialId, int amount) {
|
||||||
int amt = 0;
|
if (amount <= 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
for (ItemStack item : getContents()) {
|
for (ItemStack item : getContents()) {
|
||||||
if (item != null && item.getTypeId() == materialId) {
|
if (item != null && item.getTypeId() == materialId) {
|
||||||
amt += item.getAmount();
|
if ((amount -= item.getAmount()) <= 0) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return amt >= amount;
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(Material material, int amount) {
|
public boolean contains(Material material, int amount) {
|
||||||
|
Validate.notNull(material, "Material cannot be null");
|
||||||
return contains(material.getId(), amount);
|
return contains(material.getId(), amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,13 +128,30 @@ public class CraftInventory implements Inventory {
|
|||||||
if (item == null) {
|
if (item == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int amt = 0;
|
if (amount <= 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
for (ItemStack i : getContents()) {
|
for (ItemStack i : getContents()) {
|
||||||
if (item.equals(i)) {
|
if (item.equals(i) && --amount <= 0) {
|
||||||
amt += item.getAmount();
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return amt >= amount;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean containsAtLeast(ItemStack item, int amount) {
|
||||||
|
if (item == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (amount <= 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
for (ItemStack i : getContents()) {
|
||||||
|
if (i != null && item.getTypeId() == i.getTypeId() && item.getDurability() == i.getDurability() && item.getEnchantments().equals(i.getEnchantments()) && (amount -= i.getAmount()) <= 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<Integer, ItemStack> all(int materialId) {
|
public HashMap<Integer, ItemStack> all(int materialId) {
|
||||||
@ -144,6 +168,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<Integer, ItemStack> all(Material material) {
|
public HashMap<Integer, ItemStack> all(Material material) {
|
||||||
|
Validate.notNull(material, "Material cannot be null");
|
||||||
return all(material.getId());
|
return all(material.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,6 +197,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int first(Material material) {
|
public int first(Material material) {
|
||||||
|
Validate.notNull(material, "Material cannot be null");
|
||||||
return first(material.getId());
|
return first(material.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +205,7 @@ public class CraftInventory implements Inventory {
|
|||||||
return first(item, true);
|
return first(item, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int first(ItemStack item, boolean withAmount) {
|
private int first(ItemStack item, boolean withAmount) {
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -224,10 +250,11 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int firstPartial(Material material) {
|
public int firstPartial(Material material) {
|
||||||
|
Validate.notNull(material, "Material cannot be null");
|
||||||
return firstPartial(material.getId());
|
return firstPartial(material.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int firstPartial(ItemStack item) {
|
private int firstPartial(ItemStack item) {
|
||||||
ItemStack[] inventory = getContents();
|
ItemStack[] inventory = getContents();
|
||||||
ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
|
ItemStack filteredItem = CraftItemStack.asCraftCopy(item);
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
@ -243,6 +270,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
|
public HashMap<Integer, ItemStack> addItem(ItemStack... items) {
|
||||||
|
Validate.noNullElements(items, "Item cannot be null");
|
||||||
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
||||||
|
|
||||||
/* TODO: some optimization
|
/* TODO: some optimization
|
||||||
@ -302,6 +330,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<Integer, ItemStack> removeItem(ItemStack... items) {
|
public HashMap<Integer, ItemStack> removeItem(ItemStack... items) {
|
||||||
|
Validate.notNull(items, "Items cannot be null");
|
||||||
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
||||||
|
|
||||||
// TODO: optimization
|
// TODO: optimization
|
||||||
@ -357,6 +386,7 @@ public class CraftInventory implements Inventory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void remove(Material material) {
|
public void remove(Material material) {
|
||||||
|
Validate.notNull(material, "Material cannot be null");
|
||||||
remove(material.getId());
|
remove(material.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user