Add protection against potential world corruption
SpaceToad opened this issue ยท 8 comments
TileEntity te = world.getTileEntity(x, y, z);
if(te != null && te instanceof TileMarker)
{
((TileMarker) world.getTileEntity(x, y, z)).updateSignals();
}
Should avoid crash.
Yup, definitely :-) There's probably a bit more code review to be made on blocks, to identify other similar situations. If you feel like it, let me know!
'instanceof' keyword returns false if your variable is null. So you can do just
if(te instanceof TileMarker) thing.
@anti344 thank ! I got into the habit of putting a null check because of ItemStack ( if(stack != null && stack.getItem() == SomeThing)), but as here we do not call a method it is true that the null check is unnecessary.
if(stack.getItem() != null && stack.getItem() == SomeThing)
That doesn't make sense. Perhaps you meant if (stack != null...
instanceof is a costly operation. The tile entity shouldn't be anything other than the one expected, if something like that happens then there is a bug somewhere. So checking for null should be enough.
That doesn't make sense. Perhaps you meant if (stack != null...
Indeed x) I edited my last post.
So checking for null should be enough.
No, if the tile entity was null, there will have been a NullPointerException and not a ClassCastException. the tile entity is not null, it was not the right, so only a instanceof can fix the crash. But it is true that this should not happen under normal circumstances, it should not have a pipe tile entity on a marker ...