1
0
Fork 0
forked from mc/VTools

improve send command

add vtools.send.recvmsg permission
This commit is contained in:
JerryXiao 2023-03-20 18:44:02 +08:00
parent bad2c2d0c1
commit 4939482e38
Signed by untrusted user: Jerry
GPG key ID: 22618F758B5BE2E5
4 changed files with 42 additions and 14 deletions

View file

@ -6,7 +6,7 @@ Tools for Velocity proxy server.
|-------|-------------|----------| |-------|-------------|----------|
|/broadcast|Broadcast a message to all servers|vtools.broadcast| |/broadcast|Broadcast a message to all servers|vtools.broadcast|
|/find|See on which server a user is|vtools.find and vtools.find.autocomplete| |/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|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| |/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| |/servers|List all servers|vtools.servers|

View file

@ -13,8 +13,8 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>8</source> <source>17</source>
<target>8</target> <target>17</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
@ -31,7 +31,7 @@
<dependency> <dependency>
<groupId>com.velocitypowered</groupId> <groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId> <artifactId>velocity-api</artifactId>
<version>3.1.0</version> <version>3.2.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -10,12 +10,13 @@ import org.slf4j.Logger;
import javax.inject.Inject; 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 { public class VTools {
private final ProxyServer server; private final ProxyServer server;
public static final TextColor COLOR_RED = TextColor.fromCSSHexString("FF5555"); public static final TextColor COLOR_RED = TextColor.fromCSSHexString("#FF5555");
public static final TextColor COLOR_YELLOW = TextColor.fromCSSHexString("FFFF55"); public static final TextColor COLOR_YELLOW = TextColor.fromCSSHexString("#FFFF55");
public static final TextColor COLOR_ORANGE = TextColor.fromCSSHexString("#FFA500");
@Inject @Inject
public VTools(ProxyServer server, Logger logger) { public VTools(ProxyServer server, Logger logger) {

View file

@ -2,6 +2,7 @@ package de.strifel.VTools.commands;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.ConnectionRequestBuilder;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.ServerConnection;
@ -9,11 +10,16 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional; 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_RED;
import static de.strifel.VTools.VTools.COLOR_YELLOW; import static de.strifel.VTools.VTools.COLOR_YELLOW;
import static de.strifel.VTools.VTools.COLOR_ORANGE;
public class CommandSend implements SimpleCommand { public class CommandSend implements SimpleCommand {
private final ProxyServer server; 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)); commandSource.sendMessage(Component.text("Command is only for players.").color(COLOR_RED));
return; return;
} }
for (Player player : server.getAllPlayers()) {
oPlayer.add(player);
}
} }
else { else {
Optional<Player> p = server.getPlayer(strings[0]); Optional<Player> p = server.getPlayer(strings[0]);
@ -58,19 +61,43 @@ public class CommandSend implements SimpleCommand {
oPlayer.add(p.get()); oPlayer.add(p.get());
} }
} }
HashMap<Player, CompletableFuture<ConnectionRequestBuilder.Result>> futures = new HashMap<>();
Optional<RegisteredServer> oServer = server.getServer(strings[1]); Optional<RegisteredServer> oServer = server.getServer(strings[1]);
if (!oPlayer.isEmpty() && oServer.isPresent()) { if (!oPlayer.isEmpty() && oServer.isPresent()) {
RegisteredServer server = oServer.get(); RegisteredServer server = oServer.get();
for (Player player : oPlayer) { for (Player player : oPlayer) {
player.createConnectionRequest(server).connect(); CompletableFuture<ConnectionRequestBuilder.Result> future = player.createConnectionRequest(server).connect();
futures.put(player, future);
if (oPlayer.size() <= 1) { 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) { 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<String, Integer> 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 { } else {
commandSource.sendMessage(Component.text("The server or user does not exist!").color(COLOR_RED)); commandSource.sendMessage(Component.text("The server or user does not exist!").color(COLOR_RED));
} }