Skip to content

Commit

Permalink
Changes to SlimefunUtils#isItemSimilar
Browse files Browse the repository at this point in the history
Now allows ItemStackWrapper with no meta, by creating one with ItemFactory, this will improve performance until isItemSimilar will be changed.
  • Loading branch information
Intybyte committed May 14, 2024
1 parent 94283dc commit 2483710
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,7 +34,7 @@
* @see CargoNetworkTask
*
*/
class ItemFilter implements Predicate<ItemStack> {
public class ItemFilter implements Predicate<ItemStack> {

/**
* Our {@link List} of items to check against, might be empty.
Expand Down Expand Up @@ -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;
Expand All @@ -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<String> loreA, List<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -440,33 +441,29 @@ 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;

Map<Enchantment, Integer> enchantsFirst = stEnch.getStoredEnchants();
Map<Enchantment, Integer> 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<DistinctiveItem> getDistinctiveItem(@Nonnull String id) {
Expand Down Expand Up @@ -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())) {
Expand Down

0 comments on commit 2483710

Please sign in to comment.