13
0
geforkt von Mirrors/Velocity

Use more idiomatic adventure text serializer

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-06-28 23:13:57 -04:00
Ursprung 0c6039a773
Commit c2edd82b16
12 geänderte Dateien mit 64 neuen und 55 gelöschten Zeilen

Datei anzeigen

@ -54,10 +54,8 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.KeyPair; import java.security.KeyPair;
import java.util.AbstractCollection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -73,10 +71,10 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.IntFunction; import java.util.function.IntFunction;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.text.Component; import net.kyori.text.Component;
import net.kyori.text.TextComponent;
import net.kyori.text.TranslatableComponent;
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.asynchttpclient.AsyncHttpClient; import org.asynchttpclient.AsyncHttpClient;
@ -92,12 +90,15 @@ public class VelocityServer implements ProxyServer {
.registerTypeHierarchyAdapter(Favicon.class, new FaviconSerializer()) .registerTypeHierarchyAdapter(Favicon.class, new FaviconSerializer())
.registerTypeHierarchyAdapter(GameProfile.class, new GameProfileSerializer()) .registerTypeHierarchyAdapter(GameProfile.class, new GameProfileSerializer())
.create(); .create();
private static final Gson PRE_1_16_COMPONENT_SERIALIZER = private static final Gson PRE_1_16_PING_SERIALIZER = GsonComponentSerializer
wrapAndAddTextSerializers(GsonComponentSerializer.colorDownsamplingGson()) .colorDownsamplingGson()
.serializer()
.newBuilder()
.registerTypeHierarchyAdapter(Favicon.class, new FaviconSerializer()) .registerTypeHierarchyAdapter(Favicon.class, new FaviconSerializer())
.create(); .create();
private static final Gson POST_1_16_COMPONENT_SERIALIZER = private static final Gson POST_1_16_PING_SERIALIZER = GsonComponentSerializer.gson()
wrapAndAddTextSerializers(GsonComponentSerializer.gson()) .serializer()
.newBuilder()
.registerTypeHierarchyAdapter(Favicon.class, new FaviconSerializer()) .registerTypeHierarchyAdapter(Favicon.class, new FaviconSerializer())
.create(); .create();
@ -614,13 +615,8 @@ public class VelocityServer implements ProxyServer {
getAllPlayers().iterator()); getAllPlayers().iterator());
} }
public static Gson getGsonInstance(ProtocolVersion version) { public static Gson getPingGsonInstance(ProtocolVersion version) {
return version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0 ? POST_1_16_COMPONENT_SERIALIZER return version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0 ? POST_1_16_PING_SERIALIZER
: PRE_1_16_COMPONENT_SERIALIZER; : PRE_1_16_PING_SERIALIZER;
}
private static GsonBuilder wrapAndAddTextSerializers(GsonComponentSerializer serializer) {
return net.kyori.text.serializer.gson.GsonComponentSerializer
.populate(serializer.serializer().newBuilder());
} }
} }

Datei anzeigen

