Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change behaviour of Instance getBlock to stop throwing NPE #1940

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

BomBardyGamer
Copy link
Contributor

This change means that, if Instance#getBlock is called on a block in an unloaded chunk, the method simply returns Block.AIR, instead of throwing a NullPointerException.

This behaviour is in line with Chunk#getBlock, and also in line with vanilla behaviour.

Motivation

Currently, if you try to get a block in an unloaded chunk using Instance#getBlock, it will throw a NullPointerException. This behaviour is very annoying for several reasons:

  • It is not documented anywhere. It is not at all obvious from any documentation anywhere that this is the case.
  • This is not something you should expect this method to do, especially consdering the overridden signature is @Nullable, when it can actually never return null because of the NPE behaviour.
  • It means that you either have to check if the chunk is loaded, or use a try catch around getBlock, catching a NullPointerException, every time you get a block from an Instance. Both of these are undesirable, and the latter is bad practice.

This aims to completely eliminate the possibility of encountering a NullPointerException when using Instance#getBlock, therefore avoiding confusion, saving inevitable debugging time, and moving the API in line with its Chunk counterpart.

@ElAndree
Copy link

ElAndree commented Sep 9, 2023

returning Block.AIR is very dangerous because you cannot be sure if the chunk is just unloaded or if there is really air.
why arent you just returning null instead of throwing a npe?

@BomBardyGamer
Copy link
Contributor Author

I was just about to update this saying why I'm not doing that. The original change did that.

The reason why I have decided that this is better is because there is very few circumstances where you actually really want to know that the chunk is unloaded, and this change would force you to do a null check to handle that case every time.

I think it is better if you have to be more explicit, checking if the chunk is loaded with isChunkLoaded, for the few cases where you actually want to know if the chunk is loaded or not.

@UserUNP
Copy link

UserUNP commented Sep 9, 2023

Returning null would probably be better so the code wouldn't check if a chunk is loaded twice.
It's also more convenient if the goal is simply not only checking if a chunk is loaded, but also getting the block as well.

@mworzala
Copy link
Member

I think that I also prefer the idea of returning null in case you do need to know that it is unloaded, and treating that as air is simple enough. That said, it could also potentially be a new condition to decide this.

@TheMode
Copy link
Member

TheMode commented Sep 10, 2023

Something like a new getVisibleBlockcould be more fitting, as unloaded chunks are invisible

@BomBardyGamer
Copy link
Contributor Author

BomBardyGamer commented Sep 10, 2023

That's not a bad idea. Though I'm not sure if it would be confusing for people not expecting it to be there.

Also, had a discussion about this last night, and @ZakShearman suggested this should be an exception thrown here as it is actually an exceptional condition (what exceptions are meant to be used for), but that it should be a custom one at least, definitely not NullPointerException. That's also an option, like a ChunkNotLoadedException.

@BomBardyGamer BomBardyGamer changed the title Return Block.AIR instead of throwing NPE for Instance getBlock Change behaviour of Instance getBlock to stop throwing NPE Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants