From 5e30669f2c73125ca74ccfe0affc34fda7a4dc07 Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 15 Jan 2020 17:58:15 -0500 Subject: [PATCH 1/2] Updated to support the Songoda core settings manager. --- .../songoda/epicenchants/EpicEnchants.java | 63 ++-- .../command/commands/CommandReload.java | 2 +- .../command/commands/CommandSettings.java | 3 +- .../epicenchants/managers/FileManager.java | 11 +- .../epicenchants/objects/BookItem.java | 6 +- .../epicenchants/utils/EnchantUtils.java | 6 +- .../epicenchants/utils/SpecialItems.java | 4 +- .../epicenchants/utils/settings/Category.java | 23 -- .../epicenchants/utils/settings/Setting.java | 113 ------- .../epicenchants/utils/settings/Settings.java | 52 +++ .../utils/settings/SettingsManager.java | 311 ------------------ 11 files changed, 88 insertions(+), 506 deletions(-) delete mode 100644 src/main/java/com/songoda/epicenchants/utils/settings/Category.java delete mode 100644 src/main/java/com/songoda/epicenchants/utils/settings/Setting.java create mode 100644 src/main/java/com/songoda/epicenchants/utils/settings/Settings.java delete mode 100644 src/main/java/com/songoda/epicenchants/utils/settings/SettingsManager.java diff --git a/src/main/java/com/songoda/epicenchants/EpicEnchants.java b/src/main/java/com/songoda/epicenchants/EpicEnchants.java index ad8b82e..713660a 100644 --- a/src/main/java/com/songoda/epicenchants/EpicEnchants.java +++ b/src/main/java/com/songoda/epicenchants/EpicEnchants.java @@ -4,6 +4,7 @@ import com.songoda.core.SongodaCore; import com.songoda.core.SongodaPlugin; import com.songoda.core.compatibility.CompatibleMaterial; import com.songoda.core.configuration.Config; +import com.songoda.core.gui.GuiManager; import com.songoda.core.hooks.EconomyManager; import com.songoda.epicenchants.command.CommandManager; import com.songoda.epicenchants.listeners.ArmorListener; @@ -19,8 +20,7 @@ import com.songoda.epicenchants.utils.EnchantUtils; import com.songoda.epicenchants.utils.Metrics; import com.songoda.epicenchants.utils.SpecialItems; import com.songoda.epicenchants.utils.objects.FastInv; -import com.songoda.epicenchants.utils.settings.Setting; -import com.songoda.epicenchants.utils.settings.SettingsManager; +import com.songoda.epicenchants.utils.settings.Settings; import com.songoda.epicenchants.utils.single.ItemGroup; import org.bukkit.Bukkit; import org.bukkit.plugin.PluginManager; @@ -28,19 +28,16 @@ import org.bukkit.plugin.PluginManager; import java.util.List; import java.util.stream.Collectors; -import static com.songoda.epicenchants.utils.single.GeneralUtils.color; -import static org.bukkit.Bukkit.getConsoleSender; - public class EpicEnchants extends SongodaPlugin { private static EpicEnchants INSTANCE; + private final GuiManager guiManager = new GuiManager(this); private EnchantManager enchantManager; private InfoManager infoManager; private GroupManager groupManager; private FileManager fileManager; private HookManager hookManager; - private SettingsManager settingsManager; private CommandManager commandManager; private SpecialItems specialItems; @@ -61,12 +58,13 @@ public class EpicEnchants extends SongodaPlugin { // Run Songoda Updater SongodaCore.registerPlugin(this, 67, CompatibleMaterial.DIAMOND_SWORD); - // Setup Setting Manager - this.settingsManager = new SettingsManager(this); - this.settingsManager.setupConfig(); + EconomyManager.load(); - // Setup Language - this.setLocale(getConfig().getString("System.Language Mode"), false); + // Setup Config + Settings.setupConfig(); + this.setLocale(Settings.LANGUGE_MODE.getString(), false); + + EconomyManager.getManager().setPreferredHook(Settings.ECONOMY_PLUGIN.getString()); preload(); @@ -84,9 +82,9 @@ public class EpicEnchants extends SongodaPlugin { infoManager.loadMenus(); hookManager.setup(); - PluginManager pluginManager = Bukkit.getPluginManager(); - // Listeners + guiManager.init(); + PluginManager pluginManager = Bukkit.getPluginManager(); pluginManager.registerEvents(new BookListener(this), this); pluginManager.registerEvents(new ArmorListener(), this); pluginManager.registerEvents(new PlayerListener(this), this); @@ -95,20 +93,6 @@ public class EpicEnchants extends SongodaPlugin { pluginManager.registerEvents(new BlackScrollListener(this), this); pluginManager.registerEvents(new DustListener(this), this); - String economyPlugin = null; - - // Setup Economy - if (Setting.VAULT_ECONOMY.getBoolean() && pluginManager.isPluginEnabled("Vault")) - economyPlugin = "Vault"; - else if (Setting.RESERVE_ECONOMY.getBoolean() && pluginManager.isPluginEnabled("Reserve")) - economyPlugin = "Reserve"; - else if (Setting.PLAYER_POINTS_ECONOMY.getBoolean() && pluginManager.isPluginEnabled("PlayerPoints")) - economyPlugin = "PlayerPoints"; - - EconomyManager.load(); - if (economyPlugin != null) - EconomyManager.getManager().setPreferredHook(economyPlugin); - // Start Metrics new Metrics(this); @@ -125,25 +109,10 @@ public class EpicEnchants extends SongodaPlugin { @Override public void onPluginDisable() { - getConsoleSender().sendMessage(color("&a=============================")); - getConsoleSender().sendMessage(color("&7" + getDescription().getName() + " " + getDescription().getVersion() + " by &5Songoda <3&7!")); - getConsoleSender().sendMessage(color("&7Action: &cDisabling&7...")); - getConsoleSender().sendMessage(color("&a=============================")); } @Override public void onConfigReload() { - - } - - @Override - public List getExtraConfig() { - return null; - } - - public void reload() { - reloadConfig(); - fileManager.clear(); fileManager.loadFiles(); @@ -160,6 +129,12 @@ public class EpicEnchants extends SongodaPlugin { this.locale.reloadMessages(); } + @Override + public List getExtraConfig() { + return null; + } + + public EnchantManager getEnchantManager() { return this.enchantManager; } @@ -196,7 +171,7 @@ public class EpicEnchants extends SongodaPlugin { return commandManager; } - public SettingsManager getSettingsManager() { - return settingsManager; + public GuiManager getGuiManager() { + return guiManager; } } diff --git a/src/main/java/com/songoda/epicenchants/command/commands/CommandReload.java b/src/main/java/com/songoda/epicenchants/command/commands/CommandReload.java index 4b7e1f8..1c76533 100644 --- a/src/main/java/com/songoda/epicenchants/command/commands/CommandReload.java +++ b/src/main/java/com/songoda/epicenchants/command/commands/CommandReload.java @@ -14,7 +14,7 @@ public class CommandReload extends AbstractCommand { @Override protected AbstractCommand.ReturnType runCommand(EpicEnchants instance, CommandSender sender, String... args) { - instance.reload(); + instance.reloadConfig(); instance.getLocale().getMessage("command.reload").sendPrefixedMessage(sender); return ReturnType.SUCCESS; } diff --git a/src/main/java/com/songoda/epicenchants/command/commands/CommandSettings.java b/src/main/java/com/songoda/epicenchants/command/commands/CommandSettings.java index b09664b..de0cd3c 100644 --- a/src/main/java/com/songoda/epicenchants/command/commands/CommandSettings.java +++ b/src/main/java/com/songoda/epicenchants/command/commands/CommandSettings.java @@ -1,5 +1,6 @@ package com.songoda.epicenchants.command.commands; +import com.songoda.core.configuration.editor.PluginConfigGui; import com.songoda.epicenchants.EpicEnchants; import com.songoda.epicenchants.command.AbstractCommand; import org.bukkit.command.CommandSender; @@ -15,7 +16,7 @@ public class CommandSettings extends AbstractCommand { @Override protected ReturnType runCommand(EpicEnchants instance, CommandSender sender, String... args) { - instance.getSettingsManager().openSettingsManager((Player) sender); + instance.getGuiManager().showGUI((Player) sender, new PluginConfigGui(instance)); return ReturnType.SUCCESS; } diff --git a/src/main/java/com/songoda/epicenchants/managers/FileManager.java b/src/main/java/com/songoda/epicenchants/managers/FileManager.java index 414d972..aaac24a 100644 --- a/src/main/java/com/songoda/epicenchants/managers/FileManager.java +++ b/src/main/java/com/songoda/epicenchants/managers/FileManager.java @@ -3,13 +3,16 @@ package com.songoda.epicenchants.managers; import com.songoda.core.compatibility.ServerVersion; import com.songoda.epicenchants.EpicEnchants; import com.songoda.epicenchants.utils.objects.FileLocation; -import com.songoda.epicenchants.utils.settings.Setting; +import com.songoda.epicenchants.utils.settings.Settings; import org.bukkit.Bukkit; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.file.Files; import java.util.*; @@ -89,8 +92,7 @@ public class FileManager extends Manager { files.forEach(fileLocation -> { File file = new File(instance.getDataFolder() + separator + fileLocation.getPath()); - if (!file.exists() && (fileLocation.isRequired() || Setting.FIRST_LOAD.getBoolean())) { - file.getParentFile().mkdirs(); + if (!file.exists() && (fileLocation.isRequired() || Settings.FIRST_LOAD.getBoolean())) { Bukkit.getConsoleSender().sendMessage("Creating file: " + fileLocation.getPath()); try { @@ -114,7 +116,6 @@ public class FileManager extends Manager { instance.getConfig().set("System.First Load", false); instance.saveConfig(); - instance.getSettingsManager().reloadConfig(); } public FileConfiguration getConfiguration(String key) { diff --git a/src/main/java/com/songoda/epicenchants/objects/BookItem.java b/src/main/java/com/songoda/epicenchants/objects/BookItem.java index f5d2001..d3583c1 100644 --- a/src/main/java/com/songoda/epicenchants/objects/BookItem.java +++ b/src/main/java/com/songoda/epicenchants/objects/BookItem.java @@ -3,13 +3,12 @@ package com.songoda.epicenchants.objects; import com.songoda.epicenchants.EpicEnchants; import com.songoda.epicenchants.utils.itemnbtapi.NBTItem; import com.songoda.epicenchants.utils.objects.ItemBuilder; -import com.songoda.epicenchants.utils.settings.Setting; +import com.songoda.epicenchants.utils.settings.Settings; import com.songoda.epicenchants.utils.single.GeneralUtils; import com.songoda.epicenchants.utils.single.ItemGroup; import com.songoda.epicenchants.utils.single.RomanNumber; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; @@ -74,7 +73,7 @@ public class BookItem { ItemBuilder itemBuilder = new ItemBuilder(material) .name(color(displayName - .replace("{level}", "" + (Setting.ROMAN.getBoolean() ? RomanNumber.toRoman(level) : level)) + .replace("{level}", "" + (Settings.ROMAN.getBoolean() ? RomanNumber.toRoman(level) : level)) .replace("{enchant}", "" + enchant.getIdentifier()) .replace("{group_color}", enchant.getGroup().getColor()) .replace("{group_name}", enchant.getGroup().getName()) @@ -128,4 +127,5 @@ public class BookItem { return "BookItem.BookItemBuilder(instance=" + this.instance + ", material=" + this.material + ", displayName=" + this.displayName + ", lore=" + this.lore + ")"; } } + } diff --git a/src/main/java/com/songoda/epicenchants/utils/EnchantUtils.java b/src/main/java/com/songoda/epicenchants/utils/EnchantUtils.java index 26f4386..23d1584 100644 --- a/src/main/java/com/songoda/epicenchants/utils/EnchantUtils.java +++ b/src/main/java/com/songoda/epicenchants/utils/EnchantUtils.java @@ -9,7 +9,7 @@ import com.songoda.epicenchants.objects.Enchant; import com.songoda.epicenchants.utils.itemnbtapi.NBTCompound; import com.songoda.epicenchants.utils.itemnbtapi.NBTItem; import com.songoda.epicenchants.utils.objects.ItemBuilder; -import com.songoda.epicenchants.utils.settings.Setting; +import com.songoda.epicenchants.utils.settings.Settings; import com.songoda.epicenchants.utils.single.GeneralUtils; import org.bukkit.Material; import org.bukkit.entity.LivingEntity; @@ -70,8 +70,8 @@ public class EnchantUtils { itemBuilder.removeLore(instance.getSpecialItems().getWhiteScrollLore()); } - itemBuilder.removeLore(enchant.getFormat(-1, Setting.ROMAN.getBoolean()).replace("-1", "").trim()); - itemBuilder.addLore(enchant.getFormat(level, Setting.ROMAN.getBoolean())); + itemBuilder.removeLore(enchant.getFormat(-1, Settings.ROMAN.getBoolean()).replace("-1", "").trim()); + itemBuilder.addLore(enchant.getFormat(level, Settings.ROMAN.getBoolean())); if (hasProtection) { itemBuilder.addLore(instance.getSpecialItems().getWhiteScrollLore()); diff --git a/src/main/java/com/songoda/epicenchants/utils/SpecialItems.java b/src/main/java/com/songoda/epicenchants/utils/SpecialItems.java index e5f165c..1e62a32 100644 --- a/src/main/java/com/songoda/epicenchants/utils/SpecialItems.java +++ b/src/main/java/com/songoda/epicenchants/utils/SpecialItems.java @@ -4,7 +4,7 @@ import com.songoda.epicenchants.EpicEnchants; import com.songoda.epicenchants.objects.Group; import com.songoda.epicenchants.utils.itemnbtapi.NBTItem; import com.songoda.epicenchants.utils.objects.ItemBuilder; -import com.songoda.epicenchants.utils.settings.Setting; +import com.songoda.epicenchants.utils.settings.Settings; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.inventory.ItemStack; @@ -33,7 +33,7 @@ public class SpecialItems { } public ItemStack getBlackScroll(int amount, int chance) { - int successRate = chance == -1 ? ThreadLocalRandom.current().nextInt(Setting.BLACK_MIN.getInt(), Setting.BLACK_MAX.getInt() + 1) : chance; + int successRate = chance == -1 ? ThreadLocalRandom.current().nextInt(Settings.BLACK_MIN.getInt(), Settings.BLACK_MAX.getInt() + 1) : chance; NBTItem nbtItem = new ItemBuilder(instance.getFileManager().getConfiguration("items/special-items").getConfigurationSection("black-scroll"), of("success-rate", successRate)).nbt(); nbtItem.setBoolean("black-scroll", true); diff --git a/src/main/java/com/songoda/epicenchants/utils/settings/Category.java b/src/main/java/com/songoda/epicenchants/utils/settings/Category.java deleted file mode 100644 index d3a07cd..0000000 --- a/src/main/java/com/songoda/epicenchants/utils/settings/Category.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.songoda.epicenchants.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 patterns, change them up then open up a", - "GUI to see how it works."), - - 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/epicenchants/utils/settings/Setting.java b/src/main/java/com/songoda/epicenchants/utils/settings/Setting.java deleted file mode 100644 index 83d06dd..0000000 --- a/src/main/java/com/songoda/epicenchants/utils/settings/Setting.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.songoda.epicenchants.utils.settings; - - -import com.songoda.epicenchants.EpicEnchants; -import org.bukkit.Material; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -public enum Setting { - - ROMAN("Main.Roman Numerals", true), - - BLACK_MIN("Main.Black Scroll Min", 20), - BLACK_MAX("Main.Black Scroll Max", 100), - - VAULT_ECONOMY("Economy.Use Vault Economy", true, - "Should Vault be used?"), - - RESERVE_ECONOMY("Economy.Use Reserve Economy", true, - "Should Reserve be used?"), - - PLAYER_POINTS_ECONOMY("Economy.Use Player Points Economy", false, - "Should PlayerPoints be used?"), - - GLASS_TYPE_1("Interfaces.Glass Type 1", 7), - GLASS_TYPE_2("Interfaces.Glass Type 2", 11), - GLASS_TYPE_3("Interfaces.Glass Type 3", 3), - - FIRST_LOAD("System.First Load", true), - - 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 EpicEnchants.getInstance().getConfig().getIntegerList(setting); - } - - public List getStringList() { - return EpicEnchants.getInstance().getConfig().getStringList(setting); - } - - public boolean getBoolean() { - return EpicEnchants.getInstance().getConfig().getBoolean(setting); - } - - public int getInt() { - return EpicEnchants.getInstance().getConfig().getInt(setting); - } - - public long getLong() { - return EpicEnchants.getInstance().getConfig().getLong(setting); - } - - public String getString() { - return EpicEnchants.getInstance().getConfig().getString(setting); - } - - public char getChar() { - return EpicEnchants.getInstance().getConfig().getString(setting).charAt(0); - } - - public double getDouble() { - return EpicEnchants.getInstance().getConfig().getDouble(setting); - } - - public Material getMaterial() { - String materialStr = EpicEnchants.getInstance().getConfig().getString(setting); - Material material = Material.getMaterial(materialStr); - - if (material == null) { - System.out.println(String.format("Config value \"%s\" has an invalid material name: \"%s\"", setting, materialStr)); - } - - return material; - } -} \ No newline at end of file diff --git a/src/main/java/com/songoda/epicenchants/utils/settings/Settings.java b/src/main/java/com/songoda/epicenchants/utils/settings/Settings.java new file mode 100644 index 0000000..9771876 --- /dev/null +++ b/src/main/java/com/songoda/epicenchants/utils/settings/Settings.java @@ -0,0 +1,52 @@ +package com.songoda.epicenchants.utils.settings; + +import com.songoda.core.configuration.Config; +import com.songoda.core.configuration.ConfigSetting; +import com.songoda.core.hooks.EconomyManager; +import com.songoda.epicenchants.EpicEnchants; + +import java.util.stream.Collectors; + +public class Settings { + + static final Config config = EpicEnchants.getInstance().getCoreConfig(); + + public static final ConfigSetting ROMAN = new ConfigSetting(config, "Main.Roman Numerals", true); + + public static final ConfigSetting BLACK_MIN = new ConfigSetting(config, "Main.Black Scroll Min", 20); + public static final ConfigSetting BLACK_MAX = new ConfigSetting(config, "Main.Black Scroll Max", 100); + + public static final ConfigSetting ECONOMY_PLUGIN = new ConfigSetting(config, "Main.Economy", EconomyManager.getEconomy() == null ? "Vault" : EconomyManager.getEconomy().getName(), + "Which economy plugin should be used?", + "Supported plugins you have installed: \"" + EconomyManager.getManager().getRegisteredPlugins().stream().collect(Collectors.joining("\", \"")) + "\"."); + + public static final ConfigSetting GLASS_TYPE_1 = new ConfigSetting(config, "Interfaces.Glass Type 1", 7); + public static final ConfigSetting GLASS_TYPE_2 = new ConfigSetting(config, "Interfaces.Glass Type 2", 11); + public static final ConfigSetting GLASS_TYPE_3 = new ConfigSetting(config, "Interfaces.Glass Type 3", 3); + + public static final ConfigSetting FIRST_LOAD = new ConfigSetting(config, "System.First Load", true); + + public static final ConfigSetting LANGUGE_MODE = new ConfigSetting(config, "System.Language Mode", "en_US", + "The enabled language file.", + "More language files (if available) can be found in the plugins data folder."); + + /** + * In order to set dynamic economy comment correctly, this needs to be + * called after EconomyManager load + */ + public static void setupConfig() { + config.load(); + config.setAutoremove(true).setAutosave(true); + + // convert economy settings + if (config.getBoolean("Economy.Use Vault Economy") && EconomyManager.getManager().isEnabled("Vault")) { + config.set("Main.Economy", "Vault"); + } else if (config.getBoolean("Economy.Use Reserve Economy") && EconomyManager.getManager().isEnabled("Reserve")) { + config.set("Main.Economy", "Reserve"); + } else if (config.getBoolean("Economy.Use Player Points Economy") && EconomyManager.getManager().isEnabled("PlayerPoints")) { + config.set("Main.Economy", "PlayerPoints"); + } + + config.saveChanges(); + } +} diff --git a/src/main/java/com/songoda/epicenchants/utils/settings/SettingsManager.java b/src/main/java/com/songoda/epicenchants/utils/settings/SettingsManager.java deleted file mode 100644 index edf7ae7..0000000 --- a/src/main/java/com/songoda/epicenchants/utils/settings/SettingsManager.java +++ /dev/null @@ -1,311 +0,0 @@ -package com.songoda.epicenchants.utils.settings; - -import com.songoda.core.compatibility.ServerVersion; -import com.songoda.epicenchants.EpicEnchants; -import com.songoda.epicenchants.utils.Methods; -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.inventory.InventoryType; -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 EpicEnchants plugin; - private Map cat = new HashMap<>(); - private Map current = new HashMap<>(); - - public SettingsManager(EpicEnchants plugin) { - this.plugin = plugin; - Bukkit.getPluginManager().registerEvents(this, plugin); - } - - @EventHandler - public void onInventoryClick(InventoryClickEvent event) { - if (event.getView().getType() != InventoryType.CHEST) return; - 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(EpicEnchants.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(ServerVersion.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(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); - lore.add(Methods.formatText("&7" + config.getInt(fKey))); - } else if (config.isLong(fKey)) { - item.setType(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) ? Material.CLOCK : Material.valueOf("WATCH")); - lore.add(Methods.formatText("&7" + config.getLong(fKey))); - } else if (config.isDouble(fKey)) { - item.setType(ServerVersion.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 { - if (!plugin.getDataFolder().exists()) - plugin.getDataFolder().mkdir(); - BufferedWriter writer = - new BufferedWriter(new FileWriter(new File(plugin.getDataFolder() + File.separator + "config.yml"))); - writer.write(config.toString()); - writer.flush(); - writer.close(); - - } catch (IOException e) { - e.printStackTrace(); - } - } -} \ No newline at end of file From b4dffc81067d364030129aa6942f61cc9ccce658 Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 15 Jan 2020 18:01:00 -0500 Subject: [PATCH 2/2] version 1.1.1 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c7560a8..f31935e 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ com.songoda EpicEnchants 4.0.0 - 1.1 + 1.1.1 clean install EpicEnchants-${project.version}