|
85 | 85 | import java.util.Locale; |
86 | 86 | import java.util.Map; |
87 | 87 | import java.util.Optional; |
| 88 | +import java.util.Set; |
88 | 89 | import java.util.function.Consumer; |
89 | 90 | import java.util.function.Function; |
90 | 91 |
|
91 | | -import static com.github.retrooper.packetevents.util.adventure.AdventureIndexUtil.indexValueOrThrow; |
92 | | - |
93 | 92 | public class AdventureNBTSerializer implements ComponentSerializer<Component, Component, NBT> { |
94 | 93 |
|
| 94 | + private static final Set<TextDecoration> DECORATIONS = TextDecoration.NAMES.values(); |
| 95 | + |
95 | 96 | private final ClientVersion version; |
96 | 97 | private final boolean downsampleColor; |
97 | 98 |
|
@@ -470,22 +471,27 @@ public AdventureNBTSerializer(boolean downsampleColor) { |
470 | 471 | Style.Builder style = Style.style(); |
471 | 472 | NBTReader reader = new NBTReader(wrapper, input); |
472 | 473 |
|
473 | | - reader.useUTF("font", value -> style.font(Key.key(value))); |
474 | | - reader.useUTF("color", value -> { |
475 | | - TextColor color = this.deserializeColor(value); |
| 474 | + String font = reader.getUTF("font"); |
| 475 | + if (font != null) style.font(Key.key(font)); |
| 476 | + |
| 477 | + String colorName = reader.getUTF("color"); |
| 478 | + if (colorName != null) { |
| 479 | + TextColor color = this.deserializeColor(colorName); |
476 | 480 | if (color != null) style.color(color); |
477 | | - }); |
| 481 | + } |
| 482 | + |
478 | 483 | if (BackwardCompatUtil.IS_4_18_0_OR_NEWER) { |
479 | | - reader.useNumber("shadow_color", num -> |
480 | | - style.shadowColor(ShadowColor.shadowColor(num.intValue()))); |
| 484 | + Number shadowColor = reader.getNumber("shadow_color"); |
| 485 | + if (shadowColor != null) style.shadowColor(ShadowColor.shadowColor(shadowColor.intValue())); |
481 | 486 | } |
482 | 487 |
|
483 | | - for (String decorationKey : TextDecoration.NAMES.keys()) { |
484 | | - reader.useBoolean(decorationKey, value -> style.decoration( |
485 | | - indexValueOrThrow(TextDecoration.NAMES, decorationKey), |
486 | | - TextDecoration.State.byBoolean(value))); |
| 488 | + for (TextDecoration decoration : DECORATIONS) { |
| 489 | + Boolean value = reader.getBoolean(decoration.toString()); |
| 490 | + if (value != null) style.decoration(decoration, TextDecoration.State.byBoolean(value)); |
487 | 491 | } |
488 | | - reader.useUTF("insertion", style::insertion); |
| 492 | + |
| 493 | + String insertion = reader.getUTF("insertion"); |
| 494 | + if (insertion != null) style.insertion(insertion); |
489 | 495 |
|
490 | 496 | boolean modernEvents = this.version.isNewerThanOrEquals(ClientVersion.V_1_21_5); |
491 | 497 | NBTReader clickEvent = reader.child(modernEvents ? "click_event" : "clickEvent"); |
@@ -605,7 +611,7 @@ public AdventureNBTSerializer(boolean downsampleColor) { |
605 | 611 | if (shadowColor != null) writer.writeInt("shadow_color", shadowColor.value()); |
606 | 612 | } |
607 | 613 |
|
608 | | - for (TextDecoration decoration : TextDecoration.NAMES.values()) { |
| 614 | + for (TextDecoration decoration : DECORATIONS) { |
609 | 615 | TextDecoration.State state = style.decoration(decoration); |
610 | 616 | if (state != TextDecoration.State.NOT_SET) { |
611 | 617 | writer.writeBoolean(decoration.toString(), state == TextDecoration.State.TRUE); |
@@ -868,6 +874,23 @@ public <R> R readUTF(String key, Function<String, R> function) { |
868 | 874 | return withTag(key, tag -> function.apply(requireType(tag, NBTType.STRING).getValue())); |
869 | 875 | } |
870 | 876 |
|
| 877 | + public @Nullable String getUTF(String key) { |
| 878 | + NBT tag = compound.getTagOrNull(key); |
| 879 | + return tag == null ? null : requireType(tag, NBTType.STRING).getValue(); |
| 880 | + } |
| 881 | + |
| 882 | + public @Nullable Number getNumber(String key) { |
| 883 | + NBT tag = compound.getTagOrNull(key); |
| 884 | + if (tag == null) return null; |
| 885 | + if (tag instanceof NBTNumber) return ((NBTNumber) tag).getAsNumber(); |
| 886 | + throw new IllegalArgumentException("Expected number but got " + tag.getType()); |
| 887 | + } |
| 888 | + |
| 889 | + public @Nullable Boolean getBoolean(String key) { |
| 890 | + Number number = getNumber(key); |
| 891 | + return number == null ? null : number.byteValue() != 0; |
| 892 | + } |
| 893 | + |
871 | 894 | public void useByteArray(String key, Consumer<byte[]> consumer) { |
872 | 895 | useTag(key, tag -> consumer.accept(requireType(tag, NBTType.BYTE_ARRAY).getValue())); |
873 | 896 | } |
|
0 commit comments