From 489c5fcb82e41a69281451d81eb3abd6655511cb Mon Sep 17 00:00:00 2001 From: Dzious Date: Sun, 15 Nov 2020 19:01:11 +0100 Subject: [PATCH] [Fix] reload command not reloading --- .../command/CommandEnchantControl.java | 32 +++++++++++-------- .../plugin/EnchantmentManager.java | 14 +++++--- .../enchantcontrol/utils/ConfigManager.java | 27 ++++++++++++---- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/com/dzious/bukkit/enchantcontrol/command/CommandEnchantControl.java b/src/com/dzious/bukkit/enchantcontrol/command/CommandEnchantControl.java index 9971c79..9a454e3 100644 --- a/src/com/dzious/bukkit/enchantcontrol/command/CommandEnchantControl.java +++ b/src/com/dzious/bukkit/enchantcontrol/command/CommandEnchantControl.java @@ -1,19 +1,20 @@ package com.dzious.bukkit.enchantcontrol.command; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import com.dzious.bukkit.enchantcontrol.EnchantControl; import com.dzious.bukkit.enchantcontrol.utils.Utils; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.TabCompleter; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import org.bukkit.configuration.InvalidConfigurationException; public class CommandEnchantControl implements CommandExecutor, TabCompleter { @@ -22,10 +23,7 @@ public class CommandEnchantControl implements CommandExecutor, TabCompleter { private Map> tabComplete = new HashMap<>(); - List enchantcontrolTabComplete = Arrays.asList( - "reload" - ); - + List enchantcontrolTabComplete = Arrays.asList("reload"); public CommandEnchantControl(EnchantControl plugin) { this.plugin = plugin; @@ -35,9 +33,17 @@ public CommandEnchantControl(EnchantControl plugin) { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { plugin.getLogManager().logDebugConsole("Enchant Control Command"); - if (sender instanceof ConsoleCommandSender) { - if (args.length > 0 && args[0] == "reload") { + plugin.getLogManager().logDebugConsole("length : " + args.length); + for (int i = 0; i < args.length; i++) + plugin.getLogManager().logDebugConsole("arg[" + i + "] : " + args[i]); + + if (args.length > 0 && args[0].equalsIgnoreCase("reload")) { + try { + plugin.getConfigManager().reload(); plugin.getEnchantmentManager().reload(); + } catch (IOException | InvalidConfigurationException e) { + plugin.getLogManager().logSevere("Config file does not exists. Reload failed."); + e.printStackTrace(); } return (true); } diff --git a/src/com/dzious/bukkit/enchantcontrol/plugin/EnchantmentManager.java b/src/com/dzious/bukkit/enchantcontrol/plugin/EnchantmentManager.java index 8a419af..ce74450 100644 --- a/src/com/dzious/bukkit/enchantcontrol/plugin/EnchantmentManager.java +++ b/src/com/dzious/bukkit/enchantcontrol/plugin/EnchantmentManager.java @@ -1,13 +1,17 @@ package com.dzious.bukkit.enchantcontrol.plugin; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import com.dzious.bukkit.enchantcontrol.EnchantControl; + import org.bukkit.ChatColor; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; -import java.util.*; - public class EnchantmentManager { private EnchantControl plugin; @@ -47,9 +51,9 @@ public void reload() for (Map.Entry enchantment : enchantments.entrySet()) { plugin.getLogManager().logDebugConsole("Currently processing : " + ChatColor.GREEN + enchantment.getKey() + ChatColor.WHITE + ". It's level is " + ChatColor.RED + enchantment.getValue()); - String[] enchantmentId = enchantment.toString().split(":"); - if (plugin.getConfigManager().doPathExist("enchantment." + enchantmentId[0] + "." + enchantmentId[1])) { - int newLevel = plugin.getConfigManager().getIntFromPath("enchantment." + enchantmentId[0] + "." + enchantmentId[1]); + String[] enchantmentId = enchantment.getKey().getKey().toString().split(":"); + if (plugin.getConfigManager().doPathExist("enchantments." + enchantmentId[0] + "." + enchantmentId[1])) { + int newLevel = plugin.getConfigManager().getIntFromPath("enchantments." + enchantmentId[0] + "." + enchantmentId[1]); if (enchantment.getValue() != newLevel) { plugin.getLogManager().logDebugConsole("Replaced " + enchantment.getKey() + ". Old value was " + enchantment.getValue() + ", new value is " + newLevel + "."); enchantment.setValue(newLevel); diff --git a/src/com/dzious/bukkit/enchantcontrol/utils/ConfigManager.java b/src/com/dzious/bukkit/enchantcontrol/utils/ConfigManager.java index 42aeba4..9242def 100644 --- a/src/com/dzious/bukkit/enchantcontrol/utils/ConfigManager.java +++ b/src/com/dzious/bukkit/enchantcontrol/utils/ConfigManager.java @@ -1,20 +1,33 @@ package com.dzious.bukkit.enchantcontrol.utils; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + import com.dzious.bukkit.enchantcontrol.EnchantControl; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; -import java.io.File; -import java.util.*; - public class ConfigManager { private FileConfiguration configFile = null; + private final EnchantControl plugin; - public ConfigManager (EnchantControl plugin) { - File file = new File(plugin.getDataFolder(), "config.yml"); + public ConfigManager(EnchantControl plugin) { + this.plugin = plugin; + File file = new File(this.plugin.getDataFolder(), "config.yml"); if (file.exists() == false) - plugin.saveDefaultConfig(); - configFile = plugin.getConfig(); + this.plugin.saveDefaultConfig(); + configFile = this.plugin.getConfig(); + } + + public void reload() throws FileNotFoundException, IOException, InvalidConfigurationException { + File file = new File(plugin.getDataFolder(), "config.yml"); + configFile.load(file); } public boolean doPathExist (String path) {