From 47f4f5b2b12f06bf0c1bfb48826f4378b23d7e5d Mon Sep 17 00:00:00 2001 From: Silent Date: Mon, 2 Aug 2021 18:03:30 +0200 Subject: [PATCH] refactor and localHeads option --- pom.xml | 6 +-- src/main/java/tsp/headdb/HeadDB.java | 6 +-- .../tsp/headdb/database/HeadDatabase.java | 7 ++-- .../headdb/event/HeadDatabaseUpdateEvent.java | 38 ------------------- .../tsp/headdb/listener/JoinListener.java | 5 ++- src/main/java/tsp/headdb/util/Utils.java | 10 ++++- src/main/resources/config.yml | 3 ++ 7 files changed, 24 insertions(+), 51 deletions(-) delete mode 100644 src/main/java/tsp/headdb/event/HeadDatabaseUpdateEvent.java diff --git a/pom.xml b/pom.xml index 78d7f21..44c792c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 2.4.0 + 2.4.1 jar HeadDB @@ -92,8 +92,8 @@ maven-compiler-plugin 3.8.1 - 16 - 16 + 1.8 + 1.8 diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 3bfb3f6..e2b639e 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -11,7 +11,6 @@ import tsp.headdb.listener.MenuListener; import tsp.headdb.util.Log; import tsp.headdb.util.Metrics; import tsp.headdb.util.Storage; -import tsp.headdb.util.Utils; import org.bukkit.plugin.RegisteredServiceProvider; import net.milkbowl.vault.economy.Economy; @@ -48,9 +47,6 @@ public class HeadDB extends JavaPlugin { } } - Log.debug("Starting metrics..."); - new Metrics(this, Utils.METRICS_ID); - Log.debug("Registering listeners..."); new PagedPaneListener(this); new MenuListener(this); @@ -59,6 +55,8 @@ public class HeadDB extends JavaPlugin { Log.debug("Registering commands..."); getCommand("headdb").setExecutor(new Command_headdb()); + Log.debug("Starting metrics..."); + new Metrics(this, 9152); Log.info("Done!"); } diff --git a/src/main/java/tsp/headdb/database/HeadDatabase.java b/src/main/java/tsp/headdb/database/HeadDatabase.java index eec0107..e37dadd 100644 --- a/src/main/java/tsp/headdb/database/HeadDatabase.java +++ b/src/main/java/tsp/headdb/database/HeadDatabase.java @@ -1,6 +1,5 @@ package tsp.headdb.database; -import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; import org.json.simple.JSONArray; @@ -8,8 +7,8 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import tsp.headdb.api.Head; -import tsp.headdb.event.HeadDatabaseUpdateEvent; import tsp.headdb.util.Log; +import tsp.headdb.util.Utils; import java.io.BufferedReader; import java.io.IOException; @@ -164,10 +163,11 @@ public class HeadDatabase { for (Object o : array) { JSONObject obj = (JSONObject) o; String uuid = obj.get("uuid").toString(); + Log.debug(!Utils.isValid(uuid) + "Invalid UUID: " + uuid); Head head = new Head(id) .withName(obj.get("name").toString()) - .withUniqueId(uuid.isEmpty() ? UUID.randomUUID() : UUID.fromString(uuid)) + .withUniqueId(Utils.isValid(uuid) ? UUID.fromString(uuid) : UUID.randomUUID()) .withValue(obj.get("value").toString()) .withTags(obj.get("tags") != null ? obj.get("tags").toString() : "None") .withCategory(category); @@ -207,7 +207,6 @@ public class HeadDatabase { for (Map.Entry> entry : heads.entrySet()) { HEADS.put(entry.getKey(), entry.getValue()); } - Bukkit.getPluginManager().callEvent(new HeadDatabaseUpdateEvent(this)); return true; } diff --git a/src/main/java/tsp/headdb/event/HeadDatabaseUpdateEvent.java b/src/main/java/tsp/headdb/event/HeadDatabaseUpdateEvent.java deleted file mode 100644 index abf3c2f..0000000 --- a/src/main/java/tsp/headdb/event/HeadDatabaseUpdateEvent.java +++ /dev/null @@ -1,38 +0,0 @@ -package tsp.headdb.event; - -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; -import org.jetbrains.annotations.NotNull; -import tsp.headdb.database.HeadDatabase; - -public class HeadDatabaseUpdateEvent extends Event implements Cancellable { - - private final HandlerList handlerList = new HandlerList(); - private boolean cancelled; - private final HeadDatabase database; - - public HeadDatabaseUpdateEvent(HeadDatabase database) { - this.database = database; - } - - public HeadDatabase getDatabase() { - return database; - } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean b) { - this.cancelled = b; - } - - @Override - public @NotNull HandlerList getHandlers() { - return handlerList; - } - -} diff --git a/src/main/java/tsp/headdb/listener/JoinListener.java b/src/main/java/tsp/headdb/listener/JoinListener.java index 7322f81..2ecdd2a 100644 --- a/src/main/java/tsp/headdb/listener/JoinListener.java +++ b/src/main/java/tsp/headdb/listener/JoinListener.java @@ -8,7 +8,10 @@ import tsp.headdb.HeadDB; public class JoinListener implements Listener { public JoinListener(HeadDB plugin) { - plugin.getServer().getPluginManager().registerEvents(this, plugin); + // If local heads are disabled, there is no need for this listener. + if (HeadDB.getInstance().getConfiguration().getBoolean("localHeads")) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } } @EventHandler diff --git a/src/main/java/tsp/headdb/util/Utils.java b/src/main/java/tsp/headdb/util/Utils.java index 73a2c3f..b49d990 100644 --- a/src/main/java/tsp/headdb/util/Utils.java +++ b/src/main/java/tsp/headdb/util/Utils.java @@ -3,9 +3,17 @@ package tsp.headdb.util; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Utils { - public static final int METRICS_ID = 9152; + public static final Pattern UUID_PATTERN = Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); + + public static boolean isValid(String uuid) { + Matcher matcher = UUID_PATTERN.matcher(uuid); + return matcher.find(); + } public static void sendMessage(CommandSender sender, String message) { sender.sendMessage(colorize(message)); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4f33ee5..a29feea 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,6 +7,9 @@ asyncStartup: false # If the cached heads are older than these amount of seconds, the plugin will refresh the database refresh: 3600 +# If local heads should be enabled. Only keeps track of joined players when enabled! +localHeads: true + # Economy options economy: enable: false