Skip to content

Commit 9896207

Browse files
committed
fix: resolve key exhaustion bug in board system causing IndexOutOfBoundsException
1 parent 3b1ee2f commit 9896207

File tree

1 file changed

+16
-17
lines changed
  • src/main/java/com/conaxgames/libraries/board

1 file changed

+16
-17
lines changed

src/main/java/com/conaxgames/libraries/board/Board.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private void init() {
4545
this.objective.setDisplayName(this.adapter.getTitle(player));
4646
}
4747

48-
// Pre-computed key pool for performance
48+
// Pre-computed key pool for performance with proper recycling
4949
private static final String[] AVAILABLE_KEYS;
5050
static {
5151
ChatColor[] colors = ChatColor.values();
@@ -55,23 +55,23 @@ private void init() {
5555
}
5656
}
5757

58-
private int keyIndex = 0;
59-
6058
public String getNewKey(BoardEntry entry) {
61-
// Use pre-computed keys for much better performance
62-
if (keyIndex >= AVAILABLE_KEYS.length) {
63-
throw new IndexOutOfBoundsException("No more keys available!");
64-
}
65-
66-
String colorText = AVAILABLE_KEYS[keyIndex++];
67-
68-
if (entry.getText().length() > 16) {
69-
String sub = entry.getText().substring(0, 16);
70-
colorText = colorText + ChatColor.getLastColors(sub);
59+
// Find first unused key from pre-computed pool
60+
for (String baseKey : AVAILABLE_KEYS) {
61+
String colorText = baseKey;
62+
63+
if (entry.getText().length() > 16) {
64+
String sub = entry.getText().substring(0, 16);
65+
colorText = colorText + ChatColor.getLastColors(sub);
66+
}
67+
68+
if (!keys.contains(colorText)) {
69+
keys.add(colorText);
70+
return colorText;
71+
}
7172
}
7273

73-
keys.add(colorText);
74-
return colorText;
74+
throw new IndexOutOfBoundsException("No more keys available!");
7575
}
7676

7777
public List<String> getBoardEntriesFormatted() {
@@ -166,7 +166,6 @@ public void clearAllEntries() {
166166
entry.remove();
167167
}
168168
entries.clear();
169-
keys.clear();
170-
keyIndex = 0; // Reset key index for reuse
169+
keys.clear(); // Keys will be recycled automatically
171170
}
172171
}

0 commit comments

Comments
 (0)