diff --git a/build.gradle.kts b/build.gradle.kts index 782df8f..2cdd95d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,26 +3,27 @@ plugins { } group = "me.thienbao860" -version = "1.2.3" +version = "1.2.4" repositories { mavenCentral() - maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") + maven("https://repo.papermc.io/repository/maven-public/") maven("https://repo.extendedclip.com/releases/") maven("https://jitpack.io") } dependencies { - compileOnly("org.spigotmc:spigot-api:+") + compileOnly("io.papermc.paper:paper-api:1.21.5-R0.1-SNAPSHOT") compileOnly("me.clip:placeholderapi:2.11.6") compileOnly("com.github.MilkBowl:VaultAPI:1.7.1") } tasks { java { - sourceCompatibility = JavaVersion.VERSION_1_8 + disableAutoTargetJvm() targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_1_8 withJavadocJar() withSourcesJar() diff --git a/src/main/java/me/thienbao860/expansion/world/WorldExpansion.java b/src/main/java/me/thienbao860/expansion/world/WorldExpansion.java index 45b4081..da7e559 100644 --- a/src/main/java/me/thienbao860/expansion/world/WorldExpansion.java +++ b/src/main/java/me/thienbao860/expansion/world/WorldExpansion.java @@ -1,5 +1,7 @@ package me.thienbao860.expansion.world; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.google.common.primitives.Ints; import me.clip.placeholderapi.expansion.Cacheable; import me.clip.placeholderapi.expansion.PlaceholderExpansion; @@ -18,6 +20,10 @@ import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -25,6 +31,10 @@ public class WorldExpansion extends PlaceholderExpansion implements Listener, Ca // Define a regular expression pattern to match the strings private final static Pattern PATTERN = Pattern.compile("(\\b\\w+\\b)|(\\b\\w+\\b)(\\B_+?\\B)(?<=\\*)\\w+(?=\\*(?:_|$))"); + private final Cache cache = CacheBuilder.newBuilder() + .expireAfterWrite(1, TimeUnit.MINUTES) + .build(); + private final Map worldData; private Economy econ = null; @@ -50,7 +60,7 @@ public WorldExpansion() { @Override public @NotNull String getVersion() { - return "1.2.3"; + return "1.2.4"; } @Override @@ -136,12 +146,24 @@ public String onRequest(final @Nullable OfflinePlayer offlinePlayer, final @NotN return String.valueOf(world.getAllowMonsters()); case "difficulty": return world.getDifficulty().name().toLowerCase(); - case "entities": - if (args.length < 3 || !"living".equals(args[1])) { - return String.valueOf(world.getEntities().size()); + + case "total": + if (args.length < 3) { + return null; + } + + switch (args[1]) { + case "entities": + return getFromCache("totalEntities" + world.getName(), () -> world.getEntities().size()); + case "living": + if (args.length < 4 || !args[2].equals("living")) { + return null; + } + return getFromCache("totalLivingEntities" + world.getName(), () -> world.getLivingEntities().size()); + case "chunks": + return getFromCache("totalChunks" + world.getName(), () -> world.getLoadedChunks().length); } - return String.valueOf(world.getLivingEntities().size()); case "players": if (args.length == 2) { return String.valueOf(world.getPlayers().size()); @@ -347,8 +369,25 @@ private String timeFormat(final long tick, final boolean is12) { return list.toArray(new String[0]); } + /** + * Get a value from the {@link #cache}. Imported from Server-Expansion + * + * @param key key + * @param callable cache method + * @return value if found otherwise empty + */ + private @NotNull String getFromCache(@NotNull final String key, @NotNull final Callable callable) { + try { + return String.valueOf(cache.get(key, callable)); + } catch (ExecutionException e) { + getPlaceholderAPI().getLogger().log(Level.SEVERE, "Could not get key \"" + key + "\" from cache", e); + return ""; + } + } + @Override public void clear() { + this.cache.invalidateAll(); this.worldData.clear(); this.econ = null; this.perms = null;