From 7a3417564dc455648f4b600ce301f3dacf3da061 Mon Sep 17 00:00:00 2001 From: Ynverxe Date: Sun, 30 Jul 2023 20:35:08 -0300 Subject: [PATCH] feat: add more nbt read methods --- .../github/ynverxe/nbt_structure/nbt/NBTReadable.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/com/github/ynverxe/nbt_structure/nbt/NBTReadable.java b/src/main/java/com/github/ynverxe/nbt_structure/nbt/NBTReadable.java index ebf201b..be57c68 100644 --- a/src/main/java/com/github/ynverxe/nbt_structure/nbt/NBTReadable.java +++ b/src/main/java/com/github/ynverxe/nbt_structure/nbt/NBTReadable.java @@ -4,6 +4,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Objects; +import java.util.Optional; import java.util.function.Function; public interface NBTReadable { @@ -18,6 +19,10 @@ public interface NBTReadable { return new Tag<>(key, found); } + default > @NotNull Optional readOptional(@NotNull String key, @NotNull TagType type) { + return Optional.ofNullable(read(key, type)); + } + default > @Nullable N read(@NotNull String key, @NotNull TagType type) { return type.cast(read(key)); } @@ -41,6 +46,10 @@ default T parseValue(@NotNull String key, @NotNull TagType> type, return found != null ? parser.apply(found) : null; } + default @NotNull Optional readOptionalValue(@NotNull String key, @NotNull TagType> type) { + return Optional.ofNullable(readValue(key, type)); + } + default @Nullable Number readNumber(@NotNull String key) { return readValue(key); }