3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-26 16:12:42 +01:00

Use mcstructs for component conversion

Dieser Commit ist enthalten in:
Nassim Jahnke 2023-12-26 23:38:02 +01:00
Ursprung 0e31664bfe
Commit c62a8274ed
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
23 geänderte Dateien mit 275 neuen und 447 gelöschten Zeilen

Datei anzeigen

@ -1,25 +0,0 @@
plugins {
id("com.github.johnrengelman.shadow")
}
// Shade and relocate adventure in an extra module, so that common/the rest can directly depend on a
// relocated adventure without breaking native platform's adventure usage with project wide relocation
tasks {
shadowJar {
relocate("net.kyori", "com.viaversion.viaversion.libs.kyori")
}
build {
dependsOn(shadowJar)
}
}
dependencies {
api(libs.bundles.adventure) {
exclude("org.checkerframework")
exclude("net.kyori", "adventure-api")
exclude("net.kyori", "adventure-bom")
exclude("com.google.code.gson", "gson")
}
}
publishShadowJar()

Datei anzeigen

@ -15,9 +15,6 @@ sourceSets {
} }
dependencies { dependencies {
api(projects.adventure) {
targetConfiguration = "shadow"
}
api(libs.fastutil) api(libs.fastutil)
api(libs.flare) api(libs.flare)
api(libs.flareFastutil) api(libs.flareFastutil)

Datei anzeigen

@ -26,6 +26,7 @@ fun ShadowJar.configureRelocations() {
relocate("com.github.steveice10.opennbt", "com.viaversion.viaversion.libs.opennbt") relocate("com.github.steveice10.opennbt", "com.viaversion.viaversion.libs.opennbt")
relocate("it.unimi.dsi.fastutil", "com.viaversion.viaversion.libs.fastutil") relocate("it.unimi.dsi.fastutil", "com.viaversion.viaversion.libs.fastutil")
relocate("space.vectrix.flare", "com.viaversion.viaversion.libs.flare") relocate("space.vectrix.flare", "com.viaversion.viaversion.libs.flare")
relocate("net.lenni0451.mcstructs", "com.viaversion.viaversion.libs.mcstructs")
} }
fun ShadowJar.configureExcludes() { fun ShadowJar.configureExcludes() {

Datei anzeigen

@ -20,14 +20,12 @@ val main = setOf(
projects.viaversionVelocity projects.viaversionVelocity
).map { it.dependencyProject } ).map { it.dependencyProject }
val special = setOf( // val special = setOf().map { it.dependencyProject }
projects.adventure
).map { it.dependencyProject }
subprojects { subprojects {
when (this) { when (this) {
in main -> plugins.apply("via.shadow-conventions") in main -> plugins.apply("via.shadow-conventions")
in special -> plugins.apply("via.base-conventions") // in special -> plugins.apply("via.base-conventions")
else -> plugins.apply("via.standard-conventions") else -> plugins.apply("via.standard-conventions")
} }
} }

Datei anzeigen

@ -1,6 +1,9 @@
dependencies { dependencies {
api(projects.viaversionApi) api(projects.viaversionApi)
api(projects.viaversionApiLegacy) api(projects.viaversionApiLegacy)
api(rootProject.libs.text) {
exclude("com.google.code.gson", "gson")
}
implementation(projects.compat.snakeyaml2Compat) implementation(projects.compat.snakeyaml2Compat)
implementation(projects.compat.snakeyaml1Compat) implementation(projects.compat.snakeyaml1Compat)

Datei anzeigen

@ -1,75 +0,0 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2023 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_13to1_12_2;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.libs.kyori.adventure.text.Component;
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.kyori.adventure.text.serializer.gson.legacyimpl.NBTLegacyHoverEventSerializer;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.logging.Level;
public final class ChatRewriter {
public static final GsonComponentSerializer HOVER_GSON_SERIALIZER = GsonComponentSerializer.builder().emitLegacyHoverEvent().legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get()).build();
public static JsonObject emptyComponent() {
final JsonObject object = new JsonObject();
object.addProperty("text", "");
return object;
}
public static String emptyComponentString() {
return "{\"text\":\"\"}";
}
public static String legacyTextToJsonString(String message, boolean itemData) {
// Not used for chat messages, so no need for url extraction
Component component = LegacyComponentSerializer.legacySection().deserialize(message);
if (itemData) {
component = Component.text().decoration(TextDecoration.ITALIC, false).append(component).build();
}
return GsonComponentSerializer.gson().serialize(component);
}
public static String legacyTextToJsonString(String legacyText) {
return legacyTextToJsonString(legacyText, false);
}
public static JsonElement legacyTextToJson(String legacyText) {
return JsonParser.parseString(legacyTextToJsonString(legacyText, false));
}
public static String jsonToLegacyText(String value) {
try {
Component component = HOVER_GSON_SERIALIZER.deserialize(value);
return LegacyComponentSerializer.legacySection().serialize(component);
} catch (Exception e) {
Via.getPlatform().getLogger().log(Level.WARNING, "Error converting json text to legacy: " + value, e);
return "";
}
}
@Deprecated/*(forRemoval = true)*/
public static void processTranslate(JsonElement value) {
Via.getManager().getProtocolManager().getProtocol(Protocol1_13To1_12_2.class).getComponentRewriter().processText(value);
}
}

Datei anzeigen

@ -62,6 +62,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockSto
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker;
import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.util.ChatColorUtil; import com.viaversion.viaversion.util.ChatColorUtil;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.GsonUtil; import com.viaversion.viaversion.util.GsonUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -435,7 +436,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
// On create or update // On create or update
if (mode == 0 || mode == 2) { if (mode == 0 || mode == 2) {
String value = wrapper.read(Type.STRING); // Value String value = wrapper.read(Type.STRING); // Value
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(value)); wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(value));
String type = wrapper.read(Type.STRING); String type = wrapper.read(Type.STRING);
// integer or hearts // integer or hearts
@ -456,7 +457,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
if (action == 0 || action == 2) { if (action == 0 || action == 2) {
String displayName = wrapper.read(Type.STRING); // Display Name String displayName = wrapper.read(Type.STRING); // Display Name
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(displayName)); wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(displayName));
String prefix = wrapper.read(Type.STRING); // Prefix moved String prefix = wrapper.read(Type.STRING); // Prefix moved
String suffix = wrapper.read(Type.STRING); // Suffix moved String suffix = wrapper.read(Type.STRING); // Suffix moved
@ -480,8 +481,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
wrapper.write(Type.VAR_INT, colour); wrapper.write(Type.VAR_INT, colour);
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(prefix)); // Prefix wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(prefix)); // Prefix
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(suffix)); // Suffix wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(suffix)); // Suffix
} }
if (action == 0 || action == 3 || action == 4) { if (action == 0 || action == 3 || action == 4) {

Datei anzeigen

@ -18,19 +18,19 @@
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.metadata; package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.metadata;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_13; import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_13;
import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.minecraft.Particle;
import com.viaversion.viaversion.api.type.types.version.Types1_13; import com.viaversion.viaversion.api.type.types.version.Types1_13;
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ParticleRewriter; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ParticleRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.WorldPackets; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.packets.WorldPackets;
import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.List; import java.util.List;
public class MetadataRewriter1_13To1_12_2 extends EntityRewriter<ClientboundPackets1_12_1, Protocol1_13To1_12_2> { public class MetadataRewriter1_13To1_12_2 extends EntityRewriter<ClientboundPackets1_12_1, Protocol1_13To1_12_2> {
@ -51,7 +51,7 @@ public class MetadataRewriter1_13To1_12_2 extends EntityRewriter<ClientboundPack
// Handle String -> Chat DisplayName // Handle String -> Chat DisplayName
if (metadata.id() == 2) { if (metadata.id() == 2) {
if (metadata.getValue() != null && !((String) metadata.getValue()).isEmpty()) { if (metadata.getValue() != null && !((String) metadata.getValue()).isEmpty()) {
metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, ChatRewriter.legacyTextToJson((String) metadata.getValue())); metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, ComponentUtil.legacyToJson((String) metadata.getValue()));
} else { } else {
metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, null); metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, null);
} }

Datei anzeigen

@ -31,7 +31,6 @@ import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
@ -40,6 +39,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.MappingData
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SoundSource; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SoundSource;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SpawnEggRewriter; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SpawnEggRewriter;
import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.Key;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
@ -315,7 +315,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
if (display.get("Name") instanceof StringTag) { if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name"); StringTag name = display.get("Name");
display.put(NBT_TAG_NAME + "|Name", new StringTag(name.getValue())); display.put(NBT_TAG_NAME + "|Name", new StringTag(name.getValue()));
name.setValue(ChatRewriter.legacyTextToJsonString(name.getValue(), true)); name.setValue(ComponentUtil.legacyToJsonString(name.getValue(), true));
} }
} }
// ench is now Enchantments and now uses identifiers // ench is now Enchantments and now uses identifiers
@ -573,7 +573,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
if (display.get("Name") instanceof StringTag) { if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name"); StringTag name = display.get("Name");
StringTag via = display.remove(NBT_TAG_NAME + "|Name"); StringTag via = display.remove(NBT_TAG_NAME + "|Name");
name.setValue(via != null ? via.getValue() : ChatRewriter.jsonToLegacyText(name.getValue())); name.setValue(via != null ? via.getValue() : ComponentUtil.jsonToLegacy(name.getValue()));
} }
} }

