Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
49 changes: 44 additions & 5 deletions src/main/java/me/thienbao860/expansion/world/WorldExpansion.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,13 +20,21 @@
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;

public class WorldExpansion extends PlaceholderExpansion implements Listener, Cacheable {
// 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<String, Integer> cache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.build();

private final Map<String, WorldData> worldData;

private Economy econ = null;
Expand All @@ -50,7 +60,7 @@ public WorldExpansion() {

@Override
public @NotNull String getVersion() {
return "1.2.3";
return "1.2.4";
}

@Override
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<Integer> 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;
Expand Down