From 49488c489341d4341d11ed1ee62483d66f0546d6 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 16 Apr 2022 14:37:22 +0200 Subject: [PATCH] Remove nbt tag list/map restriction Signed-off-by: TheMode --- .../java/net/minestom/server/tag/Tag.java | 4 --- .../net/minestom/server/tag/TagNbtTest.java | 36 ++++++++++++++++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java index 9191cc4ff..97c0b89ab 100644 --- a/src/main/java/net/minestom/server/tag/Tag.java +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -97,8 +97,6 @@ public class Tag { @Contract(value = "_, _ -> new", pure = true) public Tag map(@NotNull Function readMap, @NotNull Function writeMap) { - if (entry == Serializers.NBT_ENTRY) - throw new IllegalArgumentException("Cannot create a list from a NBT entry"); var entry = this.entry; final Function readFunction = entry.read().andThen(t -> { if (t == null) return null; @@ -115,8 +113,6 @@ public class Tag { @ApiStatus.Experimental @Contract(value = "-> new", pure = true) public Tag> list() { - if (entry == Serializers.NBT_ENTRY) - throw new IllegalArgumentException("Cannot create a list from a NBT entry"); var entry = this.entry; var readFunction = entry.read(); var writeFunction = entry.write(); diff --git a/src/test/java/net/minestom/server/tag/TagNbtTest.java b/src/test/java/net/minestom/server/tag/TagNbtTest.java index 066dffa06..86e2fc3cc 100644 --- a/src/test/java/net/minestom/server/tag/TagNbtTest.java +++ b/src/test/java/net/minestom/server/tag/TagNbtTest.java @@ -1,13 +1,16 @@ package net.minestom.server.tag; import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.NBTInt; import org.jglrxavpok.hephaistos.nbt.NBTType; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Map; -import static org.junit.jupiter.api.Assertions.*; +import static net.minestom.server.api.TestUtils.assertEqualsSNBT; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; /** * Ensure that NBT tag can be read from other tags properly. @@ -15,13 +18,36 @@ import static org.junit.jupiter.api.Assertions.*; public class TagNbtTest { @Test - public void invalidListTag() { - assertThrows(IllegalArgumentException.class, () -> Tag.NBT("nbt").list()); + public void list() { + var handler = TagHandler.newHandler(); + var tag = Tag.NBT("nbt").list(); + List list = List.of(NBT.Int(1), NBT.Int(2), NBT.Int(3)); + handler.setTag(tag, list); + assertEquals(list, handler.getTag(tag)); + assertEqualsSNBT(""" + { + "nbt": [1,2,3] + } + """, handler.asCompound()); + + handler.removeTag(tag); + assertEqualsSNBT("{}", handler.asCompound()); } @Test - public void invalidMapTag() { - assertThrows(IllegalArgumentException.class, () -> Tag.NBT("nbt").map(nbt -> 1, NBT::Int)); + public void map() { + var handler = TagHandler.newHandler(); + var tag = Tag.NBT("nbt").map(nbt -> ((NBTInt) nbt).getValue(), NBT::Int); + handler.setTag(tag, 5); + assertEquals(5, handler.getTag(tag)); + assertEqualsSNBT(""" + { + "nbt":5 + } + """, handler.asCompound()); + + handler.removeTag(tag); + assertEqualsSNBT("{}", handler.asCompound()); } @Test