Handle 1.19 chat types

This commit is contained in:
Nassim Jahnke 2022-07-28 12:38:51 +02:00
parent a88747d904
commit 322af00e80
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
9 changed files with 242 additions and 16 deletions

View File

@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.WorldIdentifiers; import com.viaversion.viaversion.api.minecraft.WorldIdentifiers;
import com.viaversion.viaversion.api.protocol.version.BlockedProtocolVersions; import com.viaversion.viaversion.api.protocol.version.BlockedProtocolVersions;
import it.unimi.dsi.fastutil.ints.IntSet; import it.unimi.dsi.fastutil.ints.IntSet;
import org.checkerframework.checker.nullness.qual.Nullable;
public interface ViaVersionConfig { public interface ViaVersionConfig {
@ -455,4 +456,6 @@ public interface ViaVersionConfig {
WorldIdentifiers get1_16WorldNamesMap(); WorldIdentifiers get1_16WorldNamesMap();
boolean cache1_17Light(); boolean cache1_17Light();
@Nullable String chatTypeFormat(String translationKey);
} }

View File

@ -302,7 +302,7 @@ public interface Protocol<C1 extends ClientboundPacketType, C2 extends Clientbou
* *
* @param userConnection The user to initialise * @param userConnection The user to initialise
*/ */
default void init(UserConnection userConnection) { default void init(UserConnection connection) {
} }
/** /**

View File

@ -90,6 +90,7 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
private JsonElement resourcePack1_17PromptMessage; private JsonElement resourcePack1_17PromptMessage;
private WorldIdentifiers map1_16WorldNames; private WorldIdentifiers map1_16WorldNames;
private boolean cache1_17Light; private boolean cache1_17Light;
private Map<String, String> chatTypeFormats;
protected AbstractViaConfig(File configFile) { protected AbstractViaConfig(File configFile) {
super(configFile); super(configFile);
@ -158,6 +159,7 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
worlds.getOrDefault("nether", WorldIdentifiers.NETHER_DEFAULT), worlds.getOrDefault("nether", WorldIdentifiers.NETHER_DEFAULT),
worlds.getOrDefault("end", WorldIdentifiers.END_DEFAULT)); worlds.getOrDefault("end", WorldIdentifiers.END_DEFAULT));
cache1_17Light = getBoolean("cache-1_17-light", true); cache1_17Light = getBoolean("cache-1_17-light", true);
chatTypeFormats = get("chat-types-1_19", Map.class, new HashMap<String, String>());
} }
private BlockedProtocolVersions loadBlockedProtocolVersions() { private BlockedProtocolVersions loadBlockedProtocolVersions() {
@ -526,4 +528,9 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
public boolean cache1_17Light() { public boolean cache1_17Light() {
return cache1_17Light; return cache1_17Light;
} }
@Override
public @Nullable String chatTypeFormat(final String translationKey) {
return chatTypeFormats.get(translationKey);
}
} }

View File

@ -17,16 +17,31 @@
*/ */
package com.viaversion.viaversion.protocols.protocol1_19_1to1_19; package com.viaversion.viaversion.protocols.protocol1_19_1to1_19;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.common.base.Preconditions;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.ProfileKey; import com.viaversion.viaversion.api.minecraft.ProfileKey;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO; import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.libs.kyori.adventure.text.Component;
import com.viaversion.viaversion.libs.kyori.adventure.text.TextReplacementConfig;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.NamedTextColor;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.TextDecoration;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.storage.ChatTypeStorage;
import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.storage.NonceStorage; import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.storage.NonceStorage;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ClientboundPackets1_19; import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ClientboundPackets1_19;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ServerboundPackets1_19; import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ServerboundPackets1_19;
@ -94,18 +109,18 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
@Override @Override
public void registerMap() { public void registerMap() {
handler(wrapper -> { handler(wrapper -> {
// Back to system chat - bye bye chat formats for 1.19.0 servers // Back to system chat
// ... not that big of a deal since the majority of modded servers only has Vanilla /say command and the alike sent as proper player chat
final JsonElement signedContent = wrapper.read(Type.COMPONENT); final JsonElement signedContent = wrapper.read(Type.COMPONENT);
final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT); final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT);
wrapper.write(Type.COMPONENT, unsignedContent != null ? unsignedContent : signedContent); final int chatType = wrapper.read(Type.VAR_INT);
final int type = wrapper.read(Type.VAR_INT); wrapper.read(Type.UUID); // Sender UUID
wrapper.write(Type.BOOLEAN, type == 2); // Overlay, going by the default 1.19 chat type registry final JsonElement senderName = wrapper.read(Type.COMPONENT);
final JsonElement teamName = wrapper.read(Type.OPTIONAL_COMPONENT);
if (!decorateChatMessage(wrapper, chatType, senderName, teamName, unsignedContent != null ? unsignedContent : signedContent)) {
wrapper.cancel();
}
}); });
read(Type.UUID); // Sender uuid
read(Type.COMPONENT); // Sender display name
read(Type.OPTIONAL_COMPONENT); // Target display name
read(Type.LONG); // Timestamp read(Type.LONG); // Timestamp
read(Type.LONG); // Salt read(Type.LONG); // Salt
read(Type.BYTE_ARRAY_PRIMITIVE); // Signature read(Type.BYTE_ARRAY_PRIMITIVE); // Signature
@ -151,11 +166,20 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
map(Type.UNSIGNED_BYTE); // Gamemode map(Type.UNSIGNED_BYTE); // Gamemode
map(Type.BYTE); // Previous Gamemode map(Type.BYTE); // Previous Gamemode
map(Type.STRING_ARRAY); // World List map(Type.STRING_ARRAY); // World List
map(Type.NBT); // Registry
handler(wrapper -> { handler(wrapper -> {
// Replace chat types - not worth the effort of handling them properly final ChatTypeStorage chatTypeStorage = wrapper.user().get(ChatTypeStorage.class);
final CompoundTag tag = wrapper.get(Type.NBT, 0); chatTypeStorage.clear();
tag.put("minecraft:chat_type", CHAT_REGISTRY.clone());
final CompoundTag registry = wrapper.passthrough(Type.NBT);
final ListTag chatTypes = ((CompoundTag) registry.get("minecraft:chat_type")).get("value");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
chatTypeStorage.addChatType(idTag.asInt(), chatTypeCompound);
}
// Replace chat types - they won't actually be used
registry.put("minecraft:chat_type", CHAT_REGISTRY.clone());
}); });
} }
}); });
@ -222,4 +246,83 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
} }
}); });
} }
@Override
public void init(final UserConnection connection) {
connection.put(new ChatTypeStorage());
}
private TextReplacementConfig replace(final JsonElement replacement) {
return TextReplacementConfig.builder().matchLiteral("%s").replacement(GsonComponentSerializer.gson().deserializeFromTree(replacement)).once().build();
}
private boolean decorateChatMessage(final PacketWrapper wrapper, final int chatTypeId, final JsonElement senderName, final JsonElement teamName, final JsonElement message) {
final CompoundTag chatType = wrapper.user().get(ChatTypeStorage.class).chatType(chatTypeId);
if (chatType == null) {
Via.getPlatform().getLogger().warning("Chat message has unknown chat type id " + chatTypeId + ". Message: " + message);
return false;
}
CompoundTag chatData = chatType.<CompoundTag>get("element").get("chat");
boolean overlay = false;
if (chatData == null) {
chatData = chatType.<CompoundTag>get("element").get("overlay");
if (chatData == null) {
// Either narration or something we don't know
return false;
}
overlay = true;
}
final CompoundTag decoaration = chatData.get("decoration");
if (decoaration == null) {
wrapper.write(Type.COMPONENT, message);
wrapper.write(Type.BOOLEAN, overlay);
return true;
}
final String translationKey = (String) decoaration.get("translation_key").getValue();
final String rawTranslation = Via.getConfig().chatTypeFormat(translationKey);
if (rawTranslation == null) {
Via.getPlatform().getLogger().warning("Missing chat type translation for key " + translationKey);
return false;
}
Component component = Component.text(rawTranslation);
final CompoundTag style = decoaration.get("style");
if (style != null) {
final StringTag color = style.get("color");
if (color != null && NamedTextColor.NAMES.value(color.getValue()) != null) {
component = component.color(NamedTextColor.NAMES.value(color.getValue()));
}
for (final String key : TextDecoration.NAMES.keys()) {
if (style.contains(key) && style.<ByteTag>get(key).asByte() == 1) {
component = component.decorate(TextDecoration.NAMES.value(key));
}
}
}
final ListTag parameters = decoaration.get("parameters");
if (parameters != null) for (final Tag element : parameters) {
switch ((String) element.getValue()) {
case "sender":
component = component.replaceText(replace(senderName));
break;
case "content":
component = component.replaceText(replace(message));
break;
case "team_name":
Preconditions.checkNotNull(teamName, "Team name is null");
component = component.replaceText(replace(teamName));
break;
default:
Via.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue());
}
}
wrapper.write(Type.COMPONENT, GsonComponentSerializer.gson().serializeToTree(component));
wrapper.write(Type.BOOLEAN, overlay);
return true;
}
} }

