v0.6.1 - Added logic to settings menu. Implemented ConfigManager within it as well. Registered it with PrisonSetup main class was required.

This commit is contained in:
WildInterloper 2024-03-06 11:37:20 -05:00
parent 6ba21eaf68
commit 0ae8a88521
3 changed files with 40 additions and 24 deletions

View File

@ -3,6 +3,7 @@ package me.nvus.nvus_prison_setup.Configs;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -13,61 +14,74 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public class SettingsMenu implements Listener {
private final JavaPlugin plugin;
private final ConfigManager configManager;
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 + "NVus Prison Settings");
Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_GREEN + "Settings Menu");
// Toggle AutoPickup
ItemStack toggleAutoPickup = new ItemStack(Material.HOPPER);
ItemMeta metaAutoPickup = toggleAutoPickup.getItemMeta();
metaAutoPickup.setDisplayName(ChatColor.GREEN + "Toggle AutoPickup");
toggleAutoPickup.setItemMeta(metaAutoPickup);
// 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);
// Toggle AutoSwitch
ItemStack toggleAutoSwitch = new ItemStack(Material.LEVER);
ItemMeta metaAutoSwitch = toggleAutoSwitch.getItemMeta();
metaAutoSwitch.setDisplayName(ChatColor.GREEN + "Toggle AutoSwitch");
toggleAutoSwitch.setItemMeta(metaAutoSwitch);
// AutoSwitch Toggle Item
boolean autoswitch = config.getBoolean("AutoSwitch", false);
ItemStack toggleAutoSwitch = createToggleItem(Material.LEVER, "Toggle AutoSwitch", autoswitch);
inv.setItem(4, toggleAutoSwitch);
// Reload Configurations
// Reload Configurations Item
ItemStack reloadConfigs = new ItemStack(Material.BOOK);
ItemMeta metaReloadConfigs = reloadConfigs.getItemMeta();
metaReloadConfigs.setDisplayName(ChatColor.GREEN + "Reload Configs");
reloadConfigs.setItemMeta(metaReloadConfigs);
ItemMeta reloadMeta = reloadConfigs.getItemMeta();
reloadMeta.setDisplayName(ChatColor.GREEN + "Reload Configs");
reloadConfigs.setItemMeta(reloadMeta);
inv.setItem(5, reloadConfigs);
player.openInventory(inv);
}
private ItemStack createToggleItem(Material material, String name, boolean enabled) {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.GREEN + name + ": " + (enabled ? ChatColor.BLUE + "Enabled" : ChatColor.RED + "Disabled"));
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();
ItemStack clickedItem = event.getCurrentItem();
if (clickedItem == null || !clickedItem.hasItemMeta()) return;
String displayName = clickedItem.getItemMeta().getDisplayName();
if (displayName.equals(ChatColor.GREEN + "Toggle AutoPickup")) {
// Toggle AutoPickup logic here
} else if (displayName.equals(ChatColor.GREEN + "Toggle AutoSwitch")) {
// Toggle AutoSwitch logic here
} else if (displayName.equals(ChatColor.GREEN + "Reload Configs")) {
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"));
} else if (displayName.contains("Toggle AutoSwitch")) {
boolean currentValue = config.getBoolean("AutoSwitch", false);
config.set("AutoSwitch", !currentValue);
player.sendMessage(ChatColor.GREEN + "AutoSwitch " + (currentValue ? "disabled" : "enabled"));
} 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.");
}
configManager.saveConfig("config.yml");
player.closeInventory(); // Optional: Close the inventory after interaction
}
}

View File

@ -21,7 +21,7 @@ public class CommandListener implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// Check if the sender has the required permission
if (!sender.hasPermission("nvus.admin")) {
sender.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
return true;

View File

@ -2,6 +2,7 @@ package me.nvus.nvus_prison_setup;
import me.nvus.nvus_prison_setup.Configs.ConfigManager;
import me.nvus.nvus_prison_setup.Listeners.CommandListener;
import me.nvus.nvus_prison_setup.Configs.SettingsMenu;
import me.nvus.nvus_prison_setup.Listeners.PlayerArmor;
import me.nvus.nvus_prison_setup.Listeners.PlayerItems;
import me.nvus.nvus_prison_setup.Listeners.PlayerSpawn;
@ -31,6 +32,7 @@ public final class PrisonSetup extends JavaPlugin {
getServer().getPluginManager().registerEvents(new BlockListener(this), this);
getServer().getPluginManager().registerEvents(new ToolSwitchListener(configManager), this);
this.getCommand("nvus").setExecutor(new CommandListener(this, configManager));
new SettingsMenu(this, configManager);
getLogger().info(ChatColor.translateAlternateColorCodes('&',"&a&lNVus Prison Setup has been successfully enabled!"));