refactoring & push to 1.18

Dieser Commit ist enthalten in:
Silent 2021-12-25 14:39:02 +01:00
Ursprung 6c0979605b
Commit 5b250a91af
7 geänderte Dateien mit 54 neuen und 37 gelöschten Zeilen

22
pom.xml
Datei anzeigen

@ -6,17 +6,17 @@
<groupId>tsp.headdb</groupId> <groupId>tsp.headdb</groupId>
<artifactId>HeadDB</artifactId> <artifactId>HeadDB</artifactId>
<version>2.4.4</version> <version>2.4.5</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>HeadDB</name> <name>HeadDB</name>
<description>Database with thousands of heads</description> <description>Database with thousands of heads</description>
<repositories> <repositories>
<!-- Paper Repo --> <!-- Spigot Repo -->
<repository> <repository>
<id>papermc</id> <id>spigot-repo</id>
<url>https://papermc.io/repo/repository/maven-public/</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
<!-- Mojang Repo --> <!-- Mojang Repo -->
<repository> <repository>
@ -36,11 +36,11 @@
</repositories> </repositories>
<dependencies> <dependencies>
<!-- Paper API --> <!-- Spigot API -->
<dependency> <dependency>
<groupId>io.papermc.paper</groupId> <groupId>org.spigotmc</groupId>
<artifactId>paper-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.17-R0.1-SNAPSHOT</version> <version>1.18-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- json-simple --> <!-- json-simple -->
@ -66,7 +66,7 @@
<dependency> <dependency>
<groupId>net.wesjd</groupId> <groupId>net.wesjd</groupId>
<artifactId>anvilgui</artifactId> <artifactId>anvilgui</artifactId>
<version>1.5.1-SNAPSHOT</version> <version>1.5.3-SNAPSHOT</version>
</dependency> </dependency>
<!-- Vault --> <!-- Vault -->
<dependency> <dependency>
@ -92,8 +92,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<source>1.8</source> <source>9</source>
<target>1.8</target> <target>9</target>
</configuration> </configuration>
</plugin> </plugin>

Datei anzeigen

@ -1,7 +1,6 @@
package tsp.headdb; package tsp.headdb;
import de.leonhard.storage.Config; import de.leonhard.storage.Config;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import tsp.headdb.api.HeadAPI; import tsp.headdb.api.HeadAPI;
import tsp.headdb.command.Command_headdb; import tsp.headdb.command.Command_headdb;
@ -40,10 +39,10 @@ public class HeadDB extends JavaPlugin {
if (storage.getConfig().getBoolean("fetchStartup")) { if (storage.getConfig().getBoolean("fetchStartup")) {
if (storage.getConfig().getBoolean("asyncStartup")) { if (storage.getConfig().getBoolean("asyncStartup")) {
Log.debug("Initializing Database... (ASYNC)"); Log.debug("Initializing Database... (ASYNC)");
Bukkit.getScheduler().runTaskAsynchronously(this, () -> HeadAPI.getDatabase().update()); HeadAPI.getDatabase().updateAsync();
} else { } else {
Log.debug("Initializing Database... (SYNC)"); Log.debug("Initializing Database... (SYNC)");
HeadAPI.getDatabase().update(); HeadAPI.updateDatabase();
} }
} }

Datei anzeigen

@ -212,8 +212,8 @@ public final class HeadAPI {
/** /**
* Update the Head Database * Update the Head Database
*/ */
public static void updateDatabase() { public static boolean updateDatabase() {
database.update(); return database.update();
} }
} }

Datei anzeigen

