switch to gson and DatabaseUpdateEvent

Dieser Commit ist enthalten in:
Silent 2022-02-18 00:34:33 +01:00
Ursprung ce442b645f
Commit caab1e97a0
3 geänderte Dateien mit 68 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -6,7 +6,7 @@
<groupId>tsp.headdb</groupId>
<artifactId>HeadDB</artifactId>
<version>3.2.2</version>
<version>3.2.3</version>
<packaging>jar</packaging>
<name>HeadDB</name>

Datei anzeigen

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

Datei anzeigen

@ -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<Category, List<Head>> heads;
public DatabaseUpdateEvent(HeadDatabase database, Map<Category, List<Head>> 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<Category, List<Head>> getHeads() {
return heads;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
}