Datei anzeigen

@ -26,9 +26,9 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
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.minecraft.Position; import com.viaversion.viaversion.api.minecraft.Position;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage;
import com.viaversion.viaversion.util.ComponentUtil;
public class BannerHandler implements BlockEntityProvider.BlockEntityHandler { public class BannerHandler implements BlockEntityProvider.BlockEntityHandler {
private static final int WALL_BANNER_START = 7110; // 4 each private static final int WALL_BANNER_START = 7110; // 4 each
@ -78,7 +78,7 @@ public class BannerHandler implements BlockEntityProvider.BlockEntityHandler {
Tag name = tag.get("CustomName"); Tag name = tag.get("CustomName");
if (name instanceof StringTag) { if (name instanceof StringTag) {
((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue())); ((StringTag) name).setValue(ComponentUtil.legacyToJsonString(((StringTag) name).getValue()));
} }
return blockId; return blockId;

Datei anzeigen

@ -24,9 +24,9 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
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.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
import com.viaversion.viaversion.util.ComponentUtil;
public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandler { public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandler {
@ -36,7 +36,7 @@ public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandl
public int transform(UserConnection user, CompoundTag tag) { public int transform(UserConnection user, CompoundTag tag) {
Tag name = tag.get("CustomName"); Tag name = tag.get("CustomName");
if (name instanceof StringTag) { if (name instanceof StringTag) {
((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue())); ((StringTag) name).setValue(ComponentUtil.legacyToJsonString(((StringTag) name).getValue()));
} }
Tag out = tag.get("LastOutput"); Tag out = tag.get("LastOutput");
if (out instanceof StringTag) { if (out instanceof StringTag) {

Datei anzeigen

@ -17,7 +17,11 @@
*/ */
package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets; package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets;
import com.github.steveice10.opennbt.tag.builtin.*; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -27,7 +31,6 @@ import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
@ -37,8 +40,8 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTr
import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.rewriter.RecipeRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.Key;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@ -248,7 +251,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_13, Serve
display.put(NBT_TAG_NAME + "|Lore", new ListTag(lore.clone().getValue())); // Save old lore display.put(NBT_TAG_NAME + "|Lore", new ListTag(lore.clone().getValue())); // Save old lore
for (Tag loreEntry : lore) { for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) { if (loreEntry instanceof StringTag) {
String jsonText = ChatRewriter.legacyTextToJsonString(((StringTag) loreEntry).getValue(), true); String jsonText = ComponentUtil.legacyToJsonString(((StringTag) loreEntry).getValue(), true);
((StringTag) loreEntry).setValue(jsonText); ((StringTag) loreEntry).setValue(jsonText);
} }
} }
@ -277,7 +280,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_13, Serve
} else { } else {
for (Tag loreEntry : lore) { for (Tag loreEntry : lore) {
if (loreEntry instanceof StringTag) { if (loreEntry instanceof StringTag) {
((StringTag) loreEntry).setValue(ChatRewriter.jsonToLegacyText(((StringTag) loreEntry).getValue())); ((StringTag) loreEntry).setValue(ComponentUtil.jsonToLegacy(((StringTag) loreEntry).getValue()));
} }
} }
} }

Datei anzeigen

@ -18,7 +18,12 @@
package com.viaversion.viaversion.protocols.protocol1_19_1to1_19; package com.viaversion.viaversion.protocols.protocol1_19_1to1_19;
import com.github.steveice10.opennbt.stringified.SNBT; import com.github.steveice10.opennbt.stringified.SNBT;
import com.github.steveice10.opennbt.tag.builtin.*; import com.github.steveice10.opennbt.tag.builtin.ByteTag;
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.common.base.Preconditions;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
@ -32,12 +37,6 @@ import com.viaversion.viaversion.api.protocol.AbstractProtocol;
import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
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.TranslatableComponent;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.NamedTextColor;
import com.viaversion.viaversion.libs.kyori.adventure.text.format.Style;
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.ChatTypeStorage;
@ -46,11 +45,16 @@ import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ClientboundPacke
import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ServerboundPackets1_19; import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ServerboundPackets1_19;
import com.viaversion.viaversion.util.CipherUtil; import com.viaversion.viaversion.util.CipherUtil;
import com.viaversion.viaversion.util.Pair; import com.viaversion.viaversion.util.Pair;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import net.lenni0451.mcstructs.core.TextFormatting;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.Style;
import net.lenni0451.mcstructs.text.components.TranslationComponent;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPackets1_19, ClientboundPackets1_19_1, ServerboundPackets1_19, ServerboundPackets1_19_1> { public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPackets1_19, ClientboundPackets1_19_1, ServerboundPackets1_19, ServerboundPackets1_19_1> {
@ -350,32 +354,45 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
} }
final String translationKey = (String) decoaration.get("translation_key").getValue(); final String translationKey = (String) decoaration.get("translation_key").getValue();
final TranslatableComponent.Builder componentBuilder = Component.translatable().key(translationKey); final Style style = new Style();
// Add the style // Add the style
final CompoundTag style = decoaration.get("style"); final CompoundTag styleTag = decoaration.get("style");
if (style != null) { if (styleTag != null) {
final Style.Builder styleBuilder = Style.style(); final StringTag color = styleTag.get("color");
final StringTag color = style.get("color");
if (color != null) { if (color != null) {
final NamedTextColor textColor = NamedTextColor.NAMES.value(color.getValue()); final TextFormatting textColor = TextFormatting.getByName(color.getValue());
if (textColor != null) { if (textColor != null) {
styleBuilder.color(NamedTextColor.NAMES.value(color.getValue())); style.setFormatting(textColor);
} }
} }
for (final String key : TextDecoration.NAMES.keys()) { for (final Map.Entry<String, TextFormatting> entry : TextFormatting.FORMATTINGS.entrySet()) {
if (style.contains(key)) { final Tag tag = styleTag.get(entry.getKey());
styleBuilder.decoration(TextDecoration.NAMES.value(key), style.<ByteTag>get(key).asByte() == 1); if (!(tag instanceof ByteTag)) {
continue;
}
final boolean value = ((NumberTag) tag).asBoolean();
final TextFormatting formatting = entry.getValue();
if (formatting == TextFormatting.OBFUSCATED) {
style.setObfuscated(value);
} else if (formatting == TextFormatting.BOLD) {
style.setBold(value);
} else if (formatting == TextFormatting.STRIKETHROUGH) {
style.setStrikethrough(value);
} else if (formatting == TextFormatting.UNDERLINE) {
style.setUnderlined(value);
} else if (formatting == TextFormatting.ITALIC) {
style.setItalic(value);
} }
} }
componentBuilder.style(styleBuilder.build());
} }
// Add the replacements // Add the replacements
final ListTag parameters = decoaration.get("parameters"); final ListTag parameters = decoaration.get("parameters");
final List<ATextComponent> arguments = new ArrayList<>();
if (parameters != null) { if (parameters != null) {
final List<Component> arguments = new ArrayList<>();
for (final Tag element : parameters) { for (final Tag element : parameters) {
JsonElement argument = null; JsonElement argument = null;
switch ((String) element.getValue()) { switch ((String) element.getValue()) {
@ -393,11 +410,12 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
Via.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue()); Via.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue());
} }
if (argument != null) { if (argument != null) {
arguments.add(GsonComponentSerializer.gson().deserializeFromTree(argument)); arguments.add(TextComponentSerializer.LATEST.deserialize(argument));
} }
} }
componentBuilder.args(arguments);
} }
return new ChatDecorationResult(GsonComponentSerializer.gson().serializeToTree(componentBuilder.build()), overlay);
final TranslationComponent translatable = new TranslationComponent(translationKey, arguments);
return new ChatDecorationResult(TextComponentSerializer.LATEST.serializeJson(translatable), overlay);
} }
} }

