Added tag default value

This commit is contained in:
TheMode 2021-05-17 12:44:22 +02:00
parent 9c41a19592
commit 0a7b773aa8
2 changed files with 20 additions and 0 deletions

View File

@ -1,11 +1,13 @@
package net.minestom.server.tag; package net.minestom.server.tag;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBT; import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
public class Tag<T> { public class Tag<T> {
@ -13,6 +15,8 @@ public class Tag<T> {
private final Function<NBTCompound, T> readFunction; private final Function<NBTCompound, T> readFunction;
private final BiConsumer<NBTCompound, T> writeConsumer; private final BiConsumer<NBTCompound, T> writeConsumer;
protected volatile Supplier<T> defaultValue;
private Tag(@NotNull String key, private Tag(@NotNull String key,
@NotNull Function<NBTCompound, T> readFunction, @NotNull Function<NBTCompound, T> readFunction,
@NotNull BiConsumer<NBTCompound, T> writeConsumer) { @NotNull BiConsumer<NBTCompound, T> writeConsumer) {
@ -25,6 +29,20 @@ public class Tag<T> {
return key; return key;
} }
public Tag<T> defaultValue(@NotNull Supplier<T> defaultValue) {
this.defaultValue = defaultValue;
return this;
}
public Tag<T> defaultValue(@NotNull T defaultValue) {
defaultValue(() -> defaultValue);
return this;
}
public @Nullable Supplier<@Nullable T> getDefaultValue() {
return defaultValue;
}
protected T read(@NotNull NBTCompound nbtCompound) { protected T read(@NotNull NBTCompound nbtCompound) {
return readFunction.apply(nbtCompound); return readFunction.apply(nbtCompound);
} }

View File

@ -5,4 +5,6 @@ import org.jetbrains.annotations.Nullable;
public interface TagGetter { public interface TagGetter {
<T> @Nullable T getTag(@NotNull Tag<T> tag); <T> @Nullable T getTag(@NotNull Tag<T> tag);
boolean hasTag(@NotNull Tag<?> tag);
} }