diff --git a/api/src/main/java/us/ajg0702/queue/api/EventHandler.java b/api/src/main/java/us/ajg0702/queue/api/EventHandler.java index 8156f90..1286746 100644 --- a/api/src/main/java/us/ajg0702/queue/api/EventHandler.java +++ b/api/src/main/java/us/ajg0702/queue/api/EventHandler.java @@ -3,5 +3,10 @@ package us.ajg0702.queue.api; import us.ajg0702.queue.api.players.AdaptedPlayer; public interface EventHandler { + void handleMessage(AdaptedPlayer reciever, byte[] data); + + void onPlayerJoin(AdaptedPlayer player); + + void onPlayerLeave(AdaptedPlayer player); } diff --git a/api/src/main/java/us/ajg0702/queue/api/Logic.java b/api/src/main/java/us/ajg0702/queue/api/Logic.java index e7f5b50..781a944 100644 --- a/api/src/main/java/us/ajg0702/queue/api/Logic.java +++ b/api/src/main/java/us/ajg0702/queue/api/Logic.java @@ -19,4 +19,11 @@ public interface Logic { * @param player The player that is being queued */ QueuePlayer priorityLogic(QueueServer server, AdaptedPlayer player); + + /** + * The logic for checking if a player has been disconnected for too long + * @param player The player to check + * @return true if the player has been disconnected for too long and should be removed from the queue + */ + boolean playerDisconnectedTooLong(QueuePlayer player); } diff --git a/api/src/main/java/us/ajg0702/queue/api/PlatformMethods.java b/api/src/main/java/us/ajg0702/queue/api/PlatformMethods.java index af1697c..51f118a 100644 --- a/api/src/main/java/us/ajg0702/queue/api/PlatformMethods.java +++ b/api/src/main/java/us/ajg0702/queue/api/PlatformMethods.java @@ -9,14 +9,6 @@ import us.ajg0702.queue.api.queues.QueueServer; import java.util.List; public interface PlatformMethods { - /** - * BungeeUtils.sendCustomData(p, "position", pos+""); - * BungeeUtils.sendCustomData(p, "positionof", len+""); - * BungeeUtils.sendCustomData(p, "queuename", pl.aliases.getAlias(s)); - * BungeeUtils.sendCustomData(p, "inqueue", "true"); - * BungeeUtils.sendCustomData(p, "inqueueevent", "true"); - */ - void sendJoinQueueChannelMessages(QueueServer queueServer, QueuePlayer queuePlayer); /** * Sends a plugin message on the plugin messaging channel diff --git a/api/src/main/java/us/ajg0702/queue/api/players/QueuePlayer.java b/api/src/main/java/us/ajg0702/queue/api/players/QueuePlayer.java index be2db7d..de3e4c4 100644 --- a/api/src/main/java/us/ajg0702/queue/api/players/QueuePlayer.java +++ b/api/src/main/java/us/ajg0702/queue/api/players/QueuePlayer.java @@ -56,4 +56,10 @@ public interface QueuePlayer { * @return the player's username */ String getName(); + + /** + * Returns the number of miliseconds since this player was online + * @return The number of miliseconds since this player was online + */ + long getTimeSinceOnline(); } diff --git a/api/src/main/java/us/ajg0702/queue/api/queues/QueueServer.java b/api/src/main/java/us/ajg0702/queue/api/queues/QueueServer.java index 13e6052..be6fc20 100644 --- a/api/src/main/java/us/ajg0702/queue/api/queues/QueueServer.java +++ b/api/src/main/java/us/ajg0702/queue/api/queues/QueueServer.java @@ -198,6 +198,12 @@ public interface QueueServer { */ QueuePlayer findPlayer(AdaptedPlayer player); + /** + * Finds the player with this uuid in this queue and returns the representative QueuePlayer + * @return The QueuePlayer representing the player, null if not found + */ + QueuePlayer findPlayer(UUID uuid); + /** * Gets the most ideal server in this group to join diff --git a/common/src/main/java/us/ajg0702/queue/commands/commands/PlayerSender.java b/common/src/main/java/us/ajg0702/queue/commands/commands/PlayerSender.java index 7a86d0c..57da37c 100644 --- a/common/src/main/java/us/ajg0702/queue/commands/commands/PlayerSender.java +++ b/common/src/main/java/us/ajg0702/queue/commands/commands/PlayerSender.java @@ -23,6 +23,6 @@ public class PlayerSender implements ICommandSender { @Override public AdaptedPlayer getHandle() { - return null; + return handle; } } diff --git a/common/src/main/java/us/ajg0702/queue/commands/commands/leavequeue/LeaveCommand.java b/common/src/main/java/us/ajg0702/queue/commands/commands/leavequeue/LeaveCommand.java index ae16632..336a621 100644 --- a/common/src/main/java/us/ajg0702/queue/commands/commands/leavequeue/LeaveCommand.java +++ b/common/src/main/java/us/ajg0702/queue/commands/commands/leavequeue/LeaveCommand.java @@ -49,13 +49,10 @@ public class LeaveCommand extends BaseCommand { @Override public void execute(ICommandSender sender, String[] args) { - System.out.println("leave command"); if(!sender.isPlayer()) { - System.out.println("not player"); sender.sendMessage(getMessages().getComponent("errors.player-only")); return; } - System.out.println("player"); AdaptedPlayer player = main.getPlatformMethods().senderToPlayer(sender); List servers = main.getQueueManager().getPlayerQueues(player); @@ -64,7 +61,6 @@ public class LeaveCommand extends BaseCommand { return; } - System.out.println("0"); if(servers.size() == 1) { servers.get(0).removePlayer(player); @@ -72,7 +68,6 @@ public class LeaveCommand extends BaseCommand { return; } - System.out.println("1"); if(args.length <= 0) { sender.sendMessage(getMessages().getComponent("commands.leave.more-args", "QUEUES:"+getQueueList(servers))); @@ -91,7 +86,6 @@ public class LeaveCommand extends BaseCommand { return; } - System.out.println("2"); leavingServer.removePlayer(queuePlayer); sender.sendMessage(getMessages().getComponent("commands.leave-queue", "SERVER:"+leavingServer.getAlias())); diff --git a/common/src/main/java/us/ajg0702/queue/common/EventHandlerImpl.java b/common/src/main/java/us/ajg0702/queue/common/EventHandlerImpl.java index e8615f7..b200990 100644 --- a/common/src/main/java/us/ajg0702/queue/common/EventHandlerImpl.java +++ b/common/src/main/java/us/ajg0702/queue/common/EventHandlerImpl.java @@ -1,10 +1,13 @@ package us.ajg0702.queue.common; +import com.google.common.collect.ImmutableList; import us.ajg0702.queue.api.EventHandler; import us.ajg0702.queue.api.commands.IBaseCommand; import us.ajg0702.queue.api.players.AdaptedPlayer; +import us.ajg0702.queue.api.players.QueuePlayer; import us.ajg0702.queue.api.queues.QueueServer; import us.ajg0702.queue.commands.commands.PlayerSender; +import us.ajg0702.queue.common.players.QueuePlayerImpl; import java.io.ByteArrayInputStream; import java.io.DataInputStream; @@ -30,8 +33,6 @@ public class EventHandlerImpl implements EventHandler { String[] args = new String[1]; args[0] = rawData; moveCommand.execute(new PlayerSender(recievingPlayer), args); - //man.addToQueue(player, data); - } if(subchannel.equals("massqueue")) { String inData = in.readUTF(); @@ -89,4 +90,24 @@ public class EventHandlerImpl implements EventHandler { e1.printStackTrace(); } } + + + @Override + public void onPlayerJoin(AdaptedPlayer player) { + ImmutableList queues = main.getQueueManager().findPlayerInQueues(player); + for(QueuePlayer queuePlayer : queues) { + queuePlayer.setPlayer(player); + } + if(queues.size() > 0) { + main.getQueueManager().sendMessage(main.getQueueManager().getSingleServer(player).findPlayer(player)); + } + } + + @Override + public void onPlayerLeave(AdaptedPlayer player) { + ImmutableList queues = main.getQueueManager().findPlayerInQueues(player); + for(QueuePlayer queuePlayer : queues) { + ((QueuePlayerImpl) queuePlayer).setLeaveTime(System.currentTimeMillis()); + } + } } 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 c57fc7a..4735f45 100644 --- a/common/src/main/java/us/ajg0702/queue/common/QueueMain.java +++ b/common/src/main/java/us/ajg0702/queue/common/QueueMain.java @@ -96,6 +96,11 @@ public class QueueMain { } + public void shutdown() { + taskManager.shutdown(); + } + + private final File dataFolder; diff --git a/common/src/main/java/us/ajg0702/queue/common/QueueManagerImpl.java b/common/src/main/java/us/ajg0702/queue/common/QueueManagerImpl.java index 5a9b80d..a315f55 100644 --- a/common/src/main/java/us/ajg0702/queue/common/QueueManagerImpl.java +++ b/common/src/main/java/us/ajg0702/queue/common/QueueManagerImpl.java @@ -8,6 +8,7 @@ import us.ajg0702.queue.api.queues.QueueServer; import us.ajg0702.queue.api.server.AdaptedServer; import us.ajg0702.queue.api.server.ServerBuilder; import us.ajg0702.queue.common.players.QueuePlayerImpl; +import us.ajg0702.utils.bungee.BungeeUtils; import us.ajg0702.utils.common.Messages; import us.ajg0702.utils.common.TimeUtils; @@ -131,7 +132,11 @@ public class QueueManagerImpl implements QueueManager { if(!server.isJoinable(player)) { sendMessage(queuePlayer); } - main.getPlatformMethods().sendJoinQueueChannelMessages(server, queuePlayer); + main.getPlatformMethods().sendPluginMessage(player, "position", pos+""); + main.getPlatformMethods().sendPluginMessage(player, "positionof", len+""); + main.getPlatformMethods().sendPluginMessage(player, "queuename", server.getAlias()); + main.getPlatformMethods().sendPluginMessage(player, "inqueue", "true"); + main.getPlatformMethods().sendPluginMessage(player, "inqueueevent", "true"); return true; } @@ -232,7 +237,6 @@ public class QueueManagerImpl implements QueueManager { for(QueueServer server : servers) { String status = server.getStatusString(); for(QueuePlayer queuePlayer : server.getQueue()) { - if(!getSingleServer(queuePlayer.getPlayer()).equals(server)) continue; int pos = queuePlayer.getPosition(); if(pos == 0) { @@ -243,6 +247,8 @@ public class QueueManagerImpl implements QueueManager { AdaptedPlayer player = queuePlayer.getPlayer(); if(player == null) continue; + if(!getSingleServer(player).equals(server)) continue; + if(!server.isJoinable(player)) { queuePlayer.getPlayer().sendActionBar(msgs.getComponent("spigot.actionbar.offline", "POS:"+pos, diff --git a/common/src/main/java/us/ajg0702/queue/common/TaskManager.java b/common/src/main/java/us/ajg0702/queue/common/TaskManager.java index 2af0064..04bc2b9 100644 --- a/common/src/main/java/us/ajg0702/queue/common/TaskManager.java +++ b/common/src/main/java/us/ajg0702/queue/common/TaskManager.java @@ -9,6 +9,7 @@ import java.util.concurrent.TimeUnit; public class TaskManager { + final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); final ScheduledExecutorService updateExecutor = Executors.newScheduledThreadPool(1); @@ -17,6 +18,11 @@ public class TaskManager { this.main = main; } + public void shutdown() { + executor.shutdown(); + updateExecutor.shutdown(); + } + public String taskStatus() { List> tasks = Arrays.asList(sendTask, updateTask, messageTask, actionBarTask, queueEventTask, reloadServerTask); StringBuilder sb = new StringBuilder(); diff --git a/common/src/main/java/us/ajg0702/queue/common/players/QueuePlayerImpl.java b/common/src/main/java/us/ajg0702/queue/common/players/QueuePlayerImpl.java index 723e4d1..756b356 100644 --- a/common/src/main/java/us/ajg0702/queue/common/players/QueuePlayerImpl.java +++ b/common/src/main/java/us/ajg0702/queue/common/players/QueuePlayerImpl.java @@ -47,7 +47,7 @@ public class QueuePlayerImpl implements QueuePlayer { @Nullable @Override public AdaptedPlayer getPlayer() { - if(!player.isConnected()) return null; + if(player != null && !player.isConnected()) player = null; return player; } @@ -73,4 +73,20 @@ public class QueuePlayerImpl implements QueuePlayer { public String getName() { return name; } + + + + @Override + public long getTimeSinceOnline() { + if(player != null && player.isConnected()) { + return 0; + } + return System.currentTimeMillis()-leaveTime; + } + + + private long leaveTime = 0; + public void setLeaveTime(long leaveTime) { + this.leaveTime = leaveTime; + } } diff --git a/common/src/main/java/us/ajg0702/queue/common/queues/QueueServerImpl.java b/common/src/main/java/us/ajg0702/queue/common/queues/QueueServerImpl.java index d018ba4..c457c2d 100644 --- a/common/src/main/java/us/ajg0702/queue/common/queues/QueueServerImpl.java +++ b/common/src/main/java/us/ajg0702/queue/common/queues/QueueServerImpl.java @@ -304,18 +304,13 @@ public class QueueServerImpl implements QueueServer { } @Override - public synchronized QueuePlayer findPlayer(AdaptedPlayer player) { + public QueuePlayer findPlayer(AdaptedPlayer player) { + return findPlayer(player.getUniqueId()); + } + @Override + public synchronized QueuePlayer findPlayer(UUID uuid) { for(QueuePlayer queuePlayer : queue) { - AdaptedPlayer queuedPlayer = queuePlayer.getPlayer(); - if(queuedPlayer == null) continue; - if( - queuedPlayer - .getUniqueId() - .equals( - player - .getUniqueId() - ) - ) { + if(queuePlayer.getUniqueId().toString().equals(uuid.toString())) { return queuePlayer; } } diff --git a/common/src/main/java/us/ajg0702/queue/logic/FreeLogic.java b/common/src/main/java/us/ajg0702/queue/logic/FreeLogic.java index 247b742..bf73f3b 100644 --- a/common/src/main/java/us/ajg0702/queue/logic/FreeLogic.java +++ b/common/src/main/java/us/ajg0702/queue/logic/FreeLogic.java @@ -15,4 +15,9 @@ public class FreeLogic implements Logic { public QueuePlayer priorityLogic(QueueServer server, AdaptedPlayer player) { return null; } + + @Override + public boolean playerDisconnectedTooLong(QueuePlayer player) { + return true; + } } diff --git a/platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/PlatformMethodsImpl.java b/platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/VelocityMethods.java similarity index 89% rename from platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/PlatformMethodsImpl.java rename to platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/VelocityMethods.java index dee0256..bf4bc7e 100644 --- a/platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/PlatformMethodsImpl.java +++ b/platforms/velocity/src/main/java/us/ajg0702/queue/platforms/velocity/VelocityMethods.java @@ -15,6 +15,7 @@ import us.ajg0702.queue.api.commands.ICommandSender; import us.ajg0702.queue.api.players.AdaptedPlayer; import us.ajg0702.queue.api.players.QueuePlayer; import us.ajg0702.queue.api.queues.QueueServer; +import us.ajg0702.queue.commands.commands.PlayerSender; import us.ajg0702.queue.platforms.velocity.players.VelocityPlayer; import java.util.ArrayList; @@ -23,25 +24,18 @@ import java.util.Locale; import java.util.Optional; import java.util.logging.Logger; -public class PlatformMethodsImpl implements PlatformMethods { +public class VelocityMethods implements PlatformMethods { final ProxyServer proxyServer; final Logger logger; final VelocityQueue plugin; - public PlatformMethodsImpl(VelocityQueue plugin, ProxyServer proxyServer, Logger logger) { + public VelocityMethods(VelocityQueue plugin, ProxyServer proxyServer, Logger logger) { this.proxyServer = proxyServer; this.logger = logger; this.plugin = plugin; } - @Override - public void sendJoinQueueChannelMessages(QueueServer queueServer, QueuePlayer queuePlayer) { - AdaptedPlayer player = queuePlayer.getPlayer(); - if(player == null) return; - player.sendMessage(Component.text()); - } - @Override public void sendPluginMessage(AdaptedPlayer player, String channel, String... data) { if(player == null) return; @@ -58,6 +52,9 @@ public class PlatformMethodsImpl implements PlatformMethods { @Override public AdaptedPlayer senderToPlayer(ICommandSender sender) { + if(sender instanceof PlayerSender) { + return ((PlayerSender) sender).getHandle(); + } return new VelocityPlayer((Player) sender.getHandle()); } 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 1e01ea7..9caf4c0 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 @@ -2,14 +2,18 @@ package us.ajg0702.queue.platforms.velocity; import com.google.inject.Inject; import com.velocitypowered.api.command.CommandManager; -import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.DisconnectEvent; import com.velocitypowered.api.event.connection.PluginMessageEvent; +import com.velocitypowered.api.event.player.ServerPostConnectEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; +import net.kyori.adventure.text.Component; import us.ajg0702.queue.api.commands.IBaseCommand; import us.ajg0702.queue.commands.BaseCommand; import us.ajg0702.queue.commands.commands.leavequeue.LeaveCommand; @@ -58,7 +62,7 @@ public class VelocityQueue { public void onProxyInit(ProxyInitializeEvent e) { main = new QueueMain( logger, - new PlatformMethodsImpl(this, proxyServer, logger), + new VelocityMethods(this, proxyServer, logger), dataFolder ); main.setServerBuilder(new ServerBuilderImpl(main, proxyServer)); @@ -73,6 +77,10 @@ public class VelocityQueue { CommandManager commandManager = proxyServer.getCommandManager(); + proxyServer.getChannelRegistrar().register(MinecraftChannelIdentifier.from("ajqueue:tospigot")); + proxyServer.getChannelRegistrar().register(MinecraftChannelIdentifier.from("ajqueue:toproxy")); + + for(IBaseCommand command : commands) { commandManager.register( commandManager.metaBuilder(command.getName()) @@ -83,18 +91,41 @@ public class VelocityQueue { } } + @Subscribe + public void onProxyShutdown(ProxyShutdownEvent e) { + main.shutdown(); + } + @Subscribe public void onPluginMessage(PluginMessageEvent e) { - System.out.println("Recieved message: "+e.getIdentifier().getId()); + if(e.getIdentifier().getId().equals("ajqueue:tospigot")) { e.setResult(PluginMessageEvent.ForwardResult.handled()); + System.out.println("Skipping message: "+e.getIdentifier().getId()); + return; + } + if(!e.getIdentifier().getId().equals("ajqueue:toproxy")) { + System.out.println("Skipping message: "+e.getIdentifier().getId()); return; } - if(!e.getIdentifier().getId().equals("ajqueue:toproxy")) return; e.setResult(PluginMessageEvent.ForwardResult.handled()); + System.out.println("Processing message: "+e.getIdentifier().getId()); + if(!(e.getTarget() instanceof Player)) return; main.getEventHandler().handleMessage(new VelocityPlayer((Player) e.getTarget()), e.getData()); } + + @SuppressWarnings("UnstableApiUsage") + @Subscribe + public void onJoin(ServerPostConnectEvent e) { + if(e.getPreviousServer() != null) return; // only run if the player just joined + main.getEventHandler().onPlayerJoin(new VelocityPlayer(e.getPlayer())); + } + + @Subscribe + public void onLeave(DisconnectEvent e) { + main.getEventHandler().onPlayerLeave(new VelocityPlayer(e.getPlayer())); + } } diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/Main.java b/spigot/src/main/java/us/ajg0702/queue/spigot/Main.java index b753a86..9f41511 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/Main.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/Main.java @@ -10,6 +10,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.messaging.PluginMessageListener; +import org.jetbrains.annotations.NotNull; import us.ajg0702.queue.spigot.utils.VersionSupport; import java.util.HashMap; @@ -23,7 +24,7 @@ public class Main extends JavaPlugin implements PluginMessageListener,Listener { public void onEnable() { getServer().getMessenger().registerIncomingPluginChannel(this, "ajqueue:tospigot", this); - getServer().getMessenger().registerOutgoingPluginChannel(this, "ajqueue:tobungee"); + getServer().getMessenger().registerOutgoingPluginChannel(this, "ajqueue:toproxy"); this.getCommand("move").setExecutor(new Commands(this)); this.getCommand("leavequeue").setExecutor(new Commands(this)); @@ -62,8 +63,12 @@ public class Main extends JavaPlugin implements PluginMessageListener,Listener { HashMap queuebatch = new HashMap<>(); @Override - public void onPluginMessageReceived(String channel, Player player, byte[] message) { - if (!channel.equals("ajqueue:tospigot")) return; + public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + if (!channel.equals("ajqueue:tospigot")) { + getLogger().info("Skipping message: "+channel); + return; + } + getLogger().info("Processing message: "+channel); ByteArrayDataInput in = ByteStreams.newDataInput(message); @@ -161,7 +166,7 @@ public class Main extends JavaPlugin implements PluginMessageListener,Listener { out.writeUTF(subchannel); out.writeUTF(data); - player.sendPluginMessage(this, "ajqueue:tobungee", out.toByteArray()); + player.sendPluginMessage(this, "ajqueue:toproxy", out.toByteArray()); } public void sendMessage(String subchannel, String data) { @@ -170,7 +175,7 @@ public class Main extends JavaPlugin implements PluginMessageListener,Listener { out.writeUTF(data); Bukkit.getOnlinePlayers().iterator().next() - .sendPluginMessage(this, "ajqueue:tobungee", out.toByteArray()); + .sendPluginMessage(this, "ajqueue:toproxy", out.toByteArray()); } @EventHandler