Datei anzeigen

@ -39,8 +39,6 @@ import com.viaversion.viaversion.api.type.types.BitSetType;
import com.viaversion.viaversion.api.type.types.misc.ParticleType; import com.viaversion.viaversion.api.type.types.misc.ParticleType;
import com.viaversion.viaversion.api.type.types.version.Types1_19_3; import com.viaversion.viaversion.api.type.types.version.Types1_19_3;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.libs.kyori.adventure.text.Component;
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.ClientboundPackets1_19_1; import com.viaversion.viaversion.protocols.protocol1_19_1to1_19.ClientboundPackets1_19_1;
@ -53,8 +51,8 @@ import com.viaversion.viaversion.rewriter.CommandRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Pair; import com.viaversion.viaversion.util.Pair;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@ -200,7 +198,7 @@ public final class Protocol1_19_3To1_19_1 extends AbstractProtocol<ClientboundPa
decoratedMessage = unsignedMessage; decoratedMessage = unsignedMessage;
} }
if (decoratedMessage == null) { if (decoratedMessage == null) {
decoratedMessage = GsonComponentSerializer.gson().serializeToTree(Component.text(plainMessage)); decoratedMessage = ComponentUtil.plainTextToJson(plainMessage);
} }
final int filterMaskType = wrapper.read(Type.VAR_INT); final int filterMaskType = wrapper.read(Type.VAR_INT);

