Querz / NBT

A java implementation of the NBT protocol, including a way to implement custom tags.
MIT License
177 stars 47 forks source link

Use actual element type for empty ListTag #22

Closed Marcono1234 closed 5 years ago

Marcono1234 commented 5 years ago

Currently for empty ListTags the type EndTag is used, even if the actual type is a different one. This introduces all kind of type-safety problems, see #14.

According to https://wiki.vg/NBT#Specification it is not "required" that EndTag is used:

The notchian implementation uses TAG_End in that situation, but another reference implementation by Mojang uses 1 instead; parsers should accept any type if the length is <= 0

Therefore it might be good to at least have this library use the correct type for serialization (but keep deserialization the same for legacy support). The methods getTypeID() and getTypeClass() should be changed as well.

Also ListTag.typeClass could be Class<? extends T> and an non-type-safe list would have null as type, or maybe rather Optional<Class<? extends T>> to take advantage of some of Optional's methods. This woud prevent some casts which are currently necessary.

Let me know if I should write a pull-request for it. Wanted to know your opinion about this first.

Querz commented 5 years ago

ListTag now uses the correct type when serialising. Deserialisation will detect the type after the first element has been added by deserialisation or later by the user of this library. The only way one could obtain an empty untyped ListTag is by deserialising an empty ListTag marked as a ListTag of type EndTag. Setting the type of an empty ListTag manually without adding a new element can be achieved by using one of the ListTag#as...List() methods.