From 54bd7ba4acd8d4662fc522609d2c6f75e9dda02a Mon Sep 17 00:00:00 2001 From: Pepich Date: Wed, 29 Mar 2017 14:56:42 +0200 Subject: [PATCH] Moved loadFromConfig functionality from Main to ModuleLoader --- .../coremods/moduleLoader/ModuleLoader.java | 59 ++++++++++++++++++- src/com/redstoner/misc/Main.java | 58 +----------------- 2 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java b/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java index 565c7dd..0db7683 100644 --- a/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java +++ b/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java @@ -1,15 +1,19 @@ package com.redstoner.coremods.moduleLoader; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.HashMap; +import java.util.List; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; @@ -32,7 +36,7 @@ import net.minecraft.server.v1_11_R1.MinecraftServer; /** The module loader, mother of all modules. Responsible for loading and taking care of all modules. * * @author Pepich */ -@Version(major = 3, minor = 1, revision = 2, compatible = 2) +@Version(major = 3, minor = 1, revision = 3, compatible = 2) public final class ModuleLoader implements CoreModule { private static ModuleLoader instance; @@ -40,11 +44,15 @@ public final class ModuleLoader implements CoreModule private static URL[] urls; private static URLClassLoader mainLoader; private static HashMap loaders = new HashMap(); + private static File configFile; + private static FileConfiguration config; private ModuleLoader() { try { + config = Main.plugin.getConfig(); + configFile = new File(Main.plugin.getDataFolder(), "config.yml"); urls = new URL[] {(new File(Main.plugin.getDataFolder(), "classes")).toURI().toURL()}; mainLoader = new URLClassLoader(urls, this.getClass().getClassLoader()); } @@ -62,6 +70,55 @@ public final class ModuleLoader implements CoreModule Main.plugin); } + public static final void loadFromConfig() + { + try + { + if (!configFile.exists()) + { + configFile.getParentFile().mkdirs(); + configFile.createNewFile(); + } + config.load(configFile); + } + catch (FileNotFoundException e) + {} + catch (IOException e) + { + e.printStackTrace(); + } + catch (InvalidConfigurationException e) + { + configFile.delete(); + try + { + configFile.createNewFile(); + } + catch (IOException e1) + { + e1.printStackTrace(); + } + Utils.error("Invalid config file! Creating new, blank file!"); + } + List autoload = config.getStringList("autoLoad"); + if (autoload == null || autoload.isEmpty()) + { + config.set("autoLoad", new String[] {"# Add the modules here!"}); + Main.plugin.saveConfig(); + try + { + config.save(configFile); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + for (String s : autoload) + if (!s.startsWith("#")) + ModuleLoader.addDynamicModule(s); + } + /** This method will add a module to the module list, without enabling it.
* This method is deprecated, use addDynamicModule(String name) instead. When using this method, dynamic reloading of the module will not be supported. * diff --git a/src/com/redstoner/misc/Main.java b/src/com/redstoner/misc/Main.java index 8880806..76dc291 100644 --- a/src/com/redstoner/misc/Main.java +++ b/src/com/redstoner/misc/Main.java @@ -1,11 +1,5 @@ package com.redstoner.misc; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.List; - -import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.plugin.java.JavaPlugin; import com.redstoner.annotations.Version; @@ -16,65 +10,19 @@ import com.redstoner.misc.mysql.MysqlHandler; /** Main class. Duh. * * @author Pepich */ -@Version(major = 3, minor = 0, revision = 1, compatible = -1) +@Version(major = 3, minor = 1, revision = 0, compatible = -1) public class Main extends JavaPlugin { public static JavaPlugin plugin; - public static File configFile; @Override public void onEnable() { plugin = this; - configFile = new File(this.getDataFolder(), "config.yml"); Debugger.init(); - ModuleLoader.init(); MysqlHandler.init(); - try - { - if (!configFile.exists()) - { - configFile.getParentFile().mkdirs(); - configFile.createNewFile(); - } - getConfig().load(configFile); - } - catch (FileNotFoundException e) - {} - catch (IOException e) - { - e.printStackTrace(); - } - catch (InvalidConfigurationException e) - { - configFile.delete(); - try - { - configFile.createNewFile(); - } - catch (IOException e1) - { - e1.printStackTrace(); - } - Utils.error("Invalid config file! Creating new, blank file!"); - } - List autoload = this.getConfig().getStringList("autoLoad"); - if (autoload == null || autoload.isEmpty()) - { - getConfig().set("autoLoad", new String[] {"# Add the modules here!"}); - saveConfig(); - try - { - getConfig().save(configFile); - } - catch (IOException e) - { - e.printStackTrace(); - } - } - for (String s : autoload) - if (!s.startsWith("#")) - ModuleLoader.addDynamicModule(s); + ModuleLoader.init(); + ModuleLoader.loadFromConfig(); // ModuleLoader.addDynamicModule("com.redstoner.modules.abot.Abot"); // ModuleLoader.addDynamicModule("com.redstoner.modules.adminchat.Adminchat"); // ModuleLoader.addDynamicModule("com.redstoner.modules.adminnotes.AdminNotes");