Datei anzeigen

@ -26,7 +26,6 @@ import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.types.misc.ParticleType; import com.viaversion.viaversion.api.type.types.misc.ParticleType;
import com.viaversion.viaversion.api.type.types.version.Types1_19_4; import com.viaversion.viaversion.api.type.types.version.Types1_19_4;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ClientboundPackets1_19_3; import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ClientboundPackets1_19_3;
import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ServerboundPackets1_19_3; import com.viaversion.viaversion.protocols.protocol1_19_3to1_19_1.ServerboundPackets1_19_3;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.data.MappingData; import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.data.MappingData;
@ -37,6 +36,7 @@ import com.viaversion.viaversion.rewriter.CommandRewriter;
import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
@ -78,7 +78,7 @@ public final class Protocol1_19_4To1_19_3 extends AbstractProtocol<ClientboundPa
if (element != null) { if (element != null) {
wrapper.write(Type.COMPONENT, element); wrapper.write(Type.COMPONENT, element);
} else { } else {
wrapper.write(Type.COMPONENT, ChatRewriter.emptyComponent()); wrapper.write(Type.COMPONENT, ComponentUtil.emptyJsonComponent());
} }
final String iconBase64 = wrapper.read(Type.OPTIONAL_STRING); final String iconBase64 = wrapper.read(Type.OPTIONAL_STRING);

Datei anzeigen

@ -32,7 +32,6 @@ import com.viaversion.viaversion.api.type.types.version.Types1_19;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
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_13to1_12_2.ChatRewriter;
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.data.MappingData;
@ -48,6 +47,7 @@ import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.CipherUtil; import com.viaversion.viaversion.util.CipherUtil;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.concurrent.ThreadLocalRandom; 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> {
@ -68,7 +68,7 @@ public final class Protocol1_19To1_18_2 extends AbstractProtocol<ClientboundPack
if (!isTextComponentNull(component)) { if (!isTextComponentNull(component)) {
return component; return component;
} else { } else {
return ChatRewriter.emptyComponent(); return ComponentUtil.emptyJsonComponent();
} }
} }

Datei anzeigen

