From 7551ec698c3217b668b1b8986783414f8b6d6bfb Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Sat, 18 Jun 2022 16:29:08 +0300 Subject: [PATCH] Updated admin settings menu to have configuration sections open separate menus --- .../core/itemstack/ItemBuilder.java | 24 --- .../impl/internal/SuperiorMenuSettings.java | 152 +++++++++--------- 2 files changed, 77 insertions(+), 99 deletions(-) diff --git a/src/main/java/com/bgsoftware/superiorskyblock/core/itemstack/ItemBuilder.java b/src/main/java/com/bgsoftware/superiorskyblock/core/itemstack/ItemBuilder.java index bdfb0582b..f3285344a 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/core/itemstack/ItemBuilder.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/core/itemstack/ItemBuilder.java @@ -11,7 +11,6 @@ import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.OfflinePlayer; -import org.bukkit.configuration.ConfigurationSection; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemFlag; @@ -132,29 +131,6 @@ public ItemBuilder withLore(String firstLine, List listLine) { return this; } - public ItemBuilder withLore(String firstLine, ConfigurationSection configurationSection) { - if (itemMeta == null) - return this; - - List loreList = new ArrayList<>(); - - firstLine = Formatters.COLOR_FORMATTER.format(firstLine); - loreList.add(firstLine); - - for (String section : configurationSection.getKeys(false)) { - section = section + ": " + configurationSection.get(section).toString(); - loreList.add(ChatColor.getLastColors(firstLine) + Formatters.COLOR_FORMATTER.format(section)); - } - - if (loreList.size() > 16) { - loreList = loreList.subList(0, 16); - loreList.add(ChatColor.getLastColors(firstLine) + "..."); - } - - itemMeta.setLore(loreList); - return this; - } - public ItemBuilder replaceLore(String regex, String replace) { if (itemMeta == null || !itemMeta.hasLore()) return this; diff --git a/src/main/java/com/bgsoftware/superiorskyblock/core/menu/impl/internal/SuperiorMenuSettings.java b/src/main/java/com/bgsoftware/superiorskyblock/core/menu/impl/internal/SuperiorMenuSettings.java index c82e571fb..d06641113 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/core/menu/impl/internal/SuperiorMenuSettings.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/core/menu/impl/internal/SuperiorMenuSettings.java @@ -9,6 +9,7 @@ import com.bgsoftware.superiorskyblock.core.debug.PluginDebugger; import com.bgsoftware.superiorskyblock.core.formatting.Formatters; import com.bgsoftware.superiorskyblock.core.itemstack.ItemBuilder; +import com.bgsoftware.superiorskyblock.core.menu.PagedSuperiorMenu; import com.bgsoftware.superiorskyblock.core.menu.TemplateItem; import com.bgsoftware.superiorskyblock.core.menu.button.PagedObjectButton; import com.bgsoftware.superiorskyblock.core.menu.button.SuperiorMenuButton; @@ -16,7 +17,6 @@ import com.bgsoftware.superiorskyblock.core.menu.pattern.impl.PagedMenuPattern; import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor; import com.bgsoftware.superiorskyblock.player.chat.PlayerChat; -import com.bgsoftware.superiorskyblock.core.menu.PagedSuperiorMenu; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; @@ -46,35 +46,45 @@ public class SuperiorMenuSettings extends PagedSuperiorMenu menuPattern; - private static final List pathSlots = new ArrayList<>(); + private static final Map> pathSlots = new HashMap<>(); private static final String[] ignorePaths = new String[]{"database", "max-island-size", "island-roles", "worlds.normal-world", "commands-cooldown", "starter-chest", "event-commands"}; - private static final Map lastPage = new HashMap<>(); + + private static final Map lastPage = new HashMap<>(); + private static final Set pageMove = new HashSet<>(); private static final Set activePlayers = new HashSet<>(); private static CommentedConfiguration config; - private SuperiorMenuSettings(SuperiorPlayer superiorPlayer) { + private final String configurationPath; + + private SuperiorMenuSettings(SuperiorPlayer superiorPlayer, String configurationPath) { super(menuPattern, superiorPlayer, true); + this.configurationPath = configurationPath; setPageMoveRunnable(_superiorPlayer -> pageMove.add(_superiorPlayer.getUniqueId())); } @Override public void cloneAndOpen(ISuperiorMenu previousMenu) { - openInventory(inventoryViewer, previousMenu); + openInventory(inventoryViewer, previousMenu, currentPage, configurationPath); + } + + @Override + protected String replaceTitle(String title) { + return configurationPath.isEmpty() ? title : ChatColor.BOLD + "Section: " + configurationPath; } @Override protected List requestObjects() { List itemStacks = new ArrayList<>(); - buildFromSection(itemStacks, config.getConfigurationSection("")); + buildFromSection(itemStacks, config.getConfigurationSection(this.configurationPath)); return itemStacks; } @Override public void open(ISuperiorMenu previousMenu) { - lastPage.put(inventoryViewer.getUniqueId(), currentPage); + lastPage.put(inventoryViewer.getUniqueId(), this); super.open(previousMenu); } @@ -82,7 +92,8 @@ public void open(ISuperiorMenu previousMenu) { public void closeInventory(SuperiorSkyblockPlugin plugin, SuperiorPlayer superiorPlayer) { super.closeInventory(plugin, superiorPlayer); - if (!activePlayers.remove(superiorPlayer.getUniqueId()) && !pageMove.remove(superiorPlayer.getUniqueId())) { + if ((!previousMove || previousMenu == null) && !activePlayers.remove(superiorPlayer.getUniqueId()) && + !pageMove.remove(superiorPlayer.getUniqueId())) { reloadConfiguration(); lastPage.remove(superiorPlayer.getUniqueId()); } @@ -98,6 +109,7 @@ public static void init() { menuPattern = patternBuilder .setTitle(ChatColor.BOLD + "Settings Editor") + .setPreviousMoveAllowed(true) .setInventoryType(InventoryType.CHEST) .setRowsSize(6) .setButton(47, new DummyButton.Builder() @@ -119,44 +131,18 @@ public static void init() { } public static void openInventory(SuperiorPlayer superiorPlayer, ISuperiorMenu previousMenu) { - openInventory(superiorPlayer, previousMenu, 1); + openInventory(superiorPlayer, previousMenu, 1, ""); } - public static void openInventory(SuperiorPlayer superiorPlayer, ISuperiorMenu previousMenu, int page) { - SuperiorMenuSettings superiorMenuSettings = new SuperiorMenuSettings(superiorPlayer); + public static void openInventory(SuperiorPlayer superiorPlayer, ISuperiorMenu previousMenu, int page, String path) { + SuperiorMenuSettings superiorMenuSettings = new SuperiorMenuSettings(superiorPlayer, path); superiorMenuSettings.currentPage = page; superiorMenuSettings.open(previousMenu); } private static boolean onPlayerChat(Player player, Object message, String path) { if (!message.toString().equalsIgnoreCase("-cancel")) { - if (config.isConfigurationSection(path)) { - Matcher matcher; - if (!(matcher = Pattern.compile("(.*):(.*)").matcher(message.toString())).matches()) { - player.sendMessage(ChatColor.RED + "Please follow the : format"); - } else { - path = path + "." + matcher.group(1); - message = matcher.group(2); - - if (config.get(path) != null && config.get(path).toString().equals(message)) { - player.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "SuperiorSkyblock" + ChatColor.GRAY + " Removed the value " + matcher.group(1) + " from " + path); - message = null; - } else { - player.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "SuperiorSkyblock" + ChatColor.GRAY + " Added the value " + message.toString() + " to " + path); - - try { - message = Integer.valueOf(message.toString()); - } catch (IllegalArgumentException ex) { - if (message.toString().equalsIgnoreCase("true") || message.toString().equalsIgnoreCase("false")) { - message = Boolean.valueOf(message.toString()); - } - } - - } - - config.set(path, message); - } - } else if (config.isList(path)) { + if (config.isList(path)) { List list = config.getStringList(path); if (list.contains(message.toString())) { @@ -205,13 +191,16 @@ private static void updateConfig(Player player, String path, Object value) { private static void reopenMenu(Player player) { BukkitExecutor.sync(() -> { - Integer page = lastPage.remove(player.getUniqueId()); + PlayerChat.remove(player); + + ISuperiorMenu previousMenu = lastPage.remove(player.getUniqueId()); + if (previousMenu != null) { + previousMenu.cloneAndOpen(previousMenu.getPreviousMenu()); + } else { + SuperiorMenuSettings.openInventory(plugin.getPlayers().getSuperiorPlayer(player), null); + } - if (page == null) - page = 1; - PlayerChat.remove(player); - SuperiorMenuSettings.openInventory(plugin.getPlayers().getSuperiorPlayer(player), null, Math.max(1, page)); }); } @@ -235,37 +224,37 @@ private static void reloadConfiguration() { } private static void buildFromSection(List itemStacks, ConfigurationSection section) { + List pathSlots = new ArrayList<>(); + for (String path : section.getKeys(false)) { String fullPath = section.getCurrentPath().isEmpty() ? path : section.getCurrentPath() + "." + path; if (Arrays.stream(ignorePaths).anyMatch(fullPath::contains)) continue; - if (section.isConfigurationSection(path)) { - buildFromSection(itemStacks, section.getConfigurationSection(path)); - } else { - ItemBuilder itemBuilder = new ItemBuilder(Materials.CLOCK.toBukkitItem()).withName("&6" + - Formatters.CAPITALIZED_FORMATTER.format(fullPath.replace("-", "_") - .replace(".", "_").replace(" ", "_")) - ); - - if (section.isBoolean(path)) - itemBuilder.withLore("&7Value: " + section.getBoolean(path)); - else if (section.isInt(path)) - itemBuilder.withLore("&7Value: " + section.getInt(path)); - else if (section.isDouble(path)) - itemBuilder.withLore("&7Value: " + section.getDouble(path)); - else if (section.isString(path)) - itemBuilder.withLore("&7Value: " + section.getString(path)); - else if (section.isList(path)) - itemBuilder.withLore("&7Value:", section.getStringList(path)); - else if (section.isConfigurationSection(path)) - itemBuilder.withLore("&7Value:", section.getConfigurationSection(path)); - - pathSlots.add(fullPath); - itemStacks.add(itemBuilder.build()); - } + ItemBuilder itemBuilder = new ItemBuilder(Materials.CLOCK.toBukkitItem()).withName("&6" + + Formatters.CAPITALIZED_FORMATTER.format(path.replace("-", "_") + .replace(".", "_").replace(" ", "_")) + ); + + if (section.isBoolean(path)) + itemBuilder.withLore("&7Value: " + section.getBoolean(path)); + else if (section.isInt(path)) + itemBuilder.withLore("&7Value: " + section.getInt(path)); + else if (section.isDouble(path)) + itemBuilder.withLore("&7Value: " + section.getDouble(path)); + else if (section.isString(path)) + itemBuilder.withLore("&7Value: " + section.getString(path)); + else if (section.isList(path)) + itemBuilder.withLore("&7Value:", section.getStringList(path)); + else if (section.isConfigurationSection(path)) + itemBuilder.withLore("&7Click to edit section."); + + pathSlots.add(path); + itemStacks.add(itemBuilder.build()); } + + SuperiorMenuSettings.pathSlots.put(section.getCurrentPath(), pathSlots); } private static class SaveButton extends SuperiorMenuButton { @@ -313,25 +302,33 @@ public void onButtonClick(SuperiorSkyblockPlugin plugin, SuperiorMenuSettings su InventoryClickEvent clickEvent) { try { Player player = (Player) clickEvent.getWhoClicked(); - String path = pathSlots.get((superiorMenu.currentPage - 1) * 36 + clickEvent.getRawSlot()); + String sectionPath = pathSlots.get(superiorMenu.configurationPath).get((superiorMenu.currentPage - 1) * 36 + clickEvent.getRawSlot()); - if (path == null) + if (sectionPath == null) return; - if (config.isBoolean(path)) { - updateConfig(player, path, !config.getBoolean(path)); - activePlayers.add(player.getUniqueId()); + String fullPath = superiorMenu.configurationPath.isEmpty() ? sectionPath : + superiorMenu.configurationPath + "." + sectionPath; + + if (config.isConfigurationSection(fullPath)) { + prepareClose(player, superiorMenu); + openInventory(superiorMenu.inventoryViewer, superiorMenu, 1, fullPath); + } else if (config.isBoolean(fullPath)) { + updateConfig(player, fullPath, !config.getBoolean(fullPath)); + prepareClose(player, superiorMenu); superiorMenu.closePage(); + reopenMenu(player); } else { - activePlayers.add(player.getUniqueId()); - PlayerChat.listen(player, message -> onPlayerChat(player, message, path)); + PlayerChat.listen(player, message -> onPlayerChat(player, message, fullPath)); + prepareClose(player, superiorMenu); player.closeInventory(); + player.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "SuperiorSkyblock" + ChatColor.GRAY + " Please enter a new value (-cancel to cancel):"); - if (config.isList(path) || config.isConfigurationSection(path)) { + if (config.isList(fullPath)) { player.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + "SuperiorSkyblock" + ChatColor.GRAY + " If you enter a value that is already in the list, it will be removed."); } } @@ -340,6 +337,11 @@ public void onButtonClick(SuperiorSkyblockPlugin plugin, SuperiorMenuSettings su } } + private void prepareClose(Player player, SuperiorMenuSettings superiorMenu) { + superiorMenu.previousMove = false; + activePlayers.add(player.getUniqueId()); + } + private static class Builder extends PagedObjectBuilder { @Override