mirror of
https://github.com/ViaVersion/ViaBackwards.git
synced 2025-01-02 18:48:06 +01:00
Handle 1.19.1 chat types
This commit is contained in:
parent
7173bbbbce
commit
a856658399
@ -40,9 +40,13 @@ import com.viaversion.viaversion.data.entity.EntityTrackerBase;
|
||||
import com.viaversion.viaversion.libs.gson.JsonElement;
|
||||
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.libs.opennbt.tag.builtin.ByteTag;
|
||||
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
|
||||
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag;
|
||||
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
|
||||
import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag;
|
||||
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
|
||||
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
|
||||
@ -56,7 +60,6 @@ import com.viaversion.viaversion.rewriter.StatisticsRewriter;
|
||||
import com.viaversion.viaversion.rewriter.TagRewriter;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class Protocol1_18_2To1_19 extends BackwardsProtocol<ClientboundPackets1_19, ClientboundPackets1_18, ServerboundPackets1_19, ServerboundPackets1_17> {
|
||||
@ -201,13 +204,19 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol<ClientboundPac
|
||||
final JsonElement unsignedMessage = wrapper.read(Type.OPTIONAL_COMPONENT);
|
||||
wrapper.write(Type.COMPONENT, unsignedMessage != null ? unsignedMessage : message);
|
||||
});
|
||||
map(Type.VAR_INT, Type.BYTE); // Chat type
|
||||
map(Type.UUID); // Sender
|
||||
handler(wrapper -> {
|
||||
final int chatTypeId = wrapper.read(Type.VAR_INT);
|
||||
wrapper.write(Type.BYTE, (byte) 1);
|
||||
wrapper.passthrough(Type.UUID);
|
||||
final JsonElement senderName = wrapper.read(Type.COMPONENT);
|
||||
final JsonElement teamName = wrapper.read(Type.OPTIONAL_COMPONENT);
|
||||
final JsonElement element = wrapper.get(Type.COMPONENT, 0);
|
||||
handleChatType(wrapper, senderName, teamName, element);
|
||||
final JsonElement decoratedMessage = decorateChatMessage(wrapper, chatTypeId, senderName, teamName, element);
|
||||
if (decoratedMessage == null) {
|
||||
wrapper.cancel();
|
||||
} else {
|
||||
wrapper.set(Type.COMPONENT, 0, decoratedMessage);
|
||||
}
|
||||
});
|
||||
read(Type.LONG); // Timestamp
|
||||
read(Type.LONG); // Salt
|
||||
@ -219,9 +228,9 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol<ClientboundPac
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.COMPONENT); // Message
|
||||
map(Type.VAR_INT, Type.BYTE); // Chat type
|
||||
read(Type.VAR_INT);
|
||||
create(Type.BYTE, (byte) 0);
|
||||
create(Type.UUID, ZERO_UUID); // Sender
|
||||
handler(wrapper -> handleChatType(wrapper, null, null, wrapper.get(Type.COMPONENT, 0)));
|
||||
}
|
||||
});
|
||||
|
||||
@ -312,39 +321,43 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol<ClientboundPac
|
||||
return TextReplacementConfig.builder().matchLiteral("%s").replacement(GsonComponentSerializer.gson().deserializeFromTree(replacement)).once().build();
|
||||
}
|
||||
|
||||
private void handleChatType(final PacketWrapper wrapper, final JsonElement senderName, final JsonElement teamName, final JsonElement message) throws Exception {
|
||||
private JsonElement decorateChatMessage(final PacketWrapper wrapper, final int chatTypeId, final JsonElement senderName, final JsonElement teamName, final JsonElement message) throws Exception {
|
||||
translatableRewriter.processText(message);
|
||||
|
||||
final byte id = wrapper.get(Type.BYTE, 0);
|
||||
CompoundTag chatType = wrapper.user().get(DimensionRegistryStorage.class).chatType(id);
|
||||
CompoundTag handlers = chatType.get("element");
|
||||
|
||||
boolean handled = false;
|
||||
for (Map.Entry<String, Tag> handler : handlers) {
|
||||
byte oldId;
|
||||
switch (handler.getKey()) {
|
||||
case "chat":
|
||||
oldId = 1;
|
||||
break;
|
||||
case "overlay":
|
||||
oldId = 2;
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
CompoundTag chatType = wrapper.user().get(DimensionRegistryStorage.class).chatType(chatTypeId);
|
||||
if (chatType == null) {
|
||||
ViaBackwards.getPlatform().getLogger().warning("Chat message has unknown chat type id " + chatTypeId + ". Message: " + message);
|
||||
return null;
|
||||
}
|
||||
|
||||
CompoundTag decoration = ((CompoundTag) handler.getValue()).get("decoration");
|
||||
chatType = chatType.<CompoundTag> get("element").get("chat");
|
||||
if (chatType == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
JsonElement formattedMessage;
|
||||
if (decoration != null) {
|
||||
String translationKey = (String) decoration.get("translation_key").getValue();
|
||||
String translationKey = (String) chatType.get("translation_key").getValue();
|
||||
String rawTranslation = ViaBackwards.getConfig().chatTypeFormat(translationKey);
|
||||
if (rawTranslation == null) {
|
||||
ViaBackwards.getPlatform().getLogger().warning("Missing chat type translation for key " + translationKey);
|
||||
continue;
|
||||
return null;
|
||||
}
|
||||
|
||||
Component component = Component.text(rawTranslation);
|
||||
ListTag parameters = decoration.get("parameters");
|
||||
|
||||
CompoundTag style = chatType.get("style");
|
||||
if (style != null) {
|
||||
StringTag color = style.get("color");
|
||||
if (color != null && NamedTextColor.NAMES.value(color.getValue()) != null) {
|
||||
component.color(NamedTextColor.NAMES.value(color.getValue()));
|
||||
}
|
||||
for (String key : TextDecoration.NAMES.keys()) {
|
||||
if (style.contains(key) && style.<ByteTag> get(key).asByte() == 1) {
|
||||
component.decorate(TextDecoration.NAMES.value(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ListTag parameters = chatType.get("parameters");
|
||||
if (parameters != null) for (Tag element : parameters) {
|
||||
switch ((String) element.getValue()) {
|
||||
case "sender":
|
||||
@ -361,23 +374,7 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol<ClientboundPac
|
||||
ViaBackwards.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue());
|
||||
}
|
||||
}
|
||||
formattedMessage = GsonComponentSerializer.gson().serializeToTree(component);
|
||||
} else {
|
||||
formattedMessage = message;
|
||||
}
|
||||
|
||||
if (!handled) {
|
||||
handled = true;
|
||||
wrapper.set(Type.BYTE, 0, oldId);
|
||||
wrapper.set(Type.COMPONENT, 0, formattedMessage);
|
||||
} else {
|
||||
PacketWrapper extra = wrapper.create(ClientboundPackets1_18.CHAT_MESSAGE);
|
||||
extra.write(Type.COMPONENT, formattedMessage);
|
||||
extra.write(Type.BYTE, oldId);
|
||||
extra.write(Type.UUID, wrapper.get(Type.UUID, 0));
|
||||
}
|
||||
}
|
||||
|
||||
if (!handled) wrapper.cancel();
|
||||
return GsonComponentSerializer.gson().serializeToTree(component);
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ public final class BackwardsMappings extends com.viaversion.viabackwards.api.dat
|
||||
}
|
||||
|
||||
try {
|
||||
ListTag chatTypes = BinaryTagIO.readCompressedInputStream(VBMappingDataLoader.getResource("chat-types-1.19.nbt")).get("values");
|
||||
ListTag chatTypes = BinaryTagIO.readCompressedInputStream(VBMappingDataLoader.getResource("chat-types-1.19.1.nbt")).get("values");
|
||||
for (final Tag chatType : chatTypes) {
|
||||
final CompoundTag chatTypeCompound = (CompoundTag) chatType;
|
||||
final NumberTag idTag = chatTypeCompound.get("id");
|
||||
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user