From 42978602ce52c9e40426fada59d33b001c95819e Mon Sep 17 00:00:00 2001 From: minoneer Date: Sat, 15 Aug 2026 15:30:32 +0200 Subject: [PATCH] Fix IndexOutOfBoundsException in AlternativeCommandsHandler.getAlternative getAlternative prunes collected WeakReferences through its own iterator, which can remove every entry in a bucket. The following commands.get(0) then throws IndexOutOfBoundsException. Store the first live alias during the iteration and return that instead, or null when nothing live remains - which the caller already treats as "no alternative". Fixes #6608 --- .../essentials/AlternativeCommandsHandler.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler.java b/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler.java index 01f7fa3caf0..34319041020 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler.java +++ b/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler.java @@ -116,6 +116,8 @@ public Command getAlternative(final String label) { } // return the first command that is not an alias + Command firstAlias = null; + final Iterator> iterator = commands.iterator(); while (iterator.hasNext()) { final Command cmd = iterator.next().get(); @@ -127,10 +129,14 @@ public Command getAlternative(final String label) { if (cmd.getName().equalsIgnoreCase(label)) { return cmd; } + + if (firstAlias == null) { + firstAlias = cmd; + } } - // return the first alias - return commands.get(0).get(); + // return the first alias, or null if every reference in this bucket has been collected + return firstAlias; } public void executed(final String label, final Command pc) {