Skip to content

Commit

Permalink
feat: add utility method to read nbt entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ynverxe committed May 26, 2023
1 parent 987af76 commit 99ad92e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public interface NBTReadable {
return (T) read(key).value();
}

default <T> @Nullable T readValue(@NotNull String key, @NotNull TagType<NBT<T>> type) {
NBT<?> found = read(key);

if (found == null) return null;

return found.getAs(type);
}

default @Nullable Number readNumber(@NotNull String key) {
return readValue(key);
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/CompoundTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.github.ynverxe.nbt_structure.nbt.NBT;
import com.github.ynverxe.nbt_structure.nbt.NBTCompound;
import com.github.ynverxe.nbt_structure.nbt.TagType;
import com.github.ynverxe.nbt_structure.nbt.tree.TreeNBTContainerHandler;
import org.junit.jupiter.api.Test;

Expand All @@ -11,7 +12,7 @@ public class CompoundTest {
public void testCompoundTag() {
NBTCompound compoundTag = new NBTCompound();
compoundTag.write("name", new NBT<>("Ynverxe"));
assertEquals("Ynverxe", compoundTag.readValue("name"));
assertEquals("Ynverxe", compoundTag.readValue("name", TagType.STRING));
}

@Test
Expand All @@ -21,6 +22,6 @@ public void testCompoundTagWithPath() {
String nameKey = "compound.compound.tag";

handler.write(nameKey, new NBT<>("Ynverxe"));
assertEquals("Ynverxe", handler.readValue(nameKey));
assertEquals("Ynverxe", handler.readValue(nameKey, TagType.STRING));
}
}

0 comments on commit 99ad92e

Please sign in to comment.