This commit is contained in:
GaLicn 2025-08-16 10:10:36 +08:00
parent 411a8b9c1f
commit f636a984b8

View File

@ -51,6 +51,8 @@ public class ExtendedAEPatternUploadUtil {
// --------------------------- 配置RecipeType 中文名称映射 --------------------------- // --------------------------- 配置RecipeType 中文名称映射 ---------------------------
private static final String CONFIG_RELATIVE = "extendedae_plus/recipe_type_names.json"; private static final String CONFIG_RELATIVE = "extendedae_plus/recipe_type_names.json";
private static final Map<ResourceLocation, String> CUSTOM_NAMES = new ConcurrentHashMap<>(); private static final Map<ResourceLocation, String> CUSTOM_NAMES = new ConcurrentHashMap<>();
// 允许使用最终搜索关键字通常为 path 或自定义短语作为键例如"assembler": "组装机"
private static final Map<String, String> CUSTOM_ALIASES = new ConcurrentHashMap<>();
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
static { static {
@ -63,6 +65,10 @@ public class ExtendedAEPatternUploadUtil {
/** /**
* 从配置文件加载 RecipeType 中文名称映射文件不存在则生成模板 * 从配置文件加载 RecipeType 中文名称映射文件不存在则生成模板
* 同时支持别名形式不含冒号的键会被视为最终搜索关键字大小写不敏感
* {
* "assembler": "组装机"
* }
*/ */
public static synchronized void loadRecipeTypeNames() { public static synchronized void loadRecipeTypeNames() {
try { try {
@ -81,29 +87,39 @@ public class ExtendedAEPatternUploadUtil {
tmpl.addProperty("gtceu:assembler", "组装机"); tmpl.addProperty("gtceu:assembler", "组装机");
tmpl.addProperty("gtceu:arc_furnace", "电弧炉"); tmpl.addProperty("gtceu:arc_furnace", "电弧炉");
tmpl.addProperty("gtceu:chemical_reactor", "化学反应器"); tmpl.addProperty("gtceu:chemical_reactor", "化学反应器");
// 也支持别名最终搜索关键字形式例如
tmpl.addProperty("assembler", "组装机");
Files.writeString(cfgPath, GSON.toJson(tmpl)); Files.writeString(cfgPath, GSON.toJson(tmpl));
} }
String json = Files.readString(cfgPath); String json = Files.readString(cfgPath);
JsonObject obj = GSON.fromJson(json, JsonObject.class); JsonObject obj = GSON.fromJson(json, JsonObject.class);
Map<ResourceLocation, String> map = new HashMap<>(); Map<ResourceLocation, String> map = new HashMap<>();
Map<String, String> alias = new HashMap<>();
if (obj != null) { if (obj != null) {
for (Map.Entry<String, JsonElement> e : obj.entrySet()) { for (Map.Entry<String, JsonElement> e : obj.entrySet()) {
String k = e.getKey(); String k = e.getKey();
JsonElement v = e.getValue(); JsonElement v = e.getValue();
if (v != null && v.isJsonPrimitive()) { if (v != null && v.isJsonPrimitive()) {
try { String name = v.getAsString();
ResourceLocation rl = new ResourceLocation(k); if (name == null || name.isBlank()) continue;
String name = v.getAsString(); if (k.contains(":")) {
if (name != null && !name.isBlank()) { // 形如 namespace:path
try {
ResourceLocation rl = new ResourceLocation(k);
map.put(rl, name); map.put(rl, name);
} } catch (Exception ignored) {}
} catch (Exception ignored) {} } else {
// 视为别名最终搜索关键字大小写不敏感
alias.put(k.toLowerCase(), name);
}
} }
} }
} }
CUSTOM_NAMES.clear(); CUSTOM_NAMES.clear();
CUSTOM_NAMES.putAll(map); CUSTOM_NAMES.putAll(map);
CUSTOM_ALIASES.clear();
CUSTOM_ALIASES.putAll(alias);
} catch (IOException ignored) { } catch (IOException ignored) {
} }
} }
@ -153,6 +169,10 @@ public class ExtendedAEPatternUploadUtil {
RecipeType<?> type = recipe.getType(); RecipeType<?> type = recipe.getType();
ResourceLocation key = BuiltInRegistries.RECIPE_TYPE.getKey(type); ResourceLocation key = BuiltInRegistries.RECIPE_TYPE.getKey(type);
if (key == null) return null; if (key == null) return null;
// 先查别名 path 匹配
String alias = CUSTOM_ALIASES.get(key.getPath().toLowerCase());
if (alias != null && !alias.isBlank()) return alias;
// 再查完整ID映射
String custom = CUSTOM_NAMES.get(key); String custom = CUSTOM_NAMES.get(key);
if (custom != null && !custom.isBlank()) { if (custom != null && !custom.isBlank()) {
return custom; return custom;
@ -171,11 +191,16 @@ public class ExtendedAEPatternUploadUtil {
String idStr = String.valueOf(gtRecipe.getType()); String idStr = String.valueOf(gtRecipe.getType());
if (idStr == null || idStr.isBlank()) return null; if (idStr == null || idStr.isBlank()) return null;
ResourceLocation rl = new ResourceLocation(idStr); ResourceLocation rl = new ResourceLocation(idStr);
// 1) 配置优先 // 1) 先查别名使用 path 作为最终搜索关键字
String path = rl.getPath();
if (path != null) {
String alias = CUSTOM_ALIASES.get(path.toLowerCase());
if (alias != null && !alias.isBlank()) return alias;
}
// 2) 再查完整ID映射
String custom = CUSTOM_NAMES.get(rl); String custom = CUSTOM_NAMES.get(rl);
if (custom != null && !custom.isBlank()) return custom; if (custom != null && !custom.isBlank()) return custom;
// 2) 返回 path 作为搜索关键字 // 3) 默认返回 path 作为搜索关键字
String path = rl.getPath();
return (path != null && !path.isBlank()) ? path : idStr; return (path != null && !path.isBlank()) ? path : idStr;
} catch (Throwable t) { } catch (Throwable t) {
return null; return null;
@ -202,8 +227,13 @@ public class ExtendedAEPatternUploadUtil {
else if (lower.contains("immersiveengineering")) ns = "immersive"; else if (lower.contains("immersiveengineering")) ns = "immersive";
String token = toSearchToken(simple); String token = toSearchToken(simple);
if (ns != null && token != null && !token.isBlank()) return ns + " " + token; String key;
return token != null && !token.isBlank() ? token : ns; if (ns != null && token != null && !token.isBlank()) key = ns + " " + token;
else key = token != null && !token.isBlank() ? token : ns;
if (key == null || key.isBlank()) return null;
// 尝试别名映射大小写不敏感
String alias = CUSTOM_ALIASES.get(key.toLowerCase());
return (alias != null && !alias.isBlank()) ? alias : key;
} catch (Throwable ignored) { } catch (Throwable ignored) {
return null; return null;
} }