retrooper / packetevents

Precision meets performance—a powerful tool for mastering Minecraft packet manipulation with speed and finesse.
GNU General Public License v3.0
563 stars 151 forks source link

Proposal - Use nullables instead of optionals for packet fields that may not exist on certain versions #384

Open Jaren8r opened 2 years ago

Jaren8r commented 2 years ago

Currently, PacketEvents stores packet fields that may not exist across all supported versions as Optionals. This isn't great for several reasons:

I'm thinking that it would be better to convert all of these usages of Optional to nullable, but it would require an API breakage. I'm interested to hear thoughts on this.

NoJokeFNA commented 2 years ago

In fact, I would very much welcome this change. If you use them, but forget to call Optional#empty, you just get NullPointerException's, which you can just save. I would clearly be in favor of removing optionals if there isn't a good use case for them - such as with the current wrappers.

With the change, you could also start documenting the getters and setters and annotate them with @Nullable & @NotNull so that the difference is very clear and it is also very clear which method is for which server version.

retrooper commented 2 years ago

If possible I always try to avoid having methods for separate versions. Like bringing everything to one form. Sometimes that isn't always possible. I'll consider this.

MWHunter commented 2 years ago

I prefer the following scenario for the one time to use optional over null, but it breaks convention:

This preserves information.

I'd suggest just sticking with null for readability reasons. We aren't here to convert packets between versions and any attempts to do so should be considered out of scope and avoided for maintainability reasons.

MWHunter commented 2 years ago

A more user proof method would be to throw an exception if the field doesn't exist on the server version like PE 1.8 did