From ea2865d58bfbf0b1e01bc24456ab971d459abb86 Mon Sep 17 00:00:00 2001 From: 3944Realms Date: Wed, 15 Oct 2025 16:07:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86NBTBuilder=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib39/util/nbt/NBTBuilder.java | 368 ++++++++++++++++-- 1 file changed, 344 insertions(+), 24 deletions(-) diff --git a/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java b/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java index 57ec23b..e249e90 100644 --- a/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java +++ b/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java @@ -31,7 +31,7 @@ public class NBTBuilder { return new NBTBuilder(existingTag); } - // 基本数据类型 + // 基本数据类型 - 原始类型 public NBTBuilder string(String key, String value) { root.putString(key, value); return this; @@ -72,51 +72,204 @@ public class NBTBuilder { return this; } - // 数组类型 + // 包装类型 - null安全的版本 + public NBTBuilder string(String key, String value, String defaultValue) { + if (value != null) { + root.putString(key, value); + } else if (defaultValue != null) { + root.putString(key, defaultValue); + } + return this; + } + + public NBTBuilder string(String key, String value, boolean skipIfNull) { + if (!skipIfNull || value != null) { + root.putString(key, value != null ? value : ""); + } + return this; + } + + public NBTBuilder stringIf(String key, String value) { + if (value != null) { + root.putString(key, value); + } + return this; + } + + public NBTBuilder byteValue(String key, Byte value) { + if (value != null) { + root.putByte(key, value); + } + return this; + } + + public NBTBuilder byteValue(String key, Byte value, byte defaultValue) { + root.putByte(key, value != null ? value : defaultValue); + return this; + } + + public NBTBuilder shortValue(String key, Short value) { + if (value != null) { + root.putShort(key, value); + } + return this; + } + + public NBTBuilder shortValue(String key, Short value, short defaultValue) { + root.putShort(key, value != null ? value : defaultValue); + return this; + } + + public NBTBuilder intValue(String key, Integer value) { + if (value != null) { + root.putInt(key, value); + } + return this; + } + + public NBTBuilder intValue(String key, Integer value, int defaultValue) { + root.putInt(key, value != null ? value : defaultValue); + return this; + } + + public NBTBuilder longValue(String key, Long value) { + if (value != null) { + root.putLong(key, value); + } + return this; + } + + public NBTBuilder longValue(String key, Long value, long defaultValue) { + root.putLong(key, value != null ? value : defaultValue); + return this; + } + + public NBTBuilder floatValue(String key, Float value) { + if (value != null) { + root.putFloat(key, value); + } + return this; + } + + public NBTBuilder floatValue(String key, Float value, float defaultValue) { + root.putFloat(key, value != null ? value : defaultValue); + return this; + } + + public NBTBuilder doubleValue(String key, Double value) { + if (value != null) { + root.putDouble(key, value); + } + return this; + } + + public NBTBuilder doubleValue(String key, Double value, double defaultValue) { + root.putDouble(key, value != null ? value : defaultValue); + return this; + } + + public NBTBuilder booleanValue(String key, Boolean value) { + if (value != null) { + root.putBoolean(key, value); + } + return this; + } + + public NBTBuilder booleanValue(String key, Boolean value, boolean defaultValue) { + root.putBoolean(key, value != null ? value : defaultValue); + return this; + } + + // 数组类型 - 原始数组 public NBTBuilder byteArray(String key, byte[] value) { - root.putByteArray(key, value); + if (value != null) { + root.putByteArray(key, value); + } return this; } public NBTBuilder intArray(String key, int[] value) { - root.putIntArray(key, value); + if (value != null) { + root.putIntArray(key, value); + } return this; } public NBTBuilder longArray(String key, long[] value) { - root.putLongArray(key, value); + if (value != null) { + root.putLongArray(key, value); + } return this; } // UUID支持 public NBTBuilder uuid(String key, UUID value) { - root.putUUID(key, value); + if (value != null) { + root.putUUID(key, value); + } + return this; + } + + public NBTBuilder uuid(String key, UUID value, UUID defaultValue) { + if (value != null) { + root.putUUID(key, value); + } else if (defaultValue != null) { + root.putUUID(key, defaultValue); + } return this; } // 嵌套CompoundTag public NBTBuilder compound(String key, Consumer consumer) { - NBTBuilder nestedBuilder = new NBTBuilder(); - consumer.accept(nestedBuilder); - root.put(key, nestedBuilder.build()); + if (consumer != null) { + NBTBuilder nestedBuilder = new NBTBuilder(); + consumer.accept(nestedBuilder); + CompoundTag nestedTag = nestedBuilder.build(); + if (!nestedTag.isEmpty()) { + root.put(key, nestedTag); + } + } return this; } public NBTBuilder compound(String key, CompoundTag compoundTag) { - root.put(key, compoundTag); + if (compoundTag != null && !compoundTag.isEmpty()) { + root.put(key, compoundTag); + } + return this; + } + + public NBTBuilder compoundIf(String key, boolean condition, Consumer consumer) { + if (condition && consumer != null) { + return compound(key, consumer); + } return this; } // ListTag支持 public NBTBuilder list(String key, Consumer consumer) { - ListNBTBuilder listBuilder = new ListNBTBuilder(); - consumer.accept(listBuilder); - root.put(key, listBuilder.build()); + if (consumer != null) { + ListNBTBuilder listBuilder = new ListNBTBuilder(); + consumer.accept(listBuilder); + ListTag listTag = listBuilder.build(); + if (!listTag.isEmpty()) { + root.put(key, listTag); + } + } return this; } public NBTBuilder list(String key, ListTag listTag) { - root.put(key, listTag); + if (listTag != null && !listTag.isEmpty()) { + root.put(key, listTag); + } + return this; + } + + public NBTBuilder listIf(String key, boolean condition, Consumer consumer) { + if (condition && consumer != null) { + return list(key, consumer); + } return this; } @@ -128,6 +281,35 @@ public class NBTBuilder { return this; } + // 条件添加方法 + public NBTBuilder stringIf(String key, String value, boolean condition) { + if (condition && value != null) { + root.putString(key, value); + } + return this; + } + + public NBTBuilder intValueIf(String key, Integer value, boolean condition) { + if (condition && value != null) { + root.putInt(key, value); + } + return this; + } + + public NBTBuilder longValueIf(String key, Long value, boolean condition) { + if (condition && value != null) { + root.putLong(key, value); + } + return this; + } + + public NBTBuilder booleanValueIf(String key, Boolean value, boolean condition) { + if (condition && value != null) { + root.putBoolean(key, value); + } + return this; + } + // 移除标签 public NBTBuilder remove(String key) { root.remove(key); @@ -136,12 +318,16 @@ public class NBTBuilder { // 构建最终的CompoundTag public CompoundTag build() { + return root.copy(); + } + + // 获取原始的CompoundTag(不推荐,除非你知道在做什么) + public CompoundTag getRaw() { return root; } - /** - * ListTag专用的构建器 + * ListTag专用的构建器 - 同样支持null安全 */ public static class ListNBTBuilder { private final ListTag list; @@ -150,6 +336,7 @@ public class NBTBuilder { this.list = new ListTag(); } + // 原始类型方法 public ListNBTBuilder addString(String value) { list.add(StringTag.valueOf(value)); return this; @@ -191,24 +378,132 @@ public class NBTBuilder { } public ListNBTBuilder addByteArray(byte[] value) { - list.add(new ByteArrayTag(value)); + if (value != null) { + list.add(new ByteArrayTag(value)); + } return this; } public ListNBTBuilder addIntArray(int[] value) { - list.add(new IntArrayTag(value)); + if (value != null) { + list.add(new IntArrayTag(value)); + } return this; } public ListNBTBuilder addLongArray(long[] value) { - list.add(new LongArrayTag(value)); + if (value != null) { + list.add(new LongArrayTag(value)); + } + return this; + } + + // 包装类型方法 - null安全 + public ListNBTBuilder addString(String value, String defaultValue) { + list.add(StringTag.valueOf(value != null ? value : defaultValue)); + return this; + } + + public ListNBTBuilder addStringIf(String value) { + if (value != null) { + list.add(StringTag.valueOf(value)); + } + return this; + } + + public ListNBTBuilder addByte(Byte value) { + if (value != null) { + list.add(ByteTag.valueOf(value)); + } + return this; + } + + public ListNBTBuilder addByte(Byte value, byte defaultValue) { + list.add(ByteTag.valueOf(value != null ? value : defaultValue)); + return this; + } + + public ListNBTBuilder addShort(Short value) { + if (value != null) { + list.add(ShortTag.valueOf(value)); + } + return this; + } + + public ListNBTBuilder addShort(Short value, short defaultValue) { + list.add(ShortTag.valueOf(value != null ? value : defaultValue)); + return this; + } + + public ListNBTBuilder addInt(Integer value) { + if (value != null) { + list.add(IntTag.valueOf(value)); + } + return this; + } + + public ListNBTBuilder addInt(Integer value, int defaultValue) { + list.add(IntTag.valueOf(value != null ? value : defaultValue)); + return this; + } + + public ListNBTBuilder addLong(Long value) { + if (value != null) { + list.add(LongTag.valueOf(value)); + } + return this; + } + + public ListNBTBuilder addLong(Long value, long defaultValue) { + list.add(LongTag.valueOf(value != null ? value : defaultValue)); + return this; + } + + public ListNBTBuilder addFloat(Float value) { + if (value != null) { + list.add(FloatTag.valueOf(value)); + } + return this; + } + + public ListNBTBuilder addFloat(Float value, float defaultValue) { + list.add(FloatTag.valueOf(value != null ? value : defaultValue)); + return this; + } + + public ListNBTBuilder addDouble(Double value) { + if (value != null) { + list.add(DoubleTag.valueOf(value)); + } + return this; + } + + public ListNBTBuilder addDouble(Double value, double defaultValue) { + list.add(DoubleTag.valueOf(value != null ? value : defaultValue)); + return this; + } + + public ListNBTBuilder addBoolean(Boolean value) { + if (value != null) { + list.add(ByteTag.valueOf(value)); + } + return this; + } + + public ListNBTBuilder addBoolean(Boolean value, boolean defaultValue) { + list.add(ByteTag.valueOf(value != null ? value : defaultValue)); return this; } public ListNBTBuilder addCompound(Consumer consumer) { - NBTBuilder compoundBuilder = new NBTBuilder(); - consumer.accept(compoundBuilder); - list.add(compoundBuilder.build()); + if (consumer != null) { + NBTBuilder compoundBuilder = new NBTBuilder(); + consumer.accept(compoundBuilder); + CompoundTag compoundTag = compoundBuilder.build(); + if (!compoundTag.isEmpty()) { + list.add(compoundTag); + } + } return this; } @@ -219,6 +514,13 @@ public class NBTBuilder { return this; } + public ListNBTBuilder addIf(boolean condition, Consumer consumer) { + if (condition && consumer != null) { + consumer.accept(this); + } + return this; + } + public ListTag build() { return list; } @@ -227,13 +529,31 @@ public class NBTBuilder { // 便捷静态方法 public static CompoundTag create(Consumer consumer) { NBTBuilder builder = new NBTBuilder(); - consumer.accept(builder); + if (consumer != null) { + consumer.accept(builder); + } return builder.build(); } public static ListTag createList(Consumer consumer) { ListNBTBuilder builder = new ListNBTBuilder(); - consumer.accept(builder); + if (consumer != null) { + consumer.accept(builder); + } return builder.build(); } + + /** + * 检查构建的NBT是否为空 + */ + public boolean isEmpty() { + return root.isEmpty(); + } + + /** + * 获取NBT中所有键的集合 + */ + public java.util.Set getAllKeys() { + return root.getAllKeys(); + } } \ No newline at end of file