3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-09-29 14:40:21 +02:00

Convert all Velocity commands to use adventure text components

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-07-14 21:31:32 -04:00
Ursprung bcab3be2e2
Commit ff99b2b248
3 geänderte Dateien mit 51 neuen und 47 gelöschten Zeilen

Datei anzeigen

@ -9,8 +9,8 @@ import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import net.kyori.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.text.format.TextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
public class GlistCommand implements Command { public class GlistCommand implements Command {
@ -26,9 +26,9 @@ public class GlistCommand implements Command {
if (args.length == 0) { if (args.length == 0) {
sendTotalProxyCount(source); sendTotalProxyCount(source);
source.sendMessage( source.sendMessage(
TextComponent.builder("To view all players on servers, use ", TextColor.YELLOW) TextComponent.builder("To view all players on servers, use ", NamedTextColor.YELLOW)
.append("/glist all", TextColor.DARK_AQUA) .append("/glist all", NamedTextColor.DARK_AQUA)
.append(".", TextColor.YELLOW) .append(".", NamedTextColor.YELLOW)
.build()); .build());
} else if (args.length == 1) { } else if (args.length == 1) {
String arg = args[0]; String arg = args[0];
@ -41,20 +41,20 @@ public class GlistCommand implements Command {
Optional<RegisteredServer> registeredServer = server.getServer(arg); Optional<RegisteredServer> registeredServer = server.getServer(arg);
if (!registeredServer.isPresent()) { if (!registeredServer.isPresent()) {
source.sendMessage( source.sendMessage(
TextComponent.of("Server " + arg + " doesn't exist.", TextColor.RED)); TextComponent.of("Server " + arg + " doesn't exist.", NamedTextColor.RED));
return; return;
} }
sendServerPlayers(source, registeredServer.get(), false); sendServerPlayers(source, registeredServer.get(), false);
} }
} else { } else {
source.sendMessage(TextComponent.of("Too many arguments.", TextColor.RED)); source.sendMessage(TextComponent.of("Too many arguments.", NamedTextColor.RED));
} }
} }
private void sendTotalProxyCount(CommandSource target) { private void sendTotalProxyCount(CommandSource target) {
target.sendMessage(TextComponent.builder("There are ", TextColor.YELLOW) target.sendMessage(TextComponent.builder("There are ", NamedTextColor.YELLOW)
.append(Integer.toString(server.getAllPlayers().size()), TextColor.GREEN) .append(Integer.toString(server.getAllPlayers().size()), NamedTextColor.GREEN)
.append(" player(s) online.", TextColor.YELLOW) .append(" player(s) online.", NamedTextColor.YELLOW)
.build()); .build());
} }
@ -66,8 +66,8 @@ public class GlistCommand implements Command {
TextComponent.Builder builder = TextComponent.builder() TextComponent.Builder builder = TextComponent.builder()
.append(TextComponent.of("[" + server.getServerInfo().getName() + "] ", .append(TextComponent.of("[" + server.getServerInfo().getName() + "] ",
TextColor.DARK_AQUA)) NamedTextColor.DARK_AQUA))
.append("(" + onServer.size() + ")", TextColor.GRAY) .append("(" + onServer.size() + ")", NamedTextColor.GRAY)
.append(": ") .append(": ")
.resetStyle(); .resetStyle();

Datei anzeigen

@ -1,7 +1,6 @@
package com.velocitypowered.proxy.command; package com.velocitypowered.proxy.command;
import static net.kyori.text.TextComponent.of; import static net.kyori.adventure.text.event.HoverEvent.showText;
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;
@ -16,9 +15,9 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import net.kyori.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.text.format.TextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
public class ServerCommand implements Command { public class ServerCommand implements Command {
@ -33,7 +32,8 @@ 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(of("Only players may run this command.", TextColor.RED)); source.sendMessage(TextComponent.of("Only players may run this command.",
NamedTextColor.RED));
return; return;
} }
@ -44,7 +44,7 @@ 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(
of("Server " + serverName + " doesn't exist.", TextColor.RED)); TextComponent.of("Server " + serverName + " doesn't exist.", NamedTextColor.RED));
return; return;
} }
@ -57,24 +57,24 @@ public class ServerCommand implements Command {
private void outputServerInformation(Player executor) { private void outputServerInformation(Player executor) {
String currentServer = executor.getCurrentServer().map(ServerConnection::getServerInfo) String currentServer = executor.getCurrentServer().map(ServerConnection::getServerInfo)
.map(ServerInfo::getName).orElse("<unknown>"); .map(ServerInfo::getName).orElse("<unknown>");
executor.sendMessage(of("You are currently connected to " + currentServer + ".", executor.sendMessage(TextComponent.of("You are currently connected to " + currentServer + ".",
TextColor.YELLOW)); NamedTextColor.YELLOW));
List<RegisteredServer> servers = BuiltinCommandUtil.sortedServerList(server); List<RegisteredServer> servers = BuiltinCommandUtil.sortedServerList(server);
if (servers.size() > MAX_SERVERS_TO_LIST) { if (servers.size() > MAX_SERVERS_TO_LIST) {
executor.sendMessage(of("Too many servers to list. Tab-complete to show all servers.", executor.sendMessage(TextComponent.of(
TextColor.RED)); "Too many servers to list. Tab-complete to show all servers.", NamedTextColor.RED));
return; return;
} }
// 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(NamedTextColor.YELLOW);
for (int i = 0; i < servers.size(); i++) { for (int i = 0; i < servers.size(); i++) {
RegisteredServer rs = servers.get(i); RegisteredServer rs = servers.get(i);
serverListBuilder.append(formatServerComponent(currentServer, rs)); serverListBuilder.append(formatServerComponent(currentServer, rs));
if (i != servers.size() - 1) { if (i != servers.size() - 1) {
serverListBuilder.append(of(", ", TextColor.GRAY)); serverListBuilder.append(TextComponent.of(", ", NamedTextColor.GRAY));
} }
} }
@ -83,16 +83,20 @@ public class ServerCommand implements Command {
private TextComponent formatServerComponent(String currentPlayerServer, RegisteredServer server) { private TextComponent formatServerComponent(String currentPlayerServer, RegisteredServer server) {
ServerInfo serverInfo = server.getServerInfo(); ServerInfo serverInfo = server.getServerInfo();
TextComponent serverTextComponent = of(serverInfo.getName()); TextComponent serverTextComponent = TextComponent.of(serverInfo.getName());
String playersText = server.getPlayersConnected().size() + " player(s) online"; String playersText = server.getPlayersConnected().size() + " player(s) online";
if (serverInfo.getName().equals(currentPlayerServer)) { if (serverInfo.getName().equals(currentPlayerServer)) {
serverTextComponent = serverTextComponent.color(TextColor.GREEN) serverTextComponent = serverTextComponent.color(NamedTextColor.GREEN)
.hoverEvent(showText(of("Currently connected to this server\n" + playersText))); .hoverEvent(
showText(TextComponent.of("Currently connected to this server\n" + playersText))
);
} else { } else {
serverTextComponent = serverTextComponent.color(TextColor.GRAY) serverTextComponent = serverTextComponent.color(NamedTextColor.GRAY)
.clickEvent(ClickEvent.runCommand("/server " + serverInfo.getName())) .clickEvent(ClickEvent.runCommand("/server " + serverInfo.getName()))
.hoverEvent(showText(of("Click to connect to this server\n" + playersText))); .hoverEvent(
showText(TextComponent.of("Click to connect to this server\n" + playersText))
);
} }
return serverTextComponent; return serverTextComponent;
} }

Datei anzeigen

@ -16,11 +16,11 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.kyori.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.text.format.TextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -47,7 +47,7 @@ public class VelocityCommand implements Command {
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
.collect(Collectors.joining("|")); .collect(Collectors.joining("|"));
String commandText = "/velocity <" + availableCommands + ">"; String commandText = "/velocity <" + availableCommands + ">";
source.sendMessage(TextComponent.of(commandText, TextColor.RED)); source.sendMessage(TextComponent.of(commandText, NamedTextColor.RED));
} }
@Override @Override
@ -121,17 +121,17 @@ public class VelocityCommand implements Command {
public void execute(CommandSource source, String @NonNull [] args) { public void execute(CommandSource source, String @NonNull [] args) {
try { try {
if (server.reloadConfiguration()) { if (server.reloadConfiguration()) {
source.sendMessage(TextComponent.of("Configuration reloaded.", TextColor.GREEN)); source.sendMessage(TextComponent.of("Configuration reloaded.", NamedTextColor.GREEN));
} else { } else {
source.sendMessage(TextComponent.of( source.sendMessage(TextComponent.of(
"Unable to reload your configuration. Check the console for more details.", "Unable to reload your configuration. Check the console for more details.",
TextColor.RED)); NamedTextColor.RED));
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to reload configuration", e); logger.error("Unable to reload configuration", e);
source.sendMessage(TextComponent.of( source.sendMessage(TextComponent.of(
"Unable to reload your configuration. Check the console for more details.", "Unable to reload your configuration. Check the console for more details.",
TextColor.RED)); NamedTextColor.RED));
} }
} }
@ -152,7 +152,7 @@ public class VelocityCommand implements Command {
@Override @Override
public void execute(CommandSource source, String @NonNull [] args) { public void execute(CommandSource source, String @NonNull [] args) {
if (args.length != 0) { if (args.length != 0) {
source.sendMessage(TextComponent.of("/velocity version", TextColor.RED)); source.sendMessage(TextComponent.of("/velocity version", NamedTextColor.RED));
return; return;
} }
@ -160,7 +160,7 @@ public class VelocityCommand implements Command {
TextComponent velocity = TextComponent.builder(version.getName() + " ") TextComponent velocity = TextComponent.builder(version.getName() + " ")
.decoration(TextDecoration.BOLD, true) .decoration(TextDecoration.BOLD, true)
.color(TextColor.DARK_AQUA) .color(NamedTextColor.DARK_AQUA)
.append(TextComponent.of(version.getVersion()).decoration(TextDecoration.BOLD, false)) .append(TextComponent.of(version.getVersion()).decoration(TextDecoration.BOLD, false))
.build(); .build();
TextComponent copyright = TextComponent TextComponent copyright = TextComponent
@ -173,13 +173,13 @@ public class VelocityCommand implements Command {
TextComponent velocityWebsite = TextComponent.builder() TextComponent velocityWebsite = TextComponent.builder()
.content("Visit the ") .content("Visit the ")
.append(TextComponent.builder("Velocity website") .append(TextComponent.builder("Velocity website")
.color(TextColor.GREEN) .color(NamedTextColor.GREEN)
.clickEvent( .clickEvent(
ClickEvent.openUrl("https://www.velocitypowered.com")) ClickEvent.openUrl("https://www.velocitypowered.com"))
.build()) .build())
.append(TextComponent.of(" or the ")) .append(TextComponent.of(" or the "))
.append(TextComponent.builder("Velocity GitHub") .append(TextComponent.builder("Velocity GitHub")
.color(TextColor.GREEN) .color(NamedTextColor.GREEN)
.clickEvent(ClickEvent.openUrl( .clickEvent(ClickEvent.openUrl(
"https://github.com/VelocityPowered/Velocity")) "https://github.com/VelocityPowered/Velocity"))
.build()) .build())
@ -205,7 +205,7 @@ public class VelocityCommand implements Command {
@Override @Override
public void execute(CommandSource source, String @NonNull [] args) { public void execute(CommandSource source, String @NonNull [] args) {
if (args.length != 0) { if (args.length != 0) {
source.sendMessage(TextComponent.of("/velocity plugins", TextColor.RED)); source.sendMessage(TextComponent.of("/velocity plugins", NamedTextColor.RED));
return; return;
} }
@ -213,12 +213,12 @@ public class VelocityCommand implements Command {
int pluginCount = plugins.size(); int pluginCount = plugins.size();
if (pluginCount == 0) { if (pluginCount == 0) {
source.sendMessage(TextComponent.of("No plugins installed.", TextColor.YELLOW)); source.sendMessage(TextComponent.of("No plugins installed.", NamedTextColor.YELLOW));
return; return;
} }
TextComponent.Builder output = TextComponent.builder("Plugins: ") TextComponent.Builder output = TextComponent.builder("Plugins: ")
.color(TextColor.YELLOW); .color(NamedTextColor.YELLOW);
for (int i = 0; i < pluginCount; i++) { for (int i = 0; i < pluginCount; i++) {
PluginContainer plugin = plugins.get(i); PluginContainer plugin = plugins.get(i);
output.append(componentForPlugin(plugin.getDescription())); output.append(componentForPlugin(plugin.getDescription()));
@ -255,7 +255,7 @@ public class VelocityCommand implements Command {
hoverText.append(TextComponent.of(pdesc)); hoverText.append(TextComponent.of(pdesc));
}); });
return TextComponent.of(description.getId(), TextColor.GRAY) return TextComponent.of(description.getId(), NamedTextColor.GRAY)
.hoverEvent(HoverEvent.showText(hoverText.build())); .hoverEvent(HoverEvent.showText(hoverText.build()));
} }