0

Moved loadFromConfig functionality from Main to ModuleLoader

This commit is contained in:
Pepich 2017-03-29 14:56:42 +02:00
parent 7e01ec6c56
commit 54bd7ba4ac
2 changed files with 61 additions and 56 deletions

View File

@ -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<Module, URLClassLoader> loaders = new HashMap<Module, URLClassLoader>();
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<String> 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.</br>
* This method is deprecated, use addDynamicModule(String name) instead. When using this method, dynamic reloading of the module will not be supported.
*

View File

@ -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<String> 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");