Skip to content

Fix OutOfBoundsException in Potion/TippedArrowItemMixin #38

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

Merged
merged 2 commits into from
Dec 14, 2021

Conversation

Fourmisain
Copy link
Contributor

I ran into this while messing around with tooltips, adding a second/sub name to items.
Looking at PotionItemMixin (which is basically the exact same as TippedArrowItemMixin)

if (tooltip.size() > this.inspecio$oldTooltipLength.get() && Inspecio.get().getConfig().getEffectsConfig().hasPotions()) {
while (tooltip.size() > 1) {
if (tooltip.get(this.inspecio$oldTooltipLength.get()) == LiteralText.EMPTY) {
tooltip.remove(this.inspecio$oldTooltipLength.get().intValue());
break;
}
tooltip.remove(this.inspecio$oldTooltipLength.get().intValue());
}
}

If e.g. tooltip.size() is 3 and oldTooltipLength is 2, it'll enter the loop, remove the tooltip at index 2 and continue the loop since tooltip.size() > 1, it'll call get at the same index which fails since index 2 is now out of bounds.
Fix is to simply change the 1 to oldTooltipLength.

I rewrote the offending code but didn't quite finish it because I have a question:
Why does this removal loop break if a LiteralText.EMPTY is found?
Is this something for mod compat? Because this doesn't seem to happen in Vanilla at all and it seems much easier to e.g. redirect PotionUtil.buildTooltip() to do nothing.

@LambdAurora
Copy link
Collaborator

I honestly don't remember why the LiteralText.EMPTY, but I think in some cases there could be a linebreak then stuff that should not be removed...

And the reason for not using a Redirect is mod compatibility, multiple mods cannot redirect the same invoke.

@Fourmisain
Copy link
Contributor Author

I honestly don't remember why the LiteralText.EMPTY, but I think in some cases there could be a linebreak then stuff that should not be removed...

Alright, kept the logic in, just in case!

I rewrote it again, it's slightly less readable but O(n) and about 30% faster in common cases - not that it really matters much.

@Fourmisain Fourmisain marked this pull request as ready for review October 28, 2021 21:14
@LambdAurora LambdAurora merged commit d429ae2 into Queerbric:1.17 Dec 14, 2021
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.

2 participants