diff --git a/api/src/main/java/us/ajg0702/queue/api/spigot/AjQueueSpigotAPI.java b/api/src/main/java/us/ajg0702/queue/api/spigot/AjQueueSpigotAPI.java index 2c191fa..90d456c 100644 --- a/api/src/main/java/us/ajg0702/queue/api/spigot/AjQueueSpigotAPI.java +++ b/api/src/main/java/us/ajg0702/queue/api/spigot/AjQueueSpigotAPI.java @@ -22,13 +22,20 @@ public abstract class AjQueueSpigotAPI { public abstract Future isInQueue(UUID player); /** - * Adds a player to a queue + * Adds a player to a queue (bypassing any permission checks that would prevent it) * @param player The player to be added * @param queueName The server or group to add the player to * @return True if adding was successful, false if not. */ public abstract Future addToQueue(UUID player, String queueName); + /** + * Emulate the player running the queue command for a certain server, including any permission checks + * @param player The player to sudo the command for + * @param queueName The queue to send the player to + */ + public abstract void sudoQueue(UUID player, String queueName); + /** * Gets the name of the queue that the player is in * @param player the player diff --git a/common/src/main/java/us/ajg0702/queue/common/communication/CommunicationManager.java b/common/src/main/java/us/ajg0702/queue/common/communication/CommunicationManager.java index 750da5b..7533639 100644 --- a/common/src/main/java/us/ajg0702/queue/common/communication/CommunicationManager.java +++ b/common/src/main/java/us/ajg0702/queue/common/communication/CommunicationManager.java @@ -4,7 +4,6 @@ import us.ajg0702.queue.api.communication.ComResponse; import us.ajg0702.queue.api.players.AdaptedPlayer; import us.ajg0702.queue.common.QueueMain; import us.ajg0702.queue.common.communication.handlers.*; -import us.ajg0702.queue.common.utils.Debug; import us.ajg0702.queue.common.utils.MapBuilder; import java.io.ByteArrayInputStream; @@ -33,7 +32,8 @@ public class CommunicationManager { "inqueue", new InQueueHandler(main), "queuedfor", new QueuedForHandler(main), "status", new StatusHandler(main), - "playerstatus", new PlayerStatusHandler(main) + "playerstatus", new PlayerStatusHandler(main), + "serverqueue", new ServerQueueHandler(main) ); } diff --git a/common/src/main/java/us/ajg0702/queue/common/communication/handlers/QueueHandler.java b/common/src/main/java/us/ajg0702/queue/common/communication/handlers/QueueHandler.java index 30cf62a..7a4e943 100644 --- a/common/src/main/java/us/ajg0702/queue/common/communication/handlers/QueueHandler.java +++ b/common/src/main/java/us/ajg0702/queue/common/communication/handlers/QueueHandler.java @@ -7,6 +7,9 @@ import us.ajg0702.queue.common.QueueMain; import us.ajg0702.queue.api.communication.ComResponse; import us.ajg0702.queue.common.communication.MessageHandler; +/** + * Actually SudoQueue. Confusing naming is due to legacy support and will be removed next major revision. + */ public class QueueHandler extends MessageHandler { private final IBaseCommand moveCommand; diff --git a/common/src/main/java/us/ajg0702/queue/common/communication/handlers/ServerQueueHandler.java b/common/src/main/java/us/ajg0702/queue/common/communication/handlers/ServerQueueHandler.java new file mode 100644 index 0000000..312acff --- /dev/null +++ b/common/src/main/java/us/ajg0702/queue/common/communication/handlers/ServerQueueHandler.java @@ -0,0 +1,28 @@ +package us.ajg0702.queue.common.communication.handlers; + +import us.ajg0702.queue.api.communication.ComResponse; +import us.ajg0702.queue.api.players.AdaptedPlayer; +import us.ajg0702.queue.api.queues.QueueServer; +import us.ajg0702.queue.common.QueueMain; +import us.ajg0702.queue.common.communication.MessageHandler; + +public class ServerQueueHandler extends MessageHandler { + public ServerQueueHandler(QueueMain main) { + super(main); + } + + @Override + public ComResponse handleMessage(AdaptedPlayer player, String data) { + QueueServer server = main.getQueueManager().findServer(data); + if(server == null) { + return ComResponse + .from("serverqueue") + .id(player.getUniqueId()) + .with("invalid_server"); + } + return ComResponse + .from("serverqueue") + .id(player.getUniqueId()) + .with(main.getQueueManager().addToQueue(player, server)); + } +} diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/Commands.java b/spigot/src/main/java/us/ajg0702/queue/spigot/Commands.java index 7e2b6f9..d86936e 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/Commands.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/Commands.java @@ -7,6 +7,7 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI; public class Commands implements CommandExecutor { @@ -42,23 +43,32 @@ public class Commands implements CommandExecutor { pl.sendMessage(player, "leavequeue", arg.toString()); return true; } + + + // Queue command + + if(args.length < 1) return false; - - String srvname = args[0]; - + + String serverName = args[0]; + + boolean sudo = true; + + // /queue if(args.length > 1) { - pl.getLogger().info("Sending "+args[0]+" to queue '" + args[1] + "'"); + sudo = false; if(!sender.hasPermission("ajqueue.send")) { sender.sendMessage(color("&cYou do not have permission to do this!")); return true; } - Player tply = Bukkit.getPlayer(args[0]); - if(tply == null) { + pl.getLogger().info("Sending "+args[0]+" to queue '" + args[1] + "'"); + Player playerToSend = Bukkit.getPlayer(args[0]); + if(playerToSend == null) { sender.sendMessage(color("&cCannot find that player!")); return true; } - player = tply; - srvname = args[1]; + player = playerToSend; + serverName = args[1]; } if(player == null) { @@ -66,10 +76,14 @@ public class Commands implements CommandExecutor { return true; } - if(pl.getAConfig().getBoolean("send-queue-commands-in-batches")) { - pl.queuebatch.put(player, srvname); + if(sudo) { + if(pl.getAConfig().getBoolean("send-queue-commands-in-batches")) { + pl.queuebatch.put(player, serverName); + } else { + AjQueueSpigotAPI.getInstance().sudoQueue(player.getUniqueId(), serverName); + } } else { - pl.sendMessage(player, "queue", srvname); + AjQueueSpigotAPI.getInstance().addToQueue(player.getUniqueId(), serverName); } return true; diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/api/SpigotAPI.java b/spigot/src/main/java/us/ajg0702/queue/spigot/api/SpigotAPI.java index 1a6607f..7dcb80d 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/api/SpigotAPI.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/api/SpigotAPI.java @@ -38,7 +38,25 @@ public class SpigotAPI extends AjQueueSpigotAPI { @Override public Future addToQueue(UUID player, String queueName) { - throw new UnsupportedOperationException("Not yet implemented!"); + Player p = Bukkit.getPlayer(player); + if(p == null) throw new IllegalArgumentException("Player must be online!"); + + CompletableFuture future = new CompletableFuture<>(); + + responseManager.awaitResponse(player.toString(), "serverqueue", response -> { + future.complete(Boolean.valueOf(response.getResponse())); + }); + + main.sendMessage(p, "serverqueue", queueName); + + return future; + } + + @Override + public void sudoQueue(UUID player, String queueName) { + Player p = Bukkit.getPlayer(player); + if(p == null) throw new IllegalArgumentException("Player must be online!"); + main.sendMessage(p, "queue", queueName); } @Override