Handle tag logic inside read/write

This commit is contained in:
TheMode 2021-05-17 13:04:00 +02:00
parent 0a7b773aa8
commit 1715e55b4d

View File

@ -43,12 +43,20 @@ public class Tag<T> {
return defaultValue; return defaultValue;
} }
protected T read(@NotNull NBTCompound nbtCompound) { public @Nullable T read(@NotNull NBTCompound nbtCompound) {
return readFunction.apply(nbtCompound); if (nbtCompound.containsKey(key)) {
return readFunction.apply(nbtCompound);
} else {
return defaultValue != null ? defaultValue.get() : null;
}
} }
protected void write(@NotNull NBTCompound nbtCompound, @NotNull T value) { public void write(@NotNull NBTCompound nbtCompound, @Nullable T value) {
this.writeConsumer.accept(nbtCompound, value); if (value != null) {
this.writeConsumer.accept(nbtCompound, value);
} else {
nbtCompound.removeTag(key);
}
} }
public static @NotNull Tag<Byte> Byte(@NotNull String key) { public static @NotNull Tag<Byte> Byte(@NotNull String key) {