Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-26 00:00:28 +01:00
Use mcstructs for component conversion
Dieser Commit ist enthalten in:
Ursprung
0e31664bfe
Commit
c62a8274ed
@ -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()
|
@ -15,9 +15,6 @@ sourceSets {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(projects.adventure) {
|
||||
targetConfiguration = "shadow"
|
||||
}
|
||||
api(libs.fastutil)
|
||||
api(libs.flare)
|
||||
api(libs.flareFastutil)
|
||||
|
@ -26,6 +26,7 @@ fun ShadowJar.configureRelocations() {
|
||||
relocate("com.github.steveice10.opennbt", "com.viaversion.viaversion.libs.opennbt")
|
||||
relocate("it.unimi.dsi.fastutil", "com.viaversion.viaversion.libs.fastutil")
|
||||
relocate("space.vectrix.flare", "com.viaversion.viaversion.libs.flare")
|
||||
relocate("net.lenni0451.mcstructs", "com.viaversion.viaversion.libs.mcstructs")
|
||||
}
|
||||
|
||||
fun ShadowJar.configureExcludes() {
|
||||
|
@ -20,14 +20,12 @@ val main = setOf(
|
||||
projects.viaversionVelocity
|
||||
).map { it.dependencyProject }
|
||||
|
||||
val special = setOf(
|
||||
projects.adventure
|
||||
).map { it.dependencyProject }
|
||||
// val special = setOf().map { it.dependencyProject }
|
||||
|
||||
subprojects {
|
||||
when (this) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
dependencies {
|
||||
api(projects.viaversionApi)
|
||||
api(projects.viaversionApiLegacy)
|
||||
api(rootProject.libs.text) {
|
||||
exclude("com.google.code.gson", "gson")
|
||||
}
|
||||
implementation(projects.compat.snakeyaml2Compat)
|
||||
implementation(projects.compat.snakeyaml1Compat)
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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.rewriter.SoundRewriter;
|
||||
import com.viaversion.viaversion.util.ChatColorUtil;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
import com.viaversion.viaversion.util.GsonUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -435,7 +436,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
|
||||
// On create or update
|
||||
if (mode == 0 || mode == 2) {
|
||||
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);
|
||||
// integer or hearts
|
||||
@ -456,7 +457,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
|
||||
|
||||
if (action == 0 || action == 2) {
|
||||
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 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.COMPONENT, ChatRewriter.legacyTextToJson(prefix)); // Prefix
|
||||
wrapper.write(Type.COMPONENT, ChatRewriter.legacyTextToJson(suffix)); // Suffix
|
||||
wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(prefix)); // Prefix
|
||||
wrapper.write(Type.COMPONENT, ComponentUtil.legacyToJson(suffix)); // Suffix
|
||||
}
|
||||
|
||||
if (action == 0 || action == 3 || action == 4) {
|
||||
|
@ -18,19 +18,19 @@
|
||||
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.metadata;
|
||||
|
||||
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.EntityTypes1_13;
|
||||
import com.viaversion.viaversion.api.minecraft.item.Item;
|
||||
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.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.data.EntityTypeRewriter;
|
||||
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.rewriter.EntityRewriter;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
import java.util.List;
|
||||
|
||||
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
|
||||
if (metadata.id() == 2) {
|
||||
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 {
|
||||
metadata.setTypeAndValue(Types1_13.META_TYPES.optionalComponentType, null);
|
||||
}
|
||||
|
@ -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.type.Type;
|
||||
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.Protocol1_13To1_12_2;
|
||||
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.SpawnEggRewriter;
|
||||
import com.viaversion.viaversion.rewriter.ItemRewriter;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
import com.viaversion.viaversion.util.Key;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
@ -315,7 +315,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
|
||||
if (display.get("Name") instanceof StringTag) {
|
||||
StringTag name = display.get("Name");
|
||||
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
|
||||
@ -573,7 +573,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
|
||||
if (display.get("Name") instanceof StringTag) {
|
||||
StringTag name = display.get("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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,9 +26,9 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||
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.storage.BlockStorage;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
|
||||
public class BannerHandler implements BlockEntityProvider.BlockEntityHandler {
|
||||
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");
|
||||
if (name instanceof StringTag) {
|
||||
((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue()));
|
||||
((StringTag) name).setValue(ComponentUtil.legacyToJsonString(((StringTag) name).getValue()));
|
||||
}
|
||||
|
||||
return blockId;
|
||||
|
@ -24,9 +24,9 @@ import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
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.providers.BlockEntityProvider;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
|
||||
public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandler {
|
||||
|
||||
@ -36,7 +36,7 @@ public class CommandBlockHandler implements BlockEntityProvider.BlockEntityHandl
|
||||
public int transform(UserConnection user, CompoundTag tag) {
|
||||
Tag name = tag.get("CustomName");
|
||||
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");
|
||||
if (out instanceof StringTag) {
|
||||
|
@ -17,7 +17,11 @@
|
||||
*/
|
||||
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.gson.JsonElement;
|
||||
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.remapper.PacketHandlers;
|
||||
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.ServerboundPackets1_13;
|
||||
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.ItemRewriter;
|
||||
import com.viaversion.viaversion.rewriter.RecipeRewriter;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
import com.viaversion.viaversion.util.Key;
|
||||
|
||||
import java.util.Set;
|
||||
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
|
||||
for (Tag loreEntry : lore) {
|
||||
if (loreEntry instanceof StringTag) {
|
||||
String jsonText = ChatRewriter.legacyTextToJsonString(((StringTag) loreEntry).getValue(), true);
|
||||
String jsonText = ComponentUtil.legacyToJsonString(((StringTag) loreEntry).getValue(), true);
|
||||
((StringTag) loreEntry).setValue(jsonText);
|
||||
}
|
||||
}
|
||||
@ -277,7 +280,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_13, Serve
|
||||
} else {
|
||||
for (Tag loreEntry : lore) {
|
||||
if (loreEntry instanceof StringTag) {
|
||||
((StringTag) loreEntry).setValue(ChatRewriter.jsonToLegacyText(((StringTag) loreEntry).getValue()));
|
||||
((StringTag) loreEntry).setValue(ComponentUtil.jsonToLegacy(((StringTag) loreEntry).getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,12 @@
|
||||
package com.viaversion.viaversion.protocols.protocol1_19_1to1_19;
|
||||
|
||||
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.gson.JsonElement;
|
||||
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.remapper.PacketHandlers;
|
||||
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.ServerboundLoginPackets;
|
||||
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.util.CipherUtil;
|
||||
import com.viaversion.viaversion.util.Pair;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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> {
|
||||
|
||||
@ -350,32 +354,45 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
|
||||
}
|
||||
|
||||
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
|
||||
final CompoundTag style = decoaration.get("style");
|
||||
if (style != null) {
|
||||
final Style.Builder styleBuilder = Style.style();
|
||||
final StringTag color = style.get("color");
|
||||
final CompoundTag styleTag = decoaration.get("style");
|
||||
if (styleTag != null) {
|
||||
final StringTag color = styleTag.get("color");
|
||||
if (color != null) {
|
||||
final NamedTextColor textColor = NamedTextColor.NAMES.value(color.getValue());
|
||||
final TextFormatting textColor = TextFormatting.getByName(color.getValue());
|
||||
if (textColor != null) {
|
||||
styleBuilder.color(NamedTextColor.NAMES.value(color.getValue()));
|
||||
style.setFormatting(textColor);
|
||||
}
|
||||
}
|
||||
|
||||
for (final String key : TextDecoration.NAMES.keys()) {
|
||||
if (style.contains(key)) {
|
||||
styleBuilder.decoration(TextDecoration.NAMES.value(key), style.<ByteTag>get(key).asByte() == 1);
|
||||
for (final Map.Entry<String, TextFormatting> entry : TextFormatting.FORMATTINGS.entrySet()) {
|
||||
final Tag tag = styleTag.get(entry.getKey());
|
||||
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
|
||||
final ListTag parameters = decoaration.get("parameters");
|
||||
final List<ATextComponent> arguments = new ArrayList<>();
|
||||
if (parameters != null) {
|
||||
final List<Component> arguments = new ArrayList<>();
|
||||
for (final Tag element : parameters) {
|
||||
JsonElement argument = null;
|
||||
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());
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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.version.Types1_19_3;
|
||||
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.ServerboundLoginPackets;
|
||||
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.StatisticsRewriter;
|
||||
import com.viaversion.viaversion.rewriter.TagRewriter;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
import com.viaversion.viaversion.util.Pair;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
@ -200,7 +198,7 @@ public final class Protocol1_19_3To1_19_1 extends AbstractProtocol<ClientboundPa
|
||||
decoratedMessage = unsignedMessage;
|
||||
}
|
||||
if (decoratedMessage == null) {
|
||||
decoratedMessage = GsonComponentSerializer.gson().serializeToTree(Component.text(plainMessage));
|
||||
decoratedMessage = ComponentUtil.plainTextToJson(plainMessage);
|
||||
}
|
||||
|
||||
final int filterMaskType = wrapper.read(Type.VAR_INT);
|
||||
|
@ -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.version.Types1_19_4;
|
||||
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.ServerboundPackets1_19_3;
|
||||
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.StatisticsRewriter;
|
||||
import com.viaversion.viaversion.rewriter.TagRewriter;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
@ -78,7 +78,7 @@ public final class Protocol1_19_4To1_19_3 extends AbstractProtocol<ClientboundPa
|
||||
if (element != null) {
|
||||
wrapper.write(Type.COMPONENT, element);
|
||||
} else {
|
||||
wrapper.write(Type.COMPONENT, ChatRewriter.emptyComponent());
|
||||
wrapper.write(Type.COMPONENT, ComponentUtil.emptyJsonComponent());
|
||||
}
|
||||
|
||||
final String iconBase64 = wrapper.read(Type.OPTIONAL_STRING);
|
||||
|
@ -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.protocols.base.ClientboundLoginPackets;
|
||||
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_18to1_17_1.ClientboundPackets1_18;
|
||||
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.TagRewriter;
|
||||
import com.viaversion.viaversion.util.CipherUtil;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
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)) {
|
||||
return component;
|
||||
} else {
|
||||
return ChatRewriter.emptyComponent();
|
||||
return ComponentUtil.emptyJsonComponent();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,298 +17,33 @@
|
||||
*/
|
||||
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.google.gson.JsonArray;
|
||||
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.util.Pair;
|
||||
import com.viaversion.viaversion.util.UUIDUtil;
|
||||
import java.util.Arrays;
|
||||
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 net.lenni0451.mcstructs.text.ATextComponent;
|
||||
import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
|
||||
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public final class ComponentConverter {
|
||||
|
||||
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) {
|
||||
if (DEBUG) {
|
||||
Via.getPlatform().getLogger().info("Converting tag to json: " + tag);
|
||||
}
|
||||
|
||||
try {
|
||||
return convertToJson(null, tag);
|
||||
} catch (final Exception e) {
|
||||
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + tag, e);
|
||||
return new JsonPrimitive("<error>");
|
||||
}
|
||||
final ATextComponent component = TextComponentCodec.V1_20_3.deserializeNbtTree(NBTConverter.viaToMcStructs(tag));
|
||||
return component != null ? TextComponentSerializer.V1_19_4.serializeJson(component) : null;
|
||||
}
|
||||
|
||||
public static @Nullable Tag jsonComponentToTag(@Nullable final JsonElement component) {
|
||||
public static @Nullable Tag jsonComponentToTag(@Nullable final JsonElement element) {
|
||||
if (DEBUG) {
|
||||
Via.getPlatform().getLogger().info("Converting json to tag: " + component);
|
||||
Via.getPlatform().getLogger().info("Converting json to tag: " + element);
|
||||
}
|
||||
|
||||
try {
|
||||
return convertToTag(component);
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element);
|
||||
return component != null ? NBTConverter.mcStructsToVia(TextComponentCodec.V1_20_3.serializeNbt(component)) : null;
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
@ -25,7 +25,6 @@ import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
|
||||
import com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity;
|
||||
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
|
||||
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.protocols.protocol1_19_4to1_19_3.ClientboundPackets1_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.ItemRewriter;
|
||||
import com.viaversion.viaversion.rewriter.RecipeRewriter;
|
||||
import com.viaversion.viaversion.util.ComponentUtil;
|
||||
import com.viaversion.viaversion.util.Key;
|
||||
|
||||
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);
|
||||
for (int i = 1; i < 5; 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);
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -18,12 +18,9 @@
|
||||
package com.viaversion.viaversion.util;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
import com.viaversion.viaversion.compatibility.YamlCompat;
|
||||
import com.viaversion.viaversion.compatibility.unsafe.Yaml1Compat;
|
||||
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.FileInputStream;
|
||||
import java.io.IOException;
|
||||
@ -239,7 +236,7 @@ public abstract class Config {
|
||||
public @Nullable JsonElement getSerializedComponent(String key) {
|
||||
final Object o = this.config.get(key);
|
||||
if (o != null && !((String) o).isEmpty()) {
|
||||
return GsonComponentSerializer.gson().serializeToTree(LegacyComponentSerializer.legacySection().deserialize((String) o));
|
||||
return ComponentUtil.legacyToJson((String) o);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
@ -2,11 +2,11 @@ metadata.format.version = "1.1"
|
||||
|
||||
[versions]
|
||||
|
||||
adventure = "4.14.0"
|
||||
gson = "2.10.1"
|
||||
fastutil = "8.5.12"
|
||||
flare = "2.0.1"
|
||||
vianbt = "3.3.0"
|
||||
mcstructs = "2.4.1"
|
||||
|
||||
# Common provided
|
||||
netty = "4.0.20.Final"
|
||||
@ -27,16 +27,12 @@ velocity = "3.1.1"
|
||||
|
||||
[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" }
|
||||
fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" }
|
||||
flare = { group = "space.vectrix.flare", name = "flare", version.ref = "flare" }
|
||||
flareFastutil = { group = "space.vectrix.flare", name = "flare-fastutil", version.ref = "flare" }
|
||||
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" }
|
||||
guava = { group = "com.google.guava", name = "guava", version.ref = "guava" }
|
||||
@ -56,5 +52,4 @@ velocity = { group = "com.velocitypowered", name = "velocity-api", version.ref =
|
||||
|
||||
[bundles]
|
||||
|
||||
adventure = ["adventureApi", "adventureTextSerializerGson", "adventureTextSerializerGsonLegacy", "adventureTextSerializerLegacy"]
|
||||
junit = ["jupiterApi", "jupiterEngine"]
|
||||
|
@ -27,7 +27,6 @@ rootProject.name = "viaversion-parent"
|
||||
|
||||
includeBuild("build-logic")
|
||||
|
||||
include("adventure")
|
||||
include("compat", "compat:snakeyaml-compat-common", "compat:snakeyaml2-compat", "compat:snakeyaml1-compat", "compat:protocolsupport-compat")
|
||||
|
||||
setupViaSubproject("api")
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren