v0.6.6 - Added cooldown to settings menu. Was triggering the click event multiple times in the given frames.

This commit is contained in:
WildInterloper 2024-03-06 11:46:40 -05:00
parent 0ae8a88521
commit 324707d632
2 changed files with 47 additions and 32 deletions

View File

@ -6,7 +6,7 @@
<groupId>me.NVus</groupId> <groupId>me.NVus</groupId>
<artifactId>NVus_Prison_Setup</artifactId> <artifactId>NVus_Prison_Setup</artifactId>
<version>0.6.3</version> <version>0.6.6</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>NVus_PrisonSetup</name> <name>NVus_PrisonSetup</name>

View File

@ -13,36 +13,28 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.UUID;
public class SettingsMenu implements Listener { public class SettingsMenu implements Listener {
private final JavaPlugin plugin;
private final ConfigManager configManager; private final ConfigManager configManager;
private final HashMap<UUID, Long> lastClickTime = new HashMap<>();
public SettingsMenu(JavaPlugin plugin, ConfigManager configManager) { public SettingsMenu(JavaPlugin plugin, ConfigManager configManager) {
this.plugin = plugin;
this.configManager = configManager; this.configManager = configManager;
plugin.getServer().getPluginManager().registerEvents(this, plugin); plugin.getServer().getPluginManager().registerEvents(this, plugin);
} }
public void openSettingsMenu(Player player) { 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"); FileConfiguration config = configManager.getConfig("config.yml");
// AutoPickup Toggle Item inv.setItem(3, createToggleItem(Material.HOPPER, "Toggle AutoPickup", config.getBoolean("AutoPickup", false)));
boolean autopickup = config.getBoolean("AutoPickup", false); inv.setItem(4, createToggleItem(Material.LEVER, "Toggle AutoSwitch", config.getBoolean("AutoSwitch", false)));
ItemStack toggleAutoPickup = createToggleItem(Material.HOPPER, "Toggle AutoPickup", autopickup); inv.setItem(5, createItem(Material.BOOK, ChatColor.GREEN + "Reload Configs"));
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);
player.openInventory(inv); player.openInventory(inv);
} }
@ -55,33 +47,56 @@ public class SettingsMenu implements Listener {
return item; 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 @EventHandler
public void onInventoryClick(InventoryClickEvent event) { public void onInventoryClick(InventoryClickEvent event) {
if (!event.getView().getTitle().equals(ChatColor.DARK_GREEN + "Settings Menu")) return; if (!event.getView().getTitle().equals(ChatColor.DARK_GREEN + "Settings Menu")) return;
event.setCancelled(true); event.setCancelled(true);
Player player = (Player) event.getWhoClicked(); 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(); ItemStack clickedItem = event.getCurrentItem();
if (clickedItem == null || !clickedItem.hasItemMeta()) return; if (clickedItem == null || !clickedItem.hasItemMeta()) return;
String displayName = clickedItem.getItemMeta().getDisplayName(); String displayName = clickedItem.getItemMeta().getDisplayName();
FileConfiguration config = configManager.getConfig("config.yml");
if (displayName.contains("Toggle AutoPickup")) { if (displayName.contains("Toggle AutoPickup")) {
boolean currentValue = config.getBoolean("AutoPickup", false); toggleOption("AutoPickup", player);
config.set("AutoPickup", !currentValue);
player.sendMessage(ChatColor.GREEN + "AutoPickup " + (currentValue ? "disabled" : "enabled"));
} else if (displayName.contains("Toggle AutoSwitch")) { } else if (displayName.contains("Toggle AutoSwitch")) {
boolean currentValue = config.getBoolean("AutoSwitch", false); toggleOption("AutoSwitch", player);
config.set("AutoSwitch", !currentValue);
player.sendMessage(ChatColor.GREEN + "AutoSwitch " + (currentValue ? "disabled" : "enabled"));
} else if (displayName.contains("Reload Configs")) { } else if (displayName.contains("Reload Configs")) {
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.sendMessage(ChatColor.GREEN + key + " " + (!currentValue ? "enabled." : "disabled."));
openSettingsMenu(player); // Refresh the menu
}
private void reloadConfigs(Player player) {
configManager.reloadConfig("config.yml"); configManager.reloadConfig("config.yml");
configManager.reloadConfig("auto_switch.yml"); configManager.reloadConfig("auto_switch.yml");
configManager.reloadConfig("banned_items.yml"); configManager.reloadConfig("banned_items.yml");
player.sendMessage(ChatColor.GREEN + "Configuration files reloaded."); player.sendMessage(ChatColor.GREEN + "Configuration files reloaded.");
} openSettingsMenu(player); // Refresh the menu
configManager.saveConfig("config.yml");
player.closeInventory(); // Optional: Close the inventory after interaction
} }
} }