From 679af9ac163b77fe99835daa04b7ecbaf4595067 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 --- gradle.properties | 2 +- .../lib39/util/nbt/NBTBuilder.java | 239 ++++++++++++++++++ 2 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java diff --git a/gradle.properties b/gradle.properties index 42d8690..3a403ea 100644 --- a/gradle.properties +++ b/gradle.properties @@ -33,7 +33,7 @@ mod_name=3944Realms 's Lib Mod # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=MIT # The mod version. See https://semver.org/ -mod_version=0.0.8 +mod_version=0.0.9 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java b/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java new file mode 100644 index 0000000..57ec23b --- /dev/null +++ b/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java @@ -0,0 +1,239 @@ +package top.r3944realms.lib39.util.nbt; + +import net.minecraft.nbt.*; + +import java.util.UUID; +import java.util.function.Consumer; + +@SuppressWarnings("unused") +public class NBTBuilder { + private final CompoundTag root; + + private NBTBuilder() { + this.root = new CompoundTag(); + } + + private NBTBuilder(CompoundTag existingTag) { + this.root = existingTag; + } + + /** + * 创建一个新的NBT构建器 + */ + public static NBTBuilder builder() { + return new NBTBuilder(); + } + + /** + * 基于现有CompoundTag创建构建器 + */ + public static NBTBuilder of(CompoundTag existingTag) { + return new NBTBuilder(existingTag); + } + + // 基本数据类型 + public NBTBuilder string(String key, String value) { + root.putString(key, value); + return this; + } + + public NBTBuilder byteValue(String key, byte value) { + root.putByte(key, value); + return this; + } + + public NBTBuilder shortValue(String key, short value) { + root.putShort(key, value); + return this; + } + + public NBTBuilder intValue(String key, int value) { + root.putInt(key, value); + return this; + } + + public NBTBuilder longValue(String key, long value) { + root.putLong(key, value); + return this; + } + + public NBTBuilder floatValue(String key, float value) { + root.putFloat(key, value); + return this; + } + + public NBTBuilder doubleValue(String key, double value) { + root.putDouble(key, value); + return this; + } + + public NBTBuilder booleanValue(String key, boolean value) { + root.putBoolean(key, value); + return this; + } + + // 数组类型 + public NBTBuilder byteArray(String key, byte[] value) { + root.putByteArray(key, value); + return this; + } + + public NBTBuilder intArray(String key, int[] value) { + root.putIntArray(key, value); + return this; + } + + public NBTBuilder longArray(String key, long[] value) { + root.putLongArray(key, value); + return this; + } + + // UUID支持 + public NBTBuilder uuid(String key, UUID value) { + root.putUUID(key, value); + return this; + } + + // 嵌套CompoundTag + public NBTBuilder compound(String key, Consumer consumer) { + NBTBuilder nestedBuilder = new NBTBuilder(); + consumer.accept(nestedBuilder); + root.put(key, nestedBuilder.build()); + return this; + } + + public NBTBuilder compound(String key, CompoundTag compoundTag) { + root.put(key, compoundTag); + return this; + } + + // ListTag支持 + public NBTBuilder list(String key, Consumer consumer) { + ListNBTBuilder listBuilder = new ListNBTBuilder(); + consumer.accept(listBuilder); + root.put(key, listBuilder.build()); + return this; + } + + public NBTBuilder list(String key, ListTag listTag) { + root.put(key, listTag); + return this; + } + + // 直接操作Tag + public NBTBuilder tag(String key, Tag tag) { + if (tag != null) { + root.put(key, tag); + } + return this; + } + + // 移除标签 + public NBTBuilder remove(String key) { + root.remove(key); + return this; + } + + // 构建最终的CompoundTag + public CompoundTag build() { + return root; + } + + + /** + * ListTag专用的构建器 + */ + public static class ListNBTBuilder { + private final ListTag list; + + private ListNBTBuilder() { + this.list = new ListTag(); + } + + public ListNBTBuilder addString(String value) { + list.add(StringTag.valueOf(value)); + return this; + } + + public ListNBTBuilder addByte(byte value) { + list.add(ByteTag.valueOf(value)); + return this; + } + + public ListNBTBuilder addShort(short value) { + list.add(ShortTag.valueOf(value)); + return this; + } + + public ListNBTBuilder addInt(int value) { + list.add(IntTag.valueOf(value)); + return this; + } + + public ListNBTBuilder addLong(long value) { + list.add(LongTag.valueOf(value)); + return this; + } + + public ListNBTBuilder addFloat(float value) { + list.add(FloatTag.valueOf(value)); + return this; + } + + public ListNBTBuilder addDouble(double value) { + list.add(DoubleTag.valueOf(value)); + return this; + } + + public ListNBTBuilder addBoolean(boolean value) { + list.add(ByteTag.valueOf(value)); + return this; + } + + public ListNBTBuilder addByteArray(byte[] value) { + list.add(new ByteArrayTag(value)); + return this; + } + + public ListNBTBuilder addIntArray(int[] value) { + list.add(new IntArrayTag(value)); + return this; + } + + public ListNBTBuilder addLongArray(long[] value) { + list.add(new LongArrayTag(value)); + return this; + } + + public ListNBTBuilder addCompound(Consumer consumer) { + NBTBuilder compoundBuilder = new NBTBuilder(); + consumer.accept(compoundBuilder); + list.add(compoundBuilder.build()); + return this; + } + + public ListNBTBuilder addTag(Tag tag) { + if (tag != null) { + list.add(tag); + } + return this; + } + + public ListTag build() { + return list; + } + } + + // 便捷静态方法 + public static CompoundTag create(Consumer consumer) { + NBTBuilder builder = new NBTBuilder(); + consumer.accept(builder); + return builder.build(); + } + + public static ListTag createList(Consumer consumer) { + ListNBTBuilder builder = new ListNBTBuilder(); + consumer.accept(builder); + return builder.build(); + } +} \ No newline at end of file