Handle booleans in json->tag conversion

This commit is contained in:
Nassim Jahnke 2023-10-16 15:35:16 +10:00
parent fa7096c5c1
commit 667278f9e6
3 changed files with 25 additions and 10 deletions

View File

@ -22,7 +22,7 @@ dependencies {
api(libs.fastutil) api(libs.fastutil)
api(libs.flare) api(libs.flare)
api(libs.flareFastutil) api(libs.flareFastutil)
api(libs.nbt) api(libs.vianbt)
api(libs.gson) api(libs.gson)
compileOnlyApi(libs.snakeYaml) compileOnlyApi(libs.snakeYaml)

View File

@ -56,14 +56,25 @@ import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.Serverbou
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPackets1_20_2; import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundPackets1_20_2;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.EntityPacketRewriter1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.EntityPacketRewriter1_20_3;
import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter;
import java.util.Arrays;
import java.util.BitSet; import java.util.BitSet;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPackets1_20_2, ClientboundPackets1_20_2, ServerboundPackets1_20_2, ServerboundPackets1_20_2> { public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPackets1_20_2, ClientboundPackets1_20_2, ServerboundPackets1_20_2, ServerboundPackets1_20_2> {
public static final MappingData MAPPINGS = new MappingDataBase("1.20.2", "1.20.3"); public static final MappingData MAPPINGS = new MappingDataBase("1.20.2", "1.20.3");
private static final Set<String> BOOLEAN_TYPES = new HashSet<>(Arrays.asList(
"interpret",
"bold",
"italic",
"underlined",
"strikethrough",
"obfuscated"
));
private final EntityPacketRewriter1_20_3 entityRewriter = new EntityPacketRewriter1_20_3(this); private final EntityPacketRewriter1_20_3 entityRewriter = new EntityPacketRewriter1_20_3(this);
public Protocol1_20_3To1_20_2() { public Protocol1_20_3To1_20_2() {
@ -278,7 +289,7 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPa
public static @Nullable JsonElement tagComponentToJson(@Nullable final Tag tag) { public static @Nullable JsonElement tagComponentToJson(@Nullable final Tag tag) {
try { try {
return convertToJson(tag); return convertToJson(null, tag);
} catch (final Exception e) { } catch (final Exception e) {
Via.getPlatform().getLogger().severe("Error converting component: " + tag); Via.getPlatform().getLogger().severe("Error converting component: " + tag);
e.printStackTrace(); e.printStackTrace();
@ -396,7 +407,7 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPa
} }
} }
private static @Nullable JsonElement convertToJson(final @Nullable Tag tag) { private static @Nullable JsonElement convertToJson(final @Nullable String key, final @Nullable Tag tag) {
if (tag == null) { if (tag == null) {
return null; return null;
} else if (tag instanceof CompoundTag) { } else if (tag instanceof CompoundTag) {
@ -409,12 +420,16 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPa
final ListTag list = (ListTag) tag; final ListTag list = (ListTag) tag;
final JsonArray array = new JsonArray(); final JsonArray array = new JsonArray();
for (final Tag listEntry : list) { for (final Tag listEntry : list) {
array.add(convertToJson(listEntry)); array.add(convertToJson(null, listEntry));
} }
return array; return array;
} else if (tag instanceof NumberTag) { } else if (tag instanceof NumberTag) {
// TODO Byte could be a boolean final NumberTag numberTag = (NumberTag) tag;
return new JsonPrimitive((Number) tag.getValue()); if (key != null && BOOLEAN_TYPES.contains(key)) {
// Booleans don't have a direct representation in nbt
return new JsonPrimitive(numberTag.asBoolean());
}
return new JsonPrimitive(numberTag.getValue());
} else if (tag instanceof StringTag) { } else if (tag instanceof StringTag) {
return new JsonPrimitive(((StringTag) tag).getValue()); return new JsonPrimitive(((StringTag) tag).getValue());
} else if (tag instanceof ByteArrayTag) { } else if (tag instanceof ByteArrayTag) {
@ -448,14 +463,14 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPa
final CompoundTag showEntity = (CompoundTag) tag; final CompoundTag showEntity = (CompoundTag) tag;
final Tag idTag = showEntity.remove("id"); final Tag idTag = showEntity.remove("id");
if (idTag instanceof IntArrayTag) { if (idTag instanceof IntArrayTag) {
final JsonObject convertedElement = (JsonObject) convertToJson(tag); final JsonObject convertedElement = (JsonObject) convertToJson(key, tag);
convertedElement.addProperty("id", uuidIntsToString(((IntArrayTag) idTag).getValue())); convertedElement.addProperty("id", uuidIntsToString(((IntArrayTag) idTag).getValue()));
object.add(key, convertedElement); object.add(key, convertedElement);
return; return;
} }
} }
object.add(key, convertToJson(tag)); object.add(key, convertToJson(key, tag));
} }
private static String uuidIntsToString(final int[] parts) { private static String uuidIntsToString(final int[] parts) {

View File

@ -6,7 +6,7 @@ adventure = "4.14.0"
gson = "2.10.1" gson = "2.10.1"
fastutil = "8.5.12" fastutil = "8.5.12"
flare = "2.0.1" flare = "2.0.1"
nbt = "3.0.0" vianbt = "3.2.0"
# Common provided # Common provided
netty = "4.0.20.Final" netty = "4.0.20.Final"
@ -37,7 +37,7 @@ gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" } fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" }
flare = { group = "space.vectrix.flare", name = "flare", version.ref = "flare" } flare = { group = "space.vectrix.flare", name = "flare", version.ref = "flare" }
flareFastutil = { group = "space.vectrix.flare", name = "flare-fastutil", version.ref = "flare" } flareFastutil = { group = "space.vectrix.flare", name = "flare-fastutil", version.ref = "flare" }
nbt = { group = "com.viaversion", name = "nbt", version.ref = "nbt" } vianbt = { group = "com.viaversion", name = "nbt", version.ref = "vianbt" }
netty = { group = "io.netty", name = "netty-all", version.ref = "netty" } netty = { group = "io.netty", name = "netty-all", version.ref = "netty" }
guava = { group = "com.google.guava", name = "guava", version.ref = "guava" } guava = { group = "com.google.guava", name = "guava", version.ref = "guava" }