v0.6.6 - Added cooldown to settings menu. Was triggering the click event multiple times in the given frames.
This commit is contained in:
parent
0ae8a88521
commit
324707d632
2
pom.xml
2
pom.xml
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>me.NVus</groupId>
|
||||
<artifactId>NVus_Prison_Setup</artifactId>
|
||||
<version>0.6.3</version>
|
||||
<version>0.6.6</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>NVus_PrisonSetup</name>
|
||||
|
|
|
@ -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<UUID, Long> 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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue