2023-12-26 23:38:02 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2024-01-01 12:39:45 +01:00
|
|
|
* Copyright (C) 2016-2024 ViaVersion and contributors
|
2023-12-26 23:38:02 +01:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2023-12-27 17:30:27 +01:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
2023-12-27 11:40:45 +01:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
2023-12-26 23:38:02 +01:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
2023-12-27 17:30:27 +01:00
|
|
|
import com.viaversion.viaversion.api.Via;
|
2023-12-29 16:50:21 +01:00
|
|
|
import java.util.logging.Level;
|
2023-12-27 17:30:27 +01:00
|
|
|
import net.lenni0451.mcstructs.snbt.SNbtSerializer;
|
2023-12-26 23:38:02 +01:00
|
|
|
import net.lenni0451.mcstructs.text.ATextComponent;
|
|
|
|
import net.lenni0451.mcstructs.text.Style;
|
2023-12-27 17:30:27 +01:00
|
|
|
import net.lenni0451.mcstructs.text.events.hover.AHoverEvent;
|
|
|
|
import net.lenni0451.mcstructs.text.events.hover.impl.TextHoverEvent;
|
2023-12-26 23:38:02 +01:00
|
|
|
import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer;
|
2023-12-27 11:40:45 +01:00
|
|
|
import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
|
2023-12-26 23:38:02 +01:00
|
|
|
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
|
2023-12-27 11:40:45 +01:00
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
2023-12-26 23:38:02 +01:00
|
|
|
|
2023-12-27 11:40:45 +01:00
|
|
|
/**
|
|
|
|
* Component conversion utility, trying to divert most calls to the component library to this class instead for easy replacement.
|
|
|
|
*/
|
2023-12-26 23:38:02 +01:00
|
|
|
public final class ComponentUtil {
|
|
|
|
|
|
|
|
public static JsonObject emptyJsonComponent() {
|
2023-12-27 11:40:45 +01:00
|
|
|
return plainToJson("");
|
2023-12-26 23:38:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String emptyJsonComponentString() {
|
|
|
|
return "{\"text\":\"\"}";
|
|
|
|
}
|
|
|
|
|
2023-12-27 11:40:45 +01:00
|
|
|
public static JsonObject plainToJson(final String message) {
|
2023-12-26 23:38:02 +01:00
|
|
|
final JsonObject object = new JsonObject();
|
|
|
|
object.addProperty("text", message);
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2023-12-27 11:40:45 +01:00
|
|
|
public static @Nullable JsonElement tagToJson(@Nullable final Tag tag) {
|
2023-12-30 17:25:54 +01:00
|
|
|
try {
|
|
|
|
final ATextComponent component = TextComponentCodec.V1_20_3.deserializeNbtTree(tag);
|
|
|
|
return component != null ? SerializerVersion.V1_19_4.toJson(component) : null;
|
|
|
|
} catch (final Exception e) {
|
|
|
|
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting tag: " + tag, e);
|
|
|
|
return plainToJson("<error>");
|
|
|
|
}
|
2023-12-27 11:40:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable Tag jsonToTag(@Nullable final JsonElement element) {
|
2023-12-27 17:30:27 +01:00
|
|
|
if (element == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element);
|
2023-12-29 16:50:21 +01:00
|
|
|
return TextComponentCodec.V1_20_3.serializeNbt(component);
|
2023-12-27 17:30:27 +01:00
|
|
|
} catch (final Exception e) {
|
|
|
|
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + element, e);
|
|
|
|
return new StringTag("<error>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable JsonElement convertJson(@Nullable final JsonElement element, final SerializerVersion from, final SerializerVersion to) {
|
2024-01-27 20:51:28 +01:00
|
|
|
return element != null ? convert(from, to, from.jsonSerializer.deserialize(element)) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable JsonElement convertJson(@Nullable final String json, final SerializerVersion from, final SerializerVersion to) {
|
|
|
|
return json != null ? convert(from, to, from.jsonSerializer.deserialize(json)) : null;
|
|
|
|
}
|
2023-12-27 17:30:27 +01:00
|
|
|
|
2024-01-27 20:51:28 +01:00
|
|
|
private static JsonElement convert(final SerializerVersion from, final SerializerVersion to, final ATextComponent component) {
|
2023-12-27 17:30:27 +01:00
|
|
|
if (from.ordinal() >= SerializerVersion.V1_16.ordinal() && to.ordinal() < SerializerVersion.V1_16.ordinal()) {
|
|
|
|
// Convert hover event to legacy format
|
|
|
|
final Style style = component.getStyle();
|
|
|
|
final AHoverEvent hoverEvent = style.getHoverEvent();
|
|
|
|
if (hoverEvent != null && !(hoverEvent instanceof TextHoverEvent)) {
|
|
|
|
style.setHoverEvent(hoverEvent.toLegacy(to.jsonSerializer, to.snbtSerializer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return to.toJson(component);
|
2023-12-27 11:40:45 +01:00
|
|
|
}
|
|
|
|
|
2023-12-26 23:38:02 +01:00
|
|
|
public static JsonElement legacyToJson(final String message) {
|
2023-12-27 17:30:27 +01:00
|
|
|
return SerializerVersion.V1_12.toJson(LegacyStringDeserializer.parse(message, true));
|
2023-12-26 23:38:02 +01:00
|
|
|
}
|
|
|
|
|
2023-12-27 17:30:27 +01:00
|
|
|
public static String legacyToJsonString(final String message) {
|
|
|
|
return legacyToJsonString(message, false);
|
2023-12-26 23:38:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-12-27 19:37:48 +01:00
|
|
|
return TextComponentSerializer.V1_12.deserializeReader(value).asLegacyFormatString();
|
2023-12-26 23:38:02 +01:00
|
|
|
}
|
2023-12-26 23:59:48 +01:00
|
|
|
|
|
|
|
public static String jsonToLegacy(final JsonElement value) {
|
2023-12-27 11:40:45 +01:00
|
|
|
return TextComponentSerializer.V1_12.deserialize(value).asLegacyFormatString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum SerializerVersion {
|
2023-12-27 17:30:27 +01:00
|
|
|
V1_8(TextComponentSerializer.V1_8, SNbtSerializer.V1_8),
|
|
|
|
V1_9(TextComponentSerializer.V1_9, SNbtSerializer.V1_8),
|
|
|
|
V1_12(TextComponentSerializer.V1_12, SNbtSerializer.V1_12),
|
|
|
|
V1_14(TextComponentSerializer.V1_14, SNbtSerializer.V1_14),
|
|
|
|
V1_15(TextComponentSerializer.V1_15, SNbtSerializer.V1_14),
|
|
|
|
V1_16(TextComponentSerializer.V1_16, SNbtSerializer.V1_14),
|
|
|
|
V1_17(TextComponentSerializer.V1_17, SNbtSerializer.V1_14),
|
|
|
|
V1_18(TextComponentSerializer.V1_18, SNbtSerializer.V1_14),
|
|
|
|
V1_19_4(TextComponentSerializer.V1_19_4, SNbtSerializer.V1_14),
|
|
|
|
V1_20_3(TextComponentCodec.V1_20_3, SNbtSerializer.V1_14);
|
|
|
|
|
|
|
|
private final TextComponentSerializer jsonSerializer;
|
|
|
|
private final SNbtSerializer<?> snbtSerializer;
|
2023-12-27 11:40:45 +01:00
|
|
|
private final TextComponentCodec codec;
|
|
|
|
|
2023-12-27 17:30:27 +01:00
|
|
|
SerializerVersion(final TextComponentSerializer jsonSerializer, final SNbtSerializer<?> snbtSerializer) {
|
|
|
|
this.jsonSerializer = jsonSerializer;
|
|
|
|
this.snbtSerializer = snbtSerializer;
|
2023-12-27 11:40:45 +01:00
|
|
|
this.codec = null;
|
|
|
|
}
|
|
|
|
|
2023-12-27 17:30:27 +01:00
|
|
|
SerializerVersion(final TextComponentCodec codec, final SNbtSerializer<?> snbtSerializer) {
|
2023-12-27 11:40:45 +01:00
|
|
|
this.codec = codec;
|
2023-12-27 17:30:27 +01:00
|
|
|
this.jsonSerializer = codec.asSerializer();
|
|
|
|
this.snbtSerializer = snbtSerializer;
|
2023-12-27 11:40:45 +01:00
|
|
|
}
|
|
|
|
|
2023-12-27 17:30:27 +01:00
|
|
|
public JsonElement toJson(final ATextComponent component) {
|
|
|
|
return jsonSerializer.serializeJson(component);
|
2023-12-27 11:40:45 +01:00
|
|
|
}
|
2023-12-26 23:59:48 +01:00
|
|
|
}
|
2023-12-26 23:38:02 +01:00
|
|
|
}
|