Added JsonConverter

This commit is contained in:
RaphiMC 2023-12-09 15:21:38 +01:00
parent 4ac743fda6
commit 51d13081ac
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
4 changed files with 125 additions and 32 deletions

View File

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

@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package net.raphimc.vialegacy.util; package net.raphimc.vialegacy.api.util.converter;
import com.viaversion.viaversion.libs.opennbt.tag.builtin.*; import com.viaversion.viaversion.libs.opennbt.tag.builtin.*;
import net.lenni0451.mcstructs.nbt.INbtTag; import net.lenni0451.mcstructs.nbt.INbtTag;
@ -25,50 +25,50 @@ import java.util.Map;
public class NbtConverter { public class NbtConverter {
public static Tag mcStructsToVia(final INbtTag nbtTag) { public static Tag mcStructsToVia(final INbtTag nbtTag) {
if (nbtTag == null) return null; if (nbtTag == null) {
return null;
if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.ByteTag) { } else if (nbtTag.isByteTag()) {
return new ByteTag(((net.lenni0451.mcstructs.nbt.tags.ByteTag) nbtTag).getValue()); return new ByteTag(nbtTag.asByteTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.ShortTag) { } else if (nbtTag.isShortTag()) {
return new ShortTag(((net.lenni0451.mcstructs.nbt.tags.ShortTag) nbtTag).getValue()); return new ShortTag(nbtTag.asShortTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.IntTag) { } else if (nbtTag.isIntTag()) {
return new IntTag(((net.lenni0451.mcstructs.nbt.tags.IntTag) nbtTag).getValue()); return new IntTag(nbtTag.asIntTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.LongTag) { } else if (nbtTag.isLongTag()) {
return new LongTag(((net.lenni0451.mcstructs.nbt.tags.LongTag) nbtTag).getValue()); return new LongTag(nbtTag.asLongTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.FloatTag) { } else if (nbtTag.isFloatTag()) {
return new FloatTag(((net.lenni0451.mcstructs.nbt.tags.FloatTag) nbtTag).getValue()); return new FloatTag(nbtTag.asFloatTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.DoubleTag) { } else if (nbtTag.isDoubleTag()) {
return new DoubleTag(((net.lenni0451.mcstructs.nbt.tags.DoubleTag) nbtTag).getValue()); return new DoubleTag(nbtTag.asDoubleTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.ByteArrayTag) { } else if (nbtTag.isByteArrayTag()) {
return new ByteArrayTag(((net.lenni0451.mcstructs.nbt.tags.ByteArrayTag) nbtTag).getValue()); return new ByteArrayTag(nbtTag.asByteArrayTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.StringTag) { } else if (nbtTag.isStringTag()) {
return new StringTag(((net.lenni0451.mcstructs.nbt.tags.StringTag) nbtTag).getValue()); return new StringTag(nbtTag.asStringTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.ListTag<?>) { } else if (nbtTag.isListTag()) {
final ListTag list = new ListTag(); 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)); list.add(mcStructsToVia(t));
} }
return list; return list;
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.CompoundTag) { } else if (nbtTag.isCompoundTag()) {
final Map<String, INbtTag> values = ((net.lenni0451.mcstructs.nbt.tags.CompoundTag) nbtTag).getValue(); final Map<String, INbtTag> values = nbtTag.asCompoundTag().getValue();
final CompoundTag compound = new CompoundTag(); final CompoundTag compound = new CompoundTag();
for (Map.Entry<String, INbtTag> entry : values.entrySet()) { for (Map.Entry<String, INbtTag> entry : values.entrySet()) {
compound.put(entry.getKey(), mcStructsToVia(entry.getValue())); compound.put(entry.getKey(), mcStructsToVia(entry.getValue()));
} }
return compound; return compound;
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.IntArrayTag) { } else if (nbtTag.isIntArrayTag()) {
return new IntArrayTag(((net.lenni0451.mcstructs.nbt.tags.IntArrayTag) nbtTag).getValue()); return new IntArrayTag(nbtTag.asIntArrayTag().getValue());
} else if (nbtTag instanceof net.lenni0451.mcstructs.nbt.tags.LongArrayTag) { } else if (nbtTag.isLongArrayTag()) {
return new LongArrayTag(((net.lenni0451.mcstructs.nbt.tags.LongArrayTag) nbtTag).getValue()); return new LongArrayTag(nbtTag.asLongArrayTag().getValue());
} else { } else {
throw new IllegalArgumentException("Unsupported tag type: " + nbtTag.getClass().getName()); throw new IllegalArgumentException("Unsupported tag type: " + nbtTag.getClass().getName());
} }
} }
public static INbtTag viaToMcStructs(final Tag tag) { public static INbtTag viaToMcStructs(final Tag tag) {
if (tag == null) return null; if (tag == null) {
return null;
if (tag instanceof ByteTag) { } else if (tag instanceof ByteTag) {
return new net.lenni0451.mcstructs.nbt.tags.ByteTag(((ByteTag) tag).asByte()); return new net.lenni0451.mcstructs.nbt.tags.ByteTag(((ByteTag) tag).asByte());
} else if (tag instanceof ShortTag) { } else if (tag instanceof ShortTag) {
return new net.lenni0451.mcstructs.nbt.tags.ShortTag(((ShortTag) tag).asShort()); return new net.lenni0451.mcstructs.nbt.tags.ShortTag(((ShortTag) tag).asShort());

View File

@ -51,6 +51,7 @@ import net.raphimc.vialegacy.ViaLegacy;
import net.raphimc.vialegacy.api.data.ItemList1_6; import net.raphimc.vialegacy.api.data.ItemList1_6;
import net.raphimc.vialegacy.api.model.IdAndData; import net.raphimc.vialegacy.api.model.IdAndData;
import net.raphimc.vialegacy.api.remapper.LegacyItemRewriter; 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.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_7_6_10to1_7_2_5.ServerboundPackets1_7_2;
import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.data.Particle; 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<ClientboundPackets1_
handler(wrapper -> { handler(wrapper -> {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
final JsonElement component = wrapper.read(Type.COMPONENT); // line 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); if (text.length() > 15) text = text.substring(0, 15);
wrapper.write(Type.STRING, text); wrapper.write(Type.STRING, text);
} }

View File

@ -31,9 +31,9 @@ import com.viaversion.viaversion.rewriter.ComponentRewriter;
import net.lenni0451.mcstructs.snbt.SNbtSerializer; import net.lenni0451.mcstructs.snbt.SNbtSerializer;
import net.lenni0451.mcstructs.snbt.exceptions.SNbtSerializeException; import net.lenni0451.mcstructs.snbt.exceptions.SNbtSerializeException;
import net.raphimc.vialegacy.ViaLegacy; 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_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.protocols.release.protocol1_8to1_7_6_10.Protocol1_8to1_7_6_10;
import net.raphimc.vialegacy.util.NbtConverter;
import java.util.logging.Level; import java.util.logging.Level;