From 8e81b3e93c83902ac49af7907c39831dba0c3c8c Mon Sep 17 00:00:00 2001
From: RaphiMC <50594595+RaphiMC@users.noreply.github.com>
Date: Wed, 27 Dec 2023 20:26:26 +0100
Subject: [PATCH] Updated mcstructs API usage
---
build.gradle | 4 +-
.../api/util/converter/JsonConverter.java | 92 ---------------
.../api/util/converter/NbtConverter.java | 109 ------------------
.../Protocol1_6_1to1_5_2.java | 4 +-
.../rewriter/ChatComponentRewriter.java | 13 ++-
.../Protocol1_8to1_7_6_10.java | 5 +-
.../rewriter/ChatItemRewriter.java | 10 +-
7 files changed, 17 insertions(+), 220 deletions(-)
delete mode 100644 src/main/java/net/raphimc/vialegacy/api/util/converter/JsonConverter.java
delete mode 100644 src/main/java/net/raphimc/vialegacy/api/util/converter/NbtConverter.java
diff --git a/build.gradle b/build.gradle
index 3752b84..480696a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -23,12 +23,10 @@ repositories {
}
dependencies {
- compileOnly "com.viaversion:viaversion-common:4.9.3-SNAPSHOT"
+ compileOnly "com.viaversion:viaversion-common:4.10.0-23w51b-SNAPSHOT"
compileOnly "org.yaml:snakeyaml:2.2"
compileOnly "com.google.guava:guava:33.0.0-jre"
compileOnly "io.netty:netty-handler:4.1.104.Final"
-
- api "net.lenni0451.mcstructs:text:2.4.1"
}
sourceSets {
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
deleted file mode 100644
index 3e01fd0..0000000
--- a/src/main/java/net/raphimc/vialegacy/api/util/converter/JsonConverter.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * 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/api/util/converter/NbtConverter.java b/src/main/java/net/raphimc/vialegacy/api/util/converter/NbtConverter.java
deleted file mode 100644
index 21d7bb5..0000000
--- a/src/main/java/net/raphimc/vialegacy/api/util/converter/NbtConverter.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * 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.viaversion.viaversion.libs.opennbt.tag.builtin.*;
-import net.lenni0451.mcstructs.nbt.INbtTag;
-
-import java.util.Map;
-
-public class NbtConverter {
-
- public static Tag mcStructsToVia(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 (INbtTag t : nbtTag.asListTag().getValue()) {
- list.add(mcStructsToVia(t));
- }
- return list;
- } 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.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;
- } 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());
- } else if (tag instanceof IntTag) {
- return new net.lenni0451.mcstructs.nbt.tags.IntTag(((IntTag) tag).asInt());
- } else if (tag instanceof LongTag) {
- return new net.lenni0451.mcstructs.nbt.tags.LongTag(((LongTag) tag).asLong());
- } else if (tag instanceof FloatTag) {
- return new net.lenni0451.mcstructs.nbt.tags.FloatTag(((FloatTag) tag).asFloat());
- } else if (tag instanceof DoubleTag) {
- return new net.lenni0451.mcstructs.nbt.tags.DoubleTag(((DoubleTag) 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 list = new net.lenni0451.mcstructs.nbt.tags.ListTag<>();
- for (Tag t : ((ListTag) tag).getValue()) {
- list.add(viaToMcStructs(t));
- }
- return list;
- } else if (tag instanceof CompoundTag) {
- final Map values = ((CompoundTag) tag).getValue();
- final net.lenni0451.mcstructs.nbt.tags.CompoundTag compound = new net.lenni0451.mcstructs.nbt.tags.CompoundTag();
- for (Map.Entry 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());
- }
- }
-
-}
diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_6_1to1_5_2/Protocol1_6_1to1_5_2.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_6_1to1_5_2/Protocol1_6_1to1_5_2.java
index 9b42cc9..56c147d 100644
--- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_6_1to1_5_2/Protocol1_6_1to1_5_2.java
+++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_6_1to1_5_2/Protocol1_6_1to1_5_2.java
@@ -23,8 +23,8 @@ import com.viaversion.viaversion.api.minecraft.metadata.Metadata;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.type.Type;
-import net.lenni0451.mcstructs.text.components.StringComponent;
-import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
+import com.viaversion.viaversion.libs.mcstructs.text.components.StringComponent;
+import com.viaversion.viaversion.libs.mcstructs.text.serializer.TextComponentSerializer;
import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.protocol.StatelessProtocol;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter;
diff --git a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/rewriter/ChatComponentRewriter.java b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/rewriter/ChatComponentRewriter.java
index 1009935..4775375 100644
--- a/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/rewriter/ChatComponentRewriter.java
+++ b/src/main/java/net/raphimc/vialegacy/protocols/release/protocol1_7_2_5to1_6_4/rewriter/ChatComponentRewriter.java
@@ -17,12 +17,13 @@
*/
package net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.rewriter;
-import net.lenni0451.mcstructs.text.ATextComponent;
-import net.lenni0451.mcstructs.text.components.StringComponent;
-import net.lenni0451.mcstructs.text.components.TranslationComponent;
-import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer;
-import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
-import net.lenni0451.mcstructs.text.utils.TextUtils;
+
+import com.viaversion.viaversion.libs.mcstructs.text.ATextComponent;
+import com.viaversion.viaversion.libs.mcstructs.text.components.StringComponent;
+import com.viaversion.viaversion.libs.mcstructs.text.components.TranslationComponent;
+import com.viaversion.viaversion.libs.mcstructs.text.serializer.LegacyStringDeserializer;
+import com.viaversion.viaversion.libs.mcstructs.text.serializer.TextComponentSerializer;
+import com.viaversion.viaversion.libs.mcstructs.text.utils.TextUtils;
public class ChatComponentRewriter {
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 86c7e10..6e2a11e 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
@@ -40,18 +40,17 @@ import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_8;
import com.viaversion.viaversion.api.type.types.version.Types1_8;
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonObject;
+import com.viaversion.viaversion.libs.mcstructs.text.serializer.TextComponentSerializer;
import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets;
import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets;
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;
import com.viaversion.viaversion.protocols.protocol1_8.ServerboundPackets1_8;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
-import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
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;
@@ -1309,7 +1308,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(JsonConverter.viaToGson(component)).asLegacyFormatString();
+ String text = TextComponentSerializer.V1_8.deserialize(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 0f815f3..35426f8 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
@@ -24,14 +24,14 @@ import com.viaversion.viaversion.libs.gson.JsonArray;
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
+import com.viaversion.viaversion.libs.mcstructs.snbt.SNbtSerializer;
+import com.viaversion.viaversion.libs.mcstructs.snbt.exceptions.SNbtSerializeException;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
-import net.lenni0451.mcstructs.snbt.SNbtSerializer;
-import net.lenni0451.mcstructs.snbt.exceptions.SNbtSerializeException;
+import com.viaversion.viaversion.util.NBTConverter;
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;
@@ -377,7 +377,7 @@ public class ChatItemRewriter {
final CompoundTag tag;
try {
- tag = (CompoundTag) NbtConverter.mcStructsToVia(SNbtSerializer.V1_7.deserialize(text));
+ tag = (CompoundTag) NBTConverter.mcStructsToVia(SNbtSerializer.V1_7.deserialize(text));
} catch (Throwable e) {
ViaLegacy.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text);
throw new RuntimeException(e);
@@ -410,7 +410,7 @@ public class ChatItemRewriter {
array.add(object);
final String serializedNBT;
try {
- serializedNBT = SNbtSerializer.V1_8.serialize(NbtConverter.viaToMcStructs(tag));
+ serializedNBT = SNbtSerializer.V1_8.serialize(NBTConverter.viaToMcStructs(tag));
object.addProperty("text", serializedNBT);
hoverEvent.add("value", array);
} catch (SNbtSerializeException e) {