From 4fc33cb9f7256658a5af6953da7964da0b689d89 Mon Sep 17 00:00:00 2001 From: Tastybento Date: Wed, 27 Dec 2017 13:59:27 -0800 Subject: [PATCH] Mid-work. --- .../api/addons/ModuleClassLoader.java | 62 +++++++++++++++ .../bskyblock/api/module/Module.java | 77 +++++++++++++++++++ .../api/module/ModuleDescription.java | 9 +++ .../InvalidModuleFormatException.java | 5 ++ .../InvalidModuleInheritException.java | 5 ++ 5 files changed, 158 insertions(+) create mode 100644 src/main/java/us/tastybento/bskyblock/api/addons/ModuleClassLoader.java create mode 100644 src/main/java/us/tastybento/bskyblock/api/module/Module.java create mode 100644 src/main/java/us/tastybento/bskyblock/api/module/ModuleDescription.java create mode 100644 src/main/java/us/tastybento/bskyblock/api/module/exception/InvalidModuleFormatException.java create mode 100644 src/main/java/us/tastybento/bskyblock/api/module/exception/InvalidModuleInheritException.java diff --git a/src/main/java/us/tastybento/bskyblock/api/addons/ModuleClassLoader.java b/src/main/java/us/tastybento/bskyblock/api/addons/ModuleClassLoader.java new file mode 100644 index 000000000..92399bbdf --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/api/addons/ModuleClassLoader.java @@ -0,0 +1,62 @@ +package us.tastybento.bskyblock.api.addons; + +import java.io.BufferedReader; +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Map; + +import us.tastybento.bskyblock.api.module.Module; +import us.tastybento.bskyblock.api.module.ModuleDescription; +import us.tastybento.bskyblock.api.module.ModuleDescription.ModuleDescriptionBuilder; +import us.tastybento.bskyblock.api.module.exception.InvalidModuleFormatException; +import us.tastybento.bskyblock.api.module.exception.InvalidModuleInheritException; + +public class ModuleClassLoader extends URLClassLoader{ + + public Module module; + + public ModuleClassLoader(Mapdata, File path, BufferedReader reader, ClassLoader loaders) throws InvalidModuleInheritException, MalformedURLException, InvalidModuleFormatException { + super(new URL[]{path.toURI().toURL()}, loaders); + + Module module = null; + + Class javaClass = null; + try { + javaClass = Class.forName(data.get("Main"), true, this); + if(data.get("Main").contains("net.essence")){ + throw new InvalidModuleFormatException("Packages declaration cannot start with 'net.essence'"); + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + Class moduleClass; + try{ + moduleClass = javaClass.asSubclass(Module.class); + }catch(ClassCastException e){ + throw new InvalidModuleInheritException("Main class doesn't not extends super class 'Module'"); + } + + try { + module = moduleClass.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + + module.setDescription(this.asDescription(data)); + + this.module = module; + } + + private ModuleDescription asDescription(Map data){ + + String[] authors = data.get("Author").split("\\,"); + + return new ModuleDescriptionBuilder(data.get("Name")) + .withVersion(data.get("Version")) + .withAuthor(authors).build(); + } + +} \ No newline at end of file diff --git a/src/main/java/us/tastybento/bskyblock/api/module/Module.java b/src/main/java/us/tastybento/bskyblock/api/module/Module.java new file mode 100644 index 000000000..a91bfe483 --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/api/module/Module.java @@ -0,0 +1,77 @@ +package us.tastybento.bskyblock.api.module; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; + +import us.tastybento.bskyblock.api.module.command.CommandManager; +import us.tastybento.bskyblock.module.command.ModuleCommand; +import us.tastybento.bskyblock.module.event.ListenerManager; +import us.tastybento.bskyblock.module.event.ModuleListener; + +public abstract class Module{ + + private ListenerManager listenerManager; + private boolean enabled; + private ModuleDescription description; + private List registeredCommands; + + public Module(){ + this.enabled = false; + this.listenerManager = new ListenerManager(); + this.registeredCommands = new ArrayList<>(); + } + + public abstract void Enable(); + public abstract void Disable(); + public abstract void Load(); + + public void registerListener(ModuleListener listener){ + this.listenerManager.registerListener(listener); + } + + public void registerCommand(ModuleCommand command){ + CommandManager.registerCmd(command); + this.registeredCommands.add(command); + } + + public JavaPlugin getUsedPlugin(){ + return Main.getInstance(); + } + + public ListenerManager getListenerManager() { + return listenerManager; + } + + public void setListenerManager(ListenerManager listenerManager) { + this.listenerManager = listenerManager; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public ModuleDescription getDescription() { + return description; + } + + public void setDescription(ModuleDescription desc){ + this.description = desc; + } + + public List getRegisteredCommands() { + return registeredCommands; + } + + public void setRegisteredCommands(List registeredCommands) { + this.registeredCommands = registeredCommands; + } + + +} \ No newline at end of file diff --git a/src/main/java/us/tastybento/bskyblock/api/module/ModuleDescription.java b/src/main/java/us/tastybento/bskyblock/api/module/ModuleDescription.java new file mode 100644 index 000000000..62a8b430a --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/api/module/ModuleDescription.java @@ -0,0 +1,9 @@ +package us.tastybento.bskyblock.api.module; + +public class ModuleDescription { + + public class ModuleDescriptionBuilder { + + } + +} diff --git a/src/main/java/us/tastybento/bskyblock/api/module/exception/InvalidModuleFormatException.java b/src/main/java/us/tastybento/bskyblock/api/module/exception/InvalidModuleFormatException.java new file mode 100644 index 000000000..5d3921d50 --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/api/module/exception/InvalidModuleFormatException.java @@ -0,0 +1,5 @@ +package us.tastybento.bskyblock.api.module.exception; + +public class InvalidModuleFormatException { + +} diff --git a/src/main/java/us/tastybento/bskyblock/api/module/exception/InvalidModuleInheritException.java b/src/main/java/us/tastybento/bskyblock/api/module/exception/InvalidModuleInheritException.java new file mode 100644 index 000000000..1261c1ddb --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/api/module/exception/InvalidModuleInheritException.java @@ -0,0 +1,5 @@ +package us.tastybento.bskyblock.api.module.exception; + +public class InvalidModuleInheritException { + +}