diff --git a/pom.xml b/pom.xml index 67f91e1..8b9f403 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.spigotcloud lobby - 1.2.0-SNAPSHOT + 1.2.1-SNAPSHOT jar Lobby diff --git a/src/main/java/net/spigotcloud/lobby/Lobby.java b/src/main/java/net/spigotcloud/lobby/Lobby.java new file mode 100644 index 0000000..8522877 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/Lobby.java @@ -0,0 +1,36 @@ +package net.spigotcloud.lobby; + +import net.spigotcloud.lobby.module.ModuleLoader; +import org.bukkit.plugin.java.JavaPlugin; + +public final class Lobby extends JavaPlugin { + + private static Lobby instance; + private ModuleLoader moduleLoader; + + @Override + public void onEnable() { + + instance = this; + + this.moduleLoader = new ModuleLoader(); + + moduleLoader.reload(); + moduleLoader.enable(); + + } + + @Override + public void onDisable() { + moduleLoader.disable(); + } + + public ModuleLoader getModuleLoader() { + return this.moduleLoader; + } + + public static Lobby getInstance() { + return instance; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/module/Module.java b/src/main/java/net/spigotcloud/lobby/module/Module.java new file mode 100644 index 0000000..02bfc9e --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/module/Module.java @@ -0,0 +1,28 @@ +package net.spigotcloud.lobby.module; + +import org.bukkit.event.Listener; + +import javax.annotation.Nonnull; +import java.util.HashSet; +import java.util.Set; + +public abstract class Module { + + private final Set listeners = new HashSet<>(); + + public abstract void onEnable(); + public abstract void onDisable(); + + public String getName() { + return getClass().getSimpleName(); + } + + public void registerListener(@Nonnull Listener listener) { + this.listeners.add(listener); + } + + public Set getListeners() { + return this.listeners; + } + +} diff --git a/src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java b/src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java new file mode 100644 index 0000000..9cb0857 --- /dev/null +++ b/src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java @@ -0,0 +1,98 @@ +package net.spigotcloud.lobby.module; + +import net.spigotcloud.lobby.Lobby; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.HandlerList; + +import java.io.*; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashSet; +import java.util.Set; +import java.util.logging.Level; +import java.util.zip.ZipFile; + +public class ModuleLoader { + + private final Set modules = new HashSet<>(); + private final Set> moduleClasses = new HashSet<>(); + private final File file; + + + public ModuleLoader() { + + this.file = new File(Lobby.getInstance().getDataFolder() + "/modules"); + if (!file.exists()) { file.mkdir(); } + + } + + public void reload() { + + if (file.exists() && file.isDirectory()) { + + File[] files = file.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.endsWith(".jar"); + } + }); + + for (File jar : files) { + String mainClass = null; + try { + ZipFile zipFile = new ZipFile(jar); + + InputStream is = zipFile.getInputStream(zipFile.getEntry("module.yml")); + + YamlConfiguration config = YamlConfiguration.loadConfiguration(new InputStreamReader(is)); + mainClass = config.getString("main"); + + ClassLoader l = URLClassLoader.newInstance(new URL[]{jar.toURI().toURL()}, getClass().getClassLoader()); + + Class clazz = l.loadClass(mainClass); + moduleClasses.add(clazz); + + } catch (IOException e) { + Lobby.getInstance().getLogger().log(Level.SEVERE, "Error while loading module file " + jar.getName()); + e.printStackTrace(); + } catch (ClassNotFoundException e) { + Lobby.getInstance().getLogger().log(Level.SEVERE, "Class not found! Wrong main defined in extension.yml?: " + jar.getName() + " class: " + mainClass); + e.printStackTrace(); + } + + + } + } + } + + public void enable() { + for (Class clazz : moduleClasses) { + + try { + Object object = clazz.newInstance(); + + if (object instanceof Module) { + Module module = (Module) object; + module.getListeners().forEach(listener -> { + Lobby.getInstance().getServer().getPluginManager().registerEvents(listener, Lobby.getInstance()); + }); + module.onEnable(); + modules.add(module); + Lobby.getInstance().getLogger().log(Level.INFO, "Module " + module.getName() + " enabled!"); + } + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + } + } + + public void disable() { + for (Module module : modules) { + module.getListeners().forEach(HandlerList::unregisterAll); + module.onDisable(); + Lobby.getInstance().getLogger().log(Level.INFO, "Module " + module.getName() + " disabled!"); + } + } + + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..f47f52b --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,8 @@ +name: Lobby +version: '${project.version}' +main: net.spigotcloud.lobby.Lobby +api-version: 1.19 +softdepend: [ PointsAPI ] +authors: [ TerryGHG ] +description: Modular Lobby Plugin for Spigot 1.19+ +website: https://github.com/teraprath