From caab1e97a0e83d8b540442882e331e7bd9e6fe09 Mon Sep 17 00:00:00 2001 From: Silent Date: Fri, 18 Feb 2022 00:34:33 +0100 Subject: [PATCH] switch to gson and DatabaseUpdateEvent --- pom.xml | 2 +- .../tsp/headdb/database/HeadDatabase.java | 31 +++++------ .../tsp/headdb/event/DatabaseUpdateEvent.java | 54 +++++++++++++++++++ 3 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 src/main/java/tsp/headdb/event/DatabaseUpdateEvent.java diff --git a/pom.xml b/pom.xml index 163d046..b574444 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 3.2.2 + 3.2.3 jar HeadDB diff --git a/src/main/java/tsp/headdb/database/HeadDatabase.java b/src/main/java/tsp/headdb/database/HeadDatabase.java index e6a2c15..89e83d4 100644 --- a/src/main/java/tsp/headdb/database/HeadDatabase.java +++ b/src/main/java/tsp/headdb/database/HeadDatabase.java @@ -1,13 +1,14 @@ package tsp.headdb.database; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; -import org.json.simple.JSONArray; -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.DatabaseUpdateEvent; import tsp.headdb.util.Log; import tsp.headdb.util.Utils; @@ -17,6 +18,7 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -156,21 +158,13 @@ public class HeadDatabase { long start = System.currentTimeMillis(); List heads = new ArrayList<>(); try { - String line; - StringBuilder response = new StringBuilder(); - URLConnection connection = new URL("https://minecraft-heads.com/scripts/api.php?cat=" + category.getName() + "&tags=true").openConnection(); connection.setConnectTimeout(timeout); connection.setRequestProperty("User-Agent", plugin.getName() + "-DatabaseUpdater"); - try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { - while ((line = in.readLine()) != null) { - response.append(line); - } - } - JSONParser parser = new JSONParser(); - JSONArray array = (JSONArray) parser.parse(response.toString()); - for (Object o : array) { - JSONObject obj = (JSONObject) o; + + JsonArray array = JsonParser.parseReader(new BufferedReader(new InputStreamReader(connection.getInputStream()))).getAsJsonArray(); + for (JsonElement entry : array) { + JsonObject obj = entry.getAsJsonObject(); String rawUUID = obj.get("uuid").toString(); UUID uuid; @@ -193,7 +187,7 @@ public class HeadDatabase { long elapsed = (System.currentTimeMillis() - start); Log.debug(category.getName() + " -> Done! Time: " + elapsed + "ms (" + TimeUnit.MILLISECONDS.toSeconds(elapsed) + "s)"); - } catch (ParseException | IOException e) { + } catch (IOException e) { Log.error("[" + plugin.getName() + "] Failed to fetch heads (no-cache) | Stack Trace:"); e.printStackTrace(); } @@ -210,13 +204,14 @@ public class HeadDatabase { Bukkit.getScheduler().runTaskAsynchronously(plugin, task -> getHeadsNoCacheAsync(heads -> { if (heads == null) { Log.error("[" + plugin.getName() + "] Failed to update database! Check above for any errors."); - result.accept(null); + result.accept(Collections.emptyMap()); return; } HEADS.clear(); HEADS.putAll(heads); result.accept(heads); + Bukkit.getPluginManager().callEvent(new DatabaseUpdateEvent(this, heads)); })); } diff --git a/src/main/java/tsp/headdb/event/DatabaseUpdateEvent.java b/src/main/java/tsp/headdb/event/DatabaseUpdateEvent.java new file mode 100644 index 0000000..a0031b7 --- /dev/null +++ b/src/main/java/tsp/headdb/event/DatabaseUpdateEvent.java @@ -0,0 +1,54 @@ +package tsp.headdb.event; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import tsp.headdb.api.Head; +import tsp.headdb.database.Category; +import tsp.headdb.database.HeadDatabase; + +import java.util.List; +import java.util.Map; + +/** + * This event is called AFTER a {@link tsp.headdb.database.HeadDatabase} updates. + * The event is called asynchronously and can not be cancelled. + * + * @author TheSilentPro + */ +public class DatabaseUpdateEvent extends Event { + + private final HandlerList handlerList = new HandlerList(); + private final HeadDatabase database; + private final Map> heads; + + public DatabaseUpdateEvent(HeadDatabase database, Map> heads) { + super(true); + + this.database = database; + this.heads = heads; + } + + /** + * Retrieve the {@link HeadDatabase} associated with this event + * + * @return The database + */ + public HeadDatabase getDatabase() { + return database; + } + + /** + * Retrieve the result of the update + * + * @return The heads fetched from the update + */ + public Map> getHeads() { + return heads; + } + + @Override + public HandlerList getHandlers() { + return handlerList; + } + +}