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

Datei anzeigen

@ -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();
}
}

Datei anzeigen

@ -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();
}
}

Datei anzeigen

@ -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;
}

Datei anzeigen

@ -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<Category, List<Head>> 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<Head> heads = getHeads();
for (Head head : heads) {
@ -123,8 +119,17 @@ 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<Head> getHeads() {
if (!HEADS.isEmpty() && !isLastUpdateOld()) {
if (HEADS.isEmpty() || isLastUpdateOld()) {
update();
}
List<Head> heads = new ArrayList<>();
for (Category category : HEADS.keySet()) {
heads.addAll(HEADS.get(category));
@ -132,14 +137,10 @@ public class HeadDatabase {
return heads;
}
update();
return getHeads();
}
/**
* 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<Category, List<Head>> 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;
}

Datei anzeigen

@ -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}");
/**

Datei anzeigen

@ -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