Skip to content

Commit

Permalink
Merge pull request #26 from TheSilentPro/dev
Browse files Browse the repository at this point in the history
switch to gson and DatabaseUpdateEvent
  • Loading branch information
TheSilentPro authored Feb 17, 2022
2 parents ce442b6 + caab1e9 commit 520fd24
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
31 changes: 13 additions & 18 deletions src/main/java/tsp/headdb/database/HeadDatabase.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,6 +18,7 @@
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;
Expand Down Expand Up @@ -156,21 +158,13 @@ public void getHeadsNoCacheAsync(Consumer<Map<Category, List<Head>>> resultSet)
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;
Expand All @@ -193,7 +187,7 @@ public void getHeadsNoCacheAsync(Consumer<Map<Category, List<Head>>> resultSet)

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();
}
Expand All @@ -210,13 +204,14 @@ public void updateAsync(Consumer<Map<Category, List<Head>>> result) {
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));
}));
}

Expand Down
54 changes: 54 additions & 0 deletions src/main/java/tsp/headdb/event/DatabaseUpdateEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 520fd24

Please sign in to comment.