Match legacy vouchers by display and lore
This commit is contained in:
@@ -171,19 +171,34 @@ public final class ItemCodec {
|
||||
String display = meta.getDisplayName();
|
||||
if (display == null || display.isBlank()) return Optional.empty();
|
||||
String normalizedDisplay = strip(display);
|
||||
List<String> itemLore = meta.getLore() == null ? List.of() : meta.getLore().stream().map(this::strip).toList();
|
||||
GemDefinition best = null;
|
||||
int bestScore = 0;
|
||||
for (GemDefinition gem : registry.all()) {
|
||||
if (gem.display() == null || gem.display().isBlank() || gem.material() != item.getType()) continue;
|
||||
if (!strip(gem.display()).equals(normalizedDisplay)) continue;
|
||||
plugin.getLogger().info("检测到缺少旧版标识的宝石物品,已按显示名称迁移:" + gem.name());
|
||||
meta.getPersistentDataContainer().set(gemNameKey, PersistentDataType.STRING, gem.name());
|
||||
meta.getPersistentDataContainer().set(dataVersionKey, PersistentDataType.INTEGER, 1);
|
||||
item.setItemMeta(meta);
|
||||
NBT.modify(item, nbt -> {
|
||||
nbt.setString(LegacyItemMigrator.GEM_NAME_KEY, gem.name());
|
||||
});
|
||||
return Optional.of(gem);
|
||||
int score = 1;
|
||||
for (String tip : gem.tips()) {
|
||||
String expected = strip(tip);
|
||||
if (!expected.isBlank() && itemLore.stream().anyMatch(expected::equals)) score++;
|
||||
}
|
||||
if (score > bestScore) {
|
||||
best = gem;
|
||||
bestScore = score;
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
if (best == null) return Optional.empty();
|
||||
// A display-only match is accepted for legacy items, while matching Lore
|
||||
// selects the correct denomination when several gems share one display.
|
||||
plugin.getLogger().info("检测到缺少旧版标识的宝石物品,已按显示名和 Lore 迁移:" + best.name());
|
||||
meta.getPersistentDataContainer().set(gemNameKey, PersistentDataType.STRING, best.name());
|
||||
meta.getPersistentDataContainer().set(dataVersionKey, PersistentDataType.INTEGER, 1);
|
||||
item.setItemMeta(meta);
|
||||
GemDefinition migrated = best;
|
||||
NBT.modify(item, nbt -> {
|
||||
nbt.setString(LegacyItemMigrator.GEM_NAME_KEY, migrated.name());
|
||||
});
|
||||
return Optional.of(best);
|
||||
}
|
||||
|
||||
public Map<String, Integer> embedded(ItemStack item) {
|
||||
|
||||
Reference in New Issue
Block a user