From 37ba3c78934437d9b01240f25f2917580584676a Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 9 May 2022 20:56:06 +0200 Subject: [PATCH] Simplify tag list Signed-off-by: TheMode --- .../java/net/minestom/server/tag/Tag.java | 31 +++++-------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java index 7f5f911a8..1d050cf09 100644 --- a/src/main/java/net/minestom/server/tag/Tag.java +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -123,32 +123,15 @@ public class Tag { var listEntry = new Serializers.Entry, NBTList>( NBTType.TAG_List, read -> { - var list = (NBTList) read; - final int size = list.getSize(); - if (size == 0) - return List.of(); - T[] array = (T[]) new Object[size]; - for (int i = 0; i < size; i++) { - array[i] = readFunction.apply(list.get(i)); - } - return List.of(array); + if (read.isEmpty()) return List.of(); + return read.asListView().stream().map(readFunction).toList(); }, write -> { - final int size = write.size(); - if (size == 0) - return new NBTList<>(NBTType.TAG_String); // String is the default type for lists - NBTType type = null; - NBT[] array = new NBT[size]; - for (int i = 0; i < size; i++) { - final NBT nbt = writeFunction.apply(write.get(i)); - if (type == null) { - type = (NBTType) nbt.getID(); - } else if (type != nbt.getID()) { - throw new IllegalArgumentException("All elements of the list must have the same type"); - } - array[i] = nbt; - } - return NBT.List(type, List.of(array)); + if (write.isEmpty()) + return NBT.List(NBTType.TAG_String); // String is the default type for lists + final List list = write.stream().map(writeFunction).toList(); + final NBTType type = list.get(0).getID(); + return NBT.List(type, list); }); UnaryOperator> co = this.copy != null ? ts -> { final int size = ts.size();