mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-07 17:08:30 +01:00
Remove nbt tag list/map restriction
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
634d2c87b7
commit
49488c4893
@ -97,8 +97,6 @@ public class Tag<T> {
|
|||||||
@Contract(value = "_, _ -> new", pure = true)
|
@Contract(value = "_, _ -> new", pure = true)
|
||||||
public <R> Tag<R> map(@NotNull Function<T, R> readMap,
|
public <R> Tag<R> map(@NotNull Function<T, R> readMap,
|
||||||
@NotNull Function<R, T> writeMap) {
|
@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;
|
var entry = this.entry;
|
||||||
final Function<NBT, R> readFunction = entry.read().andThen(t -> {
|
final Function<NBT, R> readFunction = entry.read().andThen(t -> {
|
||||||
if (t == null) return null;
|
if (t == null) return null;
|
||||||
@ -115,8 +113,6 @@ public class Tag<T> {
|
|||||||
@ApiStatus.Experimental
|
@ApiStatus.Experimental
|
||||||
@Contract(value = "-> new", pure = true)
|
@Contract(value = "-> new", pure = true)
|
||||||
public Tag<List<T>> list() {
|
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 entry = this.entry;
|
||||||
var readFunction = entry.read();
|
var readFunction = entry.read();
|
||||||
var writeFunction = entry.write();
|
var writeFunction = entry.write();
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
package net.minestom.server.tag;
|
package net.minestom.server.tag;
|
||||||
|
|
||||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||||
|
import org.jglrxavpok.hephaistos.nbt.NBTInt;
|
||||||
import org.jglrxavpok.hephaistos.nbt.NBTType;
|
import org.jglrxavpok.hephaistos.nbt.NBTType;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.
|
* 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 {
|
public class TagNbtTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invalidListTag() {
|
public void list() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> Tag.NBT("nbt").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
|
@Test
|
||||||
public void invalidMapTag() {
|
public void map() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> Tag.NBT("nbt").map(nbt -> 1, NBT::Int));
|
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
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user