Skip to content

Commit

Permalink
improve material match effic
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 25, 2018
1 parent a755795 commit 5f12b12
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Expand Up @@ -111,6 +111,10 @@ public boolean tryItem(dItem item, String comparedto) {
if (comparedto.equals("potion") && CoreUtilities.toLowerCase(item.getItemStack().getType().name()).contains("potion")) {
return true;
}
dMaterial quickOf = dMaterial.quickOfNamed(comparedto);
if (quickOf != null) {
return item.getMaterial().equals(quickOf);
}
item = new dItem(item.getItemStack().clone());
item.setAmount(1);
if (CoreUtilities.toLowerCase(item.identifyNoIdentifier()).equals(comparedto)) {
Expand All @@ -137,6 +141,10 @@ public boolean tryMaterial(dMaterial mat, String comparedto) {
if (comparedto.equals("block") || comparedto.equals("material")) {
return true;
}
dMaterial quickOf = dMaterial.quickOfNamed(comparedto);
if (quickOf != null) {
return mat.equals(quickOf);
}
else if (CoreUtilities.toLowerCase(mat.realName()).equals(comparedto)) {
return true;
}
Expand Down
32 changes: 29 additions & 3 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dMaterial.java
Expand Up @@ -609,6 +609,31 @@ public static dMaterial valueOf(String string, TagContext context) {
return null;
}

public static dMaterial quickOfNamed(String string) {
string = string.toUpperCase();
int index = string.indexOf(',');
if (index < 0) {
index = string.indexOf(':');
}
int data = 0;
if (index >= 0) {
data = aH.getIntegerFrom(string.substring(index + 1));
string = string.substring(0, index);
}
Material m = Material.getMaterial(string);
if (m != null) {
return getMaterialFrom(m, data);
}
dMaterial mat = all_dMaterials.get(string);
if (mat != null) {
if (data == 0) {
return mat;
}
return getMaterialFrom(mat.material, data);
}
return null;
}

/**
* Determine whether a string is a valid material.
*
Expand All @@ -634,11 +659,12 @@ public static boolean matches(String arg) {
@Override
public boolean equals(Object object) {
if (object instanceof dMaterial) {
return ((dMaterial) object).identify().equals(this.identify());
return getMaterial() == ((dMaterial) object).getMaterial()
&& getData((byte) 0) == ((dMaterial) object).getData((byte) 0);
}
else {
dMaterial parsed = valueOf(object.toString());
return parsed != null && parsed.identify().equals(this.identify());
return equals(parsed);
}
}

Expand Down Expand Up @@ -680,7 +706,7 @@ public String name() {
}


public Byte getData(byte fallback) {
public byte getData(byte fallback) {
if (data == null) {
return fallback;
}
Expand Down

0 comments on commit 5f12b12

Please sign in to comment.