@ -17,298 +17,33 @@
*/ */
package com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util; package com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.FloatTag;
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.ShortTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag; import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.internal.LazilyParsedNumber;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.util.Pair; import net.lenni0451.mcstructs.text.ATextComponent;
import com.viaversion.viaversion.util.UUIDUtil; import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
import java.util.Arrays; import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
public final class ComponentConverter { public final class ComponentConverter {
private static final boolean DEBUG = Boolean.getBoolean("viaversion.debug.components"); private static final boolean DEBUG = Boolean.getBoolean("viaversion.debug.components");
private static final Set<String> BOOLEAN_TYPES = new HashSet<>(Arrays.asList(
"interpret",
"bold",
"italic",
"underlined",
"strikethrough",
"obfuscated"
));
// Order is important
private static final List<Pair<String, String>> COMPONENT_TYPES = Arrays.asList(
new Pair<>("text", "text"),
new Pair<>("translatable", "translate"),
new Pair<>("score", "score"),
new Pair<>("selector", "selector"),
new Pair<>("keybind", "keybind"),
new Pair<>("nbt", "nbt")
);
public static @Nullable JsonElement tagComponentToJson(@Nullable final Tag tag) { public static @Nullable JsonElement tagComponentToJson(@Nullable final Tag tag) {
if (DEBUG) { if (DEBUG) {
Via.getPlatform().getLogger().info("Converting tag to json: " + tag); Via.getPlatform().getLogger().info("Converting tag to json: " + tag);
} }
try { final ATextComponent component = TextComponentCodec.V1_20_3.deserializeNbtTree(NBTConverter.viaToMcStructs(tag));
return convertToJson(null, tag); return component != null ? TextComponentSerializer.V1_19_4.serializeJson(component) : null;
} catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + tag, e);
return new JsonPrimitive("<error>");
}
} }
public static @Nullable Tag jsonComponentToTag(@Nullable final JsonElement component) { public static @Nullable Tag jsonComponentToTag(@Nullable final JsonElement element) {
if (DEBUG) { if (DEBUG) {
Via.getPlatform().getLogger().info("Converting json to tag: " + component); Via.getPlatform().getLogger().info("Converting json to tag: " + element);
} }
try { final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element);
return convertToTag(component); return component != null ? NBTConverter.mcStructsToVia(TextComponentCodec.V1_20_3.serializeNbt(component)) : null;
} catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + component, e);
return new StringTag("<error>");
}
}
private static @Nullable Tag convertToTag(final @Nullable JsonElement element) {
if (element == null || element.isJsonNull()) {
return null;
} else if (element.isJsonObject()) {
final CompoundTag tag = new CompoundTag();
final JsonObject jsonObject = element.getAsJsonObject();
for (final Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
convertObjectEntry(entry.getKey(), entry.getValue(), tag);
}
addComponentType(jsonObject, tag);
return tag;
} else if (element.isJsonArray()) {
return convertJsonArray(element.getAsJsonArray());
} else if (element.isJsonPrimitive()) {
final JsonPrimitive primitive = element.getAsJsonPrimitive();
if (primitive.isString()) {
return new StringTag(primitive.getAsString());
} else if (primitive.isBoolean()) {
return new ByteTag((byte) (primitive.getAsBoolean() ? 1 : 0));
}
final Number number = primitive.getAsNumber();
if (number instanceof Integer) {
return new IntTag(number.intValue());
} else if (number instanceof Byte) {
return new ByteTag(number.byteValue());
} else if (number instanceof Short) {
return new ShortTag(number.shortValue());
} else if (number instanceof Long) {
return new LongTag(number.longValue());
} else if (number instanceof Double) {
return new DoubleTag(number.doubleValue());
} else if (number instanceof Float) {
return new FloatTag(number.floatValue());
} else if (number instanceof LazilyParsedNumber) {
// TODO: This might need better handling
return new IntTag(number.intValue());
}
return new IntTag(number.intValue()); // ???
}
throw new IllegalArgumentException("Unhandled json type " + element.getClass().getSimpleName() + " with value " + element.getAsString());
}
private static ListTag convertJsonArray(final JsonArray array) {
// TODO Number arrays?
final ListTag listTag = new ListTag();
boolean singleType = true;
for (final JsonElement entry : array) {
final Tag convertedEntryTag = convertToTag(entry);
if (listTag.getElementType() != null && listTag.getElementType() != convertedEntryTag.getClass()) {
singleType = false;
break;
}
listTag.add(convertedEntryTag);
}
if (singleType) {
return listTag;
}
// Generally, modern vanilla-esque serializers should not produce this format, so it should be rare
// Lists are only used for lists of components ("extra" and "with")
final ListTag processedListTag = new ListTag();
for (final JsonElement entry : array) {
final Tag convertedTag = convertToTag(entry);
if (convertedTag instanceof CompoundTag) {
processedListTag.add(convertedTag);
continue;
}
// Wrap all entries in compound tags, as lists can only consist of one type of tag
final CompoundTag compoundTag = new CompoundTag();
compoundTag.put("type", new StringTag("text"));
if (convertedTag instanceof ListTag) {
compoundTag.put("text", new StringTag());
compoundTag.put("extra", convertedTag);
} else {
compoundTag.put("text", new StringTag(convertedTag.asRawString()));
}
processedListTag.add(compoundTag);
}
return processedListTag;
}
/**
* Converts a json object entry to a tag entry.
*
* @param key key of the entry
* @param value value of the entry
* @param tag the resulting compound tag
*/
private static void convertObjectEntry(final String key, final JsonElement value, final CompoundTag tag) {
if ((key.equals("contents")) && value.isJsonObject()) {
// Store show_entity id as int array instead of uuid string
// Not really required, but we might as well make it more compact
final JsonObject hoverEvent = value.getAsJsonObject();
final CompoundTag convertedTag = (CompoundTag) convertToTag(value);
final JsonElement id = hoverEvent.get("id");
final UUID uuid;
if (id != null && id.isJsonPrimitive() && (uuid = UUIDUtil.parseUUID(id.getAsString())) != null) {
convertedTag.remove("id");
convertedTag.put("id", new IntArrayTag(UUIDUtil.toIntArray(uuid)));
}
tag.put(key, convertedTag);
return;
}
tag.put(key, convertToTag(value));
}
private static void addComponentType(final JsonObject object, final CompoundTag tag) {
if (object.has("type")) {
return;
}
// Add the type to speed up deserialization and make DFU errors slightly more useful
for (final Pair<String, String> pair : COMPONENT_TYPES) {
if (object.has(pair.value())) {
tag.put("type", new StringTag(pair.key()));
return;
}
}
}
private static @Nullable JsonElement convertToJson(final @Nullable String key, final @Nullable Tag tag) {
if (tag == null) {
return null;
} else if (tag instanceof CompoundTag) {
final JsonObject object = new JsonObject();
if (!"value".equals(key)) {
removeComponentType(object);
}
for (final Map.Entry<String, Tag> entry : ((CompoundTag) tag).entrySet()) {
convertCompoundTagEntry(entry.getKey(), entry.getValue(), object);
}
return object;
} else if (tag instanceof ListTag) {
final ListTag list = (ListTag) tag;
final JsonArray array = new JsonArray();
for (final Tag listEntry : list) {
array.add(convertToJson(null, listEntry));
}
return array;
} else if (tag instanceof NumberTag) {
final NumberTag numberTag = (NumberTag) tag;
if (key != null && BOOLEAN_TYPES.contains(key)) {
// Booleans don't have a direct representation in nbt
return new JsonPrimitive(numberTag.asBoolean());
}
return new JsonPrimitive(numberTag.getValue());
} else if (tag instanceof StringTag) {
return new JsonPrimitive(((StringTag) tag).getValue());
} else if (tag instanceof ByteArrayTag) {
final ByteArrayTag arrayTag = (ByteArrayTag) tag;
final JsonArray array = new JsonArray();
for (final byte num : arrayTag.getValue()) {
array.add(num);
}
return array;
} else if (tag instanceof IntArrayTag) {
final IntArrayTag arrayTag = (IntArrayTag) tag;
final JsonArray array = new JsonArray();
for (final int num : arrayTag.getValue()) {
array.add(num);
}
return array;
} else if (tag instanceof LongArrayTag) {
final LongArrayTag arrayTag = (LongArrayTag) tag;
final JsonArray array = new JsonArray();
for (final long num : arrayTag.getValue()) {
array.add(num);
}
return array;
}
throw new IllegalArgumentException("Unhandled tag type " + tag.getClass().getSimpleName());
}
private static void convertCompoundTagEntry(final String key, final Tag tag, final JsonObject object) {
if ((key.equals("contents")) && tag instanceof CompoundTag) {
// Back to a UUID string
final CompoundTag showEntity = (CompoundTag) tag;
final Tag idTag = showEntity.get("id");
if (idTag instanceof IntArrayTag) {
showEntity.remove("id");
final JsonObject convertedElement = (JsonObject) convertToJson(key, tag);
final UUID uuid = UUIDUtil.fromIntArray(((IntArrayTag) idTag).getValue());
convertedElement.addProperty("id", uuid.toString());
object.add(key, convertedElement);
return;
}
}
// "":1 is a valid tag, but not a valid json component
object.add(key.isEmpty() ? "text" : key, convertToJson(key, tag));
}
private static void removeComponentType(final JsonObject object) {
final JsonElement type = object.remove("type");
if (type == null || !type.isJsonPrimitive()) {
return;
}
// Remove the other fields
final String typeString = type.getAsString();
for (final Pair<String, String> pair : COMPONENT_TYPES) {
if (!pair.key().equals(typeString)) {
object.remove(pair.value());
}
}
} }
} }

