Skip to content

Commit 512f810

Browse files
authored
Merge pull request #1549 from MagnoliaLLC/2.0
perf: drop per-call allocations in AdventureNBTSerializer style (de)serialization
2 parents d4b5437 + f41d59b commit 512f810

1 file changed

Lines changed: 37 additions & 14 deletions

File tree

api/src/main/java/com/github/retrooper/packetevents/util/adventure/AdventureNBTSerializer.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@
8585
import java.util.Locale;
8686
import java.util.Map;
8787
import java.util.Optional;
88+
import java.util.Set;
8889
import java.util.function.Consumer;
8990
import java.util.function.Function;
9091

91-
import static com.github.retrooper.packetevents.util.adventure.AdventureIndexUtil.indexValueOrThrow;
92-
9392
public class AdventureNBTSerializer implements ComponentSerializer<Component, Component, NBT> {
9493

94+
private static final Set<TextDecoration> DECORATIONS = TextDecoration.NAMES.values();
95+
9596
private final ClientVersion version;
9697
private final boolean downsampleColor;
9798

@@ -470,22 +471,27 @@ public AdventureNBTSerializer(boolean downsampleColor) {
470471
Style.Builder style = Style.style();
471472
NBTReader reader = new NBTReader(wrapper, input);
472473

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);
476480
if (color != null) style.color(color);
477-
});
481+
}
482+
478483
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()));
481486
}
482487

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));
487491
}
488-
reader.useUTF("insertion", style::insertion);
492+
493+
String insertion = reader.getUTF("insertion");
494+
if (insertion != null) style.insertion(insertion);
489495

490496
boolean modernEvents = this.version.isNewerThanOrEquals(ClientVersion.V_1_21_5);
491497
NBTReader clickEvent = reader.child(modernEvents ? "click_event" : "clickEvent");
@@ -605,7 +611,7 @@ public AdventureNBTSerializer(boolean downsampleColor) {
605611
if (shadowColor != null) writer.writeInt("shadow_color", shadowColor.value());
606612
}
607613

608-
for (TextDecoration decoration : TextDecoration.NAMES.values()) {
614+
for (TextDecoration decoration : DECORATIONS) {
609615
TextDecoration.State state = style.decoration(decoration);
610616
if (state != TextDecoration.State.NOT_SET) {
611617
writer.writeBoolean(decoration.toString(), state == TextDecoration.State.TRUE);
@@ -868,6 +874,23 @@ public <R> R readUTF(String key, Function<String, R> function) {
868874
return withTag(key, tag -> function.apply(requireType(tag, NBTType.STRING).getValue()));
869875
}
870876

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+
871894
public void useByteArray(String key, Consumer<byte[]> consumer) {
872895
useTag(key, tag -> consumer.accept(requireType(tag, NBTType.BYTE_ARRAY).getValue()));
873896
}

0 commit comments

Comments
 (0)