Skip to content

Commit 2db6650

Browse files
committed
fix(EnchantManager): add string fallback for tooltip cache validation
1 parent 7e88275 commit 2db6650

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/main/java/com/fix3dll/skyblockaddons/features/enchants/EnchantManager.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ static class Cache {
394394
static final int MISSING_ENCHANTS_PER_LINE = 3;
395395

396396
private ArrayList<Component> cachedBefore = new ArrayList<>();
397+
private ArrayList<String> cachedBeforeStrings = new ArrayList<>();
397398
@Getter ArrayList<Component> cachedAfter = new ArrayList<>();
398399
boolean configChanged = false;
399400
Set<FormattedEnchant> formattedEnchants = Collections.emptySet();
@@ -420,6 +421,10 @@ public Cache() {
420421
*/
421422
public void updateBefore(List<Component> loreBeforeModifications, ItemStack itemStack) {
422423
cachedBefore = new ArrayList<>(loreBeforeModifications);
424+
this.cachedBeforeStrings = new ArrayList<>(loreBeforeModifications.size());
425+
for (Component line : loreBeforeModifications) {
426+
this.cachedBeforeStrings.add(line.getString());
427+
}
423428
this.itemStack = itemStack;
424429
formattedEnchants = Collections.emptySet();
425430
missingEnchants = null;
@@ -575,9 +580,9 @@ public List<Component> getMissingEnchantsComponent() {
575580

576581
/**
577582
* Returns {@code true} if the cache entry is valid for the given lore list and item.
578-
* Checks {@link ItemStack} identity, lore size, and element identity via {@code !=}, since
579-
* component equality is reference-based. The element check catches the edge case where
580-
* another mod replaces a component at an existing index without changing list size.
583+
* <p>
584+
* Validates the item identity, lore size, and line contents. It uses a fast reference check
585+
* followed by a pre-calculated string comparison to maintain performance and handle edge cases.
581586
* @param loreList the unmodified lore list passed to {@link EnchantManager#parseEnchants}
582587
* @param itemStack the item being rendered
583588
* @return {@code true} if the stored post-parse result can be reused, {@code false} otherwise
@@ -587,7 +592,12 @@ public boolean isCached(List<Component> loreList, ItemStack itemStack) {
587592
return false;
588593
}
589594
for (int i = 0; i < loreList.size(); i++) {
590-
if (loreList.get(i) != cachedBefore.get(i)) {
595+
Component currentLine = loreList.get(i);
596+
Component cachedLine = cachedBefore.get(i);
597+
if (currentLine == cachedLine) {
598+
continue;
599+
}
600+
if (!loreList.get(i).getString().equals(cachedBeforeStrings.get(i))) {
591601
return false;
592602
}
593603
}

0 commit comments

Comments
 (0)