Datei anzeigen

@ -0,0 +1,121 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2023 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_20_3to1_20_2.util;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.FloatTag;
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.ShortTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import java.util.Map;
import net.lenni0451.mcstructs.nbt.INbtTag;
import org.checkerframework.checker.nullness.qual.Nullable;
final class NBTConverter {
public static @Nullable Tag mcStructsToVia(@Nullable final INbtTag nbtTag) {
if (nbtTag == null) {
return null;
} else if (nbtTag.isByteTag()) {
return new ByteTag(nbtTag.asByteTag().getValue());
} else if (nbtTag.isShortTag()) {
return new ShortTag(nbtTag.asShortTag().getValue());
} else if (nbtTag.isIntTag()) {
return new IntTag(nbtTag.asIntTag().getValue());
} else if (nbtTag.isLongTag()) {
return new LongTag(nbtTag.asLongTag().getValue());
} else if (nbtTag.isFloatTag()) {
return new FloatTag(nbtTag.asFloatTag().getValue());
} else if (nbtTag.isDoubleTag()) {
return new DoubleTag(nbtTag.asDoubleTag().getValue());
} else if (nbtTag.isByteArrayTag()) {
return new ByteArrayTag(nbtTag.asByteArrayTag().getValue());
} else if (nbtTag.isStringTag()) {
return new StringTag(nbtTag.asStringTag().getValue());
} else if (nbtTag.isListTag()) {
final ListTag list = new ListTag();
for (final INbtTag t : nbtTag.asListTag().getValue()) {
list.add(mcStructsToVia(t));
}
return list;
} else if (nbtTag.isCompoundTag()) {
final Map<String, INbtTag> values = nbtTag.asCompoundTag().getValue();
final CompoundTag compound = new CompoundTag();
for (final Map.Entry<String, INbtTag> entry : values.entrySet()) {
compound.put(entry.getKey(), mcStructsToVia(entry.getValue()));
}
return compound;
} else if (nbtTag.isIntArrayTag()) {
return new IntArrayTag(nbtTag.asIntArrayTag().getValue());
} else if (nbtTag.isLongArrayTag()) {
return new LongArrayTag(nbtTag.asLongArrayTag().getValue());
} else {
throw new IllegalArgumentException("Unsupported tag type: " + nbtTag.getClass().getName());
}
}
public static @Nullable INbtTag viaToMcStructs(@Nullable final Tag tag) {
if (tag == null) {
return null;
} else if (tag instanceof ByteTag) {
return new net.lenni0451.mcstructs.nbt.tags.ByteTag(((NumberTag) tag).asByte());
} else if (tag instanceof ShortTag) {
return new net.lenni0451.mcstructs.nbt.tags.ShortTag(((NumberTag) tag).asShort());
} else if (tag instanceof IntTag) {
return new net.lenni0451.mcstructs.nbt.tags.IntTag(((NumberTag) tag).asInt());
} else if (tag instanceof LongTag) {
return new net.lenni0451.mcstructs.nbt.tags.LongTag(((NumberTag) tag).asLong());
} else if (tag instanceof FloatTag) {
return new net.lenni0451.mcstructs.nbt.tags.FloatTag(((NumberTag) tag).asFloat());
} else if (tag instanceof DoubleTag) {
return new net.lenni0451.mcstructs.nbt.tags.DoubleTag(((NumberTag) tag).asDouble());
} else if (tag instanceof ByteArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.ByteArrayTag(((ByteArrayTag) tag).getValue());
} else if (tag instanceof StringTag) {
return new net.lenni0451.mcstructs.nbt.tags.StringTag(((StringTag) tag).getValue());
} else if (tag instanceof ListTag) {
final net.lenni0451.mcstructs.nbt.tags.ListTag<INbtTag> list = new net.lenni0451.mcstructs.nbt.tags.ListTag<>();
for (final Tag t : ((ListTag) tag).getValue()) {
list.add(viaToMcStructs(t));
}
return list;
} else if (tag instanceof CompoundTag) {
final Map<String, Tag> values = ((CompoundTag) tag).getValue();
final net.lenni0451.mcstructs.nbt.tags.CompoundTag compound = new net.lenni0451.mcstructs.nbt.tags.CompoundTag();
for (final Map.Entry<String, Tag> entry : values.entrySet()) {
compound.add(entry.getKey(), viaToMcStructs(entry.getValue()));
}
return compound;
} else if (tag instanceof IntArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.IntArrayTag(((IntArrayTag) tag).getValue());
} else if (tag instanceof LongArrayTag) {
return new net.lenni0451.mcstructs.nbt.tags.LongArrayTag(((LongArrayTag) tag).getValue());
} else {
throw new IllegalArgumentException("Unsupported tag type: " + tag.getClass().getName());
}
}
}

