From 9c3a45502af05193fdde84628d9f3431b74b7a07 Mon Sep 17 00:00:00 2001 From: Silent Date: Thu, 19 Aug 2021 15:59:28 +0200 Subject: [PATCH] internal changes and update command --- pom.xml | 2 +- src/main/java/tsp/headdb/api/Head.java | 5 +-- src/main/java/tsp/headdb/api/LocalHead.java | 5 +++ .../tsp/headdb/command/Command_headdb.java | 32 ++++++++++++++++++- .../java/tsp/headdb/database/Category.java | 12 +++---- .../tsp/headdb/database/HeadDatabase.java | 22 +++++++------ .../headdb/event/PlayerHeadPurchaseEvent.java | 7 +++- .../tsp/headdb/inventory/InventoryUtils.java | 10 +----- .../tsp/headdb/listener/MenuListener.java | 1 - src/main/java/tsp/headdb/util/Utils.java | 10 ------ 10 files changed, 66 insertions(+), 40 deletions(-) diff --git a/pom.xml b/pom.xml index 44c792c..3f4aa9b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 2.4.1 + 2.4.2 jar HeadDB diff --git a/src/main/java/tsp/headdb/api/Head.java b/src/main/java/tsp/headdb/api/Head.java index 12d4ac5..13a188f 100644 --- a/src/main/java/tsp/headdb/api/Head.java +++ b/src/main/java/tsp/headdb/api/Head.java @@ -46,10 +46,11 @@ public class Head { profileField = meta.getClass().getDeclaredField("profile"); profileField.setAccessible(true); profileField.set(meta, profile); - } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e1) { + } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) { Log.error("Could not set skull owner for " + uuid.toString() + " | Stack Trace:"); - e1.printStackTrace(); + ex.printStackTrace(); } + meta.setLore(Arrays.asList( Utils.colorize("&cID: " + id), Utils.colorize("&e" + buildTagLore(tags)), diff --git a/src/main/java/tsp/headdb/api/LocalHead.java b/src/main/java/tsp/headdb/api/LocalHead.java index 2b0928e..111a7be 100644 --- a/src/main/java/tsp/headdb/api/LocalHead.java +++ b/src/main/java/tsp/headdb/api/LocalHead.java @@ -21,6 +21,7 @@ public class LocalHead extends Head { this.uuid = uuid; } + @Override public ItemStack getItemStack() { Validate.notNull(uuid, "uuid must not be null!"); @@ -36,10 +37,12 @@ public class LocalHead extends Head { return item; } + @Override public UUID getUniqueId() { return uuid; } + @Override public String getName() { return name; } @@ -64,11 +67,13 @@ public class LocalHead extends Head { return null; } + @Override public LocalHead withUniqueId(UUID uuid) { this.uuid = uuid; return this; } + @Override 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 90c266f..5246b31 100644 --- a/src/main/java/tsp/headdb/command/Command_headdb.java +++ b/src/main/java/tsp/headdb/command/Command_headdb.java @@ -9,9 +9,10 @@ import org.bukkit.inventory.ItemStack; import tsp.headdb.HeadDB; import tsp.headdb.api.Head; import tsp.headdb.api.HeadAPI; -import tsp.headdb.inventory.InventoryUtils; import tsp.headdb.util.Utils; +import java.util.concurrent.TimeUnit; + public class Command_headdb implements CommandExecutor { @Override @@ -127,6 +128,33 @@ public class Command_headdb implements CommandExecutor { } } + if (sub.equalsIgnoreCase("update") || sub.equalsIgnoreCase("u")) { + if (!sender.hasPermission("headdb.update")) { + Utils.sendMessage(sender, "&cNo permission!"); + return true; + } + + Utils.sendMessage(sender, "Updating..."); + long start = System.currentTimeMillis(); + boolean result = HeadAPI.getDatabase().update(); + if (result) { + Utils.sendMessage(sender, "&aDone! Took: " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start) + " seconds"); + } else { + Utils.sendMessage(sender, "&cFailed! Check console for errors."); + } + return true; + } + + if (sub.equalsIgnoreCase("updateasync") || sub.equalsIgnoreCase("ua")) { + if (!sender.hasPermission("headdb.update")) { + Utils.sendMessage(sender, "&cNo permission!"); + return true; + } + + Utils.sendMessage(sender, "Updating..."); + Bukkit.getScheduler().runTaskAsynchronously(HeadDB.getInstance(), () -> HeadAPI.getDatabase().update()); + } + Utils.sendMessage(sender, " "); Utils.sendMessage(sender, "&c&lHeadDB &c- &5Commands"); Utils.sendMessage(sender, "&7&oParameters:&c command &9(aliases)&c arguments... &7- Description"); @@ -134,6 +162,8 @@ public class Command_headdb implements CommandExecutor { 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 update &9(u) &7- Forcefully update the database"); + Utils.sendMessage(sender, " > &c/hdb updateasync &9(ua) &7- Forcefully update the database ASYNCHRONOUSLY"); 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/Category.java b/src/main/java/tsp/headdb/database/Category.java index 344df19..6214ad2 100644 --- a/src/main/java/tsp/headdb/database/Category.java +++ b/src/main/java/tsp/headdb/database/Category.java @@ -5,12 +5,11 @@ import org.bukkit.inventory.ItemStack; import tsp.headdb.api.Head; import tsp.headdb.api.HeadAPI; -import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; public enum Category { + ALPHABET("alphabet", ChatColor.YELLOW, 20), ANIMALS("animals", ChatColor.DARK_AQUA, 21), BLOCKS("blocks", ChatColor.DARK_GRAY, 22), @@ -26,6 +25,7 @@ public enum Category { private final ChatColor color; private final int location; private final Map item = new HashMap<>(); + private static final Category[] values = values(); Category(String name, ChatColor color, int location) { this.name = name; @@ -55,8 +55,8 @@ public enum Category { } public static Category getByName(String name) { - for (Category category : Category.values()) { - if (category.getName().equals(name)) { + for (Category category : values) { + if (category.getName().equalsIgnoreCase(name)) { return category; } } @@ -64,8 +64,8 @@ public enum Category { return null; } - public static List getCategories() { - return Arrays.asList(Category.values()); + public static Category[] getValues() { + return values; } } diff --git a/src/main/java/tsp/headdb/database/HeadDatabase.java b/src/main/java/tsp/headdb/database/HeadDatabase.java index e37dadd..25e42a9 100644 --- a/src/main/java/tsp/headdb/database/HeadDatabase.java +++ b/src/main/java/tsp/headdb/database/HeadDatabase.java @@ -8,14 +8,18 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import tsp.headdb.api.Head; import tsp.headdb.util.Log; -import tsp.headdb.util.Utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.UUID; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; @@ -139,7 +143,7 @@ public class HeadDatabase { @Nullable public Map> getHeadsNoCache() { Map> result = new HashMap<>(); - List categories = Category.getCategories(); + Category[] categories = Category.getValues(); int id = 1; for (Category category : categories) { @@ -162,12 +166,14 @@ public class HeadDatabase { JSONArray array = (JSONArray) parser.parse(response.toString()); for (Object o : array) { JSONObject obj = (JSONObject) o; - String uuid = obj.get("uuid").toString(); - Log.debug(!Utils.isValid(uuid) + "Invalid UUID: " + uuid); + UUID uuid = UUID.fromString(obj.get("uuid").toString()); + if (uuid.toString().length() != 36) { + uuid = UUID.randomUUID(); + } Head head = new Head(id) .withName(obj.get("name").toString()) - .withUniqueId(Utils.isValid(uuid) ? UUID.fromString(uuid) : UUID.randomUUID()) + .withUniqueId(uuid) .withValue(obj.get("value").toString()) .withTags(obj.get("tags") != null ? obj.get("tags").toString() : "None") .withCategory(category); @@ -204,9 +210,7 @@ public class HeadDatabase { } HEADS.clear(); - for (Map.Entry> entry : heads.entrySet()) { - HEADS.put(entry.getKey(), entry.getValue()); - } + HEADS.putAll(heads); return true; } diff --git a/src/main/java/tsp/headdb/event/PlayerHeadPurchaseEvent.java b/src/main/java/tsp/headdb/event/PlayerHeadPurchaseEvent.java index 12f8141..2372e7c 100644 --- a/src/main/java/tsp/headdb/event/PlayerHeadPurchaseEvent.java +++ b/src/main/java/tsp/headdb/event/PlayerHeadPurchaseEvent.java @@ -7,7 +7,12 @@ import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; import tsp.headdb.api.Head; - +/** + * This event is called when a player purchases a {@link Head} + * + * @author TheSilentPro + * @see tsp.headdb.inventory.InventoryUtils#purchaseHead(Player, Head, int, String, String) + */ public class PlayerHeadPurchaseEvent extends Event implements Cancellable { private final HandlerList handlerList = new HandlerList(); diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 81ac7da..0a93179 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -195,7 +195,7 @@ public class InventoryUtils { public static void openDatabase(Player player) { Inventory inventory = Bukkit.createInventory(null, 54, Utils.colorize("&c&lHeadDB &8(" + HeadAPI.getHeads().size() + ")")); - for (Category category : Category.getCategories()) { + for (Category category : Category.getValues()) { ItemStack item = getUIItem(category.getName(), category.getItem()); ItemMeta meta = item.getItemMeta(); meta.setDisplayName(Utils.colorize(category.getColor() + "&l" + category.getName().toUpperCase())); @@ -304,14 +304,6 @@ public class InventoryUtils { return true; } - @Deprecated - public static void purchaseItem(Player player, ItemStack item, int amount, String category, String description) { - if (!processPayment(player, amount, category, description)) return; - - item.setAmount(amount); - player.getInventory().addItem(item); - } - public static void purchaseHead(Player player, Head head, int amount, String category, String description) { if (!processPayment(player, amount, category, description)) return; PlayerHeadPurchaseEvent event = new PlayerHeadPurchaseEvent(player, head, getCategoryCost(player, category)); diff --git a/src/main/java/tsp/headdb/listener/MenuListener.java b/src/main/java/tsp/headdb/listener/MenuListener.java index e8a6367..09ae243 100644 --- a/src/main/java/tsp/headdb/listener/MenuListener.java +++ b/src/main/java/tsp/headdb/listener/MenuListener.java @@ -14,7 +14,6 @@ import tsp.headdb.HeadDB; import tsp.headdb.api.HeadAPI; import tsp.headdb.database.Category; import tsp.headdb.inventory.InventoryUtils; -import tsp.headdb.util.Log; import tsp.headdb.util.Utils; public class MenuListener implements Listener { diff --git a/src/main/java/tsp/headdb/util/Utils.java b/src/main/java/tsp/headdb/util/Utils.java index b49d990..2c38532 100644 --- a/src/main/java/tsp/headdb/util/Utils.java +++ b/src/main/java/tsp/headdb/util/Utils.java @@ -3,18 +3,8 @@ package tsp.headdb.util; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - public class Utils { - public static final Pattern UUID_PATTERN = Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); - - public static boolean isValid(String uuid) { - Matcher matcher = UUID_PATTERN.matcher(uuid); - return matcher.find(); - } - public static void sendMessage(CommandSender sender, String message) { sender.sendMessage(colorize(message)); }