1
0
Fork 0
forked from mc/VTools

整理代码

This commit is contained in:
Sodium-Aluminate 2023-12-21 15:53:37 +08:00
parent f36607df8d
commit 1a3c8872d1
5 changed files with 26 additions and 32 deletions

View file

@ -54,7 +54,7 @@ public class CommandGlobalChat implements SimpleCommand {
@Override @Override
public List<String> suggest(Invocation invocation) { public List<String> suggest(Invocation invocation) {
return new ArrayList<String>(); return new ArrayList<>();
} }
@Override @Override

View file

@ -1,6 +1,5 @@
package de.strifel.VTools.commands; package de.strifel.VTools.commands;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
@ -19,7 +18,6 @@ public class CommandRestart implements SimpleCommand {
@Override @Override
public void execute(SimpleCommand.Invocation invocation) { public void execute(SimpleCommand.Invocation invocation) {
CommandSource commandSource = invocation.source();
String[] strings = invocation.arguments(); String[] strings = invocation.arguments();
if (strings.length > 0) { if (strings.length > 0) {
@ -33,7 +31,7 @@ public class CommandRestart implements SimpleCommand {
@Override @Override
public List<String> suggest(SimpleCommand.Invocation invocation) { public List<String> suggest(SimpleCommand.Invocation invocation) {
return new ArrayList<String>(); return new ArrayList<>();
} }
@Override @Override

View file

@ -9,10 +9,7 @@ import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer; 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.*;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -39,23 +36,20 @@ public class CommandSend implements SimpleCommand {
for (Player player : server.getAllPlayers()) { for (Player player : server.getAllPlayers()) {
oPlayer.add(player); oPlayer.add(player);
} }
} } else if (strings[0].equals("current")) {
else if (strings[0].equals("current")) {
if (commandSource instanceof Player) { if (commandSource instanceof Player) {
Player playerSource = (Player)commandSource; Player playerSource = (Player) commandSource;
Optional<ServerConnection> conn = playerSource.getCurrentServer(); Optional<ServerConnection> conn = playerSource.getCurrentServer();
if (conn.isPresent()) { if (conn.isPresent()) {
for (Player player : conn.get().getServer().getPlayersConnected()) { for (Player player : conn.get().getServer().getPlayersConnected()) {
oPlayer.add(player); oPlayer.add(player);
} }
} }
} } else {
else {
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;
} }
} } else {
else {
Optional<Player> p = server.getPlayer(strings[0]); Optional<Player> p = server.getPlayer(strings[0]);
if (p.isPresent()) { if (p.isPresent()) {
oPlayer.add(p.get()); oPlayer.add(p.get());
@ -96,7 +90,7 @@ public class CommandSend implements SimpleCommand {
String sendResults = results.isEmpty() ? "nothing" : results.entrySet().stream().map( 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")); ).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(); }).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));
@ -108,21 +102,23 @@ public class CommandSend implements SimpleCommand {
public List<String> suggest(SimpleCommand.Invocation invocation) { public List<String> suggest(SimpleCommand.Invocation invocation) {
String[] currentArgs = invocation.arguments(); String[] currentArgs = invocation.arguments();
switch (currentArgs.length) {
List<String> arg = new ArrayList<String>(); case 0, 1 -> {
if (currentArgs.length <= 1) { List<String> arg = new ArrayList<>(server.getPlayerCount() + 2);
arg.add("all"); arg.add("all");
arg.add("current"); arg.add("current");
for (Player player : server.getAllPlayers()) { for (Player player : server.getAllPlayers()) {
arg.add(player.getUsername()); arg.add(player.getUsername());
} }
return arg; return arg;
} else if (currentArgs.length == 2) { }
for (RegisteredServer server : server.getAllServers()) { case 2 -> {
arg.add(server.getServerInfo().getName()); return server.getAllServers().stream().map(s -> s.getServerInfo().getName()).toList();
}
default -> {
return new ArrayList<>(0);
} }
} }
return arg;
} }
public boolean hasPermission(SimpleCommand.Invocation invocation) { public boolean hasPermission(SimpleCommand.Invocation invocation) {

View file

@ -34,7 +34,7 @@ public class CommandServers implements SimpleCommand {
@Override @Override
public List<String> suggest(SimpleCommand.Invocation invocation) { public List<String> suggest(SimpleCommand.Invocation invocation) {
return new ArrayList<String>(); return new ArrayList<>();
} }
@Override @Override

View file

@ -26,11 +26,11 @@ public class CommandTp implements SimpleCommand {
CommandSource commandSource = commandInvocation.source(); CommandSource commandSource = commandInvocation.source();
String[] strings = commandInvocation.arguments(); String[] strings = commandInvocation.arguments();
if (commandSource instanceof Player) { if (commandSource instanceof Player playerCommandSource) {
if (strings.length == 1) { if (strings.length == 1) {
Optional<Player> player = server.getPlayer(strings[0]); Optional<Player> player = server.getPlayer(strings[0]);
if (player.isPresent()) { 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)); commandSource.sendMessage(Component.text("Connecting to the server of " + strings[0]).color(COLOR_YELLOW));
} else { } else {
commandSource.sendMessage(Component.text("Player does not exists.").color(COLOR_RED)); commandSource.sendMessage(Component.text("Player does not exists.").color(COLOR_RED));