Make it possible to rearrange the categories menu through the config file.

Dieser Commit ist enthalten in:
Leandro 2021-07-04 19:32:13 -03:00
Ursprung 0c2b6bdc3c
Commit 9301d08472
3 geänderte Dateien mit 74 neuen und 30 gelöschten Zeilen

Datei anzeigen

@ -11,25 +11,26 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public enum Category { public enum Category {
ALPHABET("alphabet", ChatColor.YELLOW, 20),
ALPHABET("alphabet", ChatColor.YELLOW), ANIMALS("animals", ChatColor.DARK_AQUA, 21),
ANIMALS("animals", ChatColor.DARK_AQUA), BLOCKS("blocks", ChatColor.DARK_GRAY, 22),
BLOCKS("blocks", ChatColor.DARK_GRAY), DECORATION("decoration", ChatColor.LIGHT_PURPLE, 23),
DECORATION("decoration", ChatColor.LIGHT_PURPLE), FOOD_DRINKS("food-drinks", ChatColor.GOLD, 24),
FOOD_DRINKS("food-drinks", ChatColor.GOLD), HUMANS("humans", ChatColor.DARK_BLUE, 29),
HUMANS("humans", ChatColor.DARK_BLUE), HUMANOID("humanoid", ChatColor.AQUA, 30),
HUMANOID("humanoid", ChatColor.AQUA), MISCELLANEOUS("miscellaneous", ChatColor.DARK_GREEN, 31),
MISCELLANEOUS("miscellaneous", ChatColor.DARK_GREEN), MONSTERS("monsters", ChatColor.RED, 32),
MONSTERS("monsters", ChatColor.RED), PLANTS("plants", ChatColor.GREEN, 33);
PLANTS("plants", ChatColor.GREEN);
private final String name; private final String name;
private final ChatColor color; private final ChatColor color;
private final int location;
private final Map<Category, Head> item = new HashMap<>(); private final Map<Category, Head> item = new HashMap<>();
Category(String name, ChatColor color) { Category(String name, ChatColor color, int location) {
this.name = name; this.name = name;
this.color = color; this.color = color;
this.location = location;
} }
public String getName() { public String getName() {
@ -40,6 +41,10 @@ public enum Category {
return color; return color;
} }
public int getLocation() {
return location;
}
public ItemStack getItem() { public ItemStack getItem() {
if (item.containsKey(this)) { if (item.containsKey(this)) {
return item.get(this).getItemStack(); return item.get(this).getItemStack();

Datei anzeigen

@ -7,6 +7,7 @@ import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import tsp.headdb.HeadDB; import tsp.headdb.HeadDB;
import tsp.headdb.api.Head; import tsp.headdb.api.Head;
import tsp.headdb.api.HeadAPI; import tsp.headdb.api.HeadAPI;
@ -14,13 +15,28 @@ import tsp.headdb.api.LocalHead;
import tsp.headdb.database.Category; import tsp.headdb.database.Category;
import tsp.headdb.util.Utils; import tsp.headdb.util.Utils;
import tsp.headdb.util.XMaterial; import tsp.headdb.util.XMaterial;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class InventoryUtils { public class InventoryUtils {
private static final Map<String, Integer> 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) { public static void openLocalMenu(Player player) {
PagedPane pane = new PagedPane(4, 6, Utils.colorize("&c&lHeadDB &8- &aLocal Heads")); 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) { public static void openDatabase(Player player) {
Inventory inventory = Bukkit.createInventory(null, 54, Utils.colorize("&c&lHeadDB &8(" + HeadAPI.getHeads().size() + ")")); 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()) { for (Category category : Category.getCategories()) {
ItemStack item = category.getItem(); ItemStack item = category.getItem();
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
@ -152,11 +167,11 @@ public class InventoryUtils {
lore.add(Utils.colorize("&e" + HeadAPI.getHeads(category).size() + " heads")); lore.add(Utils.colorize("&e" + HeadAPI.getHeads(category).size() + " heads"));
meta.setLore(lore); meta.setLore(lore);
item.setItemMeta(meta); item.setItemMeta(meta);
inventory.addItem(item); inventory.setItem(uiGetLocation(category.getName(), category.getLocation()), item);
} }
if (player.hasPermission("headdb.favorites")) { if (player.hasPermission("headdb.favorites")) {
inventory.setItem(39, buildButton( inventory.setItem(uiGetLocation("favorites", 39), buildButton(
XMaterial.BOOK.parseItem(), XMaterial.BOOK.parseItem(),
"&eFavorites", "&eFavorites",
"", "",
@ -165,7 +180,7 @@ public class InventoryUtils {
} }
if (player.hasPermission("headdb.search")) { if (player.hasPermission("headdb.search")) {
inventory.setItem(40, buildButton( inventory.setItem(uiGetLocation("search", 40), buildButton(
XMaterial.DARK_OAK_SIGN.parseItem(), XMaterial.DARK_OAK_SIGN.parseItem(),
"&9Search", "&9Search",
"", "",
@ -174,7 +189,7 @@ public class InventoryUtils {
} }
if (player.hasPermission("headdb.local")) { if (player.hasPermission("headdb.local")) {
inventory.setItem(41, buildButton( inventory.setItem(uiGetLocation("local", 41), buildButton(
XMaterial.COMPASS.parseItem(), XMaterial.COMPASS.parseItem(),
"&aLocal", "&aLocal",
"", "",
@ -182,28 +197,22 @@ public class InventoryUtils {
)); ));
} }
fill(inventory, new ItemStack(Material.BLACK_STAINED_GLASS_PANE));
player.openInventory(inventory); player.openInventory(inventory);
} }
public static void fill(Inventory inv, ItemStack item) { public static void fill(Inventory inv, ItemStack item) {
int size = inv.getSize(); 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++) { for (int i = 0; i < size; i++) {
if (!contains(i, ignored)) { ItemStack slotItem = inv.getItem(i);
ItemStack slotItem = inv.getItem(i); if (slotItem == null || slotItem.getType() == Material.AIR) {
if (slotItem == null || slotItem.getType() == Material.AIR) { inv.setItem(i, item);
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) { private static ItemStack buildButton(ItemStack item, String name, String... lore) {
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
meta.setDisplayName(Utils.colorize(name)); meta.setDisplayName(Utils.colorize(name));

Datei anzeigen

@ -23,5 +23,35 @@ economy:
plants: 100 plants: 100
local: 1000 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 Mode
debug: false debug: false