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

Fix issue with poorly implemented modded consumables returning null as foodcomponents #235

Merged
merged 1 commit into from May 4, 2023

Conversation

NerjalNosk
Copy link

Prevents client crash upon trying to get how to render a modded consumable's details when its getFoodComponent method returns null.
Includes modification for versions from 1.19 to 1.19.4 + version bump (done via cherry-picking commits)

@squeek502
Copy link
Owner

squeek502 commented May 4, 2023

Could you provide information about how this can be reproduced? As far as I can tell, this should be impossible since isFood() is a foodComponent != null check and both HUDOverlayHandler and TooltipOverlayHandler call isFood() before getting either of the food values.

Is some mod overriding isFood and then returning null from getFoodComponent? If so, I don't see how this wouldn't cause crashes in vanilla Minecraft code since there's many places that do things like this:

        if (stack.getItem().isFood()) {
            return this.getFoodComponent().isSnack() ? 16 : 32;
        }

@NerjalNosk
Copy link
Author

Too be honest, I don't really know the details of the mod causing this, I only faced the bug itself.
I can give some logs tho

crash-2023-05-03_23.40.21-client.txt

@NerjalNosk
Copy link
Author

As for how the bug really happened, I cannot tell really, for the game only crashed spontaneously about anytime I was opening any form of inventory

@SAGESSE-CN
Copy link
Contributor

I suggest returning foodvalues.zero instead of null when it is null, and returning null causes all code(appleskin and user code of events) to add unknown branches

@NerjalNosk
Copy link
Author

That might be a good idea indeed, that's something I was not aware of.
Gotta make it a thing right away

@squeek502
Copy link
Owner

squeek502 commented May 4, 2023

Until I understand more about what's going on, it's hard to know if this is the right fix (or if this is something that AppleSkin should try to protect against, or if it's a bug in some other mod).

My suggestion would be to do something like this locally in getDefaultFoodValues:

		if (itemFood == null) {
			throw new RuntimeException("food with null FoodComponent: " + itemStack)
		}

and then reproduce the crash with that code. The crash should then let you know which ItemStack is causing the problem and we can go from there.

@NerjalNosk
Copy link
Author

From my understanding of the actual situation, that issue is something that might have already occurred with Forge as well, for there are these checks in the forge code (in squeek.appleskin.helpers.FoodHelper#getDefaultFoodValues, L38~39)

		int hunger = itemFood != null ? itemFood.getNutrition() : 0;
		float saturationModifier = itemFood != null ? itemFood.getSaturationModifier() : 0;

Which only makes me think that this was an attempt to prevent that same error to happen, although with a set of ternary operators rather than the option I went for in the end, after @SAGESSE-CN 's suggestion

@squeek502
Copy link
Owner

squeek502 commented May 4, 2023

I'm convinced that this is a fine change. Could you remove the version bump from this PR, though?

Out of curiosity, it might be worth tracking down the cause anyway. Here's a build of appleskin-fabric-mc1.19-2.4.1 with the RuntimeException from my previous comment included if you'd like to use it and reproduce the crash with it:

https://www.ryanliptak.com/misc/appleskin-fabric-mc1.19-2.4.1+issue235.jar

@NerjalNosk
Copy link
Author

Oh, sorry, I did that version change mostly to avoid getting confused between the builds. I can do that right away ofc

…ood components raises a NullPointerException, causing the client to crash.
@squeek502 squeek502 merged commit d8413a8 into squeek502:1.19-fabric May 4, 2023
@squeek502
Copy link
Owner

squeek502 commented May 4, 2023

Thanks! Ended up just going with the same implementation as the Forge branch.

@NerjalNosk
Copy link
Author

Fair enough

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

3 participants