From d6cdde98c48f3fe6b1601876009513288443fb89 Mon Sep 17 00:00:00 2001 From: Leandro Date: Sun, 4 Jul 2021 21:35:50 -0300 Subject: [PATCH] Make it possible to change the default heads in the categories menu through the config. --- .../tsp/headdb/inventory/InventoryUtils.java | 38 ++++++++++++------- src/main/resources/config.yml | 12 ++++++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index b1a0eeb..032e5fa 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -39,22 +39,32 @@ public class InventoryUtils { } public static ItemStack uiGetItem(String category, ItemStack item) { - // Try to use the cached value first; then the config; then the given default. - if (!uiItem.containsKey(category)) { - if (HeadDB.getInstance().getCfg().contains("ui.category." + category + ".item")) { - String cfg = HeadDB.getInstance().getCfg().getString("ui.category." + category + ".item"); - Material mat = Material.matchMaterial(cfg); - if (mat == null || mat == Material.AIR) { - // Material set in config is invalid, use the given default. - uiItem.put(category, item); - } else { - uiItem.put(category, new ItemStack(mat)); - } - } else { - uiItem.put(category, item); + // Try to use the cached value first. + if (uiItem.containsKey(category)) return uiItem.get(category); + + // Try to get a head from the config file. + if (HeadDB.getInstance().getCfg().contains("ui.category." + category + ".head")) { + int id = HeadDB.getInstance().getCfg().getInt("ui.category." + category + ".head"); + Head head = HeadAPI.getHeadByID(id); + if (head != null) { + uiItem.put(category, head.getItemStack()); + return uiItem.get(category); } } - return uiItem.get(category); + + // Try to get an item from the config file. + if (HeadDB.getInstance().getCfg().contains("ui.category." + category + ".item")) { + String cfg = HeadDB.getInstance().getCfg().getString("ui.category." + category + ".item"); + Material mat = Material.matchMaterial(cfg); + if (mat != null && 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) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 53cee72..13f9721 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -26,26 +26,38 @@ economy: # UI customization options. ui: category: + # Head categories. alphabet: location: 20 + head: 1788 animals: location: 21 + head: 5741 blocks: location: 22 + head: 8624 decoration: location: 23 + head: 11046 food-drinks: location: 24 + head: 17442 humans: location: 29 + head: 19361 humanoid: location: 30 + head: 28320 miscellaneous: location: 31 + head: 32746 monsters: location: 32 + head: 34819 plants: location: 33 + head: 37278 + # Meta categories, used for UI elements. favorites: location: 39 item: BOOK