@ -136,7 +136,7 @@ public class Command_headdb implements CommandExecutor {
Utils.sendMessage(sender, "Updating..."); Utils.sendMessage(sender, "Updating...");
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
boolean result = HeadAPI.getDatabase().update(); boolean result = HeadAPI.updateDatabase();
if (result) { if (result) {
Utils.sendMessage(sender, "&aDone! Took: " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start) + " seconds"); Utils.sendMessage(sender, "&aDone! Took: " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start) + " seconds");
} else { } else {
@ -152,7 +152,7 @@ public class Command_headdb implements CommandExecutor {
} }
Utils.sendMessage(sender, "Updating..."); Utils.sendMessage(sender, "Updating...");
Bukkit.getScheduler().runTaskAsynchronously(HeadDB.getInstance(), () -> HeadAPI.getDatabase().update()); HeadAPI.getDatabase().updateAsync();
return true; return true;
} }

Datei anzeigen

@ -1,5 +1,6 @@
package tsp.headdb.database; package tsp.headdb.database;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
@ -22,6 +23,7 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /**
@ -32,9 +34,9 @@ import javax.annotation.Nullable;
public class HeadDatabase { public class HeadDatabase {
private final JavaPlugin plugin; private final JavaPlugin plugin;
private final long refresh;
private int timeout;
private final Map<Category, List<Head>> HEADS = new HashMap<>(); private final Map<Category, List<Head>> HEADS = new HashMap<>();
private long refresh;
private int timeout;
private long updated; private long updated;
public HeadDatabase(JavaPlugin plugin) { public HeadDatabase(JavaPlugin plugin) {
@ -43,12 +45,6 @@ public class HeadDatabase {
this.timeout = 5000; this.timeout = 5000;
} }
public HeadDatabase(JavaPlugin plugin, long refresh) {
this.plugin = plugin;
this.refresh = refresh;
this.timeout = 5000;
}
public Head getHeadByValue(String value) { public Head getHeadByValue(String value) {
List<Head> heads = getHeads(); List<Head> heads = getHeads();
for (Head head : heads) { for (Head head : heads) {
@ -123,23 +119,28 @@ public class HeadDatabase {
return HEADS.get(category); return HEADS.get(category);
} }
/**
* Gets all heads from the cache if available.
*
* @return List containing each head in its category.
*/
@Nonnull
public List<Head> getHeads() { public List<Head> getHeads() {
if (!HEADS.isEmpty() && !isLastUpdateOld()) { if (HEADS.isEmpty() || isLastUpdateOld()) {
List<Head> heads = new ArrayList<>(); update();
for (Category category : HEADS.keySet()) {
heads.addAll(HEADS.get(category));
}
return heads;
} }
update(); List<Head> heads = new ArrayList<>();
return getHeads(); for (Category category : HEADS.keySet()) {
heads.addAll(HEADS.get(category));
}
return heads;
} }
/** /**
* Gets all heads from the api provider * 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 @Nullable
public Map<Category, List<Head>> getHeadsNoCache() { public Map<Category, List<Head>> getHeadsNoCache() {
@ -220,12 +221,26 @@ public class HeadDatabase {
return true; 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() { public long getLastUpdate() {
long now = System.nanoTime(); long now = System.nanoTime();
long elapsed = now - updated; long elapsed = now - updated;
return TimeUnit.NANOSECONDS.toSeconds(elapsed); return TimeUnit.NANOSECONDS.toSeconds(elapsed);
} }
/**
* Checks if the update is past the refresh time
*
* @return Whether the update is old
*/
public boolean isLastUpdateOld() { public boolean isLastUpdateOld() {
return getLastUpdate() >= refresh; return getLastUpdate() >= refresh;
} }
@ -242,6 +257,10 @@ public class HeadDatabase {
return refresh; return refresh;
} }
public void setRefresh(long refresh) {
this.refresh = refresh;
}
public JavaPlugin getPlugin() { public JavaPlugin getPlugin() {
return plugin; return plugin;
} }

Datei anzeigen

@ -7,7 +7,6 @@ import java.util.regex.Pattern;
public class Utils { 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}"); 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}");
/** /**

Datei anzeigen

@ -2,7 +2,7 @@
fetchStartup: true fetchStartup: true
# When enabled, heads will be fetched async (Startup Only) # 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 # If the cached heads are older than these amount of seconds, the plugin will refresh the database
refresh: 3600 refresh: 3600