Skip to content

Commit

Permalink
fix some ItemMeta mishandlings
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 18, 2020
1 parent f5e56bd commit 577accb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 79 deletions.
Expand Up @@ -38,6 +38,7 @@
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.*;

Expand Down Expand Up @@ -1316,15 +1317,17 @@ public static void registerTags() {
int found_items = 0;
if (strict) {
for (ItemStack item : object.getContents()) {
if (item != null && item.getType() == Material.WRITTEN_BOOK
&& ((BookMeta) item.getItemMeta()).getTitle().equalsIgnoreCase(search_string)) {
if (item == null || !item.hasItemMeta()) {
continue;
}
ItemMeta meta = item.getItemMeta();
if (item.getType() == Material.WRITTEN_BOOK && ((BookMeta) meta).getTitle().equalsIgnoreCase(search_string)) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
}
}
else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName() &&
item.getItemMeta().getDisplayName().equalsIgnoreCase(search_string)) {
else if (meta.hasDisplayName() && meta.getDisplayName().equalsIgnoreCase(search_string)) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
Expand All @@ -1334,17 +1337,17 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
}
else {
for (ItemStack item : object.getContents()) {
if (item != null && item.getType() == Material.WRITTEN_BOOK
&& CoreUtilities.toLowerCase(((BookMeta) item.getItemMeta()).getTitle())
.contains(CoreUtilities.toLowerCase(search_string))) {
if (item == null || !item.hasItemMeta()) {
continue;
}
ItemMeta meta = item.getItemMeta();
if (item.getType() == Material.WRITTEN_BOOK && CoreUtilities.toLowerCase(((BookMeta) meta).getTitle()).contains(CoreUtilities.toLowerCase(search_string))) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
}
}
else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName() &&
CoreUtilities.toLowerCase(item.getItemMeta().getDisplayName())
.contains(CoreUtilities.toLowerCase(search_string))) {
else if (meta.hasDisplayName() && CoreUtilities.toLowerCase(meta.getDisplayName()).contains(CoreUtilities.toLowerCase(search_string))) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
Expand Down Expand Up @@ -1398,8 +1401,12 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
if (strict) {
strict_items:
for (ItemStack item : object.getContents()) {
if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()) {
List<String> item_lore = item.getItemMeta().getLore();
if (item == null || !item.hasItemMeta()) {
continue;
}
ItemMeta meta = item.getItemMeta();
if (meta.hasLore()) {
List<String> item_lore = meta.getLore();
if (lore.size() != item_lore.size()) {
continue;
}
Expand All @@ -1417,8 +1424,12 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
}
else {
for (ItemStack item : object.getContents()) {
if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()) {
List<String> item_lore = item.getItemMeta().getLore();
if (item == null || !item.hasItemMeta()) {
continue;
}
ItemMeta meta = item.getItemMeta();
if (meta.hasLore()) {
List<String> item_lore = meta.getLore();
int loreCount = 0;
lines:
for (String line : lore) {
Expand Down
79 changes: 23 additions & 56 deletions plugin/src/main/java/com/denizenscript/denizen/objects/ItemTag.java
Expand Up @@ -262,95 +262,62 @@ public int comparesTo(ItemStack compared_to) {
if (item == null) {
return -1;
}

int determination = 0;
ItemStack compared = getItemStack();

// Will return -1 if these are not the same
// Material IDs
if (compared.getType().getId() != compared_to.getType().getId()) {
if (compared.getType() != compared_to.getType()) {
return -1;
}

// If compared_to has item meta, and compared does not, return -1
if (compared_to.hasItemMeta()) {
if (!compared.hasItemMeta()) {
return -1;
}

// If compared_to has a display name, and compared does not, return -1
if (compared_to.getItemMeta().hasDisplayName()) {
if (!compared.getItemMeta().hasDisplayName()) {
ItemMeta thisMeta = getItemMeta();
ItemMeta comparedItemMeta = compared_to.getItemMeta();
if (comparedItemMeta.hasDisplayName()) {
if (!thisMeta.hasDisplayName()) {
return -1;
}

// If compared_to's display name does not at least start with compared's item name,
// return -1.
if (compared_to.getItemMeta().getDisplayName().toUpperCase()
.startsWith(compared.getItemMeta().getDisplayName().toUpperCase())) {

// If the compared item has a longer display name than compared_to,
// it is similar, but modified. Perhaps 'engraved' or something?
if (compared.getItemMeta().getDisplayName().length() >
compared_to.getItemMeta().getDisplayName().length()) {
if (comparedItemMeta.getDisplayName().toUpperCase().startsWith(thisMeta.getDisplayName().toUpperCase())) {
if (thisMeta.getDisplayName().length() > comparedItemMeta.getDisplayName().length()) {
determination++;
}
}
else {
return -1;
}
}

// If compared_to has lore, and compared does not, return -1
if (compared_to.getItemMeta().hasLore()) {
if (!compared.getItemMeta().hasLore()) {
if (comparedItemMeta.hasLore()) {
if (!thisMeta.hasLore()) {
return -1;
}

// If compared doesn't have a piece of lore contained in compared_to, return -1
for (String lore : compared_to.getItemMeta().getLore()) {
if (!compared.getItemMeta().getLore().contains(lore)) {
for (String lore : comparedItemMeta.getLore()) {
if (!thisMeta.getLore().contains(lore)) {
return -1;
}
}

// If the compared item has more lore than compared to, it is similar, but modified.
// Still qualifies for a match, but it seems the item may be a 'better' item, so increase
// the determination.
if (compared.getItemMeta().getLore().size() > compared_to.getItemMeta().getLore().size()) {
if (thisMeta.getLore().size() > comparedItemMeta.getLore().size()) {
determination++;
}
}

if (!compared_to.getItemMeta().getEnchants().isEmpty()) {
if (compared.getItemMeta().getEnchants().isEmpty()) {
if (!comparedItemMeta.getEnchants().isEmpty()) {
if (thisMeta.getEnchants().isEmpty()) {
return -1;
}

for (Map.Entry<Enchantment, Integer> enchant : compared_to.getItemMeta().getEnchants().entrySet()) {
if (!compared.getItemMeta().getEnchants().containsKey(enchant.getKey())
|| compared.getItemMeta().getEnchants().get(enchant.getKey()) < enchant.getValue()) {
for (Map.Entry<Enchantment, Integer> enchant : comparedItemMeta.getEnchants().entrySet()) {
if (!thisMeta.getEnchants().containsKey(enchant.getKey()) || thisMeta.getEnchants().get(enchant.getKey()) < enchant.getValue()) {
return -1;
}
}

if (compared.getItemMeta().getEnchants().size() > compared_to.getItemMeta().getEnchants().size()) {
if (thisMeta.getEnchants().size() > comparedItemMeta.getEnchants().size()) {
determination++;
}
}
}

if (isRepairable()) {
if (compared.getDurability() < compared_to.getDurability()) {
determination++;
if (isRepairable()) {
if (((Damageable) thisMeta).getDamage() < ((Damageable) comparedItemMeta).getDamage()) {
determination++;
}
}
}
else
// Check data
if (getItemStack().getData().getData() != item.getData().getData()) {
return -1;
}

return determination;
}

Expand Down Expand Up @@ -382,11 +349,11 @@ public void setItemScript(ItemScriptContainer script) {
setItemStack(NMSHandler.getItemHelper().addNbtData(getItemStack(), "Denizen Item Script", new StringTag(script.getHashID())));
}
else {
ItemMeta meta = item.getItemMeta();
ItemMeta meta = getItemMeta();
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
lore.add(0, script.getHashID());
meta.setLore(lore);
item.setItemMeta(meta);
setItemMeta(meta);
}
}

Expand Down
Expand Up @@ -44,9 +44,8 @@ private ItemKnowledgeBookRecipes(ItemTag _item) {

public ListTag recipeList() {
ListTag output = new ListTag();
ItemStack itemStack = item.getItemStack();
if (itemStack.hasItemMeta() && itemStack.getItemMeta() instanceof KnowledgeBookMeta) {
for (NamespacedKey key : ((KnowledgeBookMeta) itemStack.getItemMeta()).getRecipes()) {
if (item.getItemMeta() instanceof KnowledgeBookMeta) {
for (NamespacedKey key : ((KnowledgeBookMeta) item.getItemMeta()).getRecipes()) {
output.add(key.toString());
}
}
Expand Down
Expand Up @@ -264,7 +264,6 @@ public ItemTag getItemFrom(TagContext context) {
// Set Enchantments
if (contains("enchantments")) {
for (String enchantment : getStringList("enchantments")) {

enchantment = TagManager.tag(enchantment, context);
try {
// Build enchantment context
Expand All @@ -279,7 +278,7 @@ public ItemTag getItemFrom(TagContext context) {
}
// Add enchantment
Enchantment ench = Utilities.getEnchantmentByName(enchantment);
stack.getItemStack().addUnsafeEnchantment(ench, level);
stack.getItemMeta().addEnchant(ench, level, true);
}
catch (Exception ex) {
Debug.echoError("While constructing '" + getName() + "', encountered error: '" + enchantment + "' is an invalid enchantment: " + ex.getClass().getName() + ": " + ex.getMessage());
Expand Down
Expand Up @@ -9,13 +9,10 @@
public class LeatherColorer {

public static void colorArmor(ItemTag item, String colorArg) {

if (item == null) {
return;
}

if (ColorTag.matches(colorArg)) {

try {
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
meta.setColor(ColorTag.valueOf(colorArg, CoreUtilities.basicContext).getColor());
Expand All @@ -25,6 +22,5 @@ public static void colorArmor(ItemTag item, String colorArg) {
Debug.echoError("Unable to color '" + item.identify() + "'.");
}
}

}
}

0 comments on commit 577accb

Please sign in to comment.