diff --git a/src/main/java/com/songoda/epicheads/EpicHeads.java b/src/main/java/com/songoda/epicheads/EpicHeads.java index 585efd1..262710b 100644 --- a/src/main/java/com/songoda/epicheads/EpicHeads.java +++ b/src/main/java/com/songoda/epicheads/EpicHeads.java @@ -16,8 +16,9 @@ import com.songoda.epicheads.players.PlayerManager; import com.songoda.epicheads.utils.Methods; import com.songoda.epicheads.utils.Metrics; import com.songoda.epicheads.utils.ServerVersion; -import com.songoda.epicheads.utils.SettingsManager; import com.songoda.epicheads.utils.gui.AbstractGUI; +import com.songoda.epicheads.utils.settings.Setting; +import com.songoda.epicheads.utils.settings.SettingsManager; import com.songoda.epicheads.utils.storage.Storage; import com.songoda.epicheads.utils.storage.StorageRow; import com.songoda.epicheads.utils.storage.types.StorageYaml; @@ -69,10 +70,10 @@ public class EpicHeads extends JavaPlugin { console.sendMessage(Methods.formatText("&7Action: &aEnabling&7...")); this.settingsManager = new SettingsManager(this); - this.setupConfig(); + this.settingsManager.setupConfig(); // Setup language - String langMode = SettingsManager.Setting.LANGUGE_MODE.getString(); + String langMode = Setting.LANGUGE_MODE.getString(); Locale.init(this); Locale.saveDefaultLocale("en_US"); this.locale = Locale.getLocale(getConfig().getString("System.Language Mode", langMode)); @@ -97,13 +98,13 @@ public class EpicHeads extends JavaPlugin { Bukkit.getPluginManager().registerEvents(new LoginListeners(this), this); // Setup Economy - if (SettingsManager.Setting.VAULT_ECONOMY.getBoolean() + if (Setting.VAULT_ECONOMY.getBoolean() && getServer().getPluginManager().getPlugin("Vault") != null) this.economy = new VaultEconomy(this); - else if (SettingsManager.Setting.PLAYER_POINTS_ECONOMY.getBoolean() + else if (Setting.PLAYER_POINTS_ECONOMY.getBoolean() && getServer().getPluginManager().getPlugin("PlayerPoints") != null) this.economy = new PlayerPointsEconomy(this); - else if (SettingsManager.Setting.ITEM_ECONOMY.getBoolean()) + else if (Setting.ITEM_ECONOMY.getBoolean()) this.economy = new ItemEconomy(this); // Download Heads @@ -115,7 +116,7 @@ public class EpicHeads extends JavaPlugin { // Load Favorites loadData(); - int timeout = SettingsManager.Setting.AUTOSAVE.getInt() * 60 * 20; + int timeout = Setting.AUTOSAVE.getInt() * 60 * 20; Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, timeout, timeout); // Start Metrics @@ -192,7 +193,7 @@ public class EpicHeads extends JavaPlugin { int id = Integer.parseInt((String) jsonObject.get("id")); - if (SettingsManager.Setting.DISABLED_HEADS.getIntegerList().contains(id)) continue; + if (Setting.DISABLED_HEADS.getIntegerList().contains(id)) continue; Head head = new Head(id, (String) jsonObject.get("name"), @@ -268,18 +269,11 @@ public class EpicHeads extends JavaPlugin { return serverVersion.ordinal() >= version.ordinal(); } - private void setupConfig() { - settingsManager.updateSettings(); - this.getConfig().options().copyDefaults(true); - this.saveConfig(); - } - public void reload() { saveToFile(); locale.reloadMessages(); references = new References(); - this.setupConfig(); - saveConfig(); + settingsManager.reloadConfig(); saveToFile(); downloadHeads(); loadHeads(); diff --git a/src/main/java/com/songoda/epicheads/gui/GUIHeads.java b/src/main/java/com/songoda/epicheads/gui/GUIHeads.java index 5fd6fd4..52f532c 100644 --- a/src/main/java/com/songoda/epicheads/gui/GUIHeads.java +++ b/src/main/java/com/songoda/epicheads/gui/GUIHeads.java @@ -6,8 +6,8 @@ import com.songoda.epicheads.head.Category; import com.songoda.epicheads.head.Head; import com.songoda.epicheads.players.EPlayer; import com.songoda.epicheads.utils.AbstractChatConfirm; -import com.songoda.epicheads.utils.SettingsManager; import com.songoda.epicheads.utils.gui.AbstractGUI; +import com.songoda.epicheads.utils.settings.Setting; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Material; @@ -154,13 +154,13 @@ public class GUIHeads extends AbstractGUI { if (head.getName() == null) continue; boolean free = player.hasPermission("epicheads.bypasscost") - || (SettingsManager.Setting.FREE_IN_CREATIVE.getBoolean() && player.getGameMode() == GameMode.CREATIVE); + || (Setting.FREE_IN_CREATIVE.getBoolean() && player.getGameMode() == GameMode.CREATIVE); ItemStack item = head.asItemStack(favorites.contains(head.getURL()), free); inventory.setItem(i + 9, item); - double cost = SettingsManager.Setting.HEAD_COST.getDouble(); + double cost = Setting.HEAD_COST.getDouble(); registerClickable(i + 9, ((player1, inventory1, cursor, slot, type) -> { if (type == ClickType.SHIFT_LEFT || type == ClickType.SHIFT_RIGHT) { diff --git a/src/main/java/com/songoda/epicheads/gui/GUIOverview.java b/src/main/java/com/songoda/epicheads/gui/GUIOverview.java index f0e97fe..97fe577 100644 --- a/src/main/java/com/songoda/epicheads/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epicheads/gui/GUIOverview.java @@ -5,8 +5,8 @@ import com.songoda.epicheads.head.Category; import com.songoda.epicheads.head.Head; import com.songoda.epicheads.utils.Methods; import com.songoda.epicheads.utils.ServerVersion; -import com.songoda.epicheads.utils.SettingsManager; import com.songoda.epicheads.utils.gui.AbstractGUI; +import com.songoda.epicheads.utils.settings.Setting; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -107,10 +107,10 @@ public class GUIOverview extends AbstractGUI { category.isLatestPack() ? GUIHeads.QueryTypes.PACK : GUIHeads.QueryTypes.CATEGORY, heads))); } - createButton(SettingsManager.Setting.DISCORD.getBoolean() ? 39 : 40, Material.COMPASS, plugin.getLocale().getMessage("gui.overview.search")); + createButton(Setting.DISCORD.getBoolean() ? 39 : 40, Material.COMPASS, plugin.getLocale().getMessage("gui.overview.search")); - if (SettingsManager.Setting.DISCORD.getBoolean()) { + if (Setting.DISCORD.getBoolean()) { ArrayList lore2 = new ArrayList<>(); String[] parts2 = plugin.getLocale().getMessage("gui.overview.discordlore").split("\\|"); for (String line : parts2) @@ -130,10 +130,10 @@ public class GUIOverview extends AbstractGUI { new GUIHeads(plugin, player, null, GUIHeads.QueryTypes.FAVORITES, plugin.getPlayerManager().getPlayer(player).getFavoritesAsHeads()))); - registerClickable(SettingsManager.Setting.DISCORD.getBoolean() ? 39 : 40, ((player1, inventory1, cursor, slot, type) -> + registerClickable(Setting.DISCORD.getBoolean() ? 39 : 40, ((player1, inventory1, cursor, slot, type) -> GUIHeads.doSearch(player1))); - if (SettingsManager.Setting.DISCORD.getBoolean()) { + if (Setting.DISCORD.getBoolean()) { registerClickable(41, ((player1, inventory1, cursor, slot, type) -> { player.sendMessage(Methods.formatText(plugin.getReferences().getPrefix() + "&9https://discord.gg/A9TRJQb")); player.closeInventory(); diff --git a/src/main/java/com/songoda/epicheads/head/Head.java b/src/main/java/com/songoda/epicheads/head/Head.java index 3b109c4..7453766 100644 --- a/src/main/java/com/songoda/epicheads/head/Head.java +++ b/src/main/java/com/songoda/epicheads/head/Head.java @@ -4,7 +4,7 @@ package com.songoda.epicheads.head; import com.songoda.epicheads.EpicHeads; import com.songoda.epicheads.utils.Methods; import com.songoda.epicheads.utils.ServerVersion; -import com.songoda.epicheads.utils.SettingsManager; +import com.songoda.epicheads.utils.settings.Setting; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -64,7 +64,7 @@ public class Head { ItemStack item = Methods.addTexture(new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.PLAYER_HEAD : Material.valueOf("SKULL_ITEM"), 1, (byte) 3), this.URL); - double cost = SettingsManager.Setting.HEAD_COST.getDouble(); + double cost = Setting.HEAD_COST.getDouble(); ItemMeta meta = item.getItemMeta(); meta.setDisplayName(Methods.formatText((favorite ? "&6⭐ " : "") + "&9" + name)); List lore = new ArrayList<>(); diff --git a/src/main/java/com/songoda/epicheads/listeners/DeathListeners.java b/src/main/java/com/songoda/epicheads/listeners/DeathListeners.java index 15a0278..04b20e4 100644 --- a/src/main/java/com/songoda/epicheads/listeners/DeathListeners.java +++ b/src/main/java/com/songoda/epicheads/listeners/DeathListeners.java @@ -5,7 +5,7 @@ import com.songoda.epicheads.head.Head; import com.songoda.epicheads.utils.HeadType; import com.songoda.epicheads.utils.Methods; import com.songoda.epicheads.utils.ServerVersion; -import com.songoda.epicheads.utils.SettingsManager; +import com.songoda.epicheads.utils.settings.Setting; import org.bukkit.Material; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Player; @@ -29,13 +29,13 @@ public class DeathListeners implements Listener { @EventHandler public void onDeath(EntityDeathEvent event) { - int ch = Integer.parseInt(SettingsManager.Setting.DROP_CHANCE.getString().replace("%", "")); + int ch = Integer.parseInt(Setting.DROP_CHANCE.getString().replace("%", "")); double rand = Math.random() * 100; if (rand - ch < 0 || ch == 100) { ItemStack itemNew; if (event.getEntity() instanceof Player) { - if (!SettingsManager.Setting.DROP_PLAYER_HEADS.getBoolean()) return; + if (!Setting.DROP_PLAYER_HEADS.getBoolean()) return; String encodededStr = Methods.getEncodedTexture((Player) event.getEntity()); @@ -56,7 +56,7 @@ public class DeathListeners implements Listener { itemNew = optional.get().asItemStack(); } } else { - if (!SettingsManager.Setting.DROP_MOB_HEADS.getBoolean() || event.getEntity() instanceof ArmorStand) return; + if (!Setting.DROP_MOB_HEADS.getBoolean() || event.getEntity() instanceof ArmorStand) return; Head head = new Head(-1, null, HeadType.valueOf(event.getEntity().getType().name()).getUrl(), null, null, (byte) 0); itemNew = head.asItemStack(); diff --git a/src/main/java/com/songoda/epicheads/utils/Methods.java b/src/main/java/com/songoda/epicheads/utils/Methods.java index 382c529..1252db7 100644 --- a/src/main/java/com/songoda/epicheads/utils/Methods.java +++ b/src/main/java/com/songoda/epicheads/utils/Methods.java @@ -3,6 +3,7 @@ package com.songoda.epicheads.utils; import com.mojang.authlib.GameProfile; import com.mojang.authlib.properties.Property; import com.songoda.epicheads.EpicHeads; +import com.songoda.epicheads.utils.settings.Setting; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -90,20 +91,20 @@ public class Methods { } public static ItemStack createToken(int amount) { - ItemStack itemStack = new ItemStack(Material.valueOf(SettingsManager.Setting.ITEM_TOKEN_TYPE.getString())); + ItemStack itemStack = new ItemStack(Material.valueOf(Setting.ITEM_TOKEN_TYPE.getString())); if (itemStack.getType() == (EpicHeads.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? Material.PLAYER_HEAD : Material.valueOf("SKULL_ITEM"))) { itemStack = EpicHeads.getInstance().getHeadManager().getHeads().stream() - .filter(head -> head.getId() == SettingsManager.Setting.ITEM_TOKEN_ID.getInt()) + .filter(head -> head.getId() == Setting.ITEM_TOKEN_ID.getInt()) .findFirst().get().asItemStack(); } itemStack.setAmount(amount); ItemMeta meta = itemStack.getItemMeta(); - meta.setDisplayName(formatText(SettingsManager.Setting.ITEM_TOKEN_NAME.getString())); + meta.setDisplayName(formatText(Setting.ITEM_TOKEN_NAME.getString())); List lore = new ArrayList<>(); - for (String line : SettingsManager.Setting.ITEM_TOKEN_LORE.getStringList()) + for (String line : Setting.ITEM_TOKEN_LORE.getStringList()) lore.add(formatText(line)); meta.setLore(lore); itemStack.setItemMeta(meta); diff --git a/src/main/java/com/songoda/epicheads/utils/SettingsManager.java b/src/main/java/com/songoda/epicheads/utils/SettingsManager.java deleted file mode 100644 index 3cf1226..0000000 --- a/src/main/java/com/songoda/epicheads/utils/SettingsManager.java +++ /dev/null @@ -1,232 +0,0 @@ -package com.songoda.epicheads.utils; - -import com.songoda.epicheads.EpicHeads; -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; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.AsyncPlayerChatEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -import java.util.*; -import java.util.regex.Pattern; - -/** - * Created by songo on 6/4/2017. - */ -public class SettingsManager implements Listener { - - private static final Pattern SETTINGS_PATTERN = Pattern.compile("(.{1,28}(?:\\s|$))|(.{0,28})", Pattern.DOTALL); - private final EpicHeads instance; - private String pluginName = "EpicHeads"; - private Map cat = new HashMap<>(); - private Map current = new HashMap<>(); - - public SettingsManager(EpicHeads plugin) { - this.instance = plugin; - Bukkit.getPluginManager().registerEvents(this, plugin); - } - - @EventHandler - public void onInventoryClick(InventoryClickEvent event) { - ItemStack clickedItem = event.getCurrentItem(); - - if (event.getInventory() != event.getWhoClicked().getOpenInventory().getTopInventory() - || clickedItem == null || !clickedItem.hasItemMeta() - || !clickedItem.getItemMeta().hasDisplayName()) { - return; - } - - if (event.getView().getTitle().equals(pluginName + " Settings Manager")) { - event.setCancelled(true); - if (clickedItem.getType().name().contains("STAINED_GLASS")) return; - - String type = ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName()); - this.cat.put((Player) event.getWhoClicked(), type); - this.openEditor((Player) event.getWhoClicked()); - } else if (event.getView().getTitle().equals(pluginName + " Settings Editor")) { - event.setCancelled(true); - if (clickedItem.getType().name().contains("STAINED_GLASS")) return; - - Player player = (Player) event.getWhoClicked(); - - String key = cat.get(player) + "." + ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName()); - - if (instance.getConfig().get(key).getClass().getName().equals("java.lang.Boolean")) { - this.instance.getConfig().set(key, !instance.getConfig().getBoolean(key)); - this.finishEditing(player); - } else { - this.editObject(player, key); - } - } - } - - @EventHandler - public void onChat(AsyncPlayerChatEvent event) { - Player player = event.getPlayer(); - if (!current.containsKey(player)) return; - - String value = current.get(player); - FileConfiguration config = instance.getConfig(); - if (config.isInt(value)) { - config.set(value, Integer.parseInt(event.getMessage())); - } else if (config.isDouble(value)) { - config.set(value, Double.parseDouble(event.getMessage())); - } else if (config.isString(value)) { - config.set(value, event.getMessage()); - } - - Bukkit.getScheduler().scheduleSyncDelayedTask(EpicHeads.getInstance(), () -> - this.finishEditing(player), 0L); - - event.setCancelled(true); - } - - private void finishEditing(Player player) { - this.current.remove(player); - this.instance.saveConfig(); - this.openEditor(player); - } - - private void editObject(Player player, String current) { - this.current.put(player, ChatColor.stripColor(current)); - - player.closeInventory(); - player.sendMessage(""); - player.sendMessage(Methods.formatText("&7Please enter a value for &6" + current + "&7.")); - if (instance.getConfig().isInt(current) || instance.getConfig().isDouble(current)) { - player.sendMessage(Methods.formatText("&cUse only numbers.")); - } - player.sendMessage(""); - } - - public void openSettingsManager(Player player) { - Inventory inventory = Bukkit.createInventory(null, 27, pluginName + " Settings Manager"); - ItemStack glass = Methods.getGlass(); - for (int i = 0; i < inventory.getSize(); i++) { - inventory.setItem(i, glass); - } - - int slot = 10; - for (String key : instance.getConfig().getDefaultSection().getKeys(false)) { - ItemStack item = new ItemStack(instance.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.WHITE_WOOL : Material.valueOf("WOOL"), 1, (byte) (slot - 9)); //ToDo: Make this function as it was meant to. - ItemMeta meta = item.getItemMeta(); - meta.setLore(Collections.singletonList(Methods.formatText("&6Click To Edit This Category."))); - meta.setDisplayName(Methods.formatText("&f&l" + key)); - item.setItemMeta(meta); - inventory.setItem(slot, item); - slot++; - } - - player.openInventory(inventory); - } - - private void openEditor(Player player) { - Inventory inventory = Bukkit.createInventory(null, 54, pluginName + " Settings Editor"); - FileConfiguration config = instance.getConfig(); - - int slot = 0; - for (String key : config.getConfigurationSection(cat.get(player)).getKeys(true)) { - String fKey = cat.get(player) + "." + key; - ItemStack item = new ItemStack(Material.DIAMOND_HELMET); - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(Methods.formatText("&6" + key)); - - List lore = new ArrayList<>(); - if (config.isBoolean(fKey)) { - item.setType(Material.LEVER); - lore.add(Methods.formatText(config.getBoolean(fKey) ? "&atrue" : "&cfalse")); - } else if (config.isString(fKey)) { - item.setType(Material.PAPER); - lore.add(Methods.formatText("&9" + config.getString(fKey))); - } else if (config.isInt(fKey)) { - item.setType(instance.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); - lore.add(Methods.formatText("&5" + config.getInt(fKey))); - } - - meta.setLore(lore); - item.setItemMeta(meta); - - inventory.setItem(slot, item); - slot++; - } - - player.openInventory(inventory); - } - - public void updateSettings() { - FileConfiguration config = instance.getConfig(); - - for (Setting setting : Setting.values()) { - config.addDefault(setting.setting, setting.option); - } - } - - public enum Setting { - - AUTOSAVE("Main.Auto Save Interval In Seconds", 15), - DISCORD("Main.Show Discord Button", true), - FREE_IN_CREATIVE("Main.Heads Free In Creative Mode", false), - - DROP_MOB_HEADS("Main.Drop Mob Heads", true), - DROP_PLAYER_HEADS("Main.Drop Player Heads", true), - DROP_CHANCE("Main.Head Drop Chance", "25%"), - - DISABLED_HEADS("Main.Disabled Global Heads", Arrays.asList(34567, 34568, 34569)), - - GLASS_TYPE_1("Interfaces.Glass Type 1", 7), - GLASS_TYPE_2("Interfaces.Glass Type 2", 11), - GLASS_TYPE_3("Interfaces.Glass Type 3", 3), - - HEAD_COST("Economy.Head Cost", 24.99), - VAULT_ECONOMY("Economy.Use Vault Economy", true), - PLAYER_POINTS_ECONOMY("Economy.Use Player Points Economy", false), - ITEM_ECONOMY("Economy.Use Item Economy", false), - ITEM_TOKEN_TYPE("Economy.Item.Type", EpicHeads.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? "PLAYER_HEAD" : "SKULL_ITEM"), - ITEM_TOKEN_ID("Economy.Item.Head ID", 14395), - ITEM_TOKEN_NAME("Economy.Item.Name", "&6Player Head Token"), - ITEM_TOKEN_LORE("Economy.Item.Lore", Arrays.asList("&8Use in /Heads!")), - - LANGUGE_MODE("System.Language Mode", "en_US"); - - private String setting; - private Object option; - - Setting(String setting, Object option) { - this.setting = setting; - this.option = option; - } - - public List getStringList() { - return EpicHeads.getInstance().getConfig().getStringList(setting); - } - - public List getIntegerList() { - return EpicHeads.getInstance().getConfig().getIntegerList(setting); - } - - public boolean getBoolean() { - return EpicHeads.getInstance().getConfig().getBoolean(setting); - } - - public int getInt() { - return EpicHeads.getInstance().getConfig().getInt(setting); - } - - public String getString() { - return EpicHeads.getInstance().getConfig().getString(setting); - } - - public char getChar() { return EpicHeads.getInstance().getConfig().getString(setting).charAt(0); } - - public double getDouble() { - return EpicHeads.getInstance().getConfig().getDouble(setting); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epicheads/utils/settings/Category.java b/src/main/java/com/songoda/epicheads/utils/settings/Category.java new file mode 100644 index 0000000..d72cfdf --- /dev/null +++ b/src/main/java/com/songoda/epicheads/utils/settings/Category.java @@ -0,0 +1,29 @@ +package com.songoda.epicheads.utils.settings; + +public enum Category { + + MAIN("General settings and options."), + + INTERFACES("These settings allow you to alter the way interfaces look.", + "They are used in GUI's to make paterns, change them up then open up a", + "GUI to see how it works."), + + ECONOMY("Settings regarding economy.", + "Only one economy option can be used at a time. If you enable more than", + "one of these the first one will be used."), + + ECONOMY_ITEM("Item token options."), + + SYSTEM("System related settings."); + + private String[] comments; + + + Category(String... comments) { + this.comments = comments; + } + + public String[] getComments() { + return comments; + } +} \ No newline at end of file diff --git a/src/main/java/com/songoda/epicheads/utils/settings/Setting.java b/src/main/java/com/songoda/epicheads/utils/settings/Setting.java new file mode 100644 index 0000000..40fd033 --- /dev/null +++ b/src/main/java/com/songoda/epicheads/utils/settings/Setting.java @@ -0,0 +1,143 @@ +package com.songoda.epicheads.utils.settings; + +import com.songoda.epicheads.EpicHeads; +import com.songoda.epicheads.utils.ServerVersion; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public enum Setting { + + AUTOSAVE("Main.Auto Save Interval In Seconds", 15, + "The amount of time in between saving to file.", + "This is purely a safety function to prevent against unplanned crashes or", + "restarts. With that said it is advised to keep this enabled.", + "If however you enjoy living on the edge, feel free to turn it off."), + + DISCORD("Main.Show Discord Button", true, + "This is the discord button displayed in the main GUI", + "Clicking this button will bring you to a discord where you can", + "add or remove heads to the global library this plugin uses.", + "AS well as get updates on future releases and features."), + + FREE_IN_CREATIVE("Main.Heads Free In Creative Mode", false, + "Enabling this will make it so that a player can get all heads", + "for free as long as they are in the creative game mode."), + + DROP_MOB_HEADS("Main.Drop Mob Heads", true, + "Should heads drop after a monster is killed?"), + + DROP_PLAYER_HEADS("Main.Drop Player Heads", true, + "Should a players drop their head on death?"), + + DROP_CHANCE("Main.Head Drop Chance", "25%", + "When a player or monster is killed what should be", + "the chance that their head drops?"), + + DISABLED_HEADS("Main.Disabled Global Heads", Arrays.asList(34567, 34568, 34569), + "These are head ID's from the global database that are disabled.", + "By default this is filled with non existent ID's."), + + GLASS_TYPE_1("Interfaces.Glass Type 1", 7), + GLASS_TYPE_2("Interfaces.Glass Type 2", 11), + GLASS_TYPE_3("Interfaces.Glass Type 3", 3), + + HEAD_COST("Economy.Head Cost", 24.99, + "The cost the of the head. If you wan't to use PlayerPoints", + "or item tokens you need to use whole numbers."), + + VAULT_ECONOMY("Economy.Use Vault Economy", true, + "Should Vault be used?"), + + PLAYER_POINTS_ECONOMY("Economy.Use Player Points Economy", false, + "Should PlayerPoints be used?"), + + ITEM_ECONOMY("Economy.Use Item Economy", false, + "Should item tokens be used?"), + + ITEM_TOKEN_TYPE("Economy.Item.Type", EpicHeads.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) ? "PLAYER_HEAD" : "SKULL_ITEM", + "Which item material type should be used?", + "You can use any of the materials from the following link:", + "https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html"), + + ITEM_TOKEN_ID("Economy.Item.Head ID", 14395, + "If a player head is used as the token which head ID should be used?", + "This can be any head from the global database."), + + ITEM_TOKEN_NAME("Economy.Item.Name", "&6Player Head Token", + "What should the token be named?"), + + ITEM_TOKEN_LORE("Economy.Item.Lore", Arrays.asList("&8Use in /Heads!"), + "What should the tokens lore be?"), + + LANGUGE_MODE("System.Language Mode", "en_US", + "The enabled language file.", + "More language files (if available) can be found in the plugins data folder."); + + private String setting; + private Object option; + private String[] comments; + + Setting(String setting, Object option, String... comments) { + this.setting = setting; + this.option = option; + this.comments = comments; + } + + Setting(String setting, Object option) { + this.setting = setting; + this.option = option; + this.comments = null; + } + + public static Setting getSetting(String setting) { + List settings = Arrays.stream(values()).filter(setting1 -> setting1.setting.equals(setting)).collect(Collectors.toList()); + if (settings.isEmpty()) return null; + return settings.get(0); + } + + public String getSetting() { + return setting; + } + + public Object getOption() { + return option; + } + + public String[] getComments() { + return comments; + } + + public List getIntegerList() { + return EpicHeads.getInstance().getConfig().getIntegerList(setting); + } + + public List getStringList() { + return EpicHeads.getInstance().getConfig().getStringList(setting); + } + + public boolean getBoolean() { + return EpicHeads.getInstance().getConfig().getBoolean(setting); + } + + public int getInt() { + return EpicHeads.getInstance().getConfig().getInt(setting); + } + + public long getLong() { + return EpicHeads.getInstance().getConfig().getLong(setting); + } + + public String getString() { + return EpicHeads.getInstance().getConfig().getString(setting); + } + + public char getChar() { + return EpicHeads.getInstance().getConfig().getString(setting).charAt(0); + } + + public double getDouble() { + return EpicHeads.getInstance().getConfig().getDouble(setting); + } +} \ No newline at end of file diff --git a/src/main/java/com/songoda/epicheads/utils/settings/SettingsManager.java b/src/main/java/com/songoda/epicheads/utils/settings/SettingsManager.java new file mode 100644 index 0000000..663b7e3 --- /dev/null +++ b/src/main/java/com/songoda/epicheads/utils/settings/SettingsManager.java @@ -0,0 +1,326 @@ +package com.songoda.epicheads.utils.settings; + +import com.songoda.epicheads.EpicHeads; +import com.songoda.epicheads.utils.Methods; +import com.songoda.epicheads.utils.ServerVersion; +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; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.io.*; +import java.util.*; + +/** + * Created by songoda on 6/4/2017. + */ +public class SettingsManager implements Listener { + + private final EpicHeads plugin; + private Map cat = new HashMap<>(); + private Map current = new HashMap<>(); + + public SettingsManager(EpicHeads plugin) { + this.plugin = plugin; + Bukkit.getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent event) { + ItemStack clickedItem = event.getCurrentItem(); + + if (event.getInventory() != event.getWhoClicked().getOpenInventory().getTopInventory() + || clickedItem == null || !clickedItem.hasItemMeta() + || !clickedItem.getItemMeta().hasDisplayName()) { + return; + } + + if (event.getView().getTitle().equals(plugin.getName() + " Settings Manager")) { + event.setCancelled(true); + if (clickedItem.getType().name().contains("STAINED_GLASS")) return; + + String type = ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName()); + this.cat.put((Player) event.getWhoClicked(), type); + this.openEditor((Player) event.getWhoClicked()); + } else if (event.getView().getTitle().equals(plugin.getName() + " Settings Editor")) { + event.setCancelled(true); + if (clickedItem.getType().name().contains("STAINED_GLASS")) return; + + Player player = (Player) event.getWhoClicked(); + + String key = cat.get(player) + "." + ChatColor.stripColor(clickedItem.getItemMeta().getDisplayName()); + + if (plugin.getConfig().get(key).getClass().getName().equals("java.lang.Boolean")) { + this.plugin.getConfig().set(key, !plugin.getConfig().getBoolean(key)); + this.finishEditing(player); + } else { + this.editObject(player, key); + } + } + } + + @EventHandler + public void onChat(AsyncPlayerChatEvent event) { + Player player = event.getPlayer(); + if (!current.containsKey(player)) return; + + String value = current.get(player); + FileConfiguration config = plugin.getConfig(); + if (config.isLong(value)) { + config.set(value, Long.parseLong(event.getMessage())); + } else if (config.isInt(value)) { + config.set(value, Integer.parseInt(event.getMessage())); + } else if (config.isDouble(value)) { + config.set(value, Double.parseDouble(event.getMessage())); + } else if (config.isString(value)) { + config.set(value, event.getMessage()); + } + + Bukkit.getScheduler().scheduleSyncDelayedTask(EpicHeads.getInstance(), () -> + this.finishEditing(player), 0L); + + event.setCancelled(true); + } + + private void finishEditing(Player player) { + this.current.remove(player); + this.saveConfig(); + this.openEditor(player); + } + + private void editObject(Player player, String current) { + this.current.put(player, ChatColor.stripColor(current)); + + player.closeInventory(); + player.sendMessage(""); + player.sendMessage(Methods.formatText("&7Please enter a value for &6" + current + "&7.")); + if (plugin.getConfig().isInt(current) || plugin.getConfig().isDouble(current)) { + player.sendMessage(Methods.formatText("&cUse only numbers.")); + } + player.sendMessage(""); + } + + public void openSettingsManager(Player player) { + Inventory inventory = Bukkit.createInventory(null, 27, plugin.getName() + " Settings Manager"); + ItemStack glass = Methods.getGlass(); + for (int i = 0; i < inventory.getSize(); i++) { + inventory.setItem(i, glass); + } + + int slot = 10; + for (String key : plugin.getConfig().getDefaultSection().getKeys(false)) { + ItemStack item = new ItemStack(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.LEGACY_WOOL : Material.valueOf("WOOL"), 1, (byte) (slot - 9)); + ItemMeta meta = item.getItemMeta(); + meta.setLore(Collections.singletonList(Methods.formatText("&6Click To Edit This Category."))); + meta.setDisplayName(Methods.formatText("&f&l" + key)); + item.setItemMeta(meta); + inventory.setItem(slot, item); + slot++; + } + + player.openInventory(inventory); + } + + private void openEditor(Player player) { + Inventory inventory = Bukkit.createInventory(null, 54, plugin.getName() + " Settings Editor"); + FileConfiguration config = plugin.getConfig(); + + int slot = 0; + for (String key : config.getConfigurationSection(cat.get(player)).getKeys(true)) { + String fKey = cat.get(player) + "." + key; + ItemStack item = new ItemStack(Material.DIAMOND_HELMET); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(Methods.formatText("&6" + key)); + + List lore = new ArrayList<>(); + if (config.isBoolean(fKey)) { + item.setType(Material.LEVER); + lore.add(Methods.formatText(config.getBoolean(fKey) ? "&atrue" : "&cfalse")); + } else if (config.isString(fKey)) { + item.setType(Material.PAPER); + lore.add(Methods.formatText("&7" + config.getString(fKey))); + } else if (config.isInt(fKey)) { + item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); + lore.add(Methods.formatText("&7" + config.getInt(fKey))); + } else if (config.isLong(fKey)) { + item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); + lore.add(Methods.formatText("&7" + config.getLong(fKey))); + } else if (config.isDouble(fKey)) { + item.setType(plugin.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); + lore.add(Methods.formatText("&7" + config.getDouble(fKey))); + } + + Setting setting = Setting.getSetting(fKey); + + if (setting != null && setting.getComments() != null) { + lore.add(""); + + String comment = String.join(" ", setting.getComments()); + + int lastIndex = 0; + for (int n = 0; n < comment.length(); n++) { + if (n - lastIndex < 30) + continue; + + if (comment.charAt(n) == ' ') { + lore.add(Methods.formatText("&8" + comment.substring(lastIndex, n).trim())); + lastIndex = n; + } + } + + if (lastIndex - comment.length() < 30) + lore.add(Methods.formatText("&8" + comment.substring(lastIndex).trim())); + + } + + meta.setLore(lore); + item.setItemMeta(meta); + + inventory.setItem(slot, item); + slot++; + } + + player.openInventory(inventory); + } + + public void reloadConfig() { + plugin.reloadConfig(); + this.setupConfig(); + } + + public void setupConfig() { + FileConfiguration config = plugin.getConfig(); + + for (Setting setting : Setting.values()) { + config.addDefault(setting.getSetting(), setting.getOption()); + } + plugin.getConfig().options().copyDefaults(true); + saveConfig(); + } + + private void saveConfig() { + String dump = plugin.getConfig().saveToString(); + + StringBuilder config = new StringBuilder(); + + BufferedReader bufReader = new BufferedReader(new StringReader(dump)); + + try { + boolean first = true; + + String line; + int currentTab = 0; + String category = ""; + + while ((line = bufReader.readLine()) != null) { + if (line.trim().startsWith("#")) continue; + + int tabChange = line.length() - line.trim().length(); + if (currentTab != tabChange) { + category = category.contains(".") && tabChange != 0 ? category.substring(0, category.indexOf(".")) : ""; + currentTab = tabChange; + } + + if (line.endsWith(":")) { + bufReader.mark(1000); + String found = bufReader.readLine(); + bufReader.reset(); + + if (!found.trim().startsWith("-")) { + + String newCategory = line.substring(0, line.length() - 1).trim(); + + if (category.equals("")) + category = newCategory; + else + category += "." + newCategory; + + currentTab = tabChange + 2; + + if (!first) { + config.append("\n\n"); + } else { + first = false; + } + + if (!category.contains(".")) + config.append("#").append("\n"); + try { + Category categoryObj = Category.valueOf(category.toUpperCase() + .replace(" ", "_") + .replace(".", "_")); + + config.append(new String(new char[tabChange]).replace('\0', ' ')); + for (String l : categoryObj.getComments()) + config.append("# ").append(l).append("\n"); + } catch (IllegalArgumentException e) { + config.append("# ").append(category).append("\n"); + } + if (!category.contains(".")) + config.append("#").append("\n"); + + config.append(line).append("\n"); + + continue; + } + } + + if (line.trim().startsWith("-")) { + config.append(line).append("\n"); + continue; + } + + String key = category + "." + (line.split(":")[0].trim()); + for (Setting setting : Setting.values()) { + if (!setting.getSetting().equals(key) || setting.getComments() == null) continue; + config.append(" ").append("\n"); + for (String l : setting.getComments()) { + config.append(new String(new char[currentTab]).replace('\0', ' ')); + config.append("# ").append(l).append("\n"); + } + } + config.append(line).append("\n"); + } + } catch (IOException e) { + e.printStackTrace(); + } + + try { + BufferedWriter writer = + new BufferedWriter(new FileWriter(new File(plugin.getDataFolder() + "\\config.yml"))); + writer.write(config.toString()); + writer.flush(); + writer.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void add(LinkedHashMap map, int index, K key, V value) { + assert (map != null); + assert !map.containsKey(key); + assert (index >= 0) && (index < map.size()); + + int i = 0; + List> rest = new ArrayList<>(); + for (Map.Entry entry : map.entrySet()) { + if (i++ >= index) { + rest.add(entry); + } + } + map.put(key, value); + for (Map.Entry entry : rest) { + map.remove(entry.getKey()); + map.put(entry.getKey(), entry.getValue()); + } + } +} \ No newline at end of file