@ -43,8 +43,8 @@ import java.util.Optional;
import java.util.Queue; import java.util.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
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.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.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@ -134,7 +134,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
player.getUsername(), e); player.getUsername(), e);
player.sendMessage( player.sendMessage(
TextComponent.of("An error occurred while running this command.", TextComponent.of("An error occurred while running this command.",
TextColor.RED)); NamedTextColor.RED));
return null; return null;
}); });
} else { } else {
@ -272,7 +272,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
@Override @Override
public void exception(Throwable throwable) { public void exception(Throwable throwable) {
player.disconnect(TextComponent.of("Your connection has encountered an error. Try again later.", player.disconnect(TextComponent.of("Your connection has encountered an error. Try again later.",
TextColor.RED)); NamedTextColor.RED));
} }
@Override @Override

Datei anzeigen

@ -4,8 +4,6 @@ import static com.velocitypowered.proxy.connection.util.ConnectionRequestResults
import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.CompletableFuture.completedFuture;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.velocitypowered.api.event.connection.DisconnectEvent; import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.player.KickedFromServerEvent; import com.velocitypowered.api.event.player.KickedFromServerEvent;
@ -39,6 +37,7 @@ import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeConstants; import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeConstants;
import com.velocitypowered.proxy.connection.util.ConnectionMessages; import com.velocitypowered.proxy.connection.util.ConnectionMessages;
import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.packet.Chat; import com.velocitypowered.proxy.protocol.packet.Chat;
import com.velocitypowered.proxy.protocol.packet.ClientSettings; import com.velocitypowered.proxy.protocol.packet.ClientSettings;
@ -69,7 +68,6 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
@ -266,7 +264,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
// We can use the title packet instead. // We can use the title packet instead.
TitlePacket pkt = new TitlePacket(); TitlePacket pkt = new TitlePacket();
pkt.setAction(TitlePacket.SET_ACTION_BAR); pkt.setAction(TitlePacket.SET_ACTION_BAR);
pkt.setComponent(VelocityServer.getGsonInstance(this.getProtocolVersion()).toJson(message)); pkt.setComponent(ProtocolUtils.getJsonChatSerializer(this.getProtocolVersion())
.serialize(message));
connection.write(pkt); connection.write(pkt);
} else { } else {
// Due to issues with action bar packets, we'll need to convert the text message into a // Due to issues with action bar packets, we'll need to convert the text message into a
@ -282,16 +281,16 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
@Override @Override
public void showTitle(net.kyori.adventure.title.@NonNull Title title) { public void showTitle(net.kyori.adventure.title.@NonNull Title title) {
Gson gson = VelocityServer.getGsonInstance(this.getProtocolVersion()); GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer(this.getProtocolVersion());
TitlePacket titlePkt = new TitlePacket(); TitlePacket titlePkt = new TitlePacket();
titlePkt.setAction(TitlePacket.SET_TITLE); titlePkt.setAction(TitlePacket.SET_TITLE);
titlePkt.setComponent(gson.toJson(title.title())); titlePkt.setComponent(serializer.serialize(title.title()));
connection.delayedWrite(titlePkt); connection.delayedWrite(titlePkt);
TitlePacket subtitlePkt = new TitlePacket(); TitlePacket subtitlePkt = new TitlePacket();
subtitlePkt.setAction(TitlePacket.SET_SUBTITLE); subtitlePkt.setAction(TitlePacket.SET_SUBTITLE);
subtitlePkt.setComponent(gson.toJson(title.subtitle())); subtitlePkt.setComponent(serializer.serialize(title.subtitle()));
connection.delayedWrite(titlePkt); connection.delayedWrite(titlePkt);
TitlePacket timesPkt = TitlePacket.timesForProtocolVersion(this.getProtocolVersion()); TitlePacket timesPkt = TitlePacket.timesForProtocolVersion(this.getProtocolVersion());
@ -394,7 +393,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
connection.write(TitlePacket.hideForProtocolVersion(protocolVersion)); connection.write(TitlePacket.hideForProtocolVersion(protocolVersion));
} else if (title instanceof TextTitle) { } else if (title instanceof TextTitle) {
TextTitle tt = (TextTitle) title; TextTitle tt = (TextTitle) title;
Gson gson = VelocityServer.getGsonInstance(this.getProtocolVersion());
if (tt.isResetBeforeSend()) { if (tt.isResetBeforeSend()) {
connection.delayedWrite(TitlePacket.resetForProtocolVersion(protocolVersion)); connection.delayedWrite(TitlePacket.resetForProtocolVersion(protocolVersion));
@ -404,7 +402,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
if (titleText.isPresent()) { if (titleText.isPresent()) {
TitlePacket titlePkt = new TitlePacket(); TitlePacket titlePkt = new TitlePacket();
titlePkt.setAction(TitlePacket.SET_TITLE); titlePkt.setAction(TitlePacket.SET_TITLE);
titlePkt.setComponent(gson.toJson(titleText.get())); titlePkt.setComponent(net.kyori.text.serializer.gson.GsonComponentSerializer.INSTANCE
.serialize(titleText.get()));
connection.delayedWrite(titlePkt); connection.delayedWrite(titlePkt);
} }
@ -412,7 +411,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
if (subtitleText.isPresent()) { if (subtitleText.isPresent()) {
TitlePacket titlePkt = new TitlePacket(); TitlePacket titlePkt = new TitlePacket();
titlePkt.setAction(TitlePacket.SET_SUBTITLE); titlePkt.setAction(TitlePacket.SET_SUBTITLE);
titlePkt.setComponent(gson.toJson(subtitleText.get())); titlePkt.setComponent(net.kyori.text.serializer.gson.GsonComponentSerializer.INSTANCE
.serialize(subtitleText.get()));
connection.delayedWrite(titlePkt); connection.delayedWrite(titlePkt);
} }

Datei anzeigen

@ -124,8 +124,8 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
Response profileResponse = hasJoinedResponse.get(); Response profileResponse = hasJoinedResponse.get();
if (profileResponse.getStatusCode() == 200) { if (profileResponse.getStatusCode() == 200) {
// All went well, initialize the session. // All went well, initialize the session.
initializePlayer(GENERAL_GSON.fromJson(profileResponse.getResponseBody(), GameProfile.class), initializePlayer(GENERAL_GSON.fromJson(profileResponse.getResponseBody(),
true); GameProfile.class), true);
} else if (profileResponse.getStatusCode() == 204) { } else if (profileResponse.getStatusCode() == 204) {
// Apparently an offline-mode user logged onto this online-mode proxy. // Apparently an offline-mode user logged onto this online-mode proxy.
inbound.disconnect(VelocityMessages.ONLINE_MODE_ONLY); inbound.disconnect(VelocityMessages.ONLINE_MODE_ONLY);

Datei anzeigen

@ -173,7 +173,7 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
.thenAcceptAsync( .thenAcceptAsync(
(event) -> { (event) -> {
StringBuilder json = new StringBuilder(); StringBuilder json = new StringBuilder();
VelocityServer.getGsonInstance(connection.getProtocolVersion()) VelocityServer.getPingGsonInstance(connection.getProtocolVersion())
.toJson(event.getPing(), json); .toJson(event.getPing(), json);
connection.write(new StatusResponse(json)); connection.write(new StatusResponse(json));
}, },

Datei anzeigen

@ -117,8 +117,8 @@ public class JavaPluginLoader implements PluginLoader {
while ((entry = in.getNextJarEntry()) != null) { while ((entry = in.getNextJarEntry()) != null) {
if (entry.getName().equals("velocity-plugin.json")) { if (entry.getName().equals("velocity-plugin.json")) {
try (Reader pluginInfoReader = new InputStreamReader(in, StandardCharsets.UTF_8)) { try (Reader pluginInfoReader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
return Optional.of(VelocityServer.GENERAL_GSON return Optional.of(VelocityServer.GENERAL_GSON.fromJson(pluginInfoReader,
.fromJson(pluginInfoReader, SerializedPluginDescription.class)); SerializedPluginDescription.class));
} }
} }

Datei anzeigen

@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.nbt.CompoundTag; import net.kyori.nbt.CompoundTag;
import net.kyori.nbt.TagIO; import net.kyori.nbt.TagIO;
import net.kyori.nbt.TagType; import net.kyori.nbt.TagType;
@ -460,6 +461,20 @@ public enum ProtocolUtils {
return readString(buf, DEFAULT_MAX_STRING_SIZE, buf.readableBytes()); return readString(buf, DEFAULT_MAX_STRING_SIZE, buf.readableBytes());
} }
/**
* Returns the appropriate {@link GsonComponentSerializer} for the given protocol version. This
* is used to constrain messages sent to older clients.
*
* @param version the protocol version used by the client.
* @return the appropriate {@link GsonComponentSerializer}
*/
public static GsonComponentSerializer getJsonChatSerializer(ProtocolVersion version) {
if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) {
return GsonComponentSerializer.gson();
}
return GsonComponentSerializer.colorDownsamplingGson();
}
public enum Direction { public enum Direction {
SERVERBOUND, SERVERBOUND,
CLIENTBOUND; CLIENTBOUND;

Datei anzeigen

@ -2,7 +2,6 @@ package com.velocitypowered.proxy.protocol.packet;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.ProtocolUtils;
@ -116,7 +115,8 @@ public class Chat implements MinecraftPacket {
public static Chat createClientbound(net.kyori.adventure.text.Component component, byte type, public static Chat createClientbound(net.kyori.adventure.text.Component component, byte type,
UUID sender, ProtocolVersion version) { UUID sender, ProtocolVersion version) {
Preconditions.checkNotNull(component, "component"); Preconditions.checkNotNull(component, "component");
return new Chat(VelocityServer.getGsonInstance(version).toJson(component), type, sender); return new Chat(ProtocolUtils.getJsonChatSerializer(version).serialize(component), type,
sender);
} }
public static Chat createServerbound(String message) { public static Chat createServerbound(String message) {

Datei anzeigen

@ -2,7 +2,6 @@ package com.velocitypowered.proxy.protocol.packet;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.ProtocolUtils;
@ -67,6 +66,6 @@ public class Disconnect implements MinecraftPacket {
public static Disconnect create(net.kyori.adventure.text.Component component, public static Disconnect create(net.kyori.adventure.text.Component component,
ProtocolVersion version) { ProtocolVersion version) {
Preconditions.checkNotNull(component, "component"); Preconditions.checkNotNull(component, "component");
return new Disconnect(VelocityServer.getGsonInstance(version).toJson(component)); return new Disconnect(ProtocolUtils.getJsonChatSerializer(version).serialize(component));
} }
} }

Datei anzeigen

@ -3,16 +3,12 @@ package com.velocitypowered.proxy.protocol.packet;
import static com.velocitypowered.proxy.protocol.ProtocolUtils.writeString; import static com.velocitypowered.proxy.protocol.ProtocolUtils.writeString;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.ProtocolUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.kyori.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.text.serializer.ComponentSerializer;
import net.kyori.text.serializer.gson.GsonComponentSerializer;
public class HeaderAndFooter implements MinecraftPacket { public class HeaderAndFooter implements MinecraftPacket {
@ -55,15 +51,17 @@ public class HeaderAndFooter implements MinecraftPacket {
return handler.handle(this); return handler.handle(this);
} }
public static HeaderAndFooter create(Component header, Component footer) { public static HeaderAndFooter create(net.kyori.text.Component header,
ComponentSerializer<Component, Component, String> json = GsonComponentSerializer.INSTANCE; net.kyori.text.Component footer) {
return new HeaderAndFooter(json.serialize(header), json.serialize(footer)); return new HeaderAndFooter(
net.kyori.text.serializer.gson.GsonComponentSerializer.INSTANCE.serialize(header),
net.kyori.text.serializer.gson.GsonComponentSerializer.INSTANCE.serialize(footer));
} }
public static HeaderAndFooter create(net.kyori.adventure.text.Component header, public static HeaderAndFooter create(net.kyori.adventure.text.Component header,
net.kyori.adventure.text.Component footer, ProtocolVersion protocolVersion) { net.kyori.adventure.text.Component footer, ProtocolVersion protocolVersion) {
Gson serializer = VelocityServer.getGsonInstance(protocolVersion); GsonComponentSerializer serializer = ProtocolUtils.getJsonChatSerializer(protocolVersion);
return new HeaderAndFooter(serializer.toJson(header), serializer.toJson(footer)); return new HeaderAndFooter(serializer.serialize(header), serializer.serialize(footer));
} }
public static HeaderAndFooter reset() { public static HeaderAndFooter reset() {

Datei anzeigen

@ -147,7 +147,8 @@ public class PlayerListItem implements MinecraftPacket {
ProtocolVersion version) { ProtocolVersion version) {
buf.writeBoolean(displayName != null); buf.writeBoolean(displayName != null);
if (displayName != null) { if (displayName != null) {
ProtocolUtils.writeString(buf, VelocityServer.getGsonInstance(version).toJson(displayName)); ProtocolUtils.writeString(buf, ProtocolUtils.getJsonChatSerializer(version)
.serialize(displayName));
} }
} }

Datei anzeigen

@ -50,7 +50,7 @@ public class PingSessionHandler implements MinecraftSessionHandler {
completed = true; completed = true;
connection.close(true); connection.close(true);
ServerPing ping = VelocityServer.getGsonInstance(version).fromJson(packet.getStatus(), ServerPing ping = VelocityServer.getPingGsonInstance(version).fromJson(packet.getStatus(),
ServerPing.class); ServerPing.class);
result.complete(ping); result.complete(ping);
return true; return true;