the basics are working on velocity now

This commit is contained in:
ajgeiss0702
2021-07-12 14:14:34 -07:00
parent 80f9eb262d
commit 6f24594945
15 changed files with 457 additions and 25 deletions
@@ -0,0 +1,91 @@
package us.ajg0702.queue.commands.commands.listqueues;
import com.google.common.collect.ImmutableList;
import net.kyori.adventure.text.Component;
import us.ajg0702.queue.api.commands.ICommandSender;
import us.ajg0702.queue.api.commands.ISubCommand;
import us.ajg0702.queue.api.players.AdaptedPlayer;
import us.ajg0702.queue.api.queues.QueueServer;
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.Arrays;
import java.util.List;
public class ListCommand extends BaseCommand {
private final QueueMain main;
public ListCommand(QueueMain main) {
this.main = main;
}
@Override
public String getName() {
return "listqueues";
}
@Override
public ImmutableList<String> getAliases() {
return ImmutableList.of("listq");
}
@Override
public ImmutableList<ISubCommand> getSubCommands() {
return ImmutableList.<ISubCommand>builder().build();
}
@Override
public String getPermission() {
return "ajqueue.listqueues";
}
@Override
public Messages getMessages() {
return main.getMessages();
}
@Override
public void addSubCommand(ISubCommand subCommand) {
}
@Override
public void execute(ICommandSender sender, String[] args) {
if(!checkPermission(sender)) return;
AdaptedPlayer spp = null;
if(sender.isPlayer()) {
spp = main.getPlatformMethods().senderToPlayer(sender);
}
Component m = main.getMessages().getComponent("commands.listqueues.header");
boolean none = true;
for(QueueServer s : main.getQueueManager().getServers()) {
none = false;
String color = "&a";
if(!s.isOnline()) {
color = "&c";
} else if(!s.isJoinable(spp)) {
color = "&e";
}
m = m.append(Component.text("\n"));
m = m.append(main.getMessages().getComponent("commands.listqueues.format",
"COLOR:" + main.getMessages().color(color),
"NAME:" + s.getName(),
"COUNT:" + s.getQueue().size(),
"STATUS:" + s.getStatusString(spp)
));
}
sender.sendMessage(m);
}
@Override
public List<String> autoComplete(ICommandSender sender, String[] args) {
return new ArrayList<>();
}
}
@@ -1,6 +1,7 @@
package us.ajg0702.queue.commands.commands.manage;
import com.google.common.collect.ImmutableList;
import net.kyori.adventure.text.Component;
import us.ajg0702.queue.api.commands.ICommandSender;
import us.ajg0702.queue.api.commands.ISubCommand;
import us.ajg0702.queue.commands.BaseCommand;
@@ -9,7 +10,9 @@ import us.ajg0702.queue.common.QueueMain;
import us.ajg0702.utils.common.Messages;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class ManageCommand extends BaseCommand {
@@ -18,7 +21,8 @@ public class ManageCommand extends BaseCommand {
public ManageCommand(QueueMain main) {
this.main = main;
addSubCommand(new Reload(main));
addSubCommand(new Tasks(main));
}
@@ -56,11 +60,31 @@ public class ManageCommand extends BaseCommand {
@Override
public void execute(ICommandSender sender, String[] args) {
if(args.length > 0) {
for(ISubCommand subCommand : subCommands) {
if(args[0].equalsIgnoreCase(subCommand.getName()) || subCommand.getAliases().contains(args[0].toLowerCase(Locale.ROOT))) {
subCommand.execute(sender, Arrays.copyOfRange(args, 1, args.length));
return;
}
}
}
sender.sendMessage(Component.text("/ajQueue <reload|list|send|pause>"));
}
@Override
public List<String> autoComplete(ICommandSender sender, String[] args) {
return null;
if(args.length > 0) {
for(ISubCommand subCommand : subCommands) {
if(args[0].equalsIgnoreCase(subCommand.getName()) || subCommand.getAliases().contains(args[0].toLowerCase(Locale.ROOT))) {
return subCommand.autoComplete(sender, Arrays.copyOfRange(args, 1, args.length));
}
}
}
List<String> commands = new ArrayList<>();
for(ISubCommand subCommand : subCommands) {
commands.add(subCommand.getName());
commands.addAll(subCommand.getAliases());
}
return commands;
}
}
@@ -0,0 +1,70 @@
package us.ajg0702.queue.commands.commands.manage;
import com.google.common.collect.ImmutableList;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.spongepowered.configurate.ConfigurateException;
import us.ajg0702.queue.api.commands.ICommandSender;
import us.ajg0702.queue.api.commands.ISubCommand;
import us.ajg0702.queue.commands.SubCommand;
import us.ajg0702.queue.common.QueueMain;
import us.ajg0702.utils.common.Messages;
import java.util.ArrayList;
import java.util.List;
public class Reload extends SubCommand {
QueueMain main;
public Reload(QueueMain main) {
this.main = main;
}
@Override
public String getName() {
return "reload";
}
@Override
public ImmutableList<String> getAliases() {
return ImmutableList.of();
}
@Override
public String getPermission() {
return "ajqueue.reload";
}
@Override
public Messages getMessages() {
return main.getMessages();
}
@Override
public void addSubCommand(ISubCommand subCommand) {
}
@Override
public void execute(ICommandSender sender, String[] args) {
if(!checkPermission(sender)) return;
main.getMessages().reload();
try {
main.getConfig().reload();
} catch (ConfigurateException e) {
sender.sendMessage(Component.text("An error occurred while reloading. Check the console").color(NamedTextColor.RED));
e.printStackTrace();
return;
}
main.setTimeBetweenPlayers();
main.getTaskManager().rescheduleTasks();
main.getQueueManager().reloadServers();
sender.sendMessage(getMessages().getComponent("commands.reload"));
}
@Override
public List<String> autoComplete(ICommandSender sender, String[] args) {
return new ArrayList<>();
}
}
@@ -0,0 +1,58 @@
package us.ajg0702.queue.commands.commands.manage;
import com.google.common.collect.ImmutableList;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.spongepowered.configurate.ConfigurateException;
import us.ajg0702.queue.api.commands.ICommandSender;
import us.ajg0702.queue.api.commands.ISubCommand;
import us.ajg0702.queue.commands.SubCommand;
import us.ajg0702.queue.common.QueueMain;
import us.ajg0702.utils.common.Messages;
import java.util.ArrayList;
import java.util.List;
public class Tasks extends SubCommand {
QueueMain main;
public Tasks(QueueMain main) {
this.main = main;
}
@Override
public String getName() {
return "tasks";
}
@Override
public ImmutableList<String> getAliases() {
return ImmutableList.of();
}
@Override
public String getPermission() {
return "ajqueue.tasks";
}
@Override
public Messages getMessages() {
return main.getMessages();
}
@Override
public void addSubCommand(ISubCommand subCommand) {
}
@Override
public void execute(ICommandSender sender, String[] args) {
if(!checkPermission(sender)) return;
sender.sendMessage(Component.text(main.getTaskManager().taskStatus()));
}
@Override
public List<String> autoComplete(ICommandSender sender, String[] args) {
return new ArrayList<>();
}
}
@@ -9,6 +9,7 @@ import us.ajg0702.queue.common.QueueMain;
import us.ajg0702.utils.common.Messages;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class QueueCommand extends BaseCommand {
@@ -26,7 +27,11 @@ public class QueueCommand extends BaseCommand {
@Override
public ImmutableList<String> getAliases() {
return ImmutableList.of("move", "server", "joinqueue", "joinq");
List<String> aliases = new ArrayList<>(Arrays.asList("move", "joinqueue", "joinq"));
if(main.getConfig().getBoolean("enable-server-command")) {
aliases.add("server");
}
return ImmutableList.copyOf(aliases);
}
@Override
@@ -14,7 +14,7 @@ import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.*;
import java.util.logging.Logger;
public class QueueMain {
@@ -24,6 +24,9 @@ public class QueueMain {
public double getTimeBetweenPlayers() {
return timeBetweenPlayers;
}
public void setTimeBetweenPlayers() {
this.timeBetweenPlayers = config.getDouble("wait-time");
}
private Config config;
public Config getConfig() {
@@ -59,6 +62,11 @@ public class QueueMain {
return logger;
}
private final TaskManager taskManager = new TaskManager(this);
public TaskManager getTaskManager() {
return taskManager;
}
private List<CompletableFuture<ServerBuilder>> serverCompletableFutures = new ArrayList<>();
private ServerBuilder serverBuilder;
public ServerBuilder getServerBuilder() {
@@ -104,14 +112,14 @@ public class QueueMain {
return;
}
timeBetweenPlayers = config.getDouble("wait-time");
setTimeBetweenPlayers();
queueManager = new QueueManagerImpl(this);
logic = new LogicGetter().constructLogic();
aliasManager = new LogicGetter().constructAliasManager(config);
taskManager.rescheduleTasks();
}
@@ -19,7 +19,7 @@ import java.util.concurrent.ExecutionException;
public class QueueManagerImpl implements QueueManager {
private List<QueueServer> servers;
private List<QueueServer> servers = new ArrayList<>();
private final QueueMain main;
private final Messages msgs;
@@ -130,6 +130,9 @@ public class QueueManagerImpl implements QueueManager {
);
}
if(!server.isJoinable(player)) {
sendMessage(queuePlayer);
}
main.getPlatformMethods().sendJoinQueueChannelMessages(server, queuePlayer);
return true;
}
@@ -272,10 +275,15 @@ public class QueueManagerImpl implements QueueManager {
@Override
public void sendMessages() {
for(QueueServer server : servers) {
for(QueuePlayer queuePlayer : server.getQueue()) {
sendMessage(queuePlayer);
if(servers == null) return;
try {
for(QueueServer server : servers) {
for(QueuePlayer queuePlayer : server.getQueue()) {
sendMessage(queuePlayer);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@@ -289,7 +297,7 @@ public class QueueManagerImpl implements QueueManager {
int pos = queuePlayer.getPosition();
int len = server.getQueue().size();
if(server.isJoinable(player)) {
if(!server.isJoinable(player)) {
String status = server.getStatusString(player);
if(msgs.getString("status.offline.base").isEmpty()) return;
@@ -312,6 +320,17 @@ public class QueueManagerImpl implements QueueManager {
}
}
@Override
public void updateServers() {
try {
for(QueueServer server : servers) {
server.updatePing();
}
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public QueueServer findServer(String name) {
for(QueueServer server : servers) {
@@ -360,22 +379,30 @@ public class QueueManagerImpl implements QueueManager {
player.sendMessage(msgs.getComponent("status.sending-now", "SERVER:"+server.getAlias()));
player.connect(selected);
}
return;
continue;
}
QueuePlayer nextQueuePlayer = server.getQueue().get(0);
AdaptedPlayer nextPlayer = nextQueuePlayer.getPlayer();
// If the first person int the queue is offline or already in the server, find the next online player in the queue
int i = 0;
while((nextPlayer == null || server.getServerNames().contains(nextPlayer.getServerName())) && i < server.getQueue().size()) {
if(nextPlayer != null) { // Remove them if they are already in the server
server.removePlayer(nextQueuePlayer);
if(server.getQueue().size() > i) {
nextQueuePlayer = server.getQueue().get(i);
nextPlayer = nextQueuePlayer.getPlayer();
} else {
nextPlayer = null;
break;
}
} else {
i++;
nextQueuePlayer = server.getQueue().get(i);
nextPlayer = nextQueuePlayer.getPlayer();
}
nextQueuePlayer = server.getQueue().get(i);
nextPlayer = nextQueuePlayer.getPlayer();
}
if(nextPlayer == null) continue; // None of the players in the queue are online
@@ -388,7 +415,6 @@ public class QueueManagerImpl implements QueueManager {
if(server.isPaused() && !nextPlayer.hasPermission("ajqueue.bypasspaused")) continue;
} else if(server.isPaused()) { continue; }
int tries = sendingAttempts.get(nextQueuePlayer) == null ? 0 : sendingAttempts.get(nextQueuePlayer);
int maxTries = main.getConfig().getInt("max-tries");
if(tries >= maxTries && maxTries > 0) {
@@ -0,0 +1,123 @@
package us.ajg0702.queue.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class TaskManager {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
ScheduledExecutorService updateExecutor = Executors.newScheduledThreadPool(1);
QueueMain main;
public TaskManager(QueueMain main) {
this.main = main;
}
public String taskStatus() {
List<ScheduledFuture<?>> tasks = Arrays.asList(sendTask, updateTask, messageTask, actionBarTask, queueEventTask, reloadServerTask);
StringBuilder sb = new StringBuilder();
for(ScheduledFuture<?> task : tasks) {
sb.append(task == null ? "null" : task.isDone() ? "canceled/done" : "running");
sb.append("\n");
}
return sb.toString();
}
ScheduledFuture<?> sendTask;
ScheduledFuture<?> updateTask;
ScheduledFuture<?> messageTask;
ScheduledFuture<?> actionBarTask;
ScheduledFuture<?> queueEventTask;
ScheduledFuture<?> reloadServerTask;
public void rescheduleTasks() {
cancelTasks();
sendTask = scheduleAtFixedRate(
main.getQueueManager()::sendPlayers,
0L,
(long) (main.getConfig().getDouble("wait-time")*1000L),
TimeUnit.MILLISECONDS
);
updateTask = scheduleAtFixedRate(updateExecutor,
main.getQueueManager()::updateServers,
0L,
(long) (Math.max(main.getTimeBetweenPlayers(), 2)*1000L),
TimeUnit.MILLISECONDS
);
messageTask = scheduleAtFixedRate(
main.getQueueManager()::sendMessages,
0L,
main.getConfig().getInt("message-time"),
TimeUnit.SECONDS
);
actionBarTask = scheduleAtFixedRate(
main.getQueueManager()::sendActionBars,
0L,
2L,
TimeUnit.SECONDS
);
queueEventTask = scheduleAtFixedRate(
main.getQueueManager()::sendQueueEvents,
0L,
2L,
TimeUnit.SECONDS
);
if(main.getConfig().getInt("reload-servers-interval") > 0) {
reloadServerTask = scheduleAtFixedRate(
main.getQueueManager()::reloadServers,
0L,
main.getConfig().getInt("reload-servers-interval"),
TimeUnit.SECONDS
);
}
}
public void cancelTasks() {
if(sendTask != null && !sendTask.isCancelled()) {
sendTask.cancel(false);
}
if(updateTask != null && !updateTask.isCancelled()) {
updateTask.cancel(false);
}
if(messageTask != null && !messageTask.isCancelled()) {
messageTask.cancel(false);
}
if(actionBarTask != null && !actionBarTask.isCancelled()) {
actionBarTask.cancel(false);
}
if(queueEventTask != null && !queueEventTask.isCancelled()) {
queueEventTask.cancel(false);
}
if(reloadServerTask != null && !reloadServerTask.isCancelled()) {
reloadServerTask.cancel(false);
reloadServerTask = null;
}
}
private ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
return scheduleAtFixedRate(executor, command, initialDelay, period, unit);
}
private ScheduledFuture<?> scheduleAtFixedRate(ScheduledExecutorService executor, Runnable command, long initialDelay, long period, TimeUnit unit) {
return executor.scheduleAtFixedRate(() -> {
try {
command.run();
} catch (Exception e) {
System.out.println("An error ocurred while running an ajQueue task");
e.printStackTrace();
}
}, initialDelay, period, unit);
}
}
@@ -28,6 +28,7 @@ public class QueuePlayerImpl implements QueuePlayer {
@Override
public UUID getUniqueId() {
if(uuid == null) throw new IllegalStateException("Why is my UUID null??");
return uuid;
}
@@ -117,9 +117,13 @@ public class QueueServerImpl implements QueueServer {
ping = futurePing.get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
if(main.getConfig().getBoolean("pinger-debug")) {
main.getLogger().info("[pinger] ["+server.getServerInfo().getName()+"] sending ping");
main.getLogger().info("[pinger] ["+server.getServerInfo().getName()+"] offline:");
e.printStackTrace();
}
}
if(ping != null && main.getConfig().getBoolean("pinger-debug")) {
main.getLogger().info("[pinger] ["+server.getServerInfo().getName()+"] online. motd: "+ping.getPlainDescription()+" players: "+ping.getPlayerCount()+"/"+ping.getMaxPlayers());
}
pings.put(server, ping);
i++;
@@ -297,11 +301,18 @@ public class QueueServerImpl implements QueueServer {
}
@Override
public QueuePlayer findPlayer(AdaptedPlayer player) {
public synchronized QueuePlayer findPlayer(AdaptedPlayer player) {
for(QueuePlayer queuePlayer : queue) {
AdaptedPlayer queuedPlayer = queuePlayer.getPlayer();
if(queuedPlayer == null) continue;
if(queuedPlayer.getUniqueId().equals(player.getUniqueId())) {
if(
queuedPlayer
.getUniqueId()
.equals(
player
.getUniqueId()
)
) {
return queuePlayer;
}
}