From 9a87b9ee54109d4af175d20ed7f802a3e106199d Mon Sep 17 00:00:00 2001 From: 3944Realms Date: Wed, 15 Oct 2025 17:02:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86NBTBuilder=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../lib39/util/nbt/NBTBuilder.java | 40 ++++++------------- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/gradle.properties b/gradle.properties index 3a403ea..5f8f5d2 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.9 +mod_version=0.0.10 # 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 index e249e90..c74a311 100644 --- a/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java +++ b/src/main/java/top/r3944realms/lib39/util/nbt/NBTBuilder.java @@ -1,6 +1,8 @@ package top.r3944realms.lib39.util.nbt; import net.minecraft.nbt.*; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; import java.util.UUID; import java.util.function.Consumer; @@ -20,23 +22,19 @@ public class NBTBuilder { /** * 创建一个新的NBT构建器 */ - public static NBTBuilder builder() { + @Contract(value = " -> new", pure = true) + public static @NotNull NBTBuilder builder() { return new NBTBuilder(); } /** * 基于现有CompoundTag创建构建器 */ - public static NBTBuilder of(CompoundTag existingTag) { + @Contract(value = "_ -> new", pure = true) + public static @NotNull 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; @@ -71,7 +69,12 @@ public class NBTBuilder { root.putBoolean(key, value); return this; } - + public NBTBuilder string(String key, String value) { + if (value != null) { + root.putString(key, value); + } + return this; + } // 包装类型 - null安全的版本 public NBTBuilder string(String key, String value, String defaultValue) { if (value != null) { @@ -82,19 +85,6 @@ public class NBTBuilder { 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) { @@ -318,14 +308,10 @@ public class NBTBuilder { // 构建最终的CompoundTag public CompoundTag build() { - return root.copy(); - } - - // 获取原始的CompoundTag(不推荐,除非你知道在做什么) - public CompoundTag getRaw() { return root; } + /** * ListTag专用的构建器 - 同样支持null安全 */