View File

@ -0,0 +1,47 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2022 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.protocols.protocol1_19_1to1_19.storage;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.api.connection.StorableObject;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.Protocol1_19To1_18_2;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class ChatTypeStorage implements StorableObject {
private final Int2ObjectMap<CompoundTag> chatTypes = new Int2ObjectOpenHashMap<>();
public @Nullable CompoundTag chatType(final int id) {
return chatTypes.isEmpty() ? Protocol1_19To1_18_2.MAPPINGS.chatType(id) : chatTypes.get(id);
}
public void addChatType(final int id, final CompoundTag chatType) {
chatTypes.put(id, chatType);
}
public void clear() {
chatTypes.clear();
}
@Override
public boolean clearOnServerSwitch() {
return false;
}
}

View File

@ -20,8 +20,6 @@ package com.viaversion.viaversion.protocols.protocol1_19to1_18_2;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.MappingData;
import com.viaversion.viaversion.api.data.MappingDataBase;
import com.viaversion.viaversion.api.minecraft.entities.Entity1_19Types; import com.viaversion.viaversion.api.minecraft.entities.Entity1_19Types;
import com.viaversion.viaversion.api.platform.providers.ViaProviders; import com.viaversion.viaversion.api.platform.providers.ViaProviders;
import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.AbstractProtocol;
@ -40,6 +38,7 @@ import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17;
import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.data.MappingData;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.EntityPackets; import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.EntityPackets;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.InventoryPackets; import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.WorldPackets; import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.WorldPackets;
@ -57,7 +56,7 @@ import java.util.concurrent.ThreadLocalRandom;
public final class Protocol1_19To1_18_2 extends AbstractProtocol<ClientboundPackets1_18, ClientboundPackets1_19, ServerboundPackets1_17, ServerboundPackets1_19> { public final class Protocol1_19To1_18_2 extends AbstractProtocol<ClientboundPackets1_18, ClientboundPackets1_19, ServerboundPackets1_17, ServerboundPackets1_19> {
public static final MappingData MAPPINGS = new MappingDataBase("1.18", "1.19", true); public static final MappingData MAPPINGS = new MappingData();
private final EntityPackets entityRewriter = new EntityPackets(this); private final EntityPackets entityRewriter = new EntityPackets(this);
private final InventoryPackets itemRewriter = new InventoryPackets(this); private final InventoryPackets itemRewriter = new InventoryPackets(this);

View File

@ -0,0 +1,59 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2022 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.protocols.protocol1_19to1_18_2.data;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.data.MappingDataBase;
import com.viaversion.viaversion.api.data.MappingDataLoader;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.IOException;
public final class MappingData extends MappingDataBase {
private final Int2ObjectMap<CompoundTag> defaultChatTypes = new Int2ObjectOpenHashMap<>();
public MappingData() {
super("1.18", "1.19", true);
}
@Override
protected void loadExtras(final JsonObject oldMappings, final JsonObject newMappings, @Nullable final JsonObject diffMappings) {
try {
final ListTag chatTypes = BinaryTagIO.readCompressedInputStream(MappingDataLoader.getResource("chat-types-1.19.nbt")).get("values");
for (final Tag chatType : chatTypes) {
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
final NumberTag idTag = chatTypeCompound.get("id");
defaultChatTypes.put(idTag.asInt(), chatTypeCompound);
}
} catch (final IOException e) {
e.printStackTrace();
}
}
public @Nullable CompoundTag chatType(final int id) {
return defaultChatTypes.get(id);
}
}

View File

@ -164,6 +164,14 @@ resource-pack-1_17-prompt: ''
# Only disable this if you know what you are doing. # Only disable this if you know what you are doing.
cache-1_17-light: true cache-1_17-light: true
# #
# 1.19 chat type formats used for 1.19.1+ clients.
chat-types-1_19:
"chat.type.text": "<%s> %s"
"chat.type.announcement": "[%s] %s"
"commands.message.display.incoming": "%s whispers to you: %s"
"chat.type.team.text": "%s <%s> %s"
"chat.type.emote": "* %s %s"
#
#----------------------------------------------------------# #----------------------------------------------------------#
# 1.9+ CLIENTS ON 1.8 SERVERS OPTIONS # # 1.9+ CLIENTS ON 1.8 SERVERS OPTIONS #
#----------------------------------------------------------# #----------------------------------------------------------#