From 4939482e38e83a296c42c64e74bcb88b6101430e Mon Sep 17 00:00:00 2001 From: Jerry Date: Mon, 20 Mar 2023 18:44:02 +0800 Subject: [PATCH] improve send command add vtools.send.recvmsg permission --- README.md | 2 +- pom.xml | 6 +-- src/main/java/de/strifel/VTools/VTools.java | 7 ++-- .../strifel/VTools/commands/CommandSend.java | 41 +++++++++++++++---- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 750156d..d76117b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Tools for Velocity proxy server. |-------|-------------|----------| |/broadcast|Broadcast a message to all servers|vtools.broadcast| |/find|See on which server a user is|vtools.find and vtools.find.autocomplete| -|/send|Send user or users to a different server|vtools.send| +|/send|Send user or users to a different server|vtools.send and vtools.send.recvmsg| |/staffchat|Chat over multiple servers in a staff only chat|vtools.staffchat| |/staffchat c:channelname|Chat over multiple servers in a staff only chat in a specific extra permissions channel|vtools.staffchat.channelname| |/servers|List all servers|vtools.servers| diff --git a/pom.xml b/pom.xml index 5319d98..8305827 100644 --- a/pom.xml +++ b/pom.xml @@ -13,8 +13,8 @@ org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + 17 + 17 @@ -31,7 +31,7 @@ com.velocitypowered velocity-api - 3.1.0 + 3.2.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..2496e5f 100644 --- a/src/main/java/de/strifel/VTools/VTools.java +++ b/src/main/java/de/strifel/VTools/VTools.java @@ -10,12 +10,13 @@ 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"); + public static final TextColor COLOR_ORANGE = TextColor.fromCSSHexString("#FFA500"); @Inject public VTools(ProxyServer server, Logger logger) { diff --git a/src/main/java/de/strifel/VTools/commands/CommandSend.java b/src/main/java/de/strifel/VTools/commands/CommandSend.java index 770bc5f..b1e3253 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandSend.java +++ b/src/main/java/de/strifel/VTools/commands/CommandSend.java @@ -2,6 +2,7 @@ package de.strifel.VTools.commands; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.proxy.ConnectionRequestBuilder; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ServerConnection; @@ -9,11 +10,16 @@ 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; +import static de.strifel.VTools.VTools.COLOR_ORANGE; public class CommandSend implements SimpleCommand { private final ProxyServer server; @@ -48,9 +54,6 @@ public class CommandSend implements SimpleCommand { commandSource.sendMessage(Component.text("Command is only for players.").color(COLOR_RED)); return; } - for (Player player : server.getAllPlayers()) { - oPlayer.add(player); - } } else { Optional p = server.getPlayer(strings[0]); @@ -58,19 +61,43 @@ public class CommandSend implements SimpleCommand { oPlayer.add(p.get()); } } + HashMap> futures = new HashMap<>(); Optional oServer = server.getServer(strings[1]); if (!oPlayer.isEmpty() && oServer.isPresent()) { RegisteredServer server = oServer.get(); for (Player player : oPlayer) { - player.createConnectionRequest(server).connect(); + CompletableFuture future = player.createConnectionRequest(server).connect(); + futures.put(player, future); if (oPlayer.size() <= 1) { - commandSource.sendMessage(Component.text("You send " + player.getUsername() + " to " + server.getServerInfo().getName()).color(COLOR_YELLOW)); + commandSource.sendMessage(Component.text("Sending " + player.getUsername() + " to " + server.getServerInfo().getName()).color(COLOR_ORANGE)); } - //commandSource.sendMessage(Component.text("You got send to " + server.getServerInfo().getName()).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.getServerInfo().getName())).color(COLOR_ORANGE)); } + 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.getServerInfo().getName()).color(COLOR_ORANGE)); + } + })); + })); + final HashMap results = new HashMap<>(); + futures.forEach(((player, future) -> { + try { + String status = future.get().getStatus().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)); }