diff --git a/src/main/java/net/raphimc/vialegacy/api/util/converter/JsonConverter.java b/src/main/java/net/raphimc/vialegacy/api/util/converter/JsonConverter.java
new file mode 100644
index 0000000..3e01fd0
--- /dev/null
+++ b/src/main/java/net/raphimc/vialegacy/api/util/converter/JsonConverter.java
@@ -0,0 +1,92 @@
+/*
+ * This file is part of ViaLegacy - https://github.com/RaphiMC/ViaLegacy
+ * Copyright (C) 2023 RK_01/RaphiMC 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 .
+ */
+package net.raphimc.vialegacy.api.util.converter;
+
+import com.google.gson.*;
+
+import java.util.Map;
+
+public class JsonConverter {
+
+ public static com.viaversion.viaversion.libs.gson.JsonElement gsonToVia(final JsonElement element) {
+ if (element == null) {
+ return null;
+ } else if (element.isJsonNull()) {
+ return com.viaversion.viaversion.libs.gson.JsonNull.INSTANCE;
+ } else if (element.isJsonPrimitive()) {
+ final JsonPrimitive primitive = element.getAsJsonPrimitive();
+ if (primitive.isBoolean()) {
+ return new com.viaversion.viaversion.libs.gson.JsonPrimitive(primitive.getAsBoolean());
+ } else if (primitive.isNumber()) {
+ return new com.viaversion.viaversion.libs.gson.JsonPrimitive(primitive.getAsNumber());
+ } else if (primitive.isString()) {
+ return new com.viaversion.viaversion.libs.gson.JsonPrimitive(primitive.getAsString());
+ } else {
+ throw new IllegalArgumentException("Unknown json primitive type: " + primitive);
+ }
+ } else if (element.isJsonArray()) {
+ final com.viaversion.viaversion.libs.gson.JsonArray array = new com.viaversion.viaversion.libs.gson.JsonArray();
+ for (JsonElement e : element.getAsJsonArray()) {
+ array.add(gsonToVia(e));
+ }
+ return array;
+ } else if (element.isJsonObject()) {
+ final com.viaversion.viaversion.libs.gson.JsonObject object = new com.viaversion.viaversion.libs.gson.JsonObject();
+ for (Map.Entry entry : element.getAsJsonObject().entrySet()) {
+ object.add(entry.getKey(), gsonToVia(entry.getValue()));
+ }
+ return object;
+ } else {
+ throw new IllegalArgumentException("Unknown json element type: " + element.getClass().getName());
+ }
+ }
+
+ public static JsonElement viaToGson(final com.viaversion.viaversion.libs.gson.JsonElement element) {
+ if (element == null) {
+ return null;
+ } else if (element.isJsonNull()) {
+ return JsonNull.INSTANCE;
+ } else if (element.isJsonPrimitive()) {
+ final com.viaversion.viaversion.libs.gson.JsonPrimitive primitive = element.getAsJsonPrimitive();
+ if (primitive.isBoolean()) {
+ return new JsonPrimitive(primitive.getAsBoolean());
+ } else if (primitive.isNumber()) {
+ return new JsonPrimitive(primitive.getAsNumber());
+ } else if (primitive.isString()) {
+ return new JsonPrimitive(primitive.getAsString());
+ } else {
+ throw new IllegalArgumentException("Unknown json primitive type: " + primitive);
+ }
+ } else if (element.isJsonArray()) {
+ final JsonArray array = new JsonArray();
+ for (com.viaversion.viaversion.libs.gson.JsonElement e : element.getAsJsonArray()) {
+ array.add(viaToGson(e));
+ }
+ return array;
+ } else if (element.isJsonObject()) {
+ final JsonObject object = new JsonObject();
+ for (Map.Entry entry : element.getAsJsonObject().entrySet()) {
+ object.add(entry.getKey(), viaToGson(entry.getValue()));
+ }
+ return object;
+ } else {
+ throw new IllegalArgumentException("Unknown json element type: " + element.getClass().getName());
+ }
+ }
+
+}
diff --git a/src/main/java/net/raphimc/vialegacy/util/NbtConverter.java b/src/main/java/net/raphimc/vialegacy/api/util/converter/NbtConverter.java
similarity index 62%
rename from src/main/java/net/raphimc/vialegacy/util/NbtConverter.java
rename to src/main/java/net/raphimc/vialegacy/api/util/converter/NbtConverter.java
index 6dfcdd8..21d7bb5 100644
--- a/src/main/java/net/raphimc/vialegacy/util/NbtConverter.java
+++ b/src/main/java/net/raphimc/vialegacy/api/util/converter/NbtConverter.java
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package net.raphimc.vialegacy.util;
+package net.raphimc.vialegacy.api.util.converter;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.*;
import net.lenni0451.mcstructs.nbt.INbtTag;
@@ -25,50 +25,50 @@ import java.util.Map;
public class NbtConverter {
public static Tag mcStructsToVia(final INbtTag nbtTag) {
- if (nbtTag == null) return null;
-
- if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.ByteTag) {
- return new ByteTag(((net.lenni0451.mcstructs.nbt.tags.ByteTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.ShortTag) {
- return new ShortTag(((net.lenni0451.mcstructs.nbt.tags.ShortTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.IntTag) {
- return new IntTag(((net.lenni0451.mcstructs.nbt.tags.IntTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.LongTag) {
- return new LongTag(((net.lenni0451.mcstructs.nbt.tags.LongTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.FloatTag) {
- return new FloatTag(((net.lenni0451.mcstructs.nbt.tags.FloatTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.DoubleTag) {
- return new DoubleTag(((net.lenni0451.mcstructs.nbt.tags.DoubleTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.ByteArrayTag) {
- return new ByteArrayTag(((net.lenni0451.mcstructs.nbt.tags.ByteArrayTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.StringTag) {
- return new StringTag(((net.lenni0451.mcstructs.nbt.tags.StringTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.ListTag>) {
+ 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 (INbtTag t : ((net.lenni0451.mcstructs.nbt.tags.ListTag>) nbtTag).getValue()) {
+ for (INbtTag t : nbtTag.asListTag().getValue()) {
list.add(mcStructsToVia(t));
}
return list;
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.CompoundTag) {
- final Map values = ((net.lenni0451.mcstructs.nbt.tags.CompoundTag) nbtTag).getValue();
+ } else if (nbtTag.isCompoundTag()) {
+ final Map values = nbtTag.asCompoundTag().getValue();
final CompoundTag compound = new CompoundTag();
for (Map.Entry entry : values.entrySet()) {
compound.put(entry.getKey(), mcStructsToVia(entry.getValue()));
}
return compound;
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.IntArrayTag) {
- return new IntArrayTag(((net.lenni0451.mcstructs.nbt.tags.IntArrayTag) nbtTag).getValue());
- } else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.LongArrayTag) {
- return new LongArrayTag(((net.lenni0451.mcstructs.nbt.tags.LongArrayTag) nbtTag).getValue());
+ } 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 INbtTag viaToMcStructs(final Tag tag) {
- if (tag == null) return null;
-
- if (tag instanceof ByteTag) {
+ if (tag == null) {
+ return null;
+ } else if (tag instanceof ByteTag) {
return new net.lenni0451.mcstructs.nbt.tags.ByteTag(((ByteTag) tag).asByte());
} else if (tag instanceof ShortTag) {
return new net.lenni0451.mcstructs.nbt.tags.ShortTag(((ShortTag) tag).asShort());
diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java
index 2d55c95..97cdeef 100644
--- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java
+++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/Protocol1_8to1_7_6_10.java
@@ -51,6 +51,7 @@ import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.data.ItemList1_6;
import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
+import net.raphimc.vialegacy.api.util.converter.JsonConverter;
import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ClientboundPackets1_7_2;
import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ServerboundPackets1_7_2;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.data.Particle;
@@ -1372,7 +1373,7 @@ public class Protocol1_8to1_7_6_10 extends AbstractProtocol {
for (int i = 0; i < 4; i++) {
final JsonElement component = wrapper.read(Type.COMPONENT); // line
- String text = TextComponentSerializer.V1_8.deserialize(component.toString()).asLegacyFormatString();
+ String text = TextComponentSerializer.V1_8.deserialize(JsonConverter.viaToGson(component)).asLegacyFormatString();
if (text.length() > 15) text = text.substring(0, 15);
wrapper.write(Type.STRING, text);
}
diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ChatItemRewriter.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ChatItemRewriter.java
index 325e053..0f815f3 100644
--- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ChatItemRewriter.java
+++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_8to1_7_6_10/rewriter/ChatItemRewriter.java
@@ -31,9 +31,9 @@ import com.viaversion.viaversion.rewriter.ComponentRewriter;
import net.lenni0451.mcstructs.snbt.SNbtSerializer;
import net.lenni0451.mcstructs.snbt.exceptions.SNbtSerializeException;
import net.raphimc.vialegacy.ViaLegacy;
+import net.raphimc.vialegacy.api.util.converter.NbtConverter;
import net.raphimc.vialegacy.protocols.release.protocol1_7_6_10to1_7_2_5.ClientboundPackets1_7_2;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.Protocol1_8to1_7_6_10;
-import net.raphimc.vialegacy.util.NbtConverter;
import java.util.logging.Level;