From 9301d084720d43d501e87f45bb81d70789489118 Mon Sep 17 00:00:00 2001 From: Leandro Date: Sun, 4 Jul 2021 19:32:13 -0300 Subject: [PATCH] Make it possible to rearrange the categories menu through the config file. --- .../java/tsp/headdb/database/Category.java | 29 +++++++------ .../tsp/headdb/inventory/InventoryUtils.java | 43 +++++++++++-------- src/main/resources/config.yml | 32 +++++++++++++- 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/src/main/java/tsp/headdb/database/Category.java b/src/main/java/tsp/headdb/database/Category.java index 969bec9..344df19 100644 --- a/src/main/java/tsp/headdb/database/Category.java +++ b/src/main/java/tsp/headdb/database/Category.java @@ -11,25 +11,26 @@ import java.util.List; import java.util.Map; public enum Category { - - ALPHABET("alphabet", ChatColor.YELLOW), - ANIMALS("animals", ChatColor.DARK_AQUA), - BLOCKS("blocks", ChatColor.DARK_GRAY), - DECORATION("decoration", ChatColor.LIGHT_PURPLE), - FOOD_DRINKS("food-drinks", ChatColor.GOLD), - HUMANS("humans", ChatColor.DARK_BLUE), - HUMANOID("humanoid", ChatColor.AQUA), - MISCELLANEOUS("miscellaneous", ChatColor.DARK_GREEN), - MONSTERS("monsters", ChatColor.RED), - PLANTS("plants", ChatColor.GREEN); + ALPHABET("alphabet", ChatColor.YELLOW, 20), + ANIMALS("animals", ChatColor.DARK_AQUA, 21), + BLOCKS("blocks", ChatColor.DARK_GRAY, 22), + DECORATION("decoration", ChatColor.LIGHT_PURPLE, 23), + FOOD_DRINKS("food-drinks", ChatColor.GOLD, 24), + HUMANS("humans", ChatColor.DARK_BLUE, 29), + HUMANOID("humanoid", ChatColor.AQUA, 30), + MISCELLANEOUS("miscellaneous", ChatColor.DARK_GREEN, 31), + MONSTERS("monsters", ChatColor.RED, 32), + PLANTS("plants", ChatColor.GREEN, 33); private final String name; private final ChatColor color; + private final int location; private final Map item = new HashMap<>(); - Category(String name, ChatColor color) { + Category(String name, ChatColor color, int location) { this.name = name; this.color = color; + this.location = location; } public String getName() { @@ -40,6 +41,10 @@ public enum Category { return color; } + public int getLocation() { + return location; + } + public ItemStack getItem() { if (item.containsKey(this)) { return item.get(this).getItemStack(); diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index d149c62..6c5987c 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -7,6 +7,7 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; + import tsp.headdb.HeadDB; import tsp.headdb.api.Head; import tsp.headdb.api.HeadAPI; @@ -14,13 +15,28 @@ import tsp.headdb.api.LocalHead; import tsp.headdb.database.Category; import tsp.headdb.util.Utils; import tsp.headdb.util.XMaterial; + import net.milkbowl.vault.economy.Economy; import java.util.ArrayList; -import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class InventoryUtils { + private static final Map uiLocation = new HashMap<>(); + + public static int uiGetLocation(String category, int slot) { + // Try to use the cached value first; then the config; then the given default. + if (!uiLocation.containsKey(category)) { + if (HeadDB.getInstance().getCfg().contains("ui.category." + category + ".location")) { + uiLocation.put(category, HeadDB.getInstance().getCfg().getInt("ui.category." + category + ".location")); + } else { + uiLocation.put(category, slot); + } + } + return uiLocation.get(category); + } public static void openLocalMenu(Player player) { PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &aLocal Heads")); @@ -143,7 +159,6 @@ public class InventoryUtils { public static void openDatabase(Player player) { Inventory inventory = Bukkit.createInventory(null, 54, Utils.colorize("&c&lHeadDB &8(" + HeadAPI.getHeads().size() + ")")); - fill(inventory, new ItemStack(Material.BLACK_STAINED_GLASS_PANE)); for (Category category : Category.getCategories()) { ItemStack item = category.getItem(); ItemMeta meta = item.getItemMeta(); @@ -152,11 +167,11 @@ public class InventoryUtils { lore.add(Utils.colorize("&e" + HeadAPI.getHeads(category).size() + " heads")); meta.setLore(lore); item.setItemMeta(meta); - inventory.addItem(item); + inventory.setItem(uiGetLocation(category.getName(), category.getLocation()), item); } if (player.hasPermission("headdb.favorites")) { - inventory.setItem(39, buildButton( + inventory.setItem(uiGetLocation("favorites", 39), buildButton( XMaterial.BOOK.parseItem(), "&eFavorites", "", @@ -165,7 +180,7 @@ public class InventoryUtils { } if (player.hasPermission("headdb.search")) { - inventory.setItem(40, buildButton( + inventory.setItem(uiGetLocation("search", 40), buildButton( XMaterial.DARK_OAK_SIGN.parseItem(), "&9Search", "", @@ -174,7 +189,7 @@ public class InventoryUtils { } if (player.hasPermission("headdb.local")) { - inventory.setItem(41, buildButton( + inventory.setItem(uiGetLocation("local", 41), buildButton( XMaterial.COMPASS.parseItem(), "&aLocal", "", @@ -182,28 +197,22 @@ public class InventoryUtils { )); } + fill(inventory, new ItemStack(Material.BLACK_STAINED_GLASS_PANE)); player.openInventory(inventory); } public static void fill(Inventory inv, ItemStack item) { int size = inv.getSize(); - int[] ignored = new int[]{20, 21, 22, 23, 24, 29, 30, 31, 32, 33}; - // Fill + // Fill any non-empty inventory slots with the given item. for (int i = 0; i < size; i++) { - if (!contains(i, ignored)) { - ItemStack slotItem = inv.getItem(i); - if (slotItem == null || slotItem.getType() == Material.AIR) { - inv.setItem(i, item); - } + ItemStack slotItem = inv.getItem(i); + if (slotItem == null || slotItem.getType() == Material.AIR) { + inv.setItem(i, item); } } } - private static boolean contains(int n, int... array) { - return Arrays.binarySearch(array, n) > -1; - } - private static ItemStack buildButton(ItemStack item, String name, String... lore) { ItemMeta meta = item.getItemMeta(); meta.setDisplayName(Utils.colorize(name)); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 63cdfcd..5978308 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -23,5 +23,35 @@ economy: plants: 100 local: 1000 +# UI customization options. +ui: + category: + alphabet: + location: 20 + animals: + location: 21 + blocks: + location: 22 + decoration: + location: 23 + food-drinks: + location: 24 + humans: + location: 29 + humanoid: + location: 30 + miscellaneous: + location: 31 + monsters: + location: 32 + plants: + location: 33 + favorites: + location: 39 + search: + location: 40 + local: + location: 41 + # Debug Mode -debug: false \ No newline at end of file +debug: false