CompoundTag readQuiet

This commit is contained in:
Bjarne Koll committed 2025-03-13 12:22:37 +01:00
1 parent e0e6ae86a8
commit 218bb00997
3 files changed
+92 -105

No files matched your search

@@ -1,82 +0,0 @@
From 3aeeaf31b7a7f888f54e47826e8ec0ddbe65c4bf Mon Sep 17 00:00:00 2001
From: File <noreply+automated@papermc.io>
Date: Sun, 20 Apr 1997 14:37:42 +0100
Subject: [PATCH] paper File Patches
diff --git a/net/minecraft/nbt/CompoundTag.java b/net/minecraft/nbt/CompoundTag.java
index ba35210bac334c65eaded98a484be95ab5e27acf..361bc458e0bb590c43da60a1cd993a2785ee45e9 100644
--- a/net/minecraft/nbt/CompoundTag.java
+++ b/net/minecraft/nbt/CompoundTag.java
@@ -49,7 +49,7 @@ public class CompoundTag implements Tag {
private static CompoundTag loadCompound(DataInput input, NbtAccounter nbtAccounter) throws IOException {
nbtAccounter.accountBytes(48L);
- Map<String, Tag> map = Maps.newHashMap();
+ it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<String, Tag> map = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(8, 0.8f); // Paper - Reduce memory footprint of CompoundTag
byte b;
while ((b = input.readByte()) != 0) {
@@ -166,7 +166,7 @@ public class CompoundTag implements Tag {
}
public CompoundTag() {
- this(Maps.newHashMap());
+ this(new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(8, 0.8f)); // Paper - Reduce memory footprint of CompoundTag
}
@Override
@@ -232,14 +232,34 @@ public class CompoundTag implements Tag {
}
public void putUUID(String key, UUID value) {
+ // Paper start - Support old UUID format
+ if (this.contains(key + "Most", net.minecraft.nbt.Tag.TAG_ANY_NUMERIC) && this.contains(key + "Least", net.minecraft.nbt.Tag.TAG_ANY_NUMERIC)) {
+ this.tags.remove(key + "Most");
+ this.tags.remove(key + "Least");
+ }
+ // Paper end - Support old UUID format
this.tags.put(key, NbtUtils.createUUID(value));
}
+
+ /**
+ * You must use {@link #hasUUID(String)} before or else it <b>will</b> throw an NPE.
+ */
public UUID getUUID(String key) {
+ // Paper start - Support old UUID format
+ if (!contains(key, 11) && this.contains(key + "Most", net.minecraft.nbt.Tag.TAG_ANY_NUMERIC) && this.contains(key + "Least", net.minecraft.nbt.Tag.TAG_ANY_NUMERIC)) {
+ return new UUID(this.getLong(key + "Most"), this.getLong(key + "Least"));
+ }
+ // Paper end - Support old UUID format
return NbtUtils.loadUUID(this.get(key));
}
public boolean hasUUID(String key) {
+ // Paper start - Support old UUID format
+ if (this.contains(key + "Most", net.minecraft.nbt.Tag.TAG_ANY_NUMERIC) && this.contains(key + "Least", net.minecraft.nbt.Tag.TAG_ANY_NUMERIC)) {
+ return true;
+ }
+ // Paper end - Support old UUID format
Tag tag = this.get(key);
return tag != null && tag.getType() == IntArrayTag.TYPE && ((IntArrayTag)tag).getAsIntArray().length == 4;
}
@@ -477,8 +497,16 @@ public class CompoundTag implements Tag {
@Override
public CompoundTag copy() {
- Map<String, Tag> map = Maps.newHashMap(Maps.transformValues(this.tags, Tag::copy));
- return new CompoundTag(map);
+ // Paper start - Reduce memory footprint of CompoundTag
+ it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<String, Tag> ret = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(this.tags.size(), 0.8f);
+ java.util.Iterator<java.util.Map.Entry<String, Tag>> iterator = (this.tags instanceof it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) ? ((it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)this.tags).object2ObjectEntrySet().fastIterator() : this.tags.entrySet().iterator();
+ while (iterator.hasNext()) {
+ Map.Entry<String, Tag> entry = iterator.next();
+ ret.put(entry.getKey(), entry.getValue().copy());
+ }
+
+ return new CompoundTag(ret);
+ // Paper end - Reduce memory footprint of CompoundTag
}
@Override
@@ -1,23 +0,0 @@
From 3aeeaf31b7a7f888f54e47826e8ec0ddbe65c4bf Mon Sep 17 00:00:00 2001
From: File <noreply+automated@papermc.io>
Date: Sun, 20 Apr 1997 14:37:42 +0100
Subject: [PATCH] paper File Patches
diff --git a/net/minecraft/nbt/NbtUtils.java b/net/minecraft/nbt/NbtUtils.java
index fb9099586a9c52df25b0fdcba36aa82f3a7359d2..7f2ef8acb7c8737af2c308c64445ea70df8cd200 100644
--- a/net/minecraft/nbt/NbtUtils.java
+++ b/net/minecraft/nbt/NbtUtils.java
@@ -143,8 +143,10 @@ public final class NbtUtils {
if (!tag.contains("Name", 8)) {
return Blocks.AIR.defaultBlockState();
} else {
- ResourceLocation resourceLocation = ResourceLocation.parse(tag.getString("Name"));
- Optional<? extends Holder<Block>> optional = blockGetter.get(ResourceKey.create(Registries.BLOCK, resourceLocation));
+ // Paper start - Validate resource location
+ ResourceLocation resourceLocation = ResourceLocation.tryParse(tag.getString("Name"));
+ Optional<? extends Holder<Block>> optional = resourceLocation != null ? blockGetter.get(ResourceKey.create(Registries.BLOCK, resourceLocation)) : Optional.empty();
+ // Paper end - Validate resource location
if (optional.isEmpty()) {
return Blocks.AIR.defaultBlockState();
} else {
@@ -0,0 +1,92 @@
--- a/net/minecraft/nbt/CompoundTag.java
+++ b/net/minecraft/nbt/CompoundTag.java
@@ -54,7 +_,7 @@
private static CompoundTag loadCompound(DataInput input, NbtAccounter nbtAccounter) throws IOException {
nbtAccounter.accountBytes(48L);
- Map<String, Tag> map = Maps.newHashMap();
+ it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<String, Tag> map = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(8, 0.8f); // Paper - Reduce memory footprint of CompoundTag
byte b;
while ((b = input.readByte()) != 0) {
@@ -171,7 +_,7 @@
}
public CompoundTag() {
- this(new HashMap<>());
+ this(new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(8, 0.8f)); // Paper - Reduce memory footprint of CompoundTag
}
@Override
@@ -402,9 +_,16 @@
@Override
public CompoundTag copy() {
- HashMap<String, Tag> map = new HashMap<>();
- this.tags.forEach((string, tag) -> map.put(string, tag.copy()));
- return new CompoundTag(map);
+ // Paper start - Reduce memory footprint of CompoundTag
+ it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<String, Tag> ret = new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<>(this.tags.size(), 0.8f);
+ java.util.Iterator<java.util.Map.Entry<String, Tag>> iterator = (this.tags instanceof it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap) ? ((it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap)this.tags).object2ObjectEntrySet().fastIterator() : this.tags.entrySet().iterator();
+ while (iterator.hasNext()) {
+ Map.Entry<String, Tag> entry = iterator.next();
+ ret.put(entry.getKey(), entry.getValue().copy());
+ }
+
+ return new CompoundTag(ret);
+ // Paper end - Reduce memory footprint of CompoundTag
}
@Override
@@ -525,22 +_,47 @@
this.merge((CompoundTag)mapCodec.encoder().encodeStart(ops, data).getOrThrow());
}
- public <T> Optional<T> read(String key, Codec<T> codec) {
+ public <T> Optional<T> read(String key, Codec<T> codec) { // Paper - option to read via codec without logging errors - diff on change
return this.read(key, codec, NbtOps.INSTANCE);
}
- public <T> Optional<T> read(String key, Codec<T> codec, DynamicOps<Tag> ops) {
+ public <T> Optional<T> read(String key, Codec<T> codec, DynamicOps<Tag> ops) { // Paper - option to read via codec without logging errors - diff on change
Tag tag = this.get(key);
return tag == null
? Optional.empty()
: codec.parse(ops, tag).resultOrPartial(string -> LOGGER.error("Failed to read field ({}={}): {}", key, tag, string));
}
- public <T> Optional<T> read(MapCodec<T> mapCodec) {
+ public <T> Optional<T> read(MapCodec<T> mapCodec) { // Paper - option to read via codec without logging errors - diff on change
return this.read(mapCodec, NbtOps.INSTANCE);
}
- public <T> Optional<T> read(MapCodec<T> mapCodec, DynamicOps<Tag> ops) {
+ public <T> Optional<T> read(MapCodec<T> mapCodec, DynamicOps<Tag> ops) { // Paper - option to read via codec without logging errors - diff on change
return mapCodec.decode(ops, ops.getMap(this).getOrThrow()).resultOrPartial(string -> LOGGER.error("Failed to read value ({}): {}", this, string));
}
+
+ // Paper start - option to read via codec without logging errors
+ // The below methods are 1 to 1 copies of the above-defined read methods without the logging part.
+ // Copying was chosen over overloading the read methods as a boolean parameter to mark a method as quiet
+ // is not intuitive and would require even more overloads.
+ // Not a lot of diff in these methods is expected
+ public <T> Optional<T> readQuiet(String key, Codec<T> codec) {
+ return this.readQuiet(key, codec, NbtOps.INSTANCE);
+ }
+
+ public <T> Optional<T> readQuiet(String key, Codec<T> codec, DynamicOps<Tag> ops) {
+ Tag tag = this.get(key);
+ return tag == null
+ ? Optional.empty()
+ : codec.parse(ops, tag).resultOrPartial();
+ }
+
+ public <T> Optional<T> readQuiet(MapCodec<T> mapCodec) {
+ return this.readQuiet(mapCodec, NbtOps.INSTANCE);
+ }
+
+ public <T> Optional<T> readQuiet(MapCodec<T> mapCodec, DynamicOps<Tag> ops) {
+ return mapCodec.decode(ops, ops.getMap(this).getOrThrow()).resultOrPartial();
+ }
+ // Paper end - option to read via codec without logging errors
}