Ars Additions

Ars Additions

6M Downloads

Crash on ATM10 2.4.7 from trying to call a method on null

Closed this issue ยท 2 comments

commented

Right clicked the Advanced Dominion Wand on an ME Source Jar from Ars Energistique. I believe it's because AdvancedDominionData.fromItemStack can return null.

public static AdvancedDominionData fromItemStack(ItemStack stack) {
return stack.getOrDefault(AddonDataComponentRegistry.ADVANCED_DOMINION_DATA.get(), new AdvancedDominionData(Optional.empty(), Optional.empty(), Optional.empty(), AdvancedDominionData.Mode.LOCK_FIRST));
}

You probably just need to check data for null here and cancel out early to avoid calling a method on something that might not exist:

if (!pPlayer.isShiftKeyDown()) {
AdvancedDominionData data = AdvancedDominionData.fromItemStack(stack);
data = data.toggleMode().write(stack);
PortUtil.sendMessageNoSpam(pPlayer, Component.translatable("chat.ars_additions.advanced_dominion_wand.mode", data.mode().getTranslatable()));
return InteractionResultHolder.success(stack);
}

Here is the crash log if it helps:
https://gist.github.com/Meantub/2e55c1b3d737456aa4030ed665f05ba5

commented

63 does not write the data to the stack, it gets what the stack currently is set to or a default value without setting it.
64 takes care of this by writing the new value after being toggled.
64 sets the data variable to the return value of write
write returns the previous value, or null if it was previously unset

so in the state where there is no current value, using the wand without holding shift will:
get the default value
write the default value and set "data" to null

65 then tries to call data.mode(), which, when null, is trying to call the mode method from null.

commented

That makes sense, I don't know all the inner workings of each of the methods so I could've done a better job there, I just encountered the bug and was poking around in the stack trace and thought that was a candidate. null handling in general seems kinda rough in Java.