13
0
geforkt von Mirrors/Velocity

Apply some changes from 5.0.0

Dieser Commit ist enthalten in:
Andrew Steinborn 2023-10-29 14:54:39 -04:00
Ursprung 2e8ef8b2ca
Commit 2ec830f874

Datei anzeigen

@ -28,8 +28,10 @@ import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.permission.Tristate;
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.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -50,50 +52,44 @@ public class SendCommand {
*/ */
public void register() { public void register() {
LiteralCommandNode<CommandSource> totalNode = LiteralArgumentBuilder LiteralCommandNode<CommandSource> totalNode = LiteralArgumentBuilder
.<CommandSource>literal("send") .<CommandSource>literal("send")
.requires(source -> .requires(source ->
source.getPermissionValue("velocity.command.send") == Tristate.TRUE) source.getPermissionValue("velocity.command.send") == Tristate.TRUE)
.executes(this::usage) .executes(this::usage)
.build(); .build();
ArgumentCommandNode<CommandSource, String> playerNode = RequiredArgumentBuilder ArgumentCommandNode<CommandSource, String> playerNode = RequiredArgumentBuilder
.<CommandSource, String>argument(PLAYER_ARG, StringArgumentType.word()) .<CommandSource, String>argument("player", StringArgumentType.word())
.suggests((context, builder) -> { .suggests((context, builder) -> {
String argument = context.getArguments().containsKey(PLAYER_ARG) String argument = context.getArguments().containsKey(PLAYER_ARG)
? context.getArgument(PLAYER_ARG, String.class) ? context.getArgument(PLAYER_ARG, String.class)
: ""; : "";
for (Player player : server.getAllPlayers()) { for (Player player : server.getAllPlayers()) {
String playerName = player.getUsername(); String playerName = player.getUsername();
if (playerName.regionMatches(true, 0, argument, 0, argument.length())) { if (playerName.regionMatches(true, 0, argument, 0, argument.length())) {
builder.suggest(playerName); builder.suggest(playerName);
} }
} }
if ("all".regionMatches(true, 0, argument, 0, argument.length())) { if ("all".regionMatches(true, 0, argument, 0, argument.length())) {
builder.suggest("all"); builder.suggest("all");
} }
if ("current".regionMatches(true, 0, argument, 0, argument.length()) if ("current".regionMatches(true, 0, argument, 0, argument.length())
&& context.getSource() instanceof Player) { && context.getSource() instanceof Player) {
builder.suggest("current"); builder.suggest("current");
} }
return builder.buildFuture(); return builder.buildFuture();
}) })
.executes(this::usage) .executes(this::usage)
.build(); .build();
ArgumentCommandNode<CommandSource, String> serverNode = RequiredArgumentBuilder ArgumentCommandNode<CommandSource, String> serverNode = RequiredArgumentBuilder
.<CommandSource, String>argument(SERVER_ARG, StringArgumentType.word()) .<CommandSource, String>argument("server", StringArgumentType.word())
.suggests((context, builder) -> { .suggests((context, builder) -> {
String argument = context.getArguments().containsKey(SERVER_ARG) for (RegisteredServer server : server.getAllServers()) {
? context.getArgument(SERVER_ARG, String.class) builder.suggest(server.getServerInfo().getName());
: ""; }
for (RegisteredServer server : server.getAllServers()) { return builder.buildFuture();
String serverName = server.getServerInfo().getName(); })
if (serverName.regionMatches(true, 0, argument, 0, argument.length())) { .executes(this::send)
builder.suggest(serverName); .build();
}
}
return builder.buildFuture();
})
.executes(this::send)
.build();
totalNode.addChild(playerNode); totalNode.addChild(playerNode);
playerNode.addChild(serverNode); playerNode.addChild(serverNode);
server.getCommandManager().register(new BrigadierCommand(totalNode)); server.getCommandManager().register(new BrigadierCommand(totalNode));
@ -101,7 +97,7 @@ public class SendCommand {
private int usage(CommandContext<CommandSource> context) { private int usage(CommandContext<CommandSource> context) {
context.getSource().sendMessage( context.getSource().sendMessage(
Component.translatable("velocity.command.send-usage", NamedTextColor.YELLOW) Component.translatable("velocity.command.send-usage", NamedTextColor.YELLOW)
); );
return 1; return 1;
} }
@ -110,17 +106,20 @@ public class SendCommand {
String serverName = context.getArgument(SERVER_ARG, String.class); String serverName = context.getArgument(SERVER_ARG, String.class);
String player = context.getArgument(PLAYER_ARG, String.class); String player = context.getArgument(PLAYER_ARG, String.class);
if (server.getServer(serverName).isEmpty()) { Optional<RegisteredServer> maybeServer = server.getServer(serverName);
if (maybeServer.isEmpty()) {
context.getSource().sendMessage( context.getSource().sendMessage(
CommandMessages.SERVER_DOES_NOT_EXIST.args(Component.text(serverName)) CommandMessages.SERVER_DOES_NOT_EXIST.args(Component.text(serverName))
); );
return 0; return 0;
} }
if (server.getPlayer(player).isEmpty() if (server.getPlayer(player).isEmpty()
&& !Objects.equals(player, "all") && !Objects.equals(player, "current")) { && !Objects.equals(player, "all")
&& !Objects.equals(player, "current")) {
context.getSource().sendMessage( context.getSource().sendMessage(
CommandMessages.PLAYER_NOT_FOUND.args(Component.text(player)) CommandMessages.PLAYER_NOT_FOUND.args(Component.text(player))
); );
return 0; return 0;
} }
@ -139,9 +138,10 @@ public class SendCommand {
} }
Player source = (Player) context.getSource(); Player source = (Player) context.getSource();
if (source.getCurrentServer().isPresent()) { Optional<ServerConnection> connectedServer = source.getCurrentServer();
for (Player p : source.getCurrentServer().get().getServer().getPlayersConnected()) { if (connectedServer.isPresent()) {
p.createConnectionRequest(server.getServer(serverName).get()).fireAndForget(); for (Player p : connectedServer.get().getServer().getPlayersConnected()) {
p.createConnectionRequest(maybeServer.get()).fireAndForget();
} }
return 1; return 1;
} }
@ -149,8 +149,7 @@ public class SendCommand {
} }
server.getPlayer(player).get().createConnectionRequest( server.getPlayer(player).get().createConnectionRequest(
server.getServer(serverName).get() maybeServer.get()).fireAndForget();
).fireAndForget();
return 1; return 1;
} }
} }