Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
teraprath authored May 8, 2023
1 parent bf9b096 commit 2a8930c
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.spigotcloud</groupId>
<artifactId>lobby</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.2.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lobby</name>
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/net/spigotcloud/lobby/Lobby.java
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;
}

}
28 changes: 28 additions & 0 deletions src/main/java/net/spigotcloud/lobby/module/Module.java
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 src/main/java/net/spigotcloud/lobby/module/ModuleLoader.java
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!");
}
}


}
8 changes: 8 additions & 0 deletions src/main/resources/plugin.yml
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

0 comments on commit 2a8930c

Please sign in to comment.