diff --git a/api/src/main/java/us/ajg0702/queue/api/Implementation.java b/api/src/main/java/us/ajg0702/queue/api/Implementation.java new file mode 100644 index 0000000..b24d5e6 --- /dev/null +++ b/api/src/main/java/us/ajg0702/queue/api/Implementation.java @@ -0,0 +1,11 @@ +package us.ajg0702.queue.api; + +import us.ajg0702.queue.api.commands.IBaseCommand; + +public interface Implementation { + void registerCommand(IBaseCommand command); + void unregisterCommand(String name); + default void unregisterCommand(IBaseCommand command) { + unregisterCommand(command.getName()); + } +} diff --git a/common/src/main/java/us/ajg0702/queue/commands/commands/SlashServer/SlashServerCommand.java b/common/src/main/java/us/ajg0702/queue/commands/commands/SlashServer/SlashServerCommand.java new file mode 100644 index 0000000..c625e80 --- /dev/null +++ b/common/src/main/java/us/ajg0702/queue/commands/commands/SlashServer/SlashServerCommand.java @@ -0,0 +1,55 @@ +package us.ajg0702.queue.commands.commands.SlashServer; + +import com.google.common.collect.ImmutableList; +import us.ajg0702.queue.api.commands.ICommandSender; +import us.ajg0702.queue.commands.BaseCommand; +import us.ajg0702.queue.common.QueueMain; +import us.ajg0702.utils.common.Messages; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +public class SlashServerCommand extends BaseCommand { + + final QueueMain main; + final String server; + public SlashServerCommand(QueueMain main, String server) { + this.main = main; + this.server = server; + } + + @Override + public String getName() { + return server; + } + + @Override + public ImmutableList getAliases() { + return ImmutableList.of(); + } + + @Override + public String getPermission() { + return null; + } + + @Override + public Messages getMessages() { + return main.getMessages(); + } + + @Override + public void execute(ICommandSender sender, String[] args) { + if(!sender.isPlayer()) { + sender.sendMessage(getMessages().getComponent("errors.player-only")); + return; + } + main.getQueueManager().addToQueue(main.getPlatformMethods().senderToPlayer(sender), server); + } + + @Override + public List autoComplete(ICommandSender sender, String[] args) { + return new ArrayList<>(); + } +} diff --git a/common/src/main/java/us/ajg0702/queue/commands/commands/manage/Reload.java b/common/src/main/java/us/ajg0702/queue/commands/commands/manage/Reload.java index 90c8bc7..c45b0aa 100644 --- a/common/src/main/java/us/ajg0702/queue/commands/commands/manage/Reload.java +++ b/common/src/main/java/us/ajg0702/queue/commands/commands/manage/Reload.java @@ -54,6 +54,7 @@ public class Reload extends SubCommand { main.getTaskManager().rescheduleTasks(); main.getQueueManager().reloadServers(); main.getMessages().reload(); + main.getSlashServerManager().reload(); main.getUpdater().setEnabled(main.getConfig().getBoolean("enable-updater")); diff --git a/common/src/main/java/us/ajg0702/queue/common/QueueMain.java b/common/src/main/java/us/ajg0702/queue/common/QueueMain.java index 3d8f4fb..d73b691 100644 --- a/common/src/main/java/us/ajg0702/queue/common/QueueMain.java +++ b/common/src/main/java/us/ajg0702/queue/common/QueueMain.java @@ -107,6 +107,16 @@ public class QueueMain extends AjQueueAPI { return updater; } + private final Implementation implementation; + public Implementation getImplementation() { + return implementation; + } + + private SlashServerManager slashServerManager; + public SlashServerManager getSlashServerManager() { + return slashServerManager; + } + @Override public void shutdown() { taskManager.shutdown(); @@ -117,7 +127,8 @@ public class QueueMain extends AjQueueAPI { private final File dataFolder; - public QueueMain(QueueLogger logger, PlatformMethods platformMethods, File dataFolder) { + public QueueMain(Implementation implementation, QueueLogger logger, PlatformMethods platformMethods, File dataFolder) { + this.implementation = implementation; logicGetter = new LogicGetterImpl(); @@ -147,6 +158,8 @@ public class QueueMain extends AjQueueAPI { return; } + slashServerManager = new SlashServerManager(this); + //noinspection ResultOfMethodCallIgnored messages.getComponent("one").replaceText(b -> b.match(Pattern.compile("\\e")).replacement("a")); diff --git a/common/src/main/java/us/ajg0702/queue/common/SlashServerManager.java b/common/src/main/java/us/ajg0702/queue/common/SlashServerManager.java new file mode 100644 index 0000000..584e92a --- /dev/null +++ b/common/src/main/java/us/ajg0702/queue/common/SlashServerManager.java @@ -0,0 +1,31 @@ +package us.ajg0702.queue.common; + +import us.ajg0702.queue.api.Implementation; +import us.ajg0702.queue.commands.commands.SlashServer.SlashServerCommand; + +import java.util.ArrayList; +import java.util.List; + +public class SlashServerManager { + + private final List serverCommands = new ArrayList<>(); + + private final QueueMain main; + private final Implementation implementation; + public SlashServerManager(QueueMain main) { + this.main = main; + this.implementation = this.main.getImplementation(); + reload(); + } + public void reload() { + serverCommands.forEach(implementation::unregisterCommand); + serverCommands.clear(); + + List slashServerServers = main.getConfig().getStringList("slash-servers"); + for(String server : slashServerServers) { + SlashServerCommand command = new SlashServerCommand(main, server); + serverCommands.add(command); + implementation.registerCommand(command); + } + } +} diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index bc75f67..7ddcf6b 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -1,5 +1,5 @@ # Dont touch this number please -config-version: 28 +config-version: 29 # This is the main config for ajQueue. @@ -261,4 +261,10 @@ protocol-names: velocity-kick-message: false # Should the updater be enabled? -enable-updater: true \ No newline at end of file +enable-updater: true + +# What servers should we make slash server commands for? +# For example, if survival is in this list, then if a player executes /survival +# then they will be put in the queue for survival +# This works for both servers and groups +slash-servers: [] \ No newline at end of file diff --git a/platforms/bungeecord/src/main/java/us/ajg0702/queue/platforms/bungeecord/BungeeQueue.java b/platforms/bungeecord/src/main/java/us/ajg0702/queue/platforms/bungeecord/BungeeQueue.java index 7675a92..469a0f0 100644 --- a/platforms/bungeecord/src/main/java/us/ajg0702/queue/platforms/bungeecord/BungeeQueue.java +++ b/platforms/bungeecord/src/main/java/us/ajg0702/queue/platforms/bungeecord/BungeeQueue.java @@ -11,6 +11,7 @@ import net.md_5.bungee.event.EventHandler; import org.bstats.bungeecord.Metrics; import org.bstats.charts.SimplePie; import org.checkerframework.checker.nullness.qual.NonNull; +import us.ajg0702.queue.api.Implementation; import us.ajg0702.queue.api.commands.IBaseCommand; import us.ajg0702.queue.api.util.QueueLogger; import us.ajg0702.queue.commands.BaseCommand; @@ -25,14 +26,18 @@ import us.ajg0702.queue.platforms.bungeecord.server.BungeeServer; import java.io.File; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; -public class BungeeQueue extends Plugin implements Listener { +public class BungeeQueue extends Plugin implements Listener, Implementation { private QueueMain main; List commands; + Map commandMap; + @Override public void onEnable() { QueueLogger logger = new BungeeLogger(getLogger()); @@ -41,6 +46,7 @@ public class BungeeQueue extends Plugin implements Listener { adventure = BungeeAudiences.create(this); main = new QueueMain( + this, logger, new BungeeMethods(this, getProxy(), logger), dataFolder @@ -56,9 +62,10 @@ public class BungeeQueue extends Plugin implements Listener { new ManageCommand(main) ); + commandMap = new HashMap<>(); + for(IBaseCommand command : commands) { - getProxy().getPluginManager() - .registerCommand(this, new BungeeCommand((BaseCommand) command)); + registerCommand(command); } getProxy().getPluginManager().registerListener(this, this); @@ -129,4 +136,20 @@ public class BungeeQueue extends Plugin implements Listener { false ); } + + @Override + public void unregisterCommand(String name) { + BungeeCommand bungeeCommand = commandMap.get(name); + if(bungeeCommand == null) return; + getProxy().getPluginManager().unregisterCommand(bungeeCommand); + commandMap.remove(name); + } + + @Override + public void registerCommand(IBaseCommand command) { + BungeeCommand bungeeCommand = new BungeeCommand((BaseCommand) command); + commandMap.put(command.getName(), bungeeCommand); + getProxy().getPluginManager() + .registerCommand(this, bungeeCommand); + } } diff --git a/platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/VelocityQueue.java b/platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/VelocityQueue.java index b1b1696..9f6ce7d 100644 --- a/platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/VelocityQueue.java +++ b/platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/VelocityQueue.java @@ -18,6 +18,7 @@ import net.kyori.adventure.text.Component; import org.bstats.charts.SimplePie; import org.bstats.velocity.Metrics; import org.slf4j.Logger; +import us.ajg0702.queue.api.Implementation; import us.ajg0702.queue.api.commands.IBaseCommand; import us.ajg0702.queue.commands.BaseCommand; import us.ajg0702.queue.commands.commands.leavequeue.LeaveCommand; @@ -44,7 +45,7 @@ import java.util.Optional; authors = {"ajgeiss0702"} ) -public class VelocityQueue { +public class VelocityQueue implements Implementation { final ProxyServer proxyServer; final VelocityLogger logger; @@ -66,9 +67,12 @@ public class VelocityQueue { List commands; + CommandManager commandManager; + @Subscribe public void onProxyInit(ProxyInitializeEvent e) { main = new QueueMain( + this, logger, new VelocityMethods(this, proxyServer, logger), dataFolder @@ -81,7 +85,7 @@ public class VelocityQueue { new ManageCommand(main) ); - CommandManager commandManager = proxyServer.getCommandManager(); + commandManager = proxyServer.getCommandManager(); proxyServer.getChannelRegistrar().register(MinecraftChannelIdentifier.create("ajqueue", "tospigot")); @@ -89,12 +93,7 @@ public class VelocityQueue { for(IBaseCommand command : commands) { - commandManager.register( - commandManager.metaBuilder(command.getName()) - .aliases(command.getAliases().toArray(new String[]{})) - .build(), - new VelocityCommand(main, (BaseCommand) command) - ); + registerCommand(command); } @@ -138,7 +137,6 @@ public class VelocityQueue { main.getEventHandler().onPlayerLeave(new VelocityPlayer(e.getPlayer())); } - @SuppressWarnings("SimplifyOptionalCallChains") @Subscribe public void onKick(KickedFromServerEvent e) { if(!e.getPlayer().getCurrentServer().isPresent()) return; // if the player is kicked on initial join, we dont care @@ -152,4 +150,18 @@ public class VelocityQueue { ); } + @Override + public void unregisterCommand(String name) { + commandManager.unregister(name); + } + + @Override + public void registerCommand(IBaseCommand command) { + commandManager.register( + commandManager.metaBuilder(command.getName()) + .aliases(command.getAliases().toArray(new String[]{})) + .build(), + new VelocityCommand(main, (BaseCommand) command) + ); + } }