-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Listener> 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<Listener> getListeners() { | ||
return this.listeners; | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Module> modules = new HashSet<>(); | ||
private final Set<Class<?>> 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!"); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |