Updated mcstructs API usage

This commit is contained in:
RaphiMC 2023-12-27 20:26:26 +01:00
parent 90287e9929
commit 8e81b3e93c
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
7 changed files with 17 additions and 220 deletions

View File

@ -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 {

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, JsonElement> 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<String, com.viaversion.viaversion.libs.gson.JsonElement> entry : element.getAsJsonObject().entrySet()) {
object.add(entry.getKey(), viaToGson(entry.getValue()));
}
return object;
} else {
throw new IllegalArgumentException("Unknown json element type: " + element.getClass().getName());
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, INbtTag> values = nbtTag.asCompoundTag().getValue();
final CompoundTag compound = new CompoundTag();
for (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 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<INbtTag> 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<String, Tag> values = ((CompoundTag) tag).getValue();
final net.lenni0451.mcstructs.nbt.tags.CompoundTag compound = new net.lenni0451.mcstructs.nbt.tags.CompoundTag();
for (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());
}
}
}

View File

@ -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;

View File

@ -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 {

View File

@ -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<ClientboundPackets1_
handler(wrapper -> {
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);
}

View File

@ -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) {