From 6deea17b8533e1ccef797a7eaa18212cd036f332 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 6 Mar 2022 13:00:58 +0000 Subject: [PATCH] Fixed CustomEnchantLookup performance issues --- eco-core/core-plugin/build.gradle | 1 + .../custom/CustomEnchantLookup.java | 106 +++++++++--------- 2 files changed, 51 insertions(+), 56 deletions(-) diff --git a/eco-core/core-plugin/build.gradle b/eco-core/core-plugin/build.gradle index 5a4bf8ea..03d4be46 100644 --- a/eco-core/core-plugin/build.gradle +++ b/eco-core/core-plugin/build.gradle @@ -10,5 +10,6 @@ dependencies { compileOnly 'io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT' compileOnly 'com.sk89q.worldguard:worldguard-bukkit:7.0.4-SNAPSHOT' compileOnly 'io.lumine.xikage:MythicMobs:4.9.1' + compileOnly 'com.github.ben-manes.caffeine:caffeine:3.0.5' compileOnly fileTree(dir: '../../lib', include: ['*.jar']) } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEnchantLookup.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEnchantLookup.java index 55e8f1e1..f58b486e 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEnchantLookup.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/custom/CustomEnchantLookup.java @@ -1,5 +1,7 @@ package com.willfp.ecoenchants.enchantments.custom; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import com.willfp.eco.core.EcoPlugin; import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.enchantments.EcoEnchant; @@ -17,8 +19,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.UUID; -import java.util.WeakHashMap; +import java.util.concurrent.TimeUnit; import java.util.function.Function; public class CustomEnchantLookup { @@ -30,12 +31,16 @@ public class CustomEnchantLookup { /** * Cached items. */ - private static final Map> ITEM_CACHE = new WeakHashMap<>(); + private static final Cache> ITEM_CACHE = Caffeine.newBuilder() + .expireAfterWrite(2, TimeUnit.SECONDS) + .build(); /** * Cached enchant levels. */ - private static final Map> ENCHANT_LEVELS_CACHE = new WeakHashMap<>(); + private static final Cache> ENCHANT_LEVELS_CACHE = Caffeine.newBuilder() + .expireAfterWrite(2, TimeUnit.SECONDS) + .build(); /** * Instance of EcoEnchants. @@ -54,73 +59,62 @@ public class CustomEnchantLookup { /** * Provide ItemStacks. * - * @param player The player. + * @param p The player. * @return The ItemStacks. */ - public static Map provide(@NotNull final Player player) { - if (ITEM_CACHE.containsKey(player.getUniqueId())) { - return new HashMap<>(ITEM_CACHE.get(player.getUniqueId())); - } - - Map found = new HashMap<>(); - for (Function> provider : PROVIDERS) { - found.putAll(provider.apply(player)); - } - found.keySet().removeIf(Objects::isNull); - - ITEM_CACHE.put(player.getUniqueId(), found); - PLUGIN.getScheduler().runLater(() -> ITEM_CACHE.remove(player.getUniqueId()), 40); - - return found; + public static Map provide(@NotNull final Player p) { + return ITEM_CACHE.get(p, player -> { + Map found = new HashMap<>(); + for (Function> provider : PROVIDERS) { + found.putAll(provider.apply(player)); + } + found.keySet().removeIf(Objects::isNull); + return found; + }); } /** * Provide levels. * - * @param player The player. + * @param p The player. * @return The levels. */ - public static List provideLevels(@NotNull final Player player) { - if (ENCHANT_LEVELS_CACHE.containsKey(player.getUniqueId())) { - return new ArrayList<>(ENCHANT_LEVELS_CACHE.get(player.getUniqueId())); - } + public static List provideLevels(@NotNull final Player p) { + return new ArrayList<>(ENCHANT_LEVELS_CACHE.get(p, player -> { + List found = new ArrayList<>(); - List found = new ArrayList<>(); - - for (Map.Entry entry : provide(player).entrySet()) { - ItemStack itemStack = entry.getKey(); - EnchantmentTarget.Slot slot = entry.getValue(); - if (itemStack == null) { - continue; - } - - Map enchants = EnchantChecks.getEnchantsOnItem(itemStack); - - if (enchants.isEmpty()) { - continue; - } - - for (Map.Entry enchantEntry : enchants.entrySet()) { - if (!(enchantEntry.getKey() instanceof CustomEcoEnchant enchant)) { + for (Map.Entry entry : provide(player).entrySet()) { + ItemStack itemStack = entry.getKey(); + EnchantmentTarget.Slot slot = entry.getValue(); + if (itemStack == null) { continue; } - if (slot != EnchantmentTarget.Slot.ANY) { - if (!enchant.getTargets().stream() - .map(EnchantmentTarget::getSlot).toList() - .contains(slot)) { - continue; - } + Map enchants = EnchantChecks.getEnchantsOnItem(itemStack); + + if (enchants.isEmpty()) { + continue; } - found.add(enchant.getLevel(enchantEntry.getValue())); + for (Map.Entry enchantEntry : enchants.entrySet()) { + if (!(enchantEntry.getKey() instanceof CustomEcoEnchant enchant)) { + continue; + } + + if (slot != EnchantmentTarget.Slot.ANY) { + if (!enchant.getTargets().stream() + .map(EnchantmentTarget::getSlot).toList() + .contains(slot)) { + continue; + } + } + + found.add(enchant.getLevel(enchantEntry.getValue())); + } } - } - ENCHANT_LEVELS_CACHE.put(player.getUniqueId(), found); - PLUGIN.getScheduler().runLater(() -> ENCHANT_LEVELS_CACHE.remove(player.getUniqueId()), 40); - - return found; + return found; + })); } /** @@ -129,8 +123,8 @@ public class CustomEnchantLookup { * @param player The player. */ public static void clearCache(@NotNull final Player player) { - ITEM_CACHE.remove(player.getUniqueId()); - ENCHANT_LEVELS_CACHE.remove(player.getUniqueId()); + ITEM_CACHE.invalidate(player); + ENCHANT_LEVELS_CACHE.invalidate(player); } static {