Match legacy vouchers by display and lore
This commit is contained in:
@@ -171,19 +171,34 @@ public final class ItemCodec {
|
|||||||
String display = meta.getDisplayName();
|
String display = meta.getDisplayName();
|
||||||
if (display == null || display.isBlank()) return Optional.empty();
|
if (display == null || display.isBlank()) return Optional.empty();
|
||||||
String normalizedDisplay = strip(display);
|
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()) {
|
for (GemDefinition gem : registry.all()) {
|
||||||
if (gem.display() == null || gem.display().isBlank() || gem.material() != item.getType()) continue;
|
if (gem.display() == null || gem.display().isBlank() || gem.material() != item.getType()) continue;
|
||||||
if (!strip(gem.display()).equals(normalizedDisplay)) continue;
|
if (!strip(gem.display()).equals(normalizedDisplay)) continue;
|
||||||
plugin.getLogger().info("检测到缺少旧版标识的宝石物品,已按显示名称迁移:" + gem.name());
|
int score = 1;
|
||||||
meta.getPersistentDataContainer().set(gemNameKey, PersistentDataType.STRING, gem.name());
|
for (String tip : gem.tips()) {
|
||||||
meta.getPersistentDataContainer().set(dataVersionKey, PersistentDataType.INTEGER, 1);
|
String expected = strip(tip);
|
||||||
item.setItemMeta(meta);
|
if (!expected.isBlank() && itemLore.stream().anyMatch(expected::equals)) score++;
|
||||||
NBT.modify(item, nbt -> {
|
}
|
||||||
nbt.setString(LegacyItemMigrator.GEM_NAME_KEY, gem.name());
|
if (score > bestScore) {
|
||||||
});
|
best = gem;
|
||||||
return Optional.of(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) {
|
public Map<String, Integer> embedded(ItemStack item) {
|
||||||
|
|||||||
Reference in New Issue
Block a user