diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/ItemFilter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/ItemFilter.java index 51bb743b69..759252df07 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/ItemFilter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/ItemFilter.java @@ -7,7 +7,6 @@ import javax.annotation.Nonnull; -import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; @@ -35,7 +34,7 @@ * @see CargoNetworkTask * */ -class ItemFilter implements Predicate { +public class ItemFilter implements Predicate { /** * Our {@link List} of items to check against, might be empty. @@ -207,7 +206,7 @@ public boolean test(@Nonnull ItemStack item) { * and thus only perform .getItemMeta() once */ for (ItemStackWrapper stack : items) { - boolean isSimilar = isSimilar(subject, stack, checkLore); + boolean isSimilar = SlimefunUtils.isItemSimilar(subject,stack,checkLore); if (!isSimilar) { continue; @@ -234,75 +233,4 @@ public boolean test(@Nonnull ItemStack item) { return rejectOnMatch; } - - /** Internal method to check if the items are the same, {@link SlimefunUtils#isItemSimilar} - * won't work as expected in this context so this method exists until a better fix is implemented - * - * @param first First {@link ItemStack} to check - * @param second Second {@link ItemStack} to check - * @param checkLore If {@literal true} lore will be checked too - */ - - private boolean isSimilar(ItemStack first, ItemStack second, boolean checkLore) { - if (first.getType() != second.getType()) { - return false; - } - - //region Check Slimefun - SlimefunItem firstSFitem = SlimefunItem.getByItem(first); - SlimefunItem secondSFitem = SlimefunItem.getByItem(second); - - if (firstSFitem == null ^ secondSFitem == null) { - return false; - } - - if (firstSFitem != null) { - //if slimefun item compare ids - return firstSFitem.getId().equals(secondSFitem.getId()); - } - //endregion - - //if we don't need to check the lore we are done with the checks - if (!checkLore) { - return true; - } - - //if both are null then same lore (no lore either of them) - if (!first.hasItemMeta() && !second.hasItemMeta()) { - return true; - } - - ItemMeta firstMeta = first.hasItemMeta() ? first.getItemMeta() : Bukkit.getItemFactory().getItemMeta(first.getType()); - ItemMeta secondMeta = second.hasItemMeta() ? second.getItemMeta() : Bukkit.getItemFactory().getItemMeta(second.getType()); - - return loreEquals(firstMeta.getLore(), secondMeta.getLore()); - } - - /** Checks if the 2 lore passed are the same - * - * @param loreA First list to check - * @param loreB Second list to check - * @return {@literal true} if the contents of the 2 lists are the same, {@literal false} otherwise - */ - private boolean loreEquals(List loreA, List loreB) { - //SlimefunUtils doesn't support null values - if (loreA == null && loreB == null) { - return true; - } - - if (loreA == null || loreB == null) { - return false; - } - - if (loreA.size() != loreB.size()) - return false; - - for (int i = 0; i < loreA.size(); i++) { - if (!loreA.get(i).equals(loreB.get(i))) { - return false; - } - } - - return true; - } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java index d93c3a877f..1787a38b73 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/SlimefunUtils.java @@ -13,6 +13,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; @@ -383,7 +384,7 @@ public static boolean isItemSimilar(@Nullable ItemStack item, @Nullable ItemStac ItemMetaSnapshot meta = ((SlimefunItemStack) sfitem).getItemMetaSnapshot(); return equalsItemMeta(itemMeta, meta, checkLore); - } else if (sfitem instanceof ItemStackWrapper && sfitem.hasItemMeta()) { + } else if (sfitem instanceof ItemStackWrapper) { Debug.log(TestCase.CARGO_INPUT_TESTING, " is wrapper"); /* * Cargo optimization (PR #3258) @@ -393,7 +394,7 @@ public static boolean isItemSimilar(@Nullable ItemStack item, @Nullable ItemStac */ Debug.log(TestCase.CARGO_INPUT_TESTING, " sfitem is ItemStackWrapper - possible SF Item: {}", sfitem); - ItemMeta possibleSfItemMeta = sfitem.getItemMeta(); + ItemMeta possibleSfItemMeta = sfitem.hasItemMeta() ? sfitem.getItemMeta() : Bukkit.getItemFactory().getItemMeta(sfitem.getType()); String id = Slimefun.getItemDataService().getItemData(itemMeta).orElse(null); String possibleItemId = Slimefun.getItemDataService().getItemData(possibleSfItemMeta).orElse(null); // Prioritize SlimefunItem id comparison over ItemMeta comparison @@ -440,10 +441,22 @@ public static boolean areSameEnchants(ItemStack first, ItemStack second) { ItemMeta firstM = first.getItemMeta(); ItemMeta secondM = second.getItemMeta(); - if (!(firstM instanceof EnchantmentStorageMeta || secondM instanceof EnchantmentStorageMeta)) { + boolean frstEnchant = firstM instanceof EnchantmentStorageMeta; + boolean sndEnchant = secondM instanceof EnchantmentStorageMeta; + + if (frstEnchant != sndEnchant) { + return false; + } + + if (!frstEnchant) { return true; } + /* + if (!(firstM instanceof EnchantmentStorageMeta || secondM instanceof EnchantmentStorageMeta)) { + return true; + }*/ + EnchantmentStorageMeta stEnch = (EnchantmentStorageMeta) firstM; EnchantmentStorageMeta ndEnch = (EnchantmentStorageMeta) secondM; @@ -451,22 +464,6 @@ public static boolean areSameEnchants(ItemStack first, ItemStack second) { Map enchantsSecond = ndEnch.getStoredEnchants(); return enchantsFirst.equals(enchantsSecond); - /* - - if (enchantsFirst.size() != enchantsSecond.size()) { - return false; - } - - for (var enchantment : enchantsFirst.entrySet()) { - // Check if the levels of the enchantments are equal - if (!Objects.equals(enchantment.getValue(), enchantsSecond.get(enchantment.getKey()))) { - return false; - } - } - - Bukkit.getLogger().info("enchants complete"); - - return true;*/ } private static @Nonnull Optional getDistinctiveItem(@Nonnull String id) { @@ -504,6 +501,7 @@ private static boolean equalsItemMeta(@Nonnull ItemMeta itemMeta, @Nonnull ItemM } private static boolean equalsItemMeta(@Nonnull ItemMeta itemMeta, @Nonnull ItemMeta sfitemMeta, boolean checkLore) { + Bukkit.getLogger().info("Inside itemMeta"); if (itemMeta.hasDisplayName() != sfitemMeta.hasDisplayName()) { return false; } else if (itemMeta.hasDisplayName() && sfitemMeta.hasDisplayName() && !itemMeta.getDisplayName().equals(sfitemMeta.getDisplayName())) {