Moved features to separate projects
This commit is contained in:
26
src/cz/marwland/mc/core/MarwCommandExecutor.java
Normal file
26
src/cz/marwland/mc/core/MarwCommandExecutor.java
Normal file
@ -0,0 +1,26 @@
|
||||
package cz.marwland.mc.core;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class MarwCommandExecutor implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (args.length < 1) {
|
||||
sendHelp(sender);
|
||||
} else if (args[0].equalsIgnoreCase("reload")) {
|
||||
MarwCore.getInstance().reload();
|
||||
sender.sendMessage(ChatColor.GREEN + "Reloaded!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void sendHelp(CommandSender sender) {
|
||||
sender.sendMessage("Available commands:");
|
||||
sender.sendMessage("/marw reload - Reload configs.");
|
||||
}
|
||||
}
|
@ -1,46 +1,43 @@
|
||||
package cz.marwland.mc.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import cz.marwland.mc.core.config.ConfigManager;
|
||||
import cz.marwland.mc.core.features.Base;
|
||||
import cz.marwland.mc.core.features.Feature;
|
||||
import cz.marwland.mc.essentials.features.AntiVoid;
|
||||
import cz.marwland.mc.essentials.features.Borders;
|
||||
import cz.marwland.mc.essentials.features.ChatNotifier;
|
||||
import cz.marwland.mc.essentials.features.ChorusLimiter;
|
||||
import cz.marwland.mc.essentials.features.RandomTeleport;
|
||||
import cz.marwland.mc.core.plugins.ModuleClassLoader;
|
||||
|
||||
public class MarwCore extends JavaPlugin {
|
||||
|
||||
private ConfigManager configManager;
|
||||
private HashMap<String, Feature> features = new HashMap<>();
|
||||
private static MarwCore INSTANCE = null;
|
||||
private File modulesFolder = null;
|
||||
private ModuleClassLoader moduleClassLoader;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
INSTANCE = this;
|
||||
//getServer().getPluginManager().registerEvents(new EventListener(this), this);
|
||||
this.getCommand("marw").setExecutor(new MarwCommandExecutor());
|
||||
|
||||
configManager = new ConfigManager(this);
|
||||
configManager.registerConfig("config.yml");
|
||||
configManager.loadConfig("config.yml");
|
||||
|
||||
this.addFeature(new AntiVoid());
|
||||
this.addFeature(new Base());
|
||||
this.addFeature(new Borders());
|
||||
this.addFeature(new ChatNotifier());
|
||||
this.addFeature(new ChorusLimiter());
|
||||
this.addFeature(new RandomTeleport());
|
||||
|
||||
configManager.load();
|
||||
this.features.forEach((k, v) -> v.onEnable());
|
||||
modulesFolder = this.getModulesFolderPath().toFile();
|
||||
modulesFolder.mkdirs();
|
||||
this.loadAndEnableModules();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
this.features.forEach((k, v) -> v.onDisable());
|
||||
configManager.save();
|
||||
this.configManager.save();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -50,7 +47,36 @@ public class MarwCore extends JavaPlugin {
|
||||
|
||||
public void reload() {
|
||||
this.reloadConfig();
|
||||
this.features.forEach((k, v) -> v.onReload());
|
||||
this.features.forEach((k, v) -> v.onDisable());
|
||||
this.loadAndEnableModules();
|
||||
}
|
||||
|
||||
public void loadModules() {
|
||||
if (moduleClassLoader != null) {
|
||||
try {
|
||||
moduleClassLoader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
moduleClassLoader = new ModuleClassLoader();
|
||||
moduleClassLoader.addFromDirectory(modulesFolder);
|
||||
|
||||
for (String modClass : configManager.getConfig("config.yml").getStringList("modules")) {
|
||||
Feature f = moduleClassLoader.doConstructor(modClass, this);
|
||||
if (f == null)
|
||||
continue;
|
||||
|
||||
this.addFeature(f);
|
||||
this.getLogger().log(Level.INFO, "Loaded " + f.getName() + " @ " + modClass);
|
||||
}
|
||||
}
|
||||
|
||||
public void loadAndEnableModules() {
|
||||
this.loadModules();
|
||||
configManager.load();
|
||||
this.features.forEach((k, v) -> v.onEnable());
|
||||
}
|
||||
|
||||
public ConfigManager getConfigManager() {
|
||||
@ -73,4 +99,12 @@ public class MarwCore extends JavaPlugin {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public Path getModulesFolderPath() {
|
||||
return this.getDataFolder().toPath().resolve("modules");
|
||||
}
|
||||
|
||||
public ModuleClassLoader getModuleClassLoader() {
|
||||
return this.moduleClassLoader;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,6 +22,10 @@ public class ConfigManager {
|
||||
configs.put(path, new ConfigFile(plugin, template, path));
|
||||
}
|
||||
|
||||
public void loadConfig(String path) {
|
||||
configs.get(path).load();
|
||||
}
|
||||
|
||||
public void load() {
|
||||
configs.forEach((k, v) -> v.load());
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
package cz.marwland.mc.core.features;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.defaults.BukkitCommand;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import cz.marwland.mc.core.features.Feature;
|
||||
|
||||
|
||||
public class Base extends Feature {
|
||||
|
||||
FileConfiguration cfg;
|
||||
|
||||
public Base() {
|
||||
super("marw");
|
||||
// String name, String description, String usageMessage, List<String> aliases)
|
||||
this.addCommand(new BukkitCommand("marw", "Core plugin controls.", "/marw", Arrays.asList()) {
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
||||
if (args.length < 1) {
|
||||
sendHelp(sender);
|
||||
} else if (args[0].equalsIgnoreCase("reload")) {
|
||||
plugin.reload();
|
||||
sender.sendMessage("Reloaded!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void sendHelp(CommandSender sender) {
|
||||
sender.sendMessage("Available commands:");
|
||||
sender.sendMessage("/marw reload - Reload configs.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -41,6 +41,7 @@ public abstract class Feature implements Listener {
|
||||
public void onEnable() {
|
||||
if (cmap != null) {
|
||||
for (BukkitCommand cmd : commands) {
|
||||
System.out.println("Registering: " + cmd.getName());
|
||||
cmap.register(plugin.getName().toLowerCase(), cmd);
|
||||
}
|
||||
}
|
||||
@ -54,9 +55,6 @@ public abstract class Feature implements Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onReload() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
|
87
src/cz/marwland/mc/core/plugins/ModuleClassLoader.java
Normal file
87
src/cz/marwland/mc/core/plugins/ModuleClassLoader.java
Normal file
@ -0,0 +1,87 @@
|
||||
package cz.marwland.mc.core.plugins;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import cz.marwland.mc.core.features.Feature;
|
||||
|
||||
public class ModuleClassLoader extends URLClassLoader {
|
||||
|
||||
private ArrayList<JarFile> jars = new ArrayList<>();
|
||||
|
||||
public ModuleClassLoader() {
|
||||
super(new URL[] { }, ModuleClassLoader.class.getClassLoader());
|
||||
}
|
||||
|
||||
public void addFile(File file) throws IOException {
|
||||
this.addURL(file.toURI().toURL());
|
||||
jars.add(new JarFile(file));
|
||||
}
|
||||
|
||||
public void addFromDirectory(File modDir) {
|
||||
if (!modDir.isDirectory())
|
||||
throw new IllegalArgumentException("Provided 'modDir' argument is not a directory: " + modDir.getPath());
|
||||
|
||||
for (File mod : modDir.listFiles()) {
|
||||
if (mod.isDirectory())
|
||||
continue;
|
||||
int lastdot = mod.getName().lastIndexOf('.');
|
||||
if (lastdot < 0)
|
||||
continue;
|
||||
|
||||
if (!mod.getName().substring(lastdot + 1).equalsIgnoreCase("jar"))
|
||||
continue;
|
||||
|
||||
try {
|
||||
this.addFile(mod);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Feature doConstructor(String classPath)
|
||||
throws ClassNotFoundException, ClassCastException, IllegalAccessException, InstantiationException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
Class<?> jarClass;
|
||||
jarClass = Class.forName(classPath, true, this);
|
||||
|
||||
Class<? extends Feature> pluginClass;
|
||||
pluginClass = jarClass.asSubclass(Feature.class);
|
||||
|
||||
return pluginClass.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
|
||||
public Feature doConstructor(String classPath, JavaPlugin plugin) {
|
||||
try {
|
||||
return this.doConstructor(classPath);
|
||||
} catch (ClassNotFoundException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Cannot find class '" + classPath + "'!");
|
||||
} catch (ClassCastException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Class '" + classPath + "' does not extend Feature!");
|
||||
} catch (IllegalAccessException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "No public constructor in '" + classPath + "'!");
|
||||
} catch (InstantiationException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Cannot instantiate '" + classPath + "'!");
|
||||
} catch (IllegalArgumentException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Illegal arguments to the constructor of '" + classPath + "'!");
|
||||
} catch (InvocationTargetException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Error occured whilst initializing '" + classPath + "'!");
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Class '" + classPath + "' does not have a public constructor!");
|
||||
} catch (SecurityException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "SecurityException occured whilst initializing '" + classPath + "'!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user