From 121267a71ae985dca4a2b97683bc0bb700322b24 Mon Sep 17 00:00:00 2001 From: Silent Date: Thu, 31 Dec 2020 17:49:48 +0100 Subject: [PATCH] Added tags and tag search --- src/main/java/tsp/headdb/HeadDB.java | 12 ++++----- src/main/java/tsp/headdb/api/Head.java | 27 ++++++++++++++++++- src/main/java/tsp/headdb/api/HeadAPI.java | 16 ++++++----- .../tsp/headdb/command/Command_headdb.java | 22 +++++++++++++++ .../tsp/headdb/database/HeadDatabase.java | 23 +++++++++++++--- .../tsp/headdb/inventory/InventoryUtils.java | 25 +++++++++++++++++ .../tsp/headdb/listener/JoinListener.java | 2 +- src/main/java/tsp/headdb/util/Log.java | 2 +- src/main/resources/plugin.yml | 3 +++ 9 files changed, 114 insertions(+), 18 deletions(-) diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 1e4de39..276816b 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -17,8 +17,8 @@ import tsp.headdb.util.Utils; public class HeadDB extends JavaPlugin { private static HeadDB instance; - private static Config config; - private static Json playerdata; + private Config config; + private Json playerdata; @Override public void onEnable() { @@ -42,8 +42,8 @@ public class HeadDB extends JavaPlugin { if (config.getBoolean("fetchStartup")) { if (config.getBoolean("asyncStartup")) { Log.debug("Initializing Database... (ASYNC)"); - Bukkit.getScheduler().runTaskAsynchronously(this, task -> HeadAPI.getDatabase().update()); - }else { + Bukkit.getScheduler().runTaskAsynchronously(this, () -> HeadAPI.getDatabase().update()); + } else { Log.debug("Initializing Database... (SYNC)"); HeadAPI.getDatabase().update(); } @@ -52,11 +52,11 @@ public class HeadDB extends JavaPlugin { Log.info("Done!"); } - public static Config getCfg() { + public Config getCfg() { return config; } - public static Json getPlayerdata() { + public 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 b6804b4..f5351d8 100644 --- a/src/main/java/tsp/headdb/api/Head.java +++ b/src/main/java/tsp/headdb/api/Head.java @@ -11,7 +11,9 @@ import tsp.headdb.util.Log; import tsp.headdb.util.Utils; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.UUID; public class Head { @@ -21,6 +23,7 @@ public class Head { private String value; private Category category; private int id; + private List tags; public Head() {} @@ -50,7 +53,8 @@ public class Head { } meta.setLore(Arrays.asList( Utils.colorize("&cID: " + id), - " ", + Utils.colorize("&e" + buildTagLore((String[]) tags.toArray())), + "", Utils.colorize("&8Right-Click to add/remove from favorites.") )); item.setItemMeta(meta); @@ -78,6 +82,10 @@ public class Head { return id; } + public List getTags() { + return tags; + } + public Head withName(String name) { this.name = name; return this; @@ -102,5 +110,22 @@ public class Head { this.id = id; return this; } + + public Head withTags(String tags) { + this.tags = Arrays.asList(tags.split(",")); + return this; + } + + private String buildTagLore(String... tags) { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < tags.length; i++) { + builder.append(tags[i]); + if (i != tags.length - 1) { + builder.append(","); + } + } + + return builder.toString(); + } } diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java index 1734773..4d10fdf 100644 --- a/src/main/java/tsp/headdb/api/HeadAPI.java +++ b/src/main/java/tsp/headdb/api/HeadAPI.java @@ -85,6 +85,10 @@ public final class HeadAPI { return database.getHeadByUUID(uuid); } + public static List getHeadsByTag(String tag) { + return database.getHeadsByTag(tag); + } + /** * Retrieves a {@link List} of {@link Head}'s matching a name * @@ -143,11 +147,11 @@ public final class HeadAPI { * @param id The ID of the head */ public static void addFavoriteHead(UUID uuid, int id) { - List favs = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); + List favs = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); if (!favs.contains(id)) { favs.add(id); } - HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs); + HeadDB.getInstance().getPlayerdata().set(uuid.toString() + ".favorites", favs); } /** @@ -157,14 +161,14 @@ public final class HeadAPI { * @param id The ID of the head */ public static void removeFavoriteHead(UUID uuid, int id) { - List favs = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); + List favs = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); for (int i = 0; i < favs.size(); i++) { if (favs.get(i) == id) { favs.remove(i); break; } } - HeadDB.getPlayerdata().set(uuid.toString() + ".favorites", favs); + HeadDB.getInstance().getPlayerdata().set(uuid.toString() + ".favorites", favs); } /** @@ -175,7 +179,7 @@ public final class HeadAPI { */ public static List getFavoriteHeads(UUID uuid) { List heads = new ArrayList<>(); - List ids = HeadDB.getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); + List ids = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites"); for (int id : ids) { Head head = getHeadByID(id); heads.add(head); @@ -192,7 +196,7 @@ public final class HeadAPI { */ public static List getLocalHeads() { List heads = new ArrayList<>(); - for (String key : HeadDB.getPlayerdata().singleLayerKeySet()) { + for (String key : HeadDB.getInstance().getPlayerdata().singleLayerKeySet()) { OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(key)); heads.add(new LocalHead(player.getUniqueId()) .withName(player.getName())); diff --git a/src/main/java/tsp/headdb/command/Command_headdb.java b/src/main/java/tsp/headdb/command/Command_headdb.java index e1cad59..1163e86 100644 --- a/src/main/java/tsp/headdb/command/Command_headdb.java +++ b/src/main/java/tsp/headdb/command/Command_headdb.java @@ -68,6 +68,27 @@ public class Command_headdb implements CommandExecutor { return true; } + if (sub.equalsIgnoreCase("tagsearch") || sub.equalsIgnoreCase("ts")) { + if (!sender.hasPermission("headdb.tagsearch")) { + Utils.sendMessage(sender, "&cNo permission!"); + return true; + } + if (args.length < 2) { + Utils.sendMessage(sender, "&c/hdb tagsearch "); + return true; + } + if (!(sender instanceof Player)) { + Utils.sendMessage(sender, "&cOnly players may open the database."); + return true; + } + Player player = (Player) sender; + + String tag = args[1]; + Utils.sendMessage(sender, "Searching for heads with tag &e" + tag); + InventoryUtils.openTagSearchDatabase(player, tag); + return true; + } + if (sub.equalsIgnoreCase("give") || sub.equalsIgnoreCase("g")) { if (!sender.hasPermission("headdb.give")) { Utils.sendMessage(sender, "&cNo permission!"); @@ -112,6 +133,7 @@ public class Command_headdb implements CommandExecutor { Utils.sendMessage(sender, " > &c/hdb &7- Opens the database"); Utils.sendMessage(sender, " > &c/hdb info &9(i) &7- Plugin Information"); Utils.sendMessage(sender, " > &c/hdb search &9(s) &c &7- Search for heads matching a name"); + Utils.sendMessage(sender, " > &c/hdb tagsearch &9(ts) &c &7- Search for heads matching a tag"); Utils.sendMessage(sender, " > &c/hdb give &9(g) &c &6[amount] &7- Give player a head"); Utils.sendMessage(sender, " "); return true; diff --git a/src/main/java/tsp/headdb/database/HeadDatabase.java b/src/main/java/tsp/headdb/database/HeadDatabase.java index 1b75f49..d6418fa 100644 --- a/src/main/java/tsp/headdb/database/HeadDatabase.java +++ b/src/main/java/tsp/headdb/database/HeadDatabase.java @@ -26,6 +26,7 @@ public class HeadDatabase { private final Map> HEADS = new HashMap<>(); private final String URL = "https://minecraft-heads.com/scripts/api.php?cat="; + private final String TAGS = "&tags=true"; private long updated; public HeadDatabase() {} @@ -63,6 +64,21 @@ public class HeadDatabase { return null; } + public List getHeadsByTag(String tag) { + List result = new ArrayList<>(); + List heads = getHeads(); + tag = tag.toLowerCase(Locale.ROOT); + for (Head head : heads) { + for (String t : head.getTags()) { + if (t.toLowerCase(Locale.ROOT).contains(tag)) { + result.add(head); + } + } + } + + return result; + } + public List getHeadsByName(Category category, String name) { List result = new ArrayList<>(); List heads = getHeads(category); @@ -114,7 +130,7 @@ public class HeadDatabase { String line; StringBuilder response = new StringBuilder(); - URLConnection connection = new URL(URL + category.getName()).openConnection(); + URLConnection connection = new URL(URL + category.getName() + TAGS).openConnection(); connection.setConnectTimeout(5000); connection.setRequestProperty("User-Agent", "HeadDB"); try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { @@ -130,6 +146,7 @@ public class HeadDatabase { .withName(obj.get("name").toString()) .withUUID(UUID.fromString(obj.get("uuid").toString())) .withValue(obj.get("value").toString()) + .withTags(obj.get("tags") != null ? obj.get("tags").toString() : "None") .withCategory(category); id++; @@ -162,8 +179,8 @@ public class HeadDatabase { } public boolean isLastUpdateOld() { - if (HeadDB.getCfg() == null && getLastUpdate() >= 3600) return true; - return getLastUpdate() >= HeadDB.getCfg().getLong("refresh"); + if (HeadDB.getInstance().getCfg() == null && getLastUpdate() >= 3600) return true; + return getLastUpdate() >= HeadDB.getInstance().getCfg().getLong("refresh"); } } diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 4a8bb88..c835346 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -96,6 +96,31 @@ public class InventoryUtils { pane.open(player); } + public static void openTagSearchDatabase(Player player, String tag) { + PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &eTag Search: " + tag)); + + List heads = HeadAPI.getHeadsByTag(tag); + for (Head head : heads) { + pane.addButton(new Button(head.getItemStack(), e -> { + if (e.getClick() == ClickType.SHIFT_LEFT) { + ItemStack item = head.getItemStack(); + item.setAmount(64); + player.getInventory().addItem(item); + return; + } + if (e.getClick() == ClickType.LEFT) { + player.getInventory().addItem(head.getItemStack()); + } + if (e.getClick() == ClickType.RIGHT) { + HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); + Utils.sendMessage(player, "Added &e" + head.getName() + " &7to favorites."); + } + })); + } + + pane.open(player); + } + public static void openCategoryDatabase(Player player, Category category) { PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &e" + category.getName())); diff --git a/src/main/java/tsp/headdb/listener/JoinListener.java b/src/main/java/tsp/headdb/listener/JoinListener.java index 4b2f007..6d4ef8b 100644 --- a/src/main/java/tsp/headdb/listener/JoinListener.java +++ b/src/main/java/tsp/headdb/listener/JoinListener.java @@ -13,7 +13,7 @@ public class JoinListener implements Listener { @EventHandler public void onJoin(PlayerJoinEvent e) { - HeadDB.getPlayerdata().set(e.getPlayer().getUniqueId().toString() + ".username", e.getPlayer().getName()); + HeadDB.getInstance().getPlayerdata().set(e.getPlayer().getUniqueId().toString() + ".username", e.getPlayer().getName()); } } diff --git a/src/main/java/tsp/headdb/util/Log.java b/src/main/java/tsp/headdb/util/Log.java index 023736e..c94453a 100644 --- a/src/main/java/tsp/headdb/util/Log.java +++ b/src/main/java/tsp/headdb/util/Log.java @@ -35,7 +35,7 @@ public class Log { } public static void log(LogLevel level, String message) { - if (level == LogLevel.DEBUG && !HeadDB.getCfg().getBoolean("debug")) { + if (level == LogLevel.DEBUG && !HeadDB.getInstance().getCfg().getBoolean("debug")) { return; } Bukkit.getConsoleSender().sendMessage(Utils.colorize("&7[&9&l" + name + "&7] " + level.getColor() + "[" + level.name() + "]: " + message)); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 96c4c5b..539753a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -21,6 +21,7 @@ permissions: headdb.give: true headdb.favorites: true headdb.local: true + headdb.tagsearch: true headdb.open: default: op headdb.search: @@ -30,4 +31,6 @@ permissions: headdb.favorites: default: op headdb.local: + default: op + headdb.tagsearch: default: op \ No newline at end of file