Datei anzeigen

@ -25,7 +25,6 @@ import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity; import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4; import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_19_4;
import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ServerboundPackets1_19_4; import com.viaversion.viaversion.protocols.protocol1_19_4to1_19_3.ServerboundPackets1_19_4;
@ -34,6 +33,7 @@ import com.viaversion.viaversion.protocols.protocol1_20to1_19_4.Protocol1_20To1_
import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.BlockRewriter;
import com.viaversion.viaversion.rewriter.ItemRewriter; import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.rewriter.RecipeRewriter; import com.viaversion.viaversion.rewriter.RecipeRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.Key;
public final class InventoryPackets extends ItemRewriter<ClientboundPackets1_19_4, ServerboundPackets1_19_4, Protocol1_20To1_19_4> { public final class InventoryPackets extends ItemRewriter<ClientboundPackets1_19_4, ServerboundPackets1_19_4, Protocol1_20To1_19_4> {
@ -173,7 +173,7 @@ public final class InventoryPackets extends ItemRewriter<ClientboundPackets1_19_
final ListTag messages = new ListTag(StringTag.class); final ListTag messages = new ListTag(StringTag.class);
for (int i = 1; i < 5; i++) { for (int i = 1; i < 5; i++) {
final Tag text = tag.remove("Text" + i); final Tag text = tag.remove("Text" + i);
messages.add(text != null ? text : new StringTag(ChatRewriter.emptyComponentString())); messages.add(text != null ? text : new StringTag(ComponentUtil.emptyJsonComponentString()));
} }
frontText.put("messages", messages); frontText.put("messages", messages);

Datei anzeigen

@ -0,0 +1,62 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2023 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.util;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.Style;
import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
public final class ComponentUtil {
public static JsonObject emptyJsonComponent() {
return plainTextToJson("");
}
public static String emptyJsonComponentString() {
return "{\"text\":\"\"}";
}
public static JsonObject plainTextToJson(final String message) {
final JsonObject object = new JsonObject();
object.addProperty("text", message);
return object;
}
public static JsonElement legacyToJson(final String message) {
return TextComponentSerializer.V1_12.serializeJson(LegacyStringDeserializer.parse(message, true));
}
public static String legacyToJsonString(final String legacyText) {
return legacyToJsonString(legacyText, false);
}
public static String legacyToJsonString(final String message, final boolean itemData) {
final ATextComponent component = LegacyStringDeserializer.parse(message, true);
if (itemData) {
component.setParentStyle(new Style().setItalic(false));
}
return TextComponentSerializer.V1_12.serialize(component);
}
public static String jsonToLegacy(final String value) {
return TextComponentSerializer.LATEST.deserialize(value).asLegacyFormatString();
}
}

Datei anzeigen

@ -18,12 +18,9 @@
package com.viaversion.viaversion.util; package com.viaversion.viaversion.util;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.compatibility.YamlCompat; import com.viaversion.viaversion.compatibility.YamlCompat;
import com.viaversion.viaversion.compatibility.unsafe.Yaml1Compat; import com.viaversion.viaversion.compatibility.unsafe.Yaml1Compat;
import com.viaversion.viaversion.compatibility.unsafe.Yaml2Compat; import com.viaversion.viaversion.compatibility.unsafe.Yaml2Compat;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
@ -239,7 +236,7 @@ public abstract class Config {
public @Nullable JsonElement getSerializedComponent(String key) { public @Nullable JsonElement getSerializedComponent(String key) {
final Object o = this.config.get(key); final Object o = this.config.get(key);
if (o != null && !((String) o).isEmpty()) { if (o != null && !((String) o).isEmpty()) {
return GsonComponentSerializer.gson().serializeToTree(LegacyComponentSerializer.legacySection().deserialize((String) o)); return ComponentUtil.legacyToJson((String) o);
} else { } else {
return null; return null;
} }

Datei anzeigen

@ -2,11 +2,11 @@ metadata.format.version = "1.1"
[versions] [versions]
adventure = "4.14.0"
gson = "2.10.1" gson = "2.10.1"
fastutil = "8.5.12" fastutil = "8.5.12"
flare = "2.0.1" flare = "2.0.1"
vianbt = "3.3.0" vianbt = "3.3.0"
mcstructs = "2.4.1"
# Common provided # Common provided
netty = "4.0.20.Final" netty = "4.0.20.Final"
@ -27,16 +27,12 @@ velocity = "3.1.1"
[libraries] [libraries]
adventureApi = { group = "net.kyori", name = "adventure-api", version.ref = "adventure" }
adventureTextSerializerGson = { group = "net.kyori", name = "adventure-text-serializer-gson", version.ref = "adventure" }
adventureTextSerializerGsonLegacy = { group = "net.kyori", name = "adventure-text-serializer-gson-legacy-impl", version.ref = "adventure" }
adventureTextSerializerLegacy = { group = "net.kyori", name = "adventure-text-serializer-legacy", version.ref = "adventure" }
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" } gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" } fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" }
flare = { group = "space.vectrix.flare", name = "flare", version.ref = "flare" } flare = { group = "space.vectrix.flare", name = "flare", version.ref = "flare" }
flareFastutil = { group = "space.vectrix.flare", name = "flare-fastutil", version.ref = "flare" } flareFastutil = { group = "space.vectrix.flare", name = "flare-fastutil", version.ref = "flare" }
vianbt = { group = "com.viaversion", name = "nbt", version.ref = "vianbt" } vianbt = { group = "com.viaversion", name = "nbt", version.ref = "vianbt" }
text = { group = "net.lenni0451.mcstructs", name = "text", version.ref = "mcstructs" }
netty = { group = "io.netty", name = "netty-all", version.ref = "netty" } netty = { group = "io.netty", name = "netty-all", version.ref = "netty" }
guava = { group = "com.google.guava", name = "guava", version.ref = "guava" } guava = { group = "com.google.guava", name = "guava", version.ref = "guava" }
@ -56,5 +52,4 @@ velocity = { group = "com.velocitypowered", name = "velocity-api", version.ref =
[bundles] [bundles]
adventure = ["adventureApi", "adventureTextSerializerGson", "adventureTextSerializerGsonLegacy", "adventureTextSerializerLegacy"]
junit = ["jupiterApi", "jupiterEngine"] junit = ["jupiterApi", "jupiterEngine"]

Datei anzeigen

@ -27,7 +27,6 @@ rootProject.name = "viaversion-parent"
includeBuild("build-logic") includeBuild("build-logic")
include("adventure")
include("compat", "compat:snakeyaml-compat-common", "compat:snakeyaml2-compat", "compat:snakeyaml1-compat", "compat:protocolsupport-compat") include("compat", "compat:snakeyaml-compat-common", "compat:snakeyaml2-compat", "compat:snakeyaml1-compat", "compat:protocolsupport-compat")
setupViaSubproject("api") setupViaSubproject("api")