Skip to content

Commit

Permalink
Recode inventory.contains from scratch
Browse files Browse the repository at this point in the history
A manually coded method rather than trusting Bukkit.
  • Loading branch information
mcmonkey4eva committed Nov 18, 2014
1 parent 3eb7cee commit 8d5810d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
33 changes: 30 additions & 3 deletions src/main/java/net/aufdemrand/denizen/objects/dInventory.java
Expand Up @@ -352,6 +352,33 @@ else if (inventory == null) {
loadIdentifiers();
}

public boolean containsItem(dItem item, int amount) {
if (item == null)
return false;
item.setAmount(1);
String myItem = CoreUtilities.toLowerCase(item.getFullString());
for (int i = 0; i < inventory.getSize(); i++) {
ItemStack is = inventory.getItem(i);
if (is == null)
continue;
is = is.clone();
int count = is.getAmount();
is.setAmount(1);
String newItem = CoreUtilities.toLowerCase(new dItem(is).getFullString());
if (myItem.equals(newItem)) {
if (count <= amount) {
amount -= count;
if (amount == 0)
return true;
}
else if (count > amount) {
return true;
}
}
}
return false;
}

public boolean removeItem(dItem item, int amount) {
if (item == null)
return false;
Expand Down Expand Up @@ -1290,7 +1317,7 @@ public String getAttribute(Attribute attribute) {
}

for (dItem item : dList.valueOf(attribute.getContext(1)).filter(dItem.class, attribute.getScriptEntry())) {
if (getInventory().containsAtLeast(item.getItemStack(), qty))
if (containsItem(item, qty))
return Element.TRUE.getAttribute(attribute.fulfill(attribs));
}
return Element.FALSE.getAttribute(attribute.fulfill(attribs));
Expand Down Expand Up @@ -1321,7 +1348,7 @@ public String getAttribute(Attribute attribute) {
}

for (dItem item : dList.valueOf(attribute.getContext(1)).filter(dItem.class, attribute.getScriptEntry())) {
if (!getInventory().containsAtLeast(item.getItemStack(), qty))
if (!containsItem(item, qty))
return Element.FALSE.getAttribute(attribute.fulfill(attribs));
}
return Element.TRUE.getAttribute(attribute.fulfill(attribs));
Expand Down Expand Up @@ -1366,7 +1393,7 @@ public String getAttribute(Attribute attribute) {
if (inventory.getItem(i) != null) {
dItem compare_to = new dItem(inventory.getItem(i).clone());
compare_to.setAmount(1);
if (item.identify().equalsIgnoreCase(compare_to.identify())) {
if (item.getFullString().equalsIgnoreCase(compare_to.getFullString())) {
slot = i + 1;
break;
}
Expand Down
Expand Up @@ -1238,7 +1238,7 @@ else return new Element(this.distance(toLocation))
// @attribute <l@location.is_within[<cuboid>/<ellipsoid>]>
// @returns Element(Boolean)
// @description
// Returns whether the location is within the cuboid.
// Returns whether the location is within the cuboid or ellipsoid.
// -->
if (attribute.startsWith("is_within")
&& attribute.hasContext(1)) {
Expand Down

0 comments on commit 8d5810d

Please sign in to comment.