Skip to content

Commit 9bc76cf

Browse files
JobsonMarinhoclaude
andcommitted
fix: handle legacy lore lines without §§ prefix in smartSetLore
Items leveled by older LevelTools versions don't have the §§ lore prefix, causing duplicate lore entries when the new version appends instead of replacing. Adds fallback detection by progress bar pattern and level label. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f378bf9 commit 9bc76cf

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

src/main/java/me/byteful/plugin/leveltools/util/LevelToolsUtil.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.md_5.bungee.api.ChatMessageType;
2020
import net.md_5.bungee.api.chat.TextComponent;
2121
import org.bukkit.Bukkit;
22+
import org.bukkit.ChatColor;
2223
import org.bukkit.Material;
2324
import org.bukkit.enchantments.Enchantment;
2425
import org.bukkit.entity.Player;
@@ -261,9 +262,17 @@ private static void smartSetLore(@NotNull ItemMeta meta, @NotNull List<String> t
261262
return;
262263
}
263264

264-
final int[] bounds = findPrefixBounds(lore);
265-
final int start = bounds[0];
266-
final int end = bounds[1];
265+
int[] bounds = findPrefixBounds(lore);
266+
int start = bounds[0];
267+
int end = bounds[1];
268+
269+
// Fallback: detect old LevelTools lore lines (without §§ prefix) by progress bar pattern
270+
if (start == -1) {
271+
bounds = findLegacyBounds(lore);
272+
start = bounds[0];
273+
end = bounds[1];
274+
}
275+
267276
if (start == -1) {
268277
lore.addAll(toAdd);
269278
meta.setLore(lore);
@@ -298,6 +307,36 @@ private static int[] findPrefixBounds(@NotNull List<String> lore) {
298307
return arr;
299308
}
300309

310+
private static int[] findLegacyBounds(@NotNull List<String> lore) {
311+
final int[] arr = new int[]{-1, -1};
312+
for (int i = 0; i < lore.size(); i++) {
313+
final String stripped = ChatColor.stripColor(colorize(lore.get(i)));
314+
if (stripped != null && stripped.matches("^\\[\\|+.*\\].*$")) {
315+
// Found a progress bar line from old LevelTools format
316+
// Walk backwards to find the start (skip empty lines and level label)
317+
int scanStart = i;
318+
for (int j = i - 1; j >= 0; j--) {
319+
String prev = ChatColor.stripColor(colorize(lore.get(j)));
320+
if (prev == null || prev.trim().isEmpty()) {
321+
scanStart = j;
322+
continue;
323+
}
324+
if (prev.matches(".*(?:Level|Nível|Nivel).*:\\s*\\d+.*")) {
325+
scanStart = j;
326+
continue;
327+
}
328+
break;
329+
}
330+
if (arr[0] == -1 || scanStart < arr[0]) {
331+
arr[0] = scanStart;
332+
}
333+
arr[1] = i;
334+
}
335+
}
336+
337+
return arr;
338+
}
339+
301340
public static void handleReward(LevelToolsItem tool, Player player) {
302341
Material material = tool.getItemStack().getType();
303342
ItemProfile itemProfile = getItemProfile(material);

0 commit comments

Comments
 (0)