From 6af5c2684b98460b9b7d15ec0722fab1e8d490c1 Mon Sep 17 00:00:00 2001 From: Silent Date: Tue, 8 Dec 2020 22:22:41 +0100 Subject: [PATCH] Push to 2.0 --- pom.xml | 39 +++++- src/main/java/tsp/headdb/HeadDB.java | 27 ++-- src/main/java/tsp/headdb/api/Head.java | 83 ++++-------- src/main/java/tsp/headdb/api/HeadAPI.java | 47 ++++--- src/main/java/tsp/headdb/api/LocalHead.java | 33 +---- .../tsp/headdb/command/Command_headdb.java | 6 +- .../tsp/headdb/database/HeadDatabase.java | 51 ++++---- .../tsp/headdb/listener/JoinListener.java | 1 - src/main/java/tsp/headdb/util/Config.java | 119 ------------------ src/main/resources/config.yml | 6 + src/main/resources/plugin.yml | 4 +- 11 files changed, 152 insertions(+), 264 deletions(-) delete mode 100644 src/main/java/tsp/headdb/util/Config.java diff --git a/pom.xml b/pom.xml index 26a6b21..31b8eef 100644 --- a/pom.xml +++ b/pom.xml @@ -6,11 +6,11 @@ tsp.headdb HeadDB - 1.3 + 2.0 jar HeadDB - Thousands of heads in a GUI + Database with thousands of heads @@ -23,9 +23,15 @@ mojang-repo https://libraries.minecraft.net/ + + + jitpack.io + https://jitpack.io + + com.destroystokyo.paper paper-api @@ -45,10 +51,17 @@ 1.5.21 provided + + + com.github.simplix-softworks + simplixstorage + 3.2.0 + + org.apache.maven.plugins maven-compiler-plugin @@ -57,6 +70,28 @@ 8 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + tsp.headdb.HeadDB + + + + + + diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 3e65a91..00722cd 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -1,12 +1,15 @@ package tsp.headdb; +import de.leonhard.storage.Config; +import de.leonhard.storage.Json; +import de.leonhard.storage.LightningBuilder; +import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import tsp.headdb.api.HeadAPI; import tsp.headdb.command.Command_headdb; -import tsp.headdb.database.HeadDatabase; import tsp.headdb.listener.JoinListener; import tsp.headdb.listener.PagedPaneListener; import tsp.headdb.listener.MenuListener; -import tsp.headdb.util.Config; import tsp.headdb.util.Log; import tsp.headdb.util.Metrics; import tsp.headdb.util.Utils; @@ -15,16 +18,15 @@ public class HeadDB extends JavaPlugin { private static HeadDB instance; private static Config config; - private static Config playerdata; + private static Json playerdata; @Override public void onEnable() { instance = this; Log.info("Loading HeadDB - " + getDescription().getVersion()); saveDefaultConfig(); - config = new Config("plugins/HeadDB/config.yml"); - playerdata = new Config("plugins/HeadDB/playerdata.yml"); - playerdata.create(); + config = LightningBuilder.fromPath("config.yml", "plugins/HeadDB").createConfig().addDefaultsFromInputStream(); + playerdata = LightningBuilder.fromPath("playerdata.json", "plugins/HeadDB").createJson(); Log.debug("Starting metrics..."); new Metrics(this, Utils.METRICS_ID); @@ -37,8 +39,15 @@ public class HeadDB extends JavaPlugin { Log.debug("Registering commands..."); getCommand("headdb").setExecutor(new Command_headdb()); - Log.debug("Initializing Database..."); - HeadDatabase.update(); + if (config.getBoolean("fetchStartup")) { + if (config.getBoolean("asyncStartup")) { + Log.debug("Initializing Database... (ASYNC)"); + Bukkit.getScheduler().runTaskAsynchronously(this, task -> HeadAPI.getDatabase().update()); + }else { + Log.debug("Initializing Database... (SYNC)"); + Bukkit.getScheduler().runTask(this, task -> HeadAPI.getDatabase().update()); + } + } Log.info("Done!"); } @@ -47,7 +56,7 @@ public class HeadDB extends JavaPlugin { return config; } - public static Config getPlayerdata() { + public static Json getPlayerdata() { return playerdata; } diff --git a/src/main/java/tsp/headdb/api/Head.java b/src/main/java/tsp/headdb/api/Head.java index 6acc698..b6804b4 100644 --- a/src/main/java/tsp/headdb/api/Head.java +++ b/src/main/java/tsp/headdb/api/Head.java @@ -22,6 +22,12 @@ public class Head { private Category category; private int id; + public Head() {} + + public Head(int id) { + this.id = id; + } + public ItemStack getItemStack() { Validate.notNull(name, "name must not be null!"); Validate.notNull(uuid, "uuid must not be null!"); @@ -44,6 +50,7 @@ public class Head { } meta.setLore(Arrays.asList( Utils.colorize("&cID: " + id), + " ", Utils.colorize("&8Right-Click to add/remove from favorites.") )); item.setItemMeta(meta); @@ -55,85 +62,45 @@ public class Head { return name; } - public void setName(String name) { - this.name = name; - } - public UUID getUUID() { return uuid; } - public void setUUID(UUID uuid) { - this.uuid = uuid; - } - public String getValue() { return value; } - public void setValue(String value) { - this.value = value; - } - public Category getCategory() { return category; } - public void setCategory(Category category) { - this.category = category; - } - public int getId() { return id; } - public void setId(int id) { - this.id = id; + public Head withName(String name) { + this.name = name; + return this; } - public static class Builder { + public Head withUUID(UUID uuid) { + this.uuid = uuid; + return this; + } - private String name; - private UUID uuid; - private String value; - private Category category; - private int id; + public Head withValue(String value) { + this.value = value; + return this; + } - public Builder withName(String name) { - this.name = name; - return this; - } - - public Builder withUUID(UUID uuid) { - this.uuid = uuid; - return this; - } - - public Builder withValue(String value) { - this.value = value; - return this; - } - - public Builder withCategory(Category category) { - this.category = category; - return this; - } - - public Builder withId(int id) { - this.id = id; - return this; - } - - public Head build() { - Head head = new Head(); - head.setName(name); - head.setUUID(uuid); - head.setValue(value); - head.setCategory(category); - head.setId(id); - return head; - } + public Head withCategory(Category category) { + this.category = category; + return this; + } + public Head withId(int id) { + this.id = id; + return this; } } diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java index 4be3612..d750535 100644 --- a/src/main/java/tsp/headdb/api/HeadAPI.java +++ b/src/main/java/tsp/headdb/api/HeadAPI.java @@ -8,6 +8,7 @@ import tsp.headdb.database.Category; import tsp.headdb.database.HeadDatabase; import tsp.headdb.inventory.InventoryUtils; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -20,6 +21,17 @@ import java.util.UUID; */ public class HeadAPI { + private static final HeadDatabase database = new HeadDatabase(); + + /** + * Retrieves the main {@link HeadDatabase} + * + * @return Head Database + */ + public static HeadDatabase getDatabase() { + return database; + } + /** * Opens the database for a player * @@ -55,8 +67,9 @@ public class HeadAPI { * @param id The ID of the head * @return The head */ + @Nullable public static Head getHeadByID(int id) { - return HeadDatabase.getHeadByID(id); + return database.getHeadByID(id); } /** @@ -65,8 +78,9 @@ public class HeadAPI { * @param uuid The UUID of the head * @return The head */ + @Nullable public static Head getHeadByUUID(UUID uuid) { - return HeadDatabase.getHeadByUUID(uuid); + return database.getHeadByUUID(uuid); } /** @@ -76,7 +90,7 @@ public class HeadAPI { * @return List of heads */ public static List getHeadsByName(String name) { - return HeadDatabase.getHeadsByName(name); + return database.getHeadsByName(name); } /** @@ -87,7 +101,7 @@ public class HeadAPI { * @return List of heads */ public static List getHeadsByName(Category category, String name) { - return HeadDatabase.getHeadsByName(category, name); + return database.getHeadsByName(category, name); } /** @@ -96,8 +110,9 @@ public class HeadAPI { * @param value The texture value * @return The head */ + @Nullable public static Head getHeadByValue(String value) { - return HeadDatabase.getHeadByValue(value); + return database.getHeadByValue(value); } /** @@ -107,7 +122,7 @@ public class HeadAPI { * @return List of heads */ public static List getHeads(Category category) { - return HeadDatabase.getHeads(category); + return database.getHeads(category); } /** @@ -116,7 +131,7 @@ public class HeadAPI { * @return List of all heads */ public static List getHeads() { - return HeadDatabase.getHeads(); + return database.getHeads(); } /** @@ -126,12 +141,11 @@ public class HeadAPI { * @param id The ID of the head */ public static void addFavoriteHead(UUID uuid, int id) { - List favs = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites"); + List favs = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); if (!favs.contains(id)) { favs.add(id); } HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs); - HeadDB.getPlayerdata().save(); } /** @@ -141,7 +155,7 @@ public class HeadAPI { * @param id The ID of the head */ public static void removeFavoriteHead(UUID uuid, int id) { - List favs = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites"); + List favs = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); for (int i = 0; i < favs.size(); i++) { if (favs.get(i) == id) { favs.remove(i); @@ -149,7 +163,6 @@ public class HeadAPI { } } HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs); - HeadDB.getPlayerdata().save(); } /** @@ -160,7 +173,7 @@ public class HeadAPI { */ public static List getFavoriteHeads(UUID uuid) { List heads = new ArrayList<>(); - List ids = HeadDB.getPlayerdata().getIntList(uuid.toString() + ".favorites"); + List ids = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); for (int id : ids) { Head head = getHeadByID(id); heads.add(head); @@ -177,12 +190,10 @@ public class HeadAPI { */ public static List getLocalHeads() { List heads = new ArrayList<>(); - for (String key : HeadDB.getPlayerdata().getKeys(false)) { + for (String key : HeadDB.getPlayerdata().keySet()) { OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(key)); - heads.add(new LocalHead.Builder() - .withUUID(player.getUniqueId()) - .withName(player.getName()) - .build()); + heads.add(new LocalHead(player.getUniqueId()) + .withName(player.getName())); } return heads; @@ -192,7 +203,7 @@ public class HeadAPI { * Update the Head Database */ public static void updateDatabase() { - HeadDatabase.update(); + database.update(); } } diff --git a/src/main/java/tsp/headdb/api/LocalHead.java b/src/main/java/tsp/headdb/api/LocalHead.java index eda807b..f5f558d 100644 --- a/src/main/java/tsp/headdb/api/LocalHead.java +++ b/src/main/java/tsp/headdb/api/LocalHead.java @@ -39,39 +39,18 @@ public class LocalHead { return uuid; } - public void setUuid(UUID uuid) { - this.uuid = uuid; - } - public String getName() { return name; } - public void setName(String name) { - this.name = name; + public LocalHead withUUID(UUID uuid) { + this.uuid = uuid; + return this; } - public static class Builder { - - private UUID uuid; - private String name; - - public Builder withUUID(UUID uuid) { - this.uuid = uuid; - return this; - } - - public Builder withName(String name) { - this.name = name; - return this; - } - - public LocalHead build() { - LocalHead head = new LocalHead(uuid); - head.setName(name); - return head; - } - + public LocalHead withName(String name) { + this.name = name; + return this; } } diff --git a/src/main/java/tsp/headdb/command/Command_headdb.java b/src/main/java/tsp/headdb/command/Command_headdb.java index 93d243f..e1cad59 100644 --- a/src/main/java/tsp/headdb/command/Command_headdb.java +++ b/src/main/java/tsp/headdb/command/Command_headdb.java @@ -36,6 +36,7 @@ public class Command_headdb implements CommandExecutor { if (sub.equalsIgnoreCase("info") || sub.equalsIgnoreCase("i")) { Utils.sendMessage(sender, "Running &cHeadDB v" + HeadDB.getInstance().getDescription().getVersion()); Utils.sendMessage(sender, "Created by &c" + HeadDB.getInstance().getDescription().getAuthors()); + Utils.sendMessage(sender, "There are currently &c" + HeadAPI.getHeads().size() + " &7heads in the database."); return true; } @@ -56,7 +57,10 @@ public class Command_headdb implements CommandExecutor { StringBuilder builder = new StringBuilder(); for (int i = 1; i < args.length; i++) { - builder.append(args[i]).append(" "); + builder.append(args[i]); + if (i != args.length - 1) { + builder.append(" "); + } } String name = builder.toString(); Utils.sendMessage(sender, "Searching for &e" + name); diff --git a/src/main/java/tsp/headdb/database/HeadDatabase.java b/src/main/java/tsp/headdb/database/HeadDatabase.java index 8b57f9e..1b75f49 100644 --- a/src/main/java/tsp/headdb/database/HeadDatabase.java +++ b/src/main/java/tsp/headdb/database/HeadDatabase.java @@ -1,5 +1,6 @@ package tsp.headdb.database; +import org.bukkit.ChatColor; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -23,11 +24,13 @@ import java.util.concurrent.TimeUnit; */ public class HeadDatabase { - private static final Map> HEADS = new HashMap<>(); - private static final String URL = "https://minecraft-heads.com/scripts/api.php?cat="; - private static long updated; + private final Map> HEADS = new HashMap<>(); + private final String URL = "https://minecraft-heads.com/scripts/api.php?cat="; + private long updated; + + public HeadDatabase() {} - public static Head getHeadByValue(String value) { + public Head getHeadByValue(String value) { List heads = getHeads(); for (Head head : heads) { if (head.getValue().equals(value)) { @@ -38,7 +41,7 @@ public class HeadDatabase { return null; } - public static Head getHeadByID(int id) { + public Head getHeadByID(int id) { List heads = getHeads(); for (Head head : heads) { if (head.getId() == id) { @@ -49,7 +52,7 @@ public class HeadDatabase { return null; } - public static Head getHeadByUUID(UUID uuid) { + public Head getHeadByUUID(UUID uuid) { List heads = getHeads(); for (Head head : heads) { if (head.getUUID().equals(uuid)) { @@ -60,11 +63,12 @@ public class HeadDatabase { return null; } - public static List getHeadsByName(Category category, String name) { + public List getHeadsByName(Category category, String name) { List result = new ArrayList<>(); List heads = getHeads(category); for (Head head : heads) { - if (head.getName().toLowerCase().contains(name.toLowerCase())) { + String hName = ChatColor.stripColor(head.getName().toLowerCase(Locale.ROOT)); + if (hName.contains(ChatColor.stripColor(name.toLowerCase(Locale.ROOT)))) { result.add(head); } } @@ -72,23 +76,20 @@ public class HeadDatabase { return result; } - public static List getHeadsByName(String name) { + public List getHeadsByName(String name) { List result = new ArrayList<>(); - List heads = getHeads(); - for (Head head : heads) { - if (head.getName().toLowerCase().contains(name.toLowerCase())) { - result.add(head); - } + for (Category category : Category.values()) { + result.addAll(getHeadsByName(category, name)); } return result; } - public static List getHeads(Category category) { + public List getHeads(Category category) { return HEADS.get(category); } - public static List getHeads() { + public List getHeads() { if (!HEADS.isEmpty() && !isLastUpdateOld()) { List heads = new ArrayList<>(); for (Category category : HEADS.keySet()) { @@ -101,7 +102,7 @@ public class HeadDatabase { return getHeads(); } - public static Map> getHeadsNoCache() { + public Map> getHeadsNoCache() { Map> result = new HashMap<>(); List categories = Category.getCategories(); @@ -116,9 +117,7 @@ public class HeadDatabase { URLConnection connection = new URL(URL + category.getName()).openConnection(); connection.setConnectTimeout(5000); connection.setRequestProperty("User-Agent", "HeadDB"); - try (BufferedReader in = new BufferedReader( - new InputStreamReader( - connection.getInputStream()))) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { while ((line = in.readLine()) != null) { response.append(line); } @@ -127,13 +126,11 @@ public class HeadDatabase { JSONArray array = (JSONArray) parser.parse(response.toString()); for (Object o : array) { JSONObject obj = (JSONObject) o; - Head head = new Head.Builder() + Head head = new Head(id) .withName(obj.get("name").toString()) .withUUID(UUID.fromString(obj.get("uuid").toString())) .withValue(obj.get("value").toString()) - .withCategory(category) - .withId(id) - .build(); + .withCategory(category); id++; heads.add(head); @@ -150,7 +147,7 @@ public class HeadDatabase { return result; } - public static void update() { + public void update() { Map> heads = getHeadsNoCache(); HEADS.clear(); for (Map.Entry> entry : heads.entrySet()) { @@ -158,13 +155,13 @@ public class HeadDatabase { } } - public static long getLastUpdate() { + public long getLastUpdate() { long now = System.nanoTime(); long elapsed = now - updated; return TimeUnit.NANOSECONDS.toSeconds(elapsed); } - public static boolean isLastUpdateOld() { + public boolean isLastUpdateOld() { if (HeadDB.getCfg() == null && getLastUpdate() >= 3600) return true; return getLastUpdate() >= HeadDB.getCfg().getLong("refresh"); } diff --git a/src/main/java/tsp/headdb/listener/JoinListener.java b/src/main/java/tsp/headdb/listener/JoinListener.java index e3eaab6..4b2f007 100644 --- a/src/main/java/tsp/headdb/listener/JoinListener.java +++ b/src/main/java/tsp/headdb/listener/JoinListener.java @@ -14,7 +14,6 @@ public class JoinListener implements Listener { @EventHandler public void onJoin(PlayerJoinEvent e) { HeadDB.getPlayerdata().set(e.getPlayer().getUniqueId().toString() + ".username", e.getPlayer().getName()); - HeadDB.getPlayerdata().save(); } } diff --git a/src/main/java/tsp/headdb/util/Config.java b/src/main/java/tsp/headdb/util/Config.java deleted file mode 100644 index 625dc4e..0000000 --- a/src/main/java/tsp/headdb/util/Config.java +++ /dev/null @@ -1,119 +0,0 @@ -package tsp.headdb.util; - -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.io.IOException; -import java.util.List; -import java.util.Set; - -/** - * @author TheSilentPro - */ -public class Config { - - private final File file; - private FileConfiguration config; - - public Config(File file) { - this.file = file; - this.config = YamlConfiguration.loadConfiguration(file); - } - - public Config(String path) { - this.file = new File(path); - this.config = YamlConfiguration.loadConfiguration(file); - } - - public Config(Config config) { - this.file = config.getFile(); - this.config = config.getConfig(); - } - - public String getString(String path) { - return Utils.colorize(config.getString(path)); - } - - public boolean getBoolean(String path) { - return config.getBoolean(path); - } - - public List getStringList(String path) { - return config.getStringList(path); - } - - public int getInt(String path) { - return config.getInt(path); - } - - public List getIntList(String path) { - return config.getIntegerList(path); - } - - public long getLong(String path) { - return config.getLong(path); - } - - public Set getKeys(boolean deep) { - return config.getKeys(deep); - } - - public ConfigurationSection getConfigurationSection(String path) { - return config.getConfigurationSection(path); - } - - public Set getKeys(ConfigurationSection configurationSection, boolean b) { - return configurationSection.getKeys(b); - } - - public void set(String path, Object value) { - config.set(path, value); - } - - public Object get(String path) { - return config.get(path); - } - - public FileConfiguration getConfig() { - return config; - } - - public File getFile() { - return file; - } - - public boolean exists() { - return file.exists(); - } - - public void reload() { - this.config = YamlConfiguration.loadConfiguration(file); - } - - public boolean create() { - if (!file.exists()) { - try { - return file.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - return false; - } - - public boolean save() { - try { - config.save(file); - return true; - } catch (IOException e) { - Log.error("Failed to save " + file.getName() + " | Stack Trace:"); - e.printStackTrace(); - } - - return false; - } - -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 81f0e9b..9b5f3d5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,3 +1,9 @@ +# When enabled heads will be fetched from startup, otherwise when the /hdb command is ran +fetchStartup: true + +# When enabled, heads will be fetched async (Startup Only) +asyncStartup: false + # If the cached heads are older than these amount of seconds, the plugin will refresh the database refresh: 3600 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 4953194..15450ce 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,8 +1,8 @@ name: HeadDB -description: Adds a menu with thousands of heads +description: Database with thousands of heads main: tsp.headdb.HeadDB -version: 1.3 +version: 2.0 api-version: 1.16 author: Silent