Skip to content

Commit

Permalink
Updated TalismanChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Oct 5, 2021
1 parent d542f74 commit 167f9e4
Showing 1 changed file with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,6 +38,11 @@ public class TalismanChecks {
*/
public static final Map<UUID, Set<TalismanLevel>> CACHED_TALISMANS = Collections.synchronizedMap(new HashMap<>());

/**
* Cached items.
*/
public static final Map<UUID, Set<ItemStack>> CACHED_TALISMAN_ITEMS = Collections.synchronizedMap(new WeakHashMap<>());

/**
* All providers.
*/
Expand Down Expand Up @@ -142,26 +148,26 @@ public static Set<TalismanLevel> 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<TalismanLevel> getTalismansOnPlayer(@NotNull final Player player,
public static Set<ItemStack> getTalismanItemsOnPlayer(@NotNull final Player player,
final boolean useCache,
@NotNull final ItemStack... extra) {
if (useCache) {
Set<TalismanLevel> cached = CACHED_TALISMANS.get(player.getUniqueId());
Set<ItemStack> cached = CACHED_TALISMAN_ITEMS.get(player.getUniqueId());
if (cached != null) {
return cached;
}
}

List<ItemStack> contents = new ArrayList<>();
Set<TalismanLevel> found = new HashSet<>();

List<ItemStack> rawContents = new ArrayList<>(Arrays.asList(player.getInventory().getContents()));

Expand Down Expand Up @@ -201,6 +207,50 @@ public static Set<TalismanLevel> getTalismansOnPlayer(@NotNull final Player play
contents.add(rawContent);
}

Set<ItemStack> 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<TalismanLevel> getTalismansOnPlayer(@NotNull final Player player,
final boolean useCache,
@NotNull final ItemStack... extra) {
if (useCache) {
Set<TalismanLevel> cached = CACHED_TALISMANS.get(player.getUniqueId());
if (cached != null) {
return cached;
}
}

Set<ItemStack> contents = getTalismanItemsOnPlayer(player, useCache, extra);
Set<TalismanLevel> found = new HashSet<>();

for (ItemStack itemStack : contents) {
TalismanLevel talisman = getTalismanOnItem(itemStack);
if (talisman == null) {
Expand Down

0 comments on commit 167f9e4

Please sign in to comment.