From 324707d632d6f1682d1e1c5bc2ce093bb402599a Mon Sep 17 00:00:00 2001 From: WildInterloper <156627888+WildInterloper@users.noreply.github.com> Date: Wed, 6 Mar 2024 11:46:40 -0500 Subject: [PATCH] v0.6.6 - Added cooldown to settings menu. Was triggering the click event multiple times in the given frames. --- pom.xml | 2 +- .../Configs/SettingsMenu.java | 77 +++++++++++-------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/pom.xml b/pom.xml index 9190af2..ac30e88 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.NVus NVus_Prison_Setup - 0.6.3 + 0.6.6 jar NVus_PrisonSetup diff --git a/src/main/java/me/nvus/nvus_prison_setup/Configs/SettingsMenu.java b/src/main/java/me/nvus/nvus_prison_setup/Configs/SettingsMenu.java index d63da20..715a281 100644 --- a/src/main/java/me/nvus/nvus_prison_setup/Configs/SettingsMenu.java +++ b/src/main/java/me/nvus/nvus_prison_setup/Configs/SettingsMenu.java @@ -13,36 +13,28 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.java.JavaPlugin; +import java.util.HashMap; +import java.util.UUID; + public class SettingsMenu implements Listener { + private final JavaPlugin plugin; private final ConfigManager configManager; + private final HashMap lastClickTime = new HashMap<>(); public SettingsMenu(JavaPlugin plugin, ConfigManager configManager) { + this.plugin = plugin; this.configManager = configManager; plugin.getServer().getPluginManager().registerEvents(this, plugin); } public void openSettingsMenu(Player player) { - Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_GREEN + "Settings Menu"); + Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_RED + "NVus Prison Settings"); - // Dynamically set the display based on current config settings FileConfiguration config = configManager.getConfig("config.yml"); - // AutoPickup Toggle Item - boolean autopickup = config.getBoolean("AutoPickup", false); - ItemStack toggleAutoPickup = createToggleItem(Material.HOPPER, "Toggle AutoPickup", autopickup); - inv.setItem(3, toggleAutoPickup); - - // AutoSwitch Toggle Item - boolean autoswitch = config.getBoolean("AutoSwitch", false); - ItemStack toggleAutoSwitch = createToggleItem(Material.LEVER, "Toggle AutoSwitch", autoswitch); - inv.setItem(4, toggleAutoSwitch); - - // Reload Configurations Item - ItemStack reloadConfigs = new ItemStack(Material.BOOK); - ItemMeta reloadMeta = reloadConfigs.getItemMeta(); - reloadMeta.setDisplayName(ChatColor.GREEN + "Reload Configs"); - reloadConfigs.setItemMeta(reloadMeta); - inv.setItem(5, reloadConfigs); + inv.setItem(3, createToggleItem(Material.HOPPER, "Toggle AutoPickup", config.getBoolean("AutoPickup", false))); + inv.setItem(4, createToggleItem(Material.LEVER, "Toggle AutoSwitch", config.getBoolean("AutoSwitch", false))); + inv.setItem(5, createItem(Material.BOOK, ChatColor.GREEN + "Reload Configs")); player.openInventory(inv); } @@ -55,33 +47,56 @@ public class SettingsMenu implements Listener { return item; } + private ItemStack createItem(Material material, String name) { + ItemStack item = new ItemStack(material); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(name); + item.setItemMeta(meta); + return item; + } + @EventHandler public void onInventoryClick(InventoryClickEvent event) { if (!event.getView().getTitle().equals(ChatColor.DARK_GREEN + "Settings Menu")) return; event.setCancelled(true); + Player player = (Player) event.getWhoClicked(); + UUID playerUUID = player.getUniqueId(); + + long lastClick = lastClickTime.getOrDefault(playerUUID, 0L); + long currentTime = System.currentTimeMillis(); + if (currentTime - lastClick < 500) { // Cooldown of 0.5 seconds + return; + } + lastClickTime.put(playerUUID, currentTime); + ItemStack clickedItem = event.getCurrentItem(); if (clickedItem == null || !clickedItem.hasItemMeta()) return; String displayName = clickedItem.getItemMeta().getDisplayName(); - FileConfiguration config = configManager.getConfig("config.yml"); - if (displayName.contains("Toggle AutoPickup")) { - boolean currentValue = config.getBoolean("AutoPickup", false); - config.set("AutoPickup", !currentValue); - player.sendMessage(ChatColor.GREEN + "AutoPickup " + (currentValue ? "disabled" : "enabled")); + toggleOption("AutoPickup", player); } else if (displayName.contains("Toggle AutoSwitch")) { - boolean currentValue = config.getBoolean("AutoSwitch", false); - config.set("AutoSwitch", !currentValue); - player.sendMessage(ChatColor.GREEN + "AutoSwitch " + (currentValue ? "disabled" : "enabled")); + toggleOption("AutoSwitch", player); } else if (displayName.contains("Reload Configs")) { - configManager.reloadConfig("config.yml"); - configManager.reloadConfig("auto_switch.yml"); - configManager.reloadConfig("banned_items.yml"); - player.sendMessage(ChatColor.GREEN + "Configuration files reloaded."); + reloadConfigs(player); } + } + private void toggleOption(String key, Player player) { + FileConfiguration config = configManager.getConfig("config.yml"); + boolean currentValue = config.getBoolean(key, false); + config.set(key, !currentValue); configManager.saveConfig("config.yml"); - player.closeInventory(); // Optional: Close the inventory after interaction + player.sendMessage(ChatColor.GREEN + key + " " + (!currentValue ? "enabled." : "disabled.")); + openSettingsMenu(player); // Refresh the menu + } + + private void reloadConfigs(Player player) { + configManager.reloadConfig("config.yml"); + configManager.reloadConfig("auto_switch.yml"); + configManager.reloadConfig("banned_items.yml"); + player.sendMessage(ChatColor.GREEN + "Configuration files reloaded."); + openSettingsMenu(player); // Refresh the menu } }