Skip to content

Commit

Permalink
perf: Optimize ItemHandler lore soft matching [skip ci] (#2440)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofbolyai committed May 5, 2024
1 parent f216c38 commit 14de02f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions common/src/main/java/com/wynntils/handlers/item/ItemHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2022-2023.
* Copyright © Wynntils 2022-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.handlers.item;
Expand All @@ -23,10 +23,12 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
Expand Down Expand Up @@ -218,10 +220,16 @@ private boolean isWildcardItem(ItemStack itemStack) {
* It might have additional lines added, but these are not checked.
*/
private boolean isLoreSoftMatching(ItemStack firstItem, ItemStack secondItem) {
List<StyledText> firstLines = LoreUtils.getLore(firstItem);
List<StyledText> secondLines = LoreUtils.getLore(secondItem);
int firstLinesLen = firstLines.size();
int secondLinesLen = secondLines.size();
ListTag firstLoreTags = LoreUtils.getLoreTag(firstItem);
ListTag secondLoreTags = LoreUtils.getLoreTag(secondItem);

// Tags implement equals, so we can use this to check if the lore is identical
// This is the most common short-circuit case
if (Objects.equals(firstLoreTags, secondLoreTags)) return true;

// Continue, as we allow 3 lines to differ
int firstLinesLen = firstLoreTags.size();
int secondLinesLen = secondLoreTags.size();

// Only allow a maximum number of additional lines in the longer tooltip
if (Math.abs(firstLinesLen - secondLinesLen) > 3) return false;
Expand All @@ -231,8 +239,8 @@ private boolean isLoreSoftMatching(ItemStack firstItem, ItemStack secondItem) {
if (linesToCheck < 3 && firstLinesLen != secondLinesLen) return false;

for (int i = 0; i < linesToCheck; i++) {
StyledText firstLine = firstLines.get(i);
StyledText secondLine = secondLines.get(i);
StyledText firstLine = StyledText.fromJson(firstLoreTags.get(i).getAsString());
StyledText secondLine = StyledText.fromJson(secondLoreTags.get(i).getAsString());

if (!firstLine.equals(secondLine)) return false;
}
Expand Down

0 comments on commit 14de02f

Please sign in to comment.