mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-07 17:08:30 +01:00
Simplify tag list
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
64de8f87c0
commit
37ba3c7893
@ -123,32 +123,15 @@ public class Tag<T> {
|
|||||||
var listEntry = new Serializers.Entry<List<T>, NBTList<?>>(
|
var listEntry = new Serializers.Entry<List<T>, NBTList<?>>(
|
||||||
NBTType.TAG_List,
|
NBTType.TAG_List,
|
||||||
read -> {
|
read -> {
|
||||||
var list = (NBTList<?>) read;
|
if (read.isEmpty()) return List.of();
|
||||||
final int size = list.getSize();
|
return read.asListView().stream().map(readFunction).toList();
|
||||||
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);
|
|
||||||
},
|
},
|
||||||
write -> {
|
write -> {
|
||||||
final int size = write.size();
|
if (write.isEmpty())
|
||||||
if (size == 0)
|
return NBT.List(NBTType.TAG_String); // String is the default type for lists
|
||||||
return new NBTList<>(NBTType.TAG_String); // String is the default type for lists
|
final List<NBT> list = write.stream().map(writeFunction).toList();
|
||||||
NBTType<NBT> type = null;
|
final NBTType<?> type = list.get(0).getID();
|
||||||
NBT[] array = new NBT[size];
|
return NBT.List(type, list);
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
final NBT nbt = writeFunction.apply(write.get(i));
|
|
||||||
if (type == null) {
|
|
||||||
type = (NBTType<NBT>) 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));
|
|
||||||
});
|
});
|
||||||
UnaryOperator<List<T>> co = this.copy != null ? ts -> {
|
UnaryOperator<List<T>> co = this.copy != null ? ts -> {
|
||||||
final int size = ts.size();
|
final int size = ts.size();
|
||||||
|
Loading…
Reference in New Issue
Block a user