From b3c06013de93b74306d0ffc07a1160fe15dbacab Mon Sep 17 00:00:00 2001 From: Silent Date: Wed, 16 Feb 2022 22:17:21 +0100 Subject: [PATCH] refactoring --- src/main/java/tsp/headdb/HeadDB.java | 25 ++--- src/main/java/tsp/headdb/api/Head.java | 6 +- .../tsp/headdb/command/CommandHeadDB.java | 9 +- .../tsp/headdb/inventory/InventoryUtils.java | 99 +++++++++---------- 4 files changed, 64 insertions(+), 75 deletions(-) diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index bf5378f..6a17958 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -17,8 +17,6 @@ import tsp.headdb.util.Metrics; import javax.annotation.Nullable; import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; public class HeadDB extends JavaPlugin { @@ -95,25 +93,14 @@ public class HeadDB extends JavaPlugin { } private void createLocalizationFile() { - if (getClass().getResource("messages.yml") == null || new File(getDataFolder() + "/messages.yml").exists()) { - // File exists or not default available - return; + if (getClass().getResource("messages.yml") != null && !new File(getDataFolder() + "/messages.yml").exists()) { + saveResource("messages.yml", false); + Log.debug("Localization loaded from jar file."); } - try { - saveResource("messages.yml", false); - File messagesFile = new File(getDataFolder() + "/messages.yml"); - if (!messagesFile.exists()) { - messagesFile = new File(getClass().getResource("messages.yml").toURI()); - messagesFile.createNewFile(); - } - this.localization = new Localization(messagesFile); - this.localization.load(); - Log.debug("Localization loaded from jar file."); - } catch (URISyntaxException | IOException ex) { - Log.error("Failed to load localization!"); - Log.error(ex); - } + File messagesFile = new File(getDataFolder() + "/messages.yml"); + this.localization = new Localization(messagesFile); + this.localization.load(); } diff --git a/src/main/java/tsp/headdb/api/Head.java b/src/main/java/tsp/headdb/api/Head.java index f07d0d9..959df5b 100644 --- a/src/main/java/tsp/headdb/api/Head.java +++ b/src/main/java/tsp/headdb/api/Head.java @@ -74,7 +74,11 @@ public class Head { if (itemStack == null) { itemStack = menuItem; ItemMeta meta = itemStack.getItemMeta(); - meta.setDisplayName(HeadDB.getInstance().getLocalization().getMessage("head.name")); + meta.setDisplayName(HeadDB.getInstance().getLocalization().getMessage("head.name") + .replace("%name%", name) + .replace("%id%", String.valueOf(id)) + .replace("%value%", value) + .replace("%tags%", buildTagLore(tags))); meta.setLore(HeadDB.getInstance().getLocalization().getData().getStringList("head.lore")); itemStack.setItemMeta(meta); } diff --git a/src/main/java/tsp/headdb/command/CommandHeadDB.java b/src/main/java/tsp/headdb/command/CommandHeadDB.java index 0456e87..69365f7 100644 --- a/src/main/java/tsp/headdb/command/CommandHeadDB.java +++ b/src/main/java/tsp/headdb/command/CommandHeadDB.java @@ -50,16 +50,17 @@ public class CommandHeadDB implements CommandExecutor { Utils.sendMessage(sender, localization.getMessage("noPermission")); return true; } - if (args.length < 2) { - Utils.sendMessage(sender, "&c/hdb search "); - return true; - } if (!(sender instanceof Player)) { Utils.sendMessage(sender, localization.getMessage("onlyPlayers")); return true; } Player player = (Player) sender; + if (args.length < 2) { + Utils.sendMessage(player, "&c/hdb search "); + return true; + } + StringBuilder builder = new StringBuilder(); for (int i = 1; i < args.length; i++) { builder.append(args[i]); diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index a5428be..d688ed4 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -34,52 +34,6 @@ public class InventoryUtils { private static final Map uiLocation = new HashMap<>(); private static final Map uiItem = new HashMap<>(); - public static int getUILocation(String category, int slot) { - // Try to use the cached value first. - if (uiLocation.containsKey(category)) return uiLocation.get(category); - - // Try to get the value from the config file. - if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".location")) { - uiLocation.put(category, HeadDB.getInstance().getConfig().getInt("ui.category." + category + ".location")); - return uiLocation.get(category); - } - - // No valid value in the config file, return the given default. - uiLocation.put(category, slot); - return slot; - } - - public static ItemStack getUIItem(String category, ItemStack item) { - // Try to use the cached item first. - if (uiItem.containsKey(category)) return uiItem.get(category); - - // Try to get a head from the config file. - if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".head")) { - int id = HeadDB.getInstance().getConfig().getInt("ui.category." + category + ".head"); - Head head = HeadAPI.getHeadByID(id); - if (head != null) { - uiItem.put(category, head.getMenuItem()); - return uiItem.get(category); - } - } - - // Try to get an item from the config file. - if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".item")) { - String cfg = HeadDB.getInstance().getConfig().getString("ui.category." + category + ".item"); - Material mat = Material.matchMaterial(cfg); - - // AIR is allowed as the fill material for the menu, but not as a category item. - if (mat != null && (category.equals("fill") || mat != Material.AIR)) { - uiItem.put(category, new ItemStack(mat)); - return uiItem.get(category); - } - } - - // No valid head or item in the config file, return the given default. - uiItem.put(category, item); - return item; - } - public static void openLocalMenu(Player player) { List heads = HeadAPI.getLocalHeads(); @@ -113,7 +67,6 @@ public class InventoryUtils { pane.addButton(new Button(head.getMenuItem(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { purchaseHead(player, head, 64, head.getCategory().getName(), head.getName()); - return; } if (e.getClick() == ClickType.LEFT) { purchaseHead(player, head, 1, head.getCategory().getName(), head.getName()); @@ -139,7 +92,6 @@ public class InventoryUtils { pane.addButton(new Button(head.getMenuItem(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { purchaseHead(player, head, 64, head.getCategory().getName(), head.getName()); - return; } if (e.getClick() == ClickType.LEFT) { purchaseHead(player, head, 1, head.getCategory().getName(), head.getName()); @@ -171,7 +123,6 @@ public class InventoryUtils { pane.addButton(new Button(head.getMenuItem(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { purchaseHead(player, head, 64, head.getCategory().getName(), head.getName()); - return; } if (e.getClick() == ClickType.LEFT) { purchaseHead(player, head, 1, head.getCategory().getName(), head.getName()); @@ -202,7 +153,6 @@ public class InventoryUtils { pane.addButton(new Button(head.getMenuItem(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { purchaseHead(player, head, 64, head.getCategory().getName(), head.getName()); - return; } if (e.getClick() == ClickType.LEFT) { purchaseHead(player, head, 1, head.getCategory().getName(), head.getName()); @@ -270,7 +220,7 @@ public class InventoryUtils { player.openInventory(inventory); } - public static void fill(Inventory inv) { + private static void fill(Inventory inv) { ItemStack item = getUIItem("fill", new ItemStack(Material.BLACK_STAINED_GLASS_PANE)); // Do not bother filling the inventory if item to fill it with is AIR. if (item == null || item.getType() == Material.AIR) return; @@ -285,6 +235,53 @@ public class InventoryUtils { } } + private static int getUILocation(String category, int slot) { + // Try to use the cached value first. + if (uiLocation.containsKey(category)) return uiLocation.get(category); + + // Try to get the value from the config file. + if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".location")) { + uiLocation.put(category, HeadDB.getInstance().getConfig().getInt("ui.category." + category + ".location")); + return uiLocation.get(category); + } + + // No valid value in the config file, return the given default. + uiLocation.put(category, slot); + return slot; + } + + private static ItemStack getUIItem(String category, ItemStack item) { + // Try to use the cached item first. + if (uiItem.containsKey(category)) return uiItem.get(category); + + // Try to get a head from the config file. + if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".head")) { + int id = HeadDB.getInstance().getConfig().getInt("ui.category." + category + ".head"); + Head head = HeadAPI.getHeadByID(id); + if (head != null) { + uiItem.put(category, head.getMenuItem()); + return uiItem.get(category); + } + } + + // Try to get an item from the config file. + if (HeadDB.getInstance().getConfig().contains("ui.category." + category + ".item")) { + String cfg = HeadDB.getInstance().getConfig().getString("ui.category." + category + ".item"); + Material mat = Material.matchMaterial(cfg); + + // AIR is allowed as the fill material for the menu, but not as a category item. + if (mat != null && (category.equals("fill") || mat != Material.AIR)) { + uiItem.put(category, new ItemStack(mat)); + return uiItem.get(category); + } + } + + // No valid head or item in the config file, return the given default. + uiItem.put(category, item); + return item; + } + + private static ItemStack buildButton(ItemStack item, String name, String... lore) { ItemMeta meta = item.getItemMeta(); meta.setDisplayName(Utils.colorize(name));