Fix NPE on Boat#getStatus
Tested in the three situations
TestPlugin.java
@Override
public void onEnable() {
this.getServer().getPluginManager().registerEvents(this, this);
/*
* Get status at the same time as entity spawn
*/
this.getServer().getCommandMap().register("boat", new Command("boat") {
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
if (sender instanceof Player player) {
final var loc = player.getLocation();
player.sendRawMessage( loc.getWorld().spawn(loc, Boat.class).getStatus().name() );
}
return false;
}
});
}
@EventHandler
void onEntityAddToWorld(final EntityAddToWorldEvent event) {
if (event.getEntity() instanceof Boat boat) {
this.logger.info("EntityAddToWorldEvent: " + boat.getStatus().name());
}
}
/*
* Ride on the boat and reconnect the server
*/
@EventHandler
void onEntityMount(final EntityMountEvent event) {
if (event.getMount() instanceof Boat boat) {
this.logger.info("EntityMountEvent: " + boat.getStatus().name());
}
}
My only concern, which I mentioned in the issue I opened about this a while ago https://github.com/PaperMC/Paper/issues/8661, is that it's not immediately clear to me that it's safe to call getStatus when the boat hasn't been "in the world". It calls a lot of logic about the current position of the boat and world state which could totally not be written to work when the boat entity isn't in the world.
An alternative solution would be to add a new "status" like "UNKNOWN" or "NOT_IN_WORLD" or something that it defaults to before it's been ticked. I am not advocating for either solution, it is just something that has to be considered (if you haven't already).
Fixed the patch to add a new "NOT_IN_WORLD" status and return the correct status (if possible).
I'm also experiencing this issue, so I'm glad there's a PR for it.