Plex/server/src/main/java/dev/plex/module/ModuleManager.java

162 lines
5.9 KiB
Java
Raw Normal View History

2022-03-06 01:04:34 +00:00
package dev.plex.module;
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.module.exception.ModuleLoadException;
import dev.plex.module.loader.LibraryLoader;
2022-03-06 01:04:34 +00:00
import dev.plex.util.PlexLog;
2023-03-08 20:26:10 +00:00
import lombok.Getter;
import org.apache.logging.log4j.LogManager;
import org.bukkit.configuration.file.YamlConfiguration;
2022-03-06 01:04:34 +00:00
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
@Getter
2022-03-17 22:16:17 +00:00
public class ModuleManager
{
2022-03-06 01:04:34 +00:00
private final List<PlexModule> modules = Lists.newArrayList();
private final LibraryLoader libraryLoader;
public ModuleManager()
{
this.libraryLoader = new LibraryLoader(Plex.get().getLogger());
}
2022-03-06 01:04:34 +00:00
2022-03-17 22:16:17 +00:00
public void loadAllModules()
{
2022-03-06 01:04:34 +00:00
this.modules.clear();
PlexLog.debug(String.valueOf(Plex.get().getModulesFolder().listFiles().length));
2022-03-17 22:16:17 +00:00
Arrays.stream(Plex.get().getModulesFolder().listFiles()).forEach(file ->
{
if (file.getName().endsWith(".jar"))
{
try
{
2022-03-06 01:04:34 +00:00
URLClassLoader loader = new URLClassLoader(
new URL[]{file.toURI().toURL()},
Plex.class.getClassLoader()
);
InputStreamReader internalModuleFile = new InputStreamReader(loader.getResourceAsStream("module.yml"), StandardCharsets.UTF_8);
YamlConfiguration internalModuleConfig = YamlConfiguration.loadConfiguration(internalModuleFile);
String name = internalModuleConfig.getString("name");
if (name == null)
{
throw new ModuleLoadException("Plex module name can't be null!");
}
String main = internalModuleConfig.getString("main");
if (main == null)
{
throw new ModuleLoadException("Plex module main class can't be null!");
}
2022-03-06 04:54:02 +00:00
String description = internalModuleConfig.getString("description", "A Plex module");
String version = internalModuleConfig.getString("version", "1.0");
List<String> libraries = internalModuleConfig.getStringList("libraries");
2022-03-06 01:04:34 +00:00
PlexModuleFile plexModuleFile = new PlexModuleFile(name, main, description, version);
plexModuleFile.setLibraries(libraries);
2023-03-08 20:26:10 +00:00
Class<? extends PlexModule> module = (Class<? extends PlexModule>) Class.forName(main, true, loader);
2022-03-06 01:04:34 +00:00
PlexModule plexModule = module.getConstructor().newInstance();
2022-06-06 03:39:41 +00:00
plexModule.setPlex(Plex.get());
2022-03-06 01:04:34 +00:00
plexModule.setPlexModuleFile(plexModuleFile);
plexModule.setDataFolder(new File(Plex.get().getModulesFolder() + File.separator + plexModuleFile.getName()));
2022-03-17 22:16:17 +00:00
if (!plexModule.getDataFolder().exists())
{
plexModule.getDataFolder().mkdir();
}
2022-03-06 01:04:34 +00:00
plexModule.setLogger(LogManager.getLogger(plexModuleFile.getName()));
modules.add(plexModule);
2022-03-17 22:16:17 +00:00
}
2022-04-13 02:22:17 +00:00
catch (MalformedURLException | ClassNotFoundException | InvocationTargetException |
InstantiationException | IllegalAccessException | NoSuchMethodException e)
2022-03-17 22:16:17 +00:00
{
2022-03-06 01:04:34 +00:00
e.printStackTrace();
}
}
});
}
2022-03-17 22:16:17 +00:00
public void loadModules()
{
this.modules.forEach(module ->
{
2022-03-06 01:04:34 +00:00
PlexLog.log("Loading module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion());
module.load();
2022-05-19 18:59:16 +00:00
// this.libraryLoader.createLoader(module, module.getPlexModuleFile());
2022-03-06 01:04:34 +00:00
});
}
2022-03-17 22:16:17 +00:00
public void enableModules()
{
this.modules.forEach(module ->
{
2022-03-06 01:04:34 +00:00
PlexLog.log("Enabling module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion());
module.enable();
});
}
2022-03-17 22:16:17 +00:00
public void disableModules()
{
this.modules.forEach(module ->
{
2022-03-06 01:04:34 +00:00
PlexLog.log("Disabling module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion());
2022-03-17 22:16:17 +00:00
module.getCommands().stream().toList().forEach(plexCommand ->
{
2022-03-06 01:17:14 +00:00
module.unregisterCommand(plexCommand);
Plex.get().getServer().getCommandMap().getKnownCommands().remove(plexCommand.getName());
2022-05-06 02:13:47 +00:00
plexCommand.unregister(Plex.get().getServer().getCommandMap());
try
{
Plex.get().getServer().getCommandMap().getCommand(plexCommand.getName()).unregister(Plex.get().getServer().getCommandMap());
2022-05-10 05:08:45 +00:00
}
catch (Exception ignored)
2022-05-06 02:13:47 +00:00
{
}
2022-03-06 01:17:14 +00:00
plexCommand.getAliases().forEach(alias -> Plex.get().getServer().getCommandMap().getKnownCommands().remove(alias));
});
module.getListeners().stream().toList().forEach(module::unregisterListener);
2022-03-06 01:04:34 +00:00
module.disable();
});
}
2022-03-17 22:16:17 +00:00
public void unloadModules()
{
2022-03-06 01:17:14 +00:00
this.disableModules();
2022-03-17 22:16:17 +00:00
this.modules.forEach(module ->
{
try
{
2023-03-08 20:26:10 +00:00
((URLClassLoader) module.getClass().getClassLoader()).close();
2022-03-17 22:16:17 +00:00
}
catch (IOException e)
{
2022-03-06 01:04:34 +00:00
e.printStackTrace();
}
});
}
public void reloadModules()
{
unloadModules();
loadAllModules();
loadModules();
enableModules();
}
2022-03-06 01:04:34 +00:00
}