Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-17 05:20:14 +01:00
Refactor and improve ServerCommand
The code has been cleaned up and reorganized. As a small quality of life change, the server names are now sorted in alphabetical order.
Dieser Commit ist enthalten in:
Ursprung
260f7f575a
Commit
08173c50ef
@ -1,5 +1,8 @@
|
|||||||
package com.velocitypowered.proxy.command;
|
package com.velocitypowered.proxy.command;
|
||||||
|
|
||||||
|
import static net.kyori.text.TextComponent.of;
|
||||||
|
import static net.kyori.text.event.HoverEvent.showText;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.velocitypowered.api.command.Command;
|
import com.velocitypowered.api.command.Command;
|
||||||
import com.velocitypowered.api.command.CommandSource;
|
import com.velocitypowered.api.command.CommandSource;
|
||||||
@ -9,12 +12,13 @@ import com.velocitypowered.api.proxy.ProxyServer;
|
|||||||
import com.velocitypowered.api.proxy.ServerConnection;
|
import com.velocitypowered.api.proxy.ServerConnection;
|
||||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||||
import com.velocitypowered.api.proxy.server.ServerInfo;
|
import com.velocitypowered.api.proxy.server.ServerInfo;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import net.kyori.text.TextComponent;
|
import net.kyori.text.TextComponent;
|
||||||
import net.kyori.text.event.ClickEvent;
|
import net.kyori.text.event.ClickEvent;
|
||||||
import net.kyori.text.event.HoverEvent;
|
|
||||||
import net.kyori.text.format.TextColor;
|
import net.kyori.text.format.TextColor;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
@ -29,7 +33,7 @@ public class ServerCommand implements Command {
|
|||||||
@Override
|
@Override
|
||||||
public void execute(CommandSource source, String @NonNull [] args) {
|
public void execute(CommandSource source, String @NonNull [] args) {
|
||||||
if (!(source instanceof Player)) {
|
if (!(source instanceof Player)) {
|
||||||
source.sendMessage(TextComponent.of("Only players may run this command.", TextColor.RED));
|
source.sendMessage(of("Only players may run this command.", TextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,45 +44,52 @@ public class ServerCommand implements Command {
|
|||||||
Optional<RegisteredServer> toConnect = server.getServer(serverName);
|
Optional<RegisteredServer> toConnect = server.getServer(serverName);
|
||||||
if (!toConnect.isPresent()) {
|
if (!toConnect.isPresent()) {
|
||||||
player.sendMessage(
|
player.sendMessage(
|
||||||
TextComponent.of("Server " + serverName + " doesn't exist.", TextColor.RED));
|
of("Server " + serverName + " doesn't exist.", TextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
player.createConnectionRequest(toConnect.get()).fireAndForget();
|
player.createConnectionRequest(toConnect.get()).fireAndForget();
|
||||||
} else {
|
} else {
|
||||||
String currentServer = player.getCurrentServer().map(ServerConnection::getServerInfo)
|
outputServerInformation(player);
|
||||||
.map(ServerInfo::getName)
|
}
|
||||||
.orElse("<unknown>");
|
}
|
||||||
player.sendMessage(TextComponent
|
|
||||||
.of("You are currently connected to " + currentServer + ".", TextColor.YELLOW));
|
private void outputServerInformation(Player executor) {
|
||||||
|
String currentServer = executor.getCurrentServer().map(ServerConnection::getServerInfo)
|
||||||
|
.map(ServerInfo::getName).orElse("<unknown>");
|
||||||
|
executor.sendMessage(of("You are currently connected to " + currentServer + ".",
|
||||||
|
TextColor.YELLOW));
|
||||||
|
|
||||||
// Assemble the list of servers as components
|
// Assemble the list of servers as components
|
||||||
TextComponent.Builder serverListBuilder = TextComponent.builder("Available servers: ")
|
TextComponent.Builder serverListBuilder = TextComponent.builder("Available servers: ")
|
||||||
.color(TextColor.YELLOW);
|
.color(TextColor.YELLOW);
|
||||||
List<RegisteredServer> infos = ImmutableList.copyOf(server.getAllServers());
|
List<RegisteredServer> servers = new ArrayList<>(server.getAllServers());
|
||||||
for (int i = 0; i < infos.size(); i++) {
|
servers.sort(Comparator.comparing(RegisteredServer::getServerInfo));
|
||||||
RegisteredServer rs = infos.get(i);
|
for (int i = 0; i < servers.size(); i++) {
|
||||||
TextComponent infoComponent = TextComponent.of(rs.getServerInfo().getName());
|
RegisteredServer rs = servers.get(i);
|
||||||
String playersText = rs.getPlayersConnected().size() + " player(s) online";
|
serverListBuilder.append(formatServerComponent(currentServer, rs));
|
||||||
if (rs.getServerInfo().getName().equals(currentServer)) {
|
if (i != servers.size() - 1) {
|
||||||
infoComponent = infoComponent.color(TextColor.GREEN)
|
serverListBuilder.append(of(", ", TextColor.GRAY));
|
||||||
.hoverEvent(HoverEvent.showText(
|
|
||||||
TextComponent.of("Currently connected to this server\n" + playersText)));
|
|
||||||
} else {
|
|
||||||
infoComponent = infoComponent.color(TextColor.GRAY)
|
|
||||||
.clickEvent(ClickEvent.runCommand(
|
|
||||||
"/server " + rs.getServerInfo().getName()))
|
|
||||||
.hoverEvent(HoverEvent.showText(
|
|
||||||
TextComponent.of("Click to connect to this server\n" + playersText)));
|
|
||||||
}
|
|
||||||
serverListBuilder.append(infoComponent);
|
|
||||||
if (i != infos.size() - 1) {
|
|
||||||
serverListBuilder.append(TextComponent.of(", ", TextColor.GRAY));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
player.sendMessage(serverListBuilder.build());
|
executor.sendMessage(serverListBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TextComponent formatServerComponent(String currentPlayerServer, RegisteredServer server) {
|
||||||
|
ServerInfo serverInfo = server.getServerInfo();
|
||||||
|
TextComponent serverTextComponent = of(serverInfo.getName());
|
||||||
|
|
||||||
|
String playersText = server.getPlayersConnected().size() + " player(s) online";
|
||||||
|
if (serverInfo.getName().equals(currentPlayerServer)) {
|
||||||
|
serverTextComponent = serverTextComponent.color(TextColor.GREEN)
|
||||||
|
.hoverEvent(showText(of("Currently connected to this server\n" + playersText)));
|
||||||
|
} else {
|
||||||
|
serverTextComponent = serverTextComponent.color(TextColor.GRAY)
|
||||||
|
.clickEvent(ClickEvent.runCommand("/server " + serverInfo.getName()))
|
||||||
|
.hoverEvent(showText(of("Click to connect to this server\n" + playersText)));
|
||||||
|
}
|
||||||
|
return serverTextComponent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren