From 5b250a91af897511e3d4fe06425a01db5375f32e Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 25 Dec 2021 14:39:02 +0100 Subject: [PATCH] refactoring & push to 1.18 --- pom.xml | 22 ++++---- src/main/java/tsp/headdb/HeadDB.java | 5 +- src/main/java/tsp/headdb/api/HeadAPI.java | 4 +- .../tsp/headdb/command/Command_headdb.java | 4 +- .../tsp/headdb/database/HeadDatabase.java | 53 +++++++++++++------ src/main/java/tsp/headdb/util/Utils.java | 1 - src/main/resources/config.yml | 2 +- 7 files changed, 54 insertions(+), 37 deletions(-) diff --git a/pom.xml b/pom.xml index 989c440..421e714 100644 --- a/pom.xml +++ b/pom.xml @@ -6,17 +6,17 @@ tsp.headdb HeadDB - 2.4.4 + 2.4.5 jar HeadDB Database with thousands of heads - + - papermc - https://papermc.io/repo/repository/maven-public/ + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ @@ -36,11 +36,11 @@ - + - io.papermc.paper - paper-api - 1.17-R0.1-SNAPSHOT + org.spigotmc + spigot-api + 1.18-R0.1-SNAPSHOT provided @@ -66,7 +66,7 @@ net.wesjd anvilgui - 1.5.1-SNAPSHOT + 1.5.3-SNAPSHOT @@ -92,8 +92,8 @@ maven-compiler-plugin 3.8.1 - 1.8 - 1.8 + 9 + 9 diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index e2b639e..5c9aad4 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -1,7 +1,6 @@ package tsp.headdb; import de.leonhard.storage.Config; -import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import tsp.headdb.api.HeadAPI; import tsp.headdb.command.Command_headdb; @@ -40,10 +39,10 @@ public class HeadDB extends JavaPlugin { if (storage.getConfig().getBoolean("fetchStartup")) { if (storage.getConfig().getBoolean("asyncStartup")) { Log.debug("Initializing Database... (ASYNC)"); - Bukkit.getScheduler().runTaskAsynchronously(this, () -> HeadAPI.getDatabase().update()); + HeadAPI.getDatabase().updateAsync(); } else { Log.debug("Initializing Database... (SYNC)"); - HeadAPI.getDatabase().update(); + HeadAPI.updateDatabase(); } } diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java index e36ea38..a8efe4b 100644 --- a/src/main/java/tsp/headdb/api/HeadAPI.java +++ b/src/main/java/tsp/headdb/api/HeadAPI.java @@ -212,8 +212,8 @@ public final class HeadAPI { /** * Update the Head Database */ - public static void updateDatabase() { - database.update(); + public static boolean updateDatabase() { + return database.update(); } } diff --git a/src/main/java/tsp/headdb/command/Command_headdb.java b/src/main/java/tsp/headdb/command/Command_headdb.java index 6dd8057..a01c2df 100644 --- a/src/main/java/tsp/headdb/command/Command_headdb.java +++ b/src/main/java/tsp/headdb/command/Command_headdb.java @@ -136,7 +136,7 @@ public class Command_headdb implements CommandExecutor { Utils.sendMessage(sender, "Updating..."); long start = System.currentTimeMillis(); - boolean result = HeadAPI.getDatabase().update(); + boolean result = HeadAPI.updateDatabase(); if (result) { Utils.sendMessage(sender, "&aDone! Took: " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start) + " seconds"); } else { @@ -152,7 +152,7 @@ public class Command_headdb implements CommandExecutor { } Utils.sendMessage(sender, "Updating..."); - Bukkit.getScheduler().runTaskAsynchronously(HeadDB.getInstance(), () -> HeadAPI.getDatabase().update()); + HeadAPI.getDatabase().updateAsync(); return true; } diff --git a/src/main/java/tsp/headdb/database/HeadDatabase.java b/src/main/java/tsp/headdb/database/HeadDatabase.java index 5212ed5..f04c039 100644 --- a/src/main/java/tsp/headdb/database/HeadDatabase.java +++ b/src/main/java/tsp/headdb/database/HeadDatabase.java @@ -1,5 +1,6 @@ package tsp.headdb.database; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; import org.json.simple.JSONArray; @@ -22,6 +23,7 @@ import java.util.Locale; import java.util.Map; import java.util.UUID; import java.util.concurrent.TimeUnit; +import javax.annotation.Nonnull; import javax.annotation.Nullable; /** @@ -32,9 +34,9 @@ import javax.annotation.Nullable; public class HeadDatabase { private final JavaPlugin plugin; - private final long refresh; - private int timeout; private final Map> HEADS = new HashMap<>(); + private long refresh; + private int timeout; private long updated; public HeadDatabase(JavaPlugin plugin) { @@ -43,12 +45,6 @@ public class HeadDatabase { this.timeout = 5000; } - public HeadDatabase(JavaPlugin plugin, long refresh) { - this.plugin = plugin; - this.refresh = refresh; - this.timeout = 5000; - } - public Head getHeadByValue(String value) { List heads = getHeads(); for (Head head : heads) { @@ -123,23 +119,28 @@ public class HeadDatabase { return HEADS.get(category); } + /** + * Gets all heads from the cache if available. + * + * @return List containing each head in its category. + */ + @Nonnull public List getHeads() { - if (!HEADS.isEmpty() && !isLastUpdateOld()) { - List heads = new ArrayList<>(); - for (Category category : HEADS.keySet()) { - heads.addAll(HEADS.get(category)); - } - return heads; + if (HEADS.isEmpty() || isLastUpdateOld()) { + update(); } - update(); - return getHeads(); + List heads = new ArrayList<>(); + for (Category category : HEADS.keySet()) { + heads.addAll(HEADS.get(category)); + } + return heads; } /** * Gets all heads from the api provider * - * @return Map containing each head in it's category. Returns null if the fetching failed. + * @return Map containing each head in its category. Returns null if the fetching failed. */ @Nullable public Map> getHeadsNoCache() { @@ -220,12 +221,26 @@ public class HeadDatabase { return true; } + public void updateAsync() { + Bukkit.getScheduler().runTaskAsynchronously(plugin, this::update); + } + + /** + * Get the last time the database was updated. + * + * @return Last update in seconds + */ public long getLastUpdate() { long now = System.nanoTime(); long elapsed = now - updated; return TimeUnit.NANOSECONDS.toSeconds(elapsed); } + /** + * Checks if the update is past the refresh time + * + * @return Whether the update is old + */ public boolean isLastUpdateOld() { return getLastUpdate() >= refresh; } @@ -242,6 +257,10 @@ public class HeadDatabase { return refresh; } + public void setRefresh(long refresh) { + this.refresh = refresh; + } + public JavaPlugin getPlugin() { return plugin; } diff --git a/src/main/java/tsp/headdb/util/Utils.java b/src/main/java/tsp/headdb/util/Utils.java index a107138..e0744dc 100644 --- a/src/main/java/tsp/headdb/util/Utils.java +++ b/src/main/java/tsp/headdb/util/Utils.java @@ -7,7 +7,6 @@ import java.util.regex.Pattern; public class Utils { - public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); /** diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a29feea..76ccb8d 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -2,7 +2,7 @@ fetchStartup: true # When enabled, heads will be fetched async (Startup Only) -asyncStartup: false +asyncStartup: true # If the cached heads are older than these amount of seconds, the plugin will refresh the database refresh: 3600