From 1a3c8872d1bbab9d68f48ace3a8b7ff46d1c995f Mon Sep 17 00:00:00 2001 From: Sodium-Aluminate Date: Thu, 21 Dec 2023 15:53:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VTools/commands/CommandGlobalChat.java | 2 +- .../VTools/commands/CommandRestart.java | 4 +- .../strifel/VTools/commands/CommandSend.java | 46 +++++++++---------- .../VTools/commands/CommandServers.java | 2 +- .../de/strifel/VTools/commands/CommandTp.java | 4 +- 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/main/java/de/strifel/VTools/commands/CommandGlobalChat.java b/src/main/java/de/strifel/VTools/commands/CommandGlobalChat.java index 5cfbcdf..3d09954 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandGlobalChat.java +++ b/src/main/java/de/strifel/VTools/commands/CommandGlobalChat.java @@ -54,7 +54,7 @@ public class CommandGlobalChat implements SimpleCommand { @Override public List suggest(Invocation invocation) { - return new ArrayList(); + return new ArrayList<>(); } @Override diff --git a/src/main/java/de/strifel/VTools/commands/CommandRestart.java b/src/main/java/de/strifel/VTools/commands/CommandRestart.java index a5f85c2..3b4c871 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandRestart.java +++ b/src/main/java/de/strifel/VTools/commands/CommandRestart.java @@ -1,6 +1,5 @@ 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.ProxyServer; @@ -19,7 +18,6 @@ public class CommandRestart implements SimpleCommand { @Override public void execute(SimpleCommand.Invocation invocation) { - CommandSource commandSource = invocation.source(); String[] strings = invocation.arguments(); if (strings.length > 0) { @@ -33,7 +31,7 @@ public class CommandRestart implements SimpleCommand { @Override public List suggest(SimpleCommand.Invocation invocation) { - return new ArrayList(); + return new ArrayList<>(); } @Override diff --git a/src/main/java/de/strifel/VTools/commands/CommandSend.java b/src/main/java/de/strifel/VTools/commands/CommandSend.java index b1e3253..def1c11 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandSend.java +++ b/src/main/java/de/strifel/VTools/commands/CommandSend.java @@ -9,10 +9,7 @@ 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.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; @@ -39,23 +36,20 @@ public class CommandSend implements SimpleCommand { for (Player player : server.getAllPlayers()) { oPlayer.add(player); } - } - else if (strings[0].equals("current")) { + } else if (strings[0].equals("current")) { if (commandSource instanceof Player) { - Player playerSource = (Player)commandSource; + Player playerSource = (Player) commandSource; Optional conn = playerSource.getCurrentServer(); if (conn.isPresent()) { for (Player player : conn.get().getServer().getPlayersConnected()) { oPlayer.add(player); } } - } - else { + } else { commandSource.sendMessage(Component.text("Command is only for players.").color(COLOR_RED)); return; } - } - else { + } else { Optional p = server.getPlayer(strings[0]); if (p.isPresent()) { oPlayer.add(p.get()); @@ -94,9 +88,9 @@ public class CommandSend implements SimpleCommand { } })); String sendResults = results.isEmpty() ? "nothing" : results.entrySet().stream().map( - entry -> String.format("%s : %d", entry.getKey(), entry.getValue()) + 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)); + commandSource.sendMessage(Component.text("Send Results:%n%s".formatted(sendResults)).color(COLOR_YELLOW)); }).start(); } else { commandSource.sendMessage(Component.text("The server or user does not exist!").color(COLOR_RED)); @@ -108,21 +102,23 @@ public class CommandSend implements SimpleCommand { public List suggest(SimpleCommand.Invocation invocation) { String[] currentArgs = invocation.arguments(); - - List arg = new ArrayList(); - if (currentArgs.length <= 1) { - arg.add("all"); - arg.add("current"); - for (Player player : server.getAllPlayers()) { - arg.add(player.getUsername()); + switch (currentArgs.length) { + case 0, 1 -> { + List arg = new ArrayList<>(server.getPlayerCount() + 2); + arg.add("all"); + arg.add("current"); + for (Player player : server.getAllPlayers()) { + arg.add(player.getUsername()); + } + return arg; } - return arg; - } else if (currentArgs.length == 2) { - for (RegisteredServer server : server.getAllServers()) { - arg.add(server.getServerInfo().getName()); + case 2 -> { + return server.getAllServers().stream().map(s -> s.getServerInfo().getName()).toList(); + } + default -> { + return new ArrayList<>(0); } } - return arg; } public boolean hasPermission(SimpleCommand.Invocation invocation) { diff --git a/src/main/java/de/strifel/VTools/commands/CommandServers.java b/src/main/java/de/strifel/VTools/commands/CommandServers.java index 060ebfa..1592e27 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandServers.java +++ b/src/main/java/de/strifel/VTools/commands/CommandServers.java @@ -34,7 +34,7 @@ public class CommandServers implements SimpleCommand { @Override public List suggest(SimpleCommand.Invocation invocation) { - return new ArrayList(); + return new ArrayList<>(); } @Override diff --git a/src/main/java/de/strifel/VTools/commands/CommandTp.java b/src/main/java/de/strifel/VTools/commands/CommandTp.java index 23edb77..69d5c8f 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandTp.java +++ b/src/main/java/de/strifel/VTools/commands/CommandTp.java @@ -26,11 +26,11 @@ public class CommandTp implements SimpleCommand { CommandSource commandSource = commandInvocation.source(); String[] strings = commandInvocation.arguments(); - if (commandSource instanceof Player) { + if (commandSource instanceof Player playerCommandSource) { 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.get().getCurrentServer().ifPresent(serverConnection -> playerCommandSource.createConnectionRequest(serverConnection.getServer()).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));