Handle tag logic inside read/write

This commit is contained in:
TheMode 2021-05-17 13:04:00 +02:00
parent 0a7b773aa8
commit 1715e55b4d
1 changed files with 12 additions and 4 deletions

View File

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