diff --git a/eco-api/build.gradle b/eco-api/build.gradle index 6cf7e918..87c890e1 100644 --- a/eco-api/build.gradle +++ b/eco-api/build.gradle @@ -6,6 +6,9 @@ group 'com.willfp' version rootProject.version dependencies { + // API Guava + api 'com.google.guava:guava:31.0.1-jre' + // Adventure compileOnly 'net.kyori:adventure-platform-bukkit:4.0.0' compileOnly 'net.kyori:adventure-text-minimessage:4.1.0-SNAPSHOT' @@ -15,7 +18,6 @@ dependencies { compileOnly 'org.apache.maven:maven-artifact:3.8.1' compileOnly 'com.comphenix.protocol:ProtocolLib:4.6.1-SNAPSHOT' compileOnly 'com.google.code.gson:gson:2.8.8' - compileOnly 'org.apache.commons:commons-lang3:3.0' } java { diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderEntry.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderEntry.java index 842e4cc4..c92f2098 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderEntry.java +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderEntry.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Objects; import java.util.function.Function; /** @@ -141,4 +142,21 @@ public class PlaceholderEntry { public void register() { PlaceholderManager.registerPlaceholder(this); } + + @Override + public boolean equals(@Nullable final Object o) { + if (this == o) { + return true; + } + if (!(o instanceof PlaceholderEntry entry)) { + return false; + } + return Objects.equals(this.getIdentifier(), entry.getIdentifier()) + && Objects.equals(this.getPlugin(), entry.getPlugin()); + } + + @Override + public int hashCode() { + return Objects.hash(this.getIdentifier(), this.getPlugin()); + } } diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderManager.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderManager.java index 1c1d8e41..c702fe15 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderManager.java +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderManager.java @@ -1,5 +1,8 @@ package com.willfp.eco.core.integrations.placeholder; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.willfp.eco.core.Eco; import com.willfp.eco.core.EcoPlugin; import org.bukkit.entity.Player; @@ -12,6 +15,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeUnit; /** * Class to handle placeholder integrations. @@ -27,6 +31,21 @@ public final class PlaceholderManager { */ private static final Set REGISTERED_INTEGRATIONS = new HashSet<>(); + /** + * Placeholder Cache. + */ + private static final LoadingCache PLACEHOLDER_CACHE = CacheBuilder.newBuilder() + .expireAfterWrite(50, TimeUnit.MILLISECONDS) + .build( + new CacheLoader<>() { + @Override + @NotNull + public String load(@NotNull final EntryWithPlayer key) { + return key.entry.getResult(key.player); + } + } + ); + /** * Register a new placeholder integration. * @@ -94,7 +113,7 @@ public final class PlaceholderManager { return ""; } - return entry.getResult(player); + return PLACEHOLDER_CACHE.getUnchecked(new EntryWithPlayer(entry, player)); } /** @@ -128,6 +147,11 @@ public final class PlaceholderManager { return found; } + private static record EntryWithPlayer(@NotNull PlaceholderEntry entry, + @Nullable Player player) { + + } + private PlaceholderManager() { throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); } diff --git a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java index 0f86f8d2..a4875f45 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java @@ -1,5 +1,8 @@ package com.willfp.eco.util; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gson.JsonSyntaxException; @@ -20,6 +23,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -66,6 +70,45 @@ public final class StringUtils { .emitLegacyHoverEvent() .build(); + /** + * Json -> Legacy Cache. + */ + private static final LoadingCache JSON_TO_LEGACY = CacheBuilder.newBuilder() + .expireAfterAccess(1, TimeUnit.SECONDS) + .build( + new CacheLoader<>() { + @Override + @NotNull + public String load(@NotNull final String json) { + try { + Component component = GSON_COMPONENT_SERIALIZER.deserialize(json); + return LEGACY_COMPONENT_SERIALIZER.serialize(component); + } catch (JsonSyntaxException e) { + return json; + } + } + } + ); + + /** + * Legacy -> Json Cache. + */ + private static final LoadingCache LEGACY_TO_JSON = CacheBuilder.newBuilder() + .expireAfterAccess(1, TimeUnit.SECONDS) + .build( + new CacheLoader<>() { + @Override + @NotNull + public String load(@NotNull final String legacy) { + return GSON_COMPONENT_SERIALIZER.serialize( + Component.empty().decoration(TextDecoration.ITALIC, false).append( + LEGACY_COMPONENT_SERIALIZER.deserialize(legacy) + ) + ); + } + } + ); + /** * Color map. */ @@ -423,11 +466,8 @@ public final class StringUtils { if (legacy == null) { processed = ""; } - return GSON_COMPONENT_SERIALIZER.serialize( - Component.empty().decoration(TextDecoration.ITALIC, false).append( - LEGACY_COMPONENT_SERIALIZER.deserialize(processed) - ) - ); + + return LEGACY_TO_JSON.getUnchecked(processed); } /** @@ -442,12 +482,7 @@ public final class StringUtils { return ""; } - try { - Component component = GSON_COMPONENT_SERIALIZER.deserialize(json); - return LEGACY_COMPONENT_SERIALIZER.serialize(component); - } catch (JsonSyntaxException e) { - return json; - } + return JSON_TO_LEGACY.getUnchecked(json); } /** diff --git a/eco-core/core-backend/build.gradle b/eco-core/core-backend/build.gradle index e08aa262..fa339d0b 100644 --- a/eco-core/core-backend/build.gradle +++ b/eco-core/core-backend/build.gradle @@ -7,6 +7,5 @@ dependencies { compileOnly 'org.reflections:reflections:0.9.12' compileOnly 'net.kyori:adventure-text-minimessage:4.1.0-SNAPSHOT' compileOnly 'net.kyori:adventure-platform-bukkit:4.0.0' - compileOnly 'com.google.guava:guava:31.0.1-jre' compileOnly 'org.objenesis:objenesis:3.2' } \ No newline at end of file diff --git a/eco-core/core-plugin/build.gradle b/eco-core/core-plugin/build.gradle index 135f1ae8..f6813a57 100644 --- a/eco-core/core-plugin/build.gradle +++ b/eco-core/core-plugin/build.gradle @@ -40,7 +40,6 @@ dependencies { compileOnly 'com.bgsoftware:SuperiorSkyblockAPI:1.8.3' compileOnly 'com.github.MilkBowl:VaultAPI:1.7' compileOnly 'world.bentobox:bentobox:1.17.3-SNAPSHOT' - compileOnly 'com.google.guava:guava:31.0.1-jre' compileOnly 'com.iridium:IridiumSkyblock:3.1.2' compileOnly 'com.github.WhipDevelopment:CrashClaim:f9cd7d92eb' compileOnly 'com.wolfyscript.wolfyutilities:wolfyutilities:3.16.0.0' diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoPlayerProfile.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoPlayerProfile.kt deleted file mode 100644 index ed7dac9d..00000000 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoPlayerProfile.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.willfp.eco.internal.spigot.data - -import com.willfp.eco.core.data.keys.PersistentDataKey -import com.willfp.eco.internal.spigot.data.storage.DataHandler -import java.util.UUID - diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfileHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfileHandler.kt index 7283f376..4e48329c 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfileHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfileHandler.kt @@ -23,22 +23,17 @@ class EcoProfileHandler( val data = mutableMapOf, Any>() - return if (uuid == serverProfileUUID) { - val profile = EcoServerProfile(data, handler) - loaded[uuid] = profile - profile - } else { - val profile = EcoPlayerProfile(data, uuid, handler) - loaded[uuid] = profile - profile - } + val profile = if (uuid == serverProfileUUID) + EcoServerProfile(data, handler) else EcoPlayerProfile(data, uuid, handler) + + loaded[uuid] = profile + return profile } override fun load(uuid: UUID): PlayerProfile { return loadGenericProfile(uuid) as PlayerProfile } - override fun loadServerProfile(): ServerProfile { return loadGenericProfile(serverProfileUUID) as ServerProfile } diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 975b74f3..076772c1 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -57,5 +57,4 @@ libraries: - 'mysql:mysql-connector-java:8.0.25' - 'com.google.guava:guava:31.0.1-jre' - 'com.zaxxer:HikariCP:5.0.0' - - 'org.apache.commons:commons-lang3:3.0' - 'org.objenesis:objenesis:3.2' \ No newline at end of file