diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/talismans/util/TalismanChecks.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/talismans/util/TalismanChecks.java index f178899f..785d31d3 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/talismans/talismans/util/TalismanChecks.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/talismans/util/TalismanChecks.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.WeakHashMap; import java.util.function.Function; @UtilityClass @@ -37,6 +38,11 @@ public class TalismanChecks { */ public static final Map> CACHED_TALISMANS = Collections.synchronizedMap(new HashMap<>()); + /** + * Cached items. + */ + public static final Map> CACHED_TALISMAN_ITEMS = Collections.synchronizedMap(new WeakHashMap<>()); + /** * All providers. */ @@ -142,26 +148,26 @@ public static Set getTalismansOnPlayer(@NotNull final Player play return getTalismansOnPlayer(player, true); } + /** - * Get all talismans that a player has active. + * Get all talismans ItemStacks that a player has active. * * @param player The player to query. * @param useCache If the cache should be checked. * @param extra Bonus items. * @return A set of all found talismans. */ - public static Set getTalismansOnPlayer(@NotNull final Player player, + public static Set getTalismanItemsOnPlayer(@NotNull final Player player, final boolean useCache, @NotNull final ItemStack... extra) { if (useCache) { - Set cached = CACHED_TALISMANS.get(player.getUniqueId()); + Set cached = CACHED_TALISMAN_ITEMS.get(player.getUniqueId()); if (cached != null) { return cached; } } List contents = new ArrayList<>(); - Set found = new HashSet<>(); List rawContents = new ArrayList<>(Arrays.asList(player.getInventory().getContents())); @@ -201,6 +207,50 @@ public static Set getTalismansOnPlayer(@NotNull final Player play contents.add(rawContent); } + Set items = new HashSet<>(); + + for (ItemStack itemStack : contents) { + TalismanLevel talisman = getTalismanOnItem(itemStack); + if (talisman == null) { + continue; + } + + if (items.size() >= TalismanUtils.getLimit(player)) { + break; + } + + items.add(itemStack); + } + + if (useCache) { + CACHED_TALISMAN_ITEMS.put(player.getUniqueId(), items); + PLUGIN.getScheduler().runLater(() -> CACHED_TALISMAN_ITEMS.remove(player.getUniqueId()), 40); + } + + return items; + } + + /** + * Get all talismans that a player has active. + * + * @param player The player to query. + * @param useCache If the cache should be checked. + * @param extra Bonus items. + * @return A set of all found talismans. + */ + public static Set getTalismansOnPlayer(@NotNull final Player player, + final boolean useCache, + @NotNull final ItemStack... extra) { + if (useCache) { + Set cached = CACHED_TALISMANS.get(player.getUniqueId()); + if (cached != null) { + return cached; + } + } + + Set contents = getTalismanItemsOnPlayer(player, useCache, extra); + Set found = new HashSet<>(); + for (ItemStack itemStack : contents) { TalismanLevel talisman = getTalismanOnItem(itemStack); if (talisman == null) {