From 0a7b773aa879973694525d09f71197f8a112c5cc Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 17 May 2021 12:44:22 +0200 Subject: [PATCH] Added tag default value --- src/main/java/net/minestom/server/tag/Tag.java | 18 ++++++++++++++++++ .../net/minestom/server/tag/TagGetter.java | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java index 2b9f3ab21..10ff89b1d 100644 --- a/src/main/java/net/minestom/server/tag/Tag.java +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -1,11 +1,13 @@ package net.minestom.server.tag; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBT; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.function.BiConsumer; import java.util.function.Function; +import java.util.function.Supplier; public class Tag { @@ -13,6 +15,8 @@ public class Tag { private final Function readFunction; private final BiConsumer writeConsumer; + protected volatile Supplier defaultValue; + private Tag(@NotNull String key, @NotNull Function readFunction, @NotNull BiConsumer writeConsumer) { @@ -25,6 +29,20 @@ public class Tag { return key; } + public Tag defaultValue(@NotNull Supplier defaultValue) { + this.defaultValue = defaultValue; + return this; + } + + public Tag defaultValue(@NotNull T defaultValue) { + defaultValue(() -> defaultValue); + return this; + } + + public @Nullable Supplier<@Nullable T> getDefaultValue() { + return defaultValue; + } + protected T read(@NotNull NBTCompound nbtCompound) { return readFunction.apply(nbtCompound); } diff --git a/src/main/java/net/minestom/server/tag/TagGetter.java b/src/main/java/net/minestom/server/tag/TagGetter.java index 9f5b99852..e02d0a520 100644 --- a/src/main/java/net/minestom/server/tag/TagGetter.java +++ b/src/main/java/net/minestom/server/tag/TagGetter.java @@ -5,4 +5,6 @@ import org.jetbrains.annotations.Nullable; public interface TagGetter { @Nullable T getTag(@NotNull Tag tag); + + boolean hasTag(@NotNull Tag tag); }