From b51a964033e2080bf00dc4637eb16031572a0c16 Mon Sep 17 00:00:00 2001 From: Jerry Date: Mon, 20 Mar 2023 21:05:24 +0800 Subject: [PATCH] u --- pom.xml | 16 +++- src/main/java/de/strifel/VTools/VTools.java | 22 +++--- .../VTools/commands/CommandBroadcast.java | 4 +- .../strifel/VTools/commands/CommandFind.java | 13 ++-- .../VTools/commands/CommandRestart.java | 6 +- .../strifel/VTools/commands/CommandSend.java | 75 ++++++++++++------- .../VTools/commands/CommandServers.java | 4 +- .../VTools/commands/CommandStaffChat.java | 6 +- .../de/strifel/VTools/commands/CommandTp.java | 12 +-- velocity-plugin.json | 8 ++ 10 files changed, 100 insertions(+), 66 deletions(-) create mode 100644 velocity-plugin.json diff --git a/pom.xml b/pom.xml index 5319d98..5d49247 100644 --- a/pom.xml +++ b/pom.xml @@ -13,11 +13,21 @@ org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + 17 + 17 + + + . + true + . + + velocity-plugin.json + + + @@ -31,7 +41,7 @@ com.velocitypowered velocity-api - 3.1.0 + 4.0.0-SNAPSHOT provided diff --git a/src/main/java/de/strifel/VTools/VTools.java b/src/main/java/de/strifel/VTools/VTools.java index 3b02e7b..85c9e42 100644 --- a/src/main/java/de/strifel/VTools/VTools.java +++ b/src/main/java/de/strifel/VTools/VTools.java @@ -1,7 +1,7 @@ package de.strifel.VTools; import com.velocitypowered.api.event.Subscribe; -import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.event.lifecycle.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.proxy.ProxyServer; import de.strifel.VTools.commands.*; @@ -10,12 +10,12 @@ import org.slf4j.Logger; import javax.inject.Inject; -@Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!") +@Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!", authors="unnamed") public class VTools { private final ProxyServer server; - public static final TextColor COLOR_RED = TextColor.fromCSSHexString("FF5555"); - public static final TextColor COLOR_YELLOW = TextColor.fromCSSHexString("FFFF55"); + public static final TextColor COLOR_RED = TextColor.fromCSSHexString("#FF5555"); + public static final TextColor COLOR_YELLOW = TextColor.fromCSSHexString("#FFFF55"); @Inject public VTools(ProxyServer server, Logger logger) { @@ -25,13 +25,13 @@ public class VTools { @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { - server.getCommandManager().register("send", new CommandSend(server)); - server.getCommandManager().register("broadcast", new CommandBroadcast(server), "bc", "alert"); - server.getCommandManager().register("find", new CommandFind(server), "search"); - server.getCommandManager().register("staffchat", new CommandStaffChat(server), "sc"); - server.getCommandManager().register("restart", new CommandRestart(server)); - server.getCommandManager().register("tps", new CommandTp(server), "jump"); - server.getCommandManager().register("servers", new CommandServers(server), "allservers"); + server.commandManager().register("send", new CommandSend(server)); + server.commandManager().register("broadcast", new CommandBroadcast(server), "bc", "alert"); + server.commandManager().register("find", new CommandFind(server), "search"); + server.commandManager().register("staffchat", new CommandStaffChat(server), "sc"); + server.commandManager().register("restart", new CommandRestart(server)); + server.commandManager().register("tps", new CommandTp(server), "jump"); + server.commandManager().register("servers", new CommandServers(server), "allservers"); } } diff --git a/src/main/java/de/strifel/VTools/commands/CommandBroadcast.java b/src/main/java/de/strifel/VTools/commands/CommandBroadcast.java index 8945c81..27e6b35 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandBroadcast.java +++ b/src/main/java/de/strifel/VTools/commands/CommandBroadcast.java @@ -2,7 +2,7 @@ package de.strifel.VTools.commands; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.ProxyServer; import net.kyori.adventure.text.Component; @@ -25,7 +25,7 @@ public class CommandBroadcast implements SimpleCommand { if (strings.length > 0) { String message = String.join(" ", strings).replace("&", "§"); - for (Player player : server.getAllPlayers()) { + for (Player player : server.connectedPlayers()) { player.sendMessage(Component.text(message)); } } else { diff --git a/src/main/java/de/strifel/VTools/commands/CommandFind.java b/src/main/java/de/strifel/VTools/commands/CommandFind.java index b360a91..7fea6c4 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandFind.java +++ b/src/main/java/de/strifel/VTools/commands/CommandFind.java @@ -2,13 +2,12 @@ package de.strifel.VTools.commands; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.ProxyServer; import net.kyori.adventure.text.Component; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import static de.strifel.VTools.VTools.COLOR_RED; import static de.strifel.VTools.VTools.COLOR_YELLOW; @@ -27,9 +26,9 @@ public class CommandFind implements SimpleCommand { String[] strings = invocation.arguments(); if (strings.length == 1) { - Optional player = server.getPlayer(strings[0]); - if (player.isPresent() && player.get().getCurrentServer().isPresent()) { - commandSource.sendMessage(Component.text("Player " + strings[0] + " is on " + player.get().getCurrentServer().get().getServerInfo().getName() + "!").color(COLOR_YELLOW)); + Player player = server.player(strings[0]); + if (player != null && player.connectedServer() != null) { + commandSource.sendMessage(Component.text("Player " + strings[0] + " is on " + player.connectedServer().serverInfo().name() + "!").color(COLOR_YELLOW)); } else { commandSource.sendMessage(Component.text("The player is not online!").color(COLOR_YELLOW)); } @@ -44,8 +43,8 @@ public class CommandFind implements SimpleCommand { List arg = new ArrayList<>(); if (currentArgs.length <= 1 && invocation.source().hasPermission("vtools.find.autocomplete")) { - for (Player player : server.getAllPlayers()) { - arg.add(player.getUsername()); + for (Player player : server.connectedPlayers()) { + arg.add(player.username()); } } return arg; diff --git a/src/main/java/de/strifel/VTools/commands/CommandRestart.java b/src/main/java/de/strifel/VTools/commands/CommandRestart.java index a5f85c2..4e51eb2 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandRestart.java +++ b/src/main/java/de/strifel/VTools/commands/CommandRestart.java @@ -2,7 +2,7 @@ package de.strifel.VTools.commands; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.ProxyServer; import net.kyori.adventure.text.Component; @@ -24,11 +24,11 @@ public class CommandRestart implements SimpleCommand { if (strings.length > 0) { String message = String.join(" ", strings).replace("&", "§"); - for (Player player : server.getAllPlayers()) { + for (Player player : server.connectedPlayers()) { player.disconnect(Component.text(message)); } } - server.getCommandManager().executeAsync(server.getConsoleCommandSource(), "shutdown"); + server.commandManager().execute(server.consoleCommandSource(), "shutdown"); } @Override diff --git a/src/main/java/de/strifel/VTools/commands/CommandSend.java b/src/main/java/de/strifel/VTools/commands/CommandSend.java index b61bd0a..c8c3dac 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandSend.java +++ b/src/main/java/de/strifel/VTools/commands/CommandSend.java @@ -2,15 +2,18 @@ package de.strifel.VTools.commands; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.player.ConnectionRequestBuilder; +import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.ProxyServer; -import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.server.RegisteredServer; import net.kyori.adventure.text.Component; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; import static de.strifel.VTools.VTools.COLOR_RED; import static de.strifel.VTools.VTools.COLOR_YELLOW; @@ -30,19 +33,12 @@ public class CommandSend implements SimpleCommand { if (strings.length == 2) { List oPlayer = new ArrayList<>(); if (strings[0].equals("all")) { - for (Player player : server.getAllPlayers()) { - oPlayer.add(player); - } + oPlayer.addAll(server.connectedPlayers()); } else if (strings[0].equals("current")) { if (commandSource instanceof Player) { Player playerSource = (Player)commandSource; - Optional conn = playerSource.getCurrentServer(); - if (conn.isPresent()) { - for (Player player : conn.get().getServer().getPlayersConnected()) { - oPlayer.add(player); - } - } + oPlayer.addAll(playerSource.connectedServer().target().connectedPlayers()); } else { commandSource.sendMessage(Component.text("Command is only for players.").color(COLOR_RED)); @@ -50,26 +46,47 @@ public class CommandSend implements SimpleCommand { } } else { - Optional p = server.getPlayer(strings[0]); - if (p.isPresent()) { - oPlayer.add(p.get()); + Player p = server.player(strings[0]); + if (p != null) { + oPlayer.add(p); } } - Optional oServer = server.getServer(strings[1]); - if (!oPlayer.isEmpty() && oServer.isPresent()) { - RegisteredServer server = oServer.get(); + HashMap> futures = new HashMap<>(); + RegisteredServer server = this.server.server(strings[1]); + if (!oPlayer.isEmpty() && server != null) { for (Player player : oPlayer) { - player.createConnectionRequest(server).connect(); - if (oPlayer.size() <= 1) { - commandSource.sendMessage(Component.text("You send " + player.getUsername() + " to " + server.getServerInfo().getName()).color(COLOR_YELLOW)); - } - if (player.hasPermission("vtools.send.recvmsg")) { - commandSource.sendMessage(Component.text("You got send to " + server.getServerInfo().getName()).color(COLOR_YELLOW)); + CompletableFuture future = player.createConnectionRequest(server).connect(); + futures.put(player, future); + if (oPlayer.size() == 1) { + commandSource.sendMessage(Component.text("Sending " + player.username() + " to " + server.serverInfo().name()).color(COLOR_YELLOW)); } } if (oPlayer.size() > 1) { - commandSource.sendMessage(Component.text(String.format("You send %d players to %s", oPlayer.size(), server.getServerInfo().getName())).color(COLOR_YELLOW)); + commandSource.sendMessage(Component.text(String.format("Sending %d players to %s", oPlayer.size(), server.serverInfo().name())).color(COLOR_YELLOW)); } + new Thread(() -> { + futures.forEach(((player, future) -> { + future.whenComplete(((result, throwable) -> { + if (result.isSuccessful() && player.hasPermission("vtools.send.recvmsg")) { + player.sendMessage(Component.text("You got sent to " + server.serverInfo().name()).color(COLOR_YELLOW)); + } + })); + })); + final HashMap results = new HashMap<>(); + futures.forEach(((player, future) -> { + try { + String status = future.get().status().name(); + results.put(status, results.getOrDefault(status, 0) + 1); + } catch (InterruptedException | ExecutionException e) { + String status = e.getCause().getClass().getSimpleName(); + results.put(status, results.getOrDefault(status, 0) + 1); + } + })); + String sendResults = results.isEmpty() ? "nothing" : results.entrySet().stream().map( + entry -> String.format("%s : %d", entry.getKey(), entry.getValue()) + ).collect(Collectors.joining("\n")); + commandSource.sendMessage(Component.text(String.format("Send Results:\n%s", sendResults)).color(COLOR_YELLOW)); + }).start(); } else { commandSource.sendMessage(Component.text("The server or user does not exist!").color(COLOR_RED)); } @@ -85,13 +102,13 @@ public class CommandSend implements SimpleCommand { if (currentArgs.length <= 1) { arg.add("all"); arg.add("current"); - for (Player player : server.getAllPlayers()) { - arg.add(player.getUsername()); + for (Player player : server.connectedPlayers()) { + arg.add(player.username()); } return arg; } else if (currentArgs.length == 2) { - for (RegisteredServer server : server.getAllServers()) { - arg.add(server.getServerInfo().getName()); + for (RegisteredServer server : server.registeredServers()) { + arg.add(server.serverInfo().name()); } } return arg; diff --git a/src/main/java/de/strifel/VTools/commands/CommandServers.java b/src/main/java/de/strifel/VTools/commands/CommandServers.java index 060ebfa..aafaed8 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandServers.java +++ b/src/main/java/de/strifel/VTools/commands/CommandServers.java @@ -25,8 +25,8 @@ public class CommandServers implements SimpleCommand { String[] strings = invocation.arguments(); StringBuilder servers = new StringBuilder(); - for (RegisteredServer server : server.getAllServers()) { - servers.append(server.getServerInfo().getName()); + for (RegisteredServer server : server.registeredServers()) { + servers.append(server.serverInfo().name()); servers.append(" "); } commandSource.sendMessage(Component.text(servers.toString()).color(COLOR_YELLOW)); diff --git a/src/main/java/de/strifel/VTools/commands/CommandStaffChat.java b/src/main/java/de/strifel/VTools/commands/CommandStaffChat.java index afc9f5f..1079ab8 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandStaffChat.java +++ b/src/main/java/de/strifel/VTools/commands/CommandStaffChat.java @@ -2,7 +2,7 @@ package de.strifel.VTools.commands; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.ProxyServer; import net.kyori.adventure.text.Component; @@ -27,8 +27,8 @@ public class CommandStaffChat implements SimpleCommand { if (strings.length > 0) { String channel = strings[0].startsWith("c:") && !strings[0].equals("c:") ? strings[0].split(":")[1] : null; - String message = "§4[Staff]§r " + (commandSource instanceof Player ? ((Player) commandSource).getUsername() : "Console") + (channel != null ? " (" + channel + ")" : "")+ " > " + String.join(" ", Arrays.copyOfRange(strings, channel == null ? 0 : 1, strings.length)).replace("&", "§"); - for (Player player : server.getAllPlayers()) { + String message = "§4[Staff]§r " + (commandSource instanceof Player ? ((Player) commandSource).username() : "Console") + (channel != null ? " (" + channel + ")" : "")+ " > " + String.join(" ", Arrays.copyOfRange(strings, channel == null ? 0 : 1, strings.length)).replace("&", "§"); + for (Player player : server.connectedPlayers()) { if (player.hasPermission("vtools.staffchat" + (channel != null ? "." + channel : ""))) { player.sendMessage(Component.text(message)); } diff --git a/src/main/java/de/strifel/VTools/commands/CommandTp.java b/src/main/java/de/strifel/VTools/commands/CommandTp.java index 23edb77..8de0e29 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandTp.java +++ b/src/main/java/de/strifel/VTools/commands/CommandTp.java @@ -2,7 +2,7 @@ package de.strifel.VTools.commands; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.ProxyServer; import net.kyori.adventure.text.Component; @@ -28,9 +28,9 @@ public class CommandTp implements SimpleCommand { if (commandSource instanceof Player) { if (strings.length == 1) { - Optional player = server.getPlayer(strings[0]); - if (player.isPresent()) { - player.get().getCurrentServer().ifPresent(serverConnection -> ((Player) commandSource).createConnectionRequest(serverConnection.getServer()).fireAndForget()); + Player player = server.player(strings[0]); + if (player != null) { + Optional.ofNullable(player.connectedServer()).ifPresent(serverConnection -> ((Player) commandSource).createConnectionRequest(serverConnection.target()).fireAndForget()); commandSource.sendMessage(Component.text("Connecting to the server of " + strings[0]).color(COLOR_YELLOW)); } else { commandSource.sendMessage(Component.text("Player does not exists.").color(COLOR_RED)); @@ -47,8 +47,8 @@ public class CommandTp implements SimpleCommand { public List suggest(Invocation commandInvocation) { List arg = new ArrayList<>(); if (commandInvocation.arguments().length <= 1) { - for (Player player : server.getAllPlayers()) { - arg.add(player.getUsername()); + for (Player player : server.connectedPlayers()) { + arg.add(player.username()); } } return arg; diff --git a/velocity-plugin.json b/velocity-plugin.json new file mode 100644 index 0000000..0982e0a --- /dev/null +++ b/velocity-plugin.json @@ -0,0 +1,8 @@ +{ + "id": "vtools", + "name": "VTools", + "version": "1.0-SNAPSHOT", + "authors": [ "unnamed" ], + "dependencies": [], + "main": "de.strifel.VTools.VTools" +}