geforkt von Mirrors/HeadDB
Merge pull request #26 from TheSilentPro/dev
switch to gson and DatabaseUpdateEvent
Dieser Commit ist enthalten in:
Commit
520fd243da
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>tsp.headdb</groupId>
|
<groupId>tsp.headdb</groupId>
|
||||||
<artifactId>HeadDB</artifactId>
|
<artifactId>HeadDB</artifactId>
|
||||||
<version>3.2.2</version>
|
<version>3.2.3</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HeadDB</name>
|
<name>HeadDB</name>
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package tsp.headdb.database;
|
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.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.JSONObject;
|
|
||||||
import org.json.simple.parser.JSONParser;
|
|
||||||
import org.json.simple.parser.ParseException;
|
|
||||||
import tsp.headdb.api.Head;
|
import tsp.headdb.api.Head;
|
||||||
|
import tsp.headdb.event.DatabaseUpdateEvent;
|
||||||
import tsp.headdb.util.Log;
|
import tsp.headdb.util.Log;
|
||||||
import tsp.headdb.util.Utils;
|
import tsp.headdb.util.Utils;
|
||||||
|
|
||||||
@ -17,6 +18,7 @@ import java.io.InputStreamReader;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@ -156,21 +158,13 @@ public class HeadDatabase {
|
|||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
List<Head> heads = new ArrayList<>();
|
List<Head> heads = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
String line;
|
|
||||||
StringBuilder response = new StringBuilder();
|
|
||||||
|
|
||||||
URLConnection connection = new URL("https://minecraft-heads.com/scripts/api.php?cat=" + category.getName() + "&tags=true").openConnection();
|
URLConnection connection = new URL("https://minecraft-heads.com/scripts/api.php?cat=" + category.getName() + "&tags=true").openConnection();
|
||||||
connection.setConnectTimeout(timeout);
|
connection.setConnectTimeout(timeout);
|
||||||
connection.setRequestProperty("User-Agent", plugin.getName() + "-DatabaseUpdater");
|
connection.setRequestProperty("User-Agent", plugin.getName() + "-DatabaseUpdater");
|
||||||
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
|
||||||
while ((line = in.readLine()) != null) {
|
JsonArray array = JsonParser.parseReader(new BufferedReader(new InputStreamReader(connection.getInputStream()))).getAsJsonArray();
|
||||||
response.append(line);
|
for (JsonElement entry : array) {
|
||||||
}
|
JsonObject obj = entry.getAsJsonObject();
|
||||||
}
|
|
||||||
JSONParser parser = new JSONParser();
|
|
||||||
JSONArray array = (JSONArray) parser.parse(response.toString());
|
|
||||||
for (Object o : array) {
|
|
||||||
JSONObject obj = (JSONObject) o;
|
|
||||||
String rawUUID = obj.get("uuid").toString();
|
String rawUUID = obj.get("uuid").toString();
|
||||||
|
|
||||||
UUID uuid;
|
UUID uuid;
|
||||||
@ -193,7 +187,7 @@ public class HeadDatabase {
|
|||||||
|
|
||||||
long elapsed = (System.currentTimeMillis() - start);
|
long elapsed = (System.currentTimeMillis() - start);
|
||||||
Log.debug(category.getName() + " -> Done! Time: " + elapsed + "ms (" + TimeUnit.MILLISECONDS.toSeconds(elapsed) + "s)");
|
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:");
|
Log.error("[" + plugin.getName() + "] Failed to fetch heads (no-cache) | Stack Trace:");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -210,13 +204,14 @@ public class HeadDatabase {
|
|||||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, task -> getHeadsNoCacheAsync(heads -> {
|
Bukkit.getScheduler().runTaskAsynchronously(plugin, task -> getHeadsNoCacheAsync(heads -> {
|
||||||
if (heads == null) {
|
if (heads == null) {
|
||||||
Log.error("[" + plugin.getName() + "] Failed to update database! Check above for any errors.");
|
Log.error("[" + plugin.getName() + "] Failed to update database! Check above for any errors.");
|
||||||
result.accept(null);
|
result.accept(Collections.emptyMap());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
HEADS.clear();
|
HEADS.clear();
|
||||||
HEADS.putAll(heads);
|
HEADS.putAll(heads);
|
||||||
result.accept(heads);
|
result.accept(heads);
|
||||||
|
Bukkit.getPluginManager().callEvent(new DatabaseUpdateEvent(this, heads));
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
54
src/main/java/tsp/headdb/event/DatabaseUpdateEvent.java
Normale Datei
54
src/main/java/tsp/headdb/event/DatabaseUpdateEvent.java
Normale Datei
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren