From a59cbd37a431d36ce077e78d402d51a2cb6fa7cb Mon Sep 17 00:00:00 2001 From: isXander Date: Wed, 8 Feb 2023 21:29:52 +0000 Subject: fix ArrayIndexOutOfBoundsException when navigating to end of option list with keyboard/mouse navigation --- .../mixin/client/AbstractSelectionListMixin.java | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/client/java/dev/isxander/yacl/mixin/client/AbstractSelectionListMixin.java (limited to 'src/client/java') diff --git a/src/client/java/dev/isxander/yacl/mixin/client/AbstractSelectionListMixin.java b/src/client/java/dev/isxander/yacl/mixin/client/AbstractSelectionListMixin.java new file mode 100644 index 0000000..978fd16 --- /dev/null +++ b/src/client/java/dev/isxander/yacl/mixin/client/AbstractSelectionListMixin.java @@ -0,0 +1,26 @@ +package dev.isxander.yacl.mixin.client; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.components.AbstractSelectionList; +import org.objectweb.asm.Opcodes; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import java.util.List; + +@Mixin(AbstractSelectionList.class) +public abstract class AbstractSelectionListMixin> { + @Shadow public abstract List children(); + + /** + * Mojang use the field access of children to get max index to loop through keyboard navigation to find the next entry. + * YACL modifies these children() method to filter out hidden entries, so we need to redirect the field access to the + * method, so we don't get ArrayIndexOutOfBoundsException. + */ + @Redirect(method = "nextEntry(Lnet/minecraft/client/gui/navigation/ScreenDirection;Ljava/util/function/Predicate;Lnet/minecraft/client/gui/components/AbstractSelectionList$Entry;)Lnet/minecraft/client/gui/components/AbstractSelectionList$Entry;", at = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/components/AbstractSelectionList;children:Ljava/util/List;", opcode = Opcodes.GETFIELD)) + private List modifyChildrenCall(AbstractSelectionList instance) { + return children(); + } +} -- cgit