Remove nbt tag list/map restriction

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-04-16 14:37:22 +02:00
parent 634d2c87b7
commit 49488c4893
2 changed files with 31 additions and 9 deletions

View File

@ -97,8 +97,6 @@ public class Tag<T> {
@Contract(value = "_, _ -> new", pure = true)
public <R> Tag<R> map(@NotNull Function<T, R> readMap,
@NotNull Function<R, T> writeMap) {
if (entry == Serializers.NBT_ENTRY)
throw new IllegalArgumentException("Cannot create a list from a NBT entry");
var entry = this.entry;
final Function<NBT, R> readFunction = entry.read().andThen(t -> {
if (t == null) return null;
@ -115,8 +113,6 @@ public class Tag<T> {
@ApiStatus.Experimental
@Contract(value = "-> new", pure = true)
public Tag<List<T>> 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();

View File

@ -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<NBT> 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