- Add command and listener support

This commit is contained in:
Taah 2022-03-05 17:17:14 -08:00
parent 87ddf355b5
commit 69541a20b8
2 changed files with 46 additions and 0 deletions

View File

@ -89,11 +89,18 @@ public class ModuleManager {
public void disableModules() { public void disableModules() {
this.modules.forEach(module -> { this.modules.forEach(module -> {
PlexLog.log("Disabling module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion()); PlexLog.log("Disabling module " + module.getPlexModuleFile().getName() + " with version " + module.getPlexModuleFile().getVersion());
module.getCommands().stream().toList().forEach(plexCommand -> {
module.unregisterCommand(plexCommand);
Plex.get().getServer().getCommandMap().getKnownCommands().remove(plexCommand.getName());
plexCommand.getAliases().forEach(alias -> Plex.get().getServer().getCommandMap().getKnownCommands().remove(alias));
});
module.getListeners().forEach(module::unregisterListener);
module.disable(); module.disable();
}); });
} }
public void unloadModules() { public void unloadModules() {
this.disableModules();
this.modules.forEach(module -> { this.modules.forEach(module -> {
try { try {
((URLClassLoader)module.getClass().getClassLoader()).close(); ((URLClassLoader)module.getClass().getClassLoader()).close();

View File

@ -1,18 +1,31 @@
package dev.plex.module; package dev.plex.module;
import com.google.common.collect.Lists;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.command.PlexCommand;
import dev.plex.listener.PlexListener;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bukkit.event.HandlerList;
import java.io.File; import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
@Getter @Getter
@Setter(AccessLevel.MODULE) @Setter(AccessLevel.MODULE)
public abstract class PlexModule public abstract class PlexModule
{ {
@Getter(AccessLevel.MODULE)
private final List<PlexCommand> commands = Lists.newArrayList();
@Getter(AccessLevel.MODULE)
private final List<PlexListener> listeners = Lists.newArrayList();
private Plex plex; private Plex plex;
private PlexModuleFile plexModuleFile; private PlexModuleFile plexModuleFile;
private File dataFolder; private File dataFolder;
@ -23,4 +36,30 @@ public abstract class PlexModule
public void enable() {} public void enable() {}
public void disable() {} public void disable() {}
public void registerListener(PlexListener listener)
{
listeners.add(listener);
}
public void unregisterListener(PlexListener listener)
{
listeners.remove(listener);
HandlerList.unregisterAll(listener);
}
public void registerCommand(PlexCommand command)
{
commands.add(command);
}
public void unregisterCommand(PlexCommand command)
{
commands.remove(command);
}
public PlexCommand getCommand(String name)
{
return commands.stream().filter(plexCommand -> plexCommand.getName().equalsIgnoreCase(name) || plexCommand.getAliases().stream().map(String::toLowerCase).toList().contains(name.toLowerCase(Locale.ROOT))).findFirst().orElse(null);
}
} }