修正了NBTBuilder方法

This commit is contained in:
叁玖领域 2025-10-15 17:02:30 +08:00
parent ea2865d58b
commit 9a87b9ee54
2 changed files with 14 additions and 28 deletions

View File

@ -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

View File

@ -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安全
*/