添加了NBTBuilder工具类

This commit is contained in:
叁玖领域 2025-10-15 16:07:41 +08:00
parent 679af9ac16
commit ea2865d58b

View File

@ -31,7 +31,7 @@ public class NBTBuilder {
return new NBTBuilder(existingTag); return new NBTBuilder(existingTag);
} }
// 基本数据类型 // 基本数据类型 - 原始类型
public NBTBuilder string(String key, String value) { public NBTBuilder string(String key, String value) {
root.putString(key, value); root.putString(key, value);
return this; return this;
@ -72,51 +72,204 @@ public class NBTBuilder {
return this; 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) { public NBTBuilder byteArray(String key, byte[] value) {
if (value != null) {
root.putByteArray(key, value); root.putByteArray(key, value);
}
return this; return this;
} }
public NBTBuilder intArray(String key, int[] value) { public NBTBuilder intArray(String key, int[] value) {
if (value != null) {
root.putIntArray(key, value); root.putIntArray(key, value);
}
return this; return this;
} }
public NBTBuilder longArray(String key, long[] value) { public NBTBuilder longArray(String key, long[] value) {
if (value != null) {
root.putLongArray(key, value); root.putLongArray(key, value);
}
return this; return this;
} }
// UUID支持 // UUID支持
public NBTBuilder uuid(String key, UUID value) { public NBTBuilder uuid(String key, UUID value) {
if (value != null) {
root.putUUID(key, value); 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; return this;
} }
// 嵌套CompoundTag // 嵌套CompoundTag
public NBTBuilder compound(String key, Consumer<NBTBuilder> consumer) { public NBTBuilder compound(String key, Consumer<NBTBuilder> consumer) {
if (consumer != null) {
NBTBuilder nestedBuilder = new NBTBuilder(); NBTBuilder nestedBuilder = new NBTBuilder();
consumer.accept(nestedBuilder); consumer.accept(nestedBuilder);
root.put(key, nestedBuilder.build()); CompoundTag nestedTag = nestedBuilder.build();
if (!nestedTag.isEmpty()) {
root.put(key, nestedTag);
}
}
return this; return this;
} }
public NBTBuilder compound(String key, CompoundTag compoundTag) { public NBTBuilder compound(String key, CompoundTag compoundTag) {
if (compoundTag != null && !compoundTag.isEmpty()) {
root.put(key, compoundTag); root.put(key, compoundTag);
}
return this;
}
public NBTBuilder compoundIf(String key, boolean condition, Consumer<NBTBuilder> consumer) {
if (condition && consumer != null) {
return compound(key, consumer);
}
return this; return this;
} }
// ListTag支持 // ListTag支持
public NBTBuilder list(String key, Consumer<ListNBTBuilder> consumer) { public NBTBuilder list(String key, Consumer<ListNBTBuilder> consumer) {
if (consumer != null) {
ListNBTBuilder listBuilder = new ListNBTBuilder(); ListNBTBuilder listBuilder = new ListNBTBuilder();
consumer.accept(listBuilder); consumer.accept(listBuilder);
root.put(key, listBuilder.build()); ListTag listTag = listBuilder.build();
if (!listTag.isEmpty()) {
root.put(key, listTag);
}
}
return this; return this;
} }
public NBTBuilder list(String key, ListTag listTag) { public NBTBuilder list(String key, ListTag listTag) {
if (listTag != null && !listTag.isEmpty()) {
root.put(key, listTag); root.put(key, listTag);
}
return this;
}
public NBTBuilder listIf(String key, boolean condition, Consumer<ListNBTBuilder> consumer) {
if (condition && consumer != null) {
return list(key, consumer);
}
return this; return this;
} }
@ -128,6 +281,35 @@ public class NBTBuilder {
return this; 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) { public NBTBuilder remove(String key) {
root.remove(key); root.remove(key);
@ -136,12 +318,16 @@ public class NBTBuilder {
// 构建最终的CompoundTag // 构建最终的CompoundTag
public CompoundTag build() { public CompoundTag build() {
return root.copy();
}
// 获取原始的CompoundTag不推荐除非你知道在做什么
public CompoundTag getRaw() {
return root; return root;
} }
/** /**
* ListTag专用的构建器 * ListTag专用的构建器 - 同样支持null安全
*/ */
public static class ListNBTBuilder { public static class ListNBTBuilder {
private final ListTag list; private final ListTag list;
@ -150,6 +336,7 @@ public class NBTBuilder {
this.list = new ListTag(); this.list = new ListTag();
} }
// 原始类型方法
public ListNBTBuilder addString(String value) { public ListNBTBuilder addString(String value) {
list.add(StringTag.valueOf(value)); list.add(StringTag.valueOf(value));
return this; return this;
@ -191,24 +378,132 @@ public class NBTBuilder {
} }
public ListNBTBuilder addByteArray(byte[] value) { public ListNBTBuilder addByteArray(byte[] value) {
if (value != null) {
list.add(new ByteArrayTag(value)); list.add(new ByteArrayTag(value));
}
return this; return this;
} }
public ListNBTBuilder addIntArray(int[] value) { public ListNBTBuilder addIntArray(int[] value) {
if (value != null) {
list.add(new IntArrayTag(value)); list.add(new IntArrayTag(value));
}
return this; return this;
} }
public ListNBTBuilder addLongArray(long[] value) { public ListNBTBuilder addLongArray(long[] value) {
if (value != null) {
list.add(new LongArrayTag(value)); 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; return this;
} }
public ListNBTBuilder addCompound(Consumer<NBTBuilder> consumer) { public ListNBTBuilder addCompound(Consumer<NBTBuilder> consumer) {
if (consumer != null) {
NBTBuilder compoundBuilder = new NBTBuilder(); NBTBuilder compoundBuilder = new NBTBuilder();
consumer.accept(compoundBuilder); consumer.accept(compoundBuilder);
list.add(compoundBuilder.build()); CompoundTag compoundTag = compoundBuilder.build();
if (!compoundTag.isEmpty()) {
list.add(compoundTag);
}
}
return this; return this;
} }
@ -219,6 +514,13 @@ public class NBTBuilder {
return this; return this;
} }
public ListNBTBuilder addIf(boolean condition, Consumer<ListNBTBuilder> consumer) {
if (condition && consumer != null) {
consumer.accept(this);
}
return this;
}
public ListTag build() { public ListTag build() {
return list; return list;
} }
@ -227,13 +529,31 @@ public class NBTBuilder {
// 便捷静态方法 // 便捷静态方法
public static CompoundTag create(Consumer<NBTBuilder> consumer) { public static CompoundTag create(Consumer<NBTBuilder> consumer) {
NBTBuilder builder = new NBTBuilder(); NBTBuilder builder = new NBTBuilder();
if (consumer != null) {
consumer.accept(builder); consumer.accept(builder);
}
return builder.build(); return builder.build();
} }
public static ListTag createList(Consumer<ListNBTBuilder> consumer) { public static ListTag createList(Consumer<ListNBTBuilder> consumer) {
ListNBTBuilder builder = new ListNBTBuilder(); ListNBTBuilder builder = new ListNBTBuilder();
if (consumer != null) {
consumer.accept(builder); consumer.accept(builder);
}
return builder.build(); return builder.build();
} }
/**
* 检查构建的NBT是否为空
*/
public boolean isEmpty() {
return root.isEmpty();
}
/**
* 获取NBT中所有键的集合
*/
public java.util.Set<String> getAllKeys() {
return root.getAllKeys();
}
} }