3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Expand /server command.

The command now supports displaying the server you are currently on and
takes full advantage of the JSON-based text component system to prettify
its output.
Dieser Commit ist enthalten in:
Andrew Steinborn 2018-08-26 19:41:18 -04:00
Ursprung 01c1002249
Commit 79f4abcf90

Datei anzeigen

@ -4,9 +4,12 @@ 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;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.ServerInfo; import com.velocitypowered.api.proxy.server.ServerInfo;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import net.kyori.text.TextComponent; import net.kyori.text.TextComponent;
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 java.util.List; import java.util.List;
@ -33,10 +36,31 @@ public class ServerCommand implements Command {
player.createConnectionRequest(server.get()).fireAndForget(); player.createConnectionRequest(server.get()).fireAndForget();
} else { } else {
String serverList = VelocityServer.getServer().getAllServers().stream() String currentServer = ((Player) source).getCurrentServer().map(ServerConnection::getServerInfo)
.map(ServerInfo::getName) .map(ServerInfo::getName).orElse("<unknown>");
.collect(Collectors.joining(", ")); player.sendMessage(TextComponent.of("You are currently connected to " + currentServer + ".", TextColor.YELLOW));
player.sendMessage(TextComponent.of("Available servers: " + serverList, TextColor.YELLOW));
// Assemble the list of servers as components
TextComponent.Builder serverListBuilder = TextComponent.builder("Available servers: ").color(TextColor.YELLOW);
List<ServerInfo> infos = ImmutableList.copyOf(VelocityServer.getServer().getAllServers());
for (int i = 0; i < infos.size(); i++) {
ServerInfo serverInfo = infos.get(i);
TextComponent infoComponent = TextComponent.of(serverInfo.getName());
if (serverInfo.getName().equals(currentServer)) {
infoComponent = infoComponent.color(TextColor.GREEN)
.hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Currently connected to this server")));
} else {
infoComponent = infoComponent.color(TextColor.GRAY)
.clickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/server " + serverInfo.getName()))
.hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to connect to this server")));
}
serverListBuilder.append(infoComponent);
if (i != infos.size() - 1) {
serverListBuilder.append(TextComponent.of(", ", TextColor.GRAY));
}
}
player.sendMessage(serverListBuilder.build());
} }
} }