Globox1997 / LevelZ

https://modrinth.com/mod/levelz
https://www.curseforge.com/minecraft/mc-mods/levelz
GNU General Public License v3.0
51 stars 50 forks source link

Crash in EnchantmentHelperMixin triggered in some circumstances. #115

Closed Vaelzan closed 2 years ago

Vaelzan commented 2 years ago

There's a couple of areas that can crash in EnchantmentHelperMixin. We're getting a heap of reports on the Valhelsia Discord that in Valhelsia: Enhanced Vanilla we're having a crash occur when Shulker Bullets collide with a player on servers, and narrowed it down to a onTargetDamagedMixin in LevelZ. Glancing at the rest of the code, it looks like a similar crash is possible in onUserDamagedMixin, although it's the former that is triggering the crashes that are reported to us, such as in the image below (sorry it's not on Gist, but people report bugs in weird ways on Discord).

https://cdn.discordapp.com/attachments/856905835069964319/955018589944049694/unknown.png

This: https://github.com/Globox1997/LevelZ/blob/6144f4e9e7bc07328eb315554b4cce0eb5c1f198/src/main/java/net/levelz/mixin/misc/EnchantmentHelperMixin.java#L43 and this https://github.com/Globox1997/LevelZ/blob/6144f4e9e7bc07328eb315554b4cce0eb5c1f198/src/main/java/net/levelz/mixin/misc/EnchantmentHelperMixin.java#L68 are causing crashes due to the user variable being used to create item before the check to see if it's a PlayerEntity (which is checked on the following line). It's an easy fix, just check user instanceof PlayerEntity on the lines before the lines that are creating the item variable, instead of as part of the item instanceof ToolItem check that is on the line after.

Globox1997 commented 2 years ago

Hey Vaelzan, thanks for reporting this bug, a crash log would be better than a screenshot but you already explained what the issue is, which makes it easy for me to fix it, so thx :) I wonder how the user variable is null in some cases but yes an instanceof check will fix this. Fixed with https://github.com/Globox1997/LevelZ/commit/9e7ca1cddffbf9a963a94191db0dd5b9fe7d2aa2