增加添加映射按钮,方便操作

This commit is contained in:
GaLicn 2025-08-16 10:24:16 +08:00
parent f636a984b8
commit f8fa9bac0e
3 changed files with 97 additions and 1 deletions

View File

@ -39,6 +39,8 @@ public class ProviderSelectScreen extends Screen {
// 搜索框 // 搜索框
private EditBox searchBox; private EditBox searchBox;
// 中文名输入框用于添加映射
private EditBox cnInput;
private String query = ""; private String query = "";
private boolean needsRefresh = false; private boolean needsRefresh = false;
@ -131,6 +133,22 @@ public class ProviderSelectScreen extends Screen {
.build(); .build();
this.addRenderableWidget(reload); this.addRenderableWidget(reload);
// 中文名输入框用于新增映射的值
if (cnInput == null) {
cnInput = new EditBox(this.font, centerX + 50, navY + 30, 120, 20, Component.translatable("extendedae_plus.screen.cn_name"));
} else {
cnInput.setX(centerX + 50);
cnInput.setY(navY + 30);
cnInput.setWidth(120);
}
this.addRenderableWidget(cnInput);
// 增加映射按钮使用当前搜索关键字 -> 中文
Button addMap = Button.builder(Component.translatable("extendedae_plus.screen.add_mapping"), b -> addMappingFromUI())
.bounds(centerX + 175, navY + 30, 60, 20)
.build();
this.addRenderableWidget(addMap);
// 关闭按钮 // 关闭按钮
Button close = Button.builder(Component.translatable("gui.cancel"), b -> onClose()) Button close = Button.builder(Component.translatable("gui.cancel"), b -> onClose())
.bounds(centerX - 40, navY + 30, 80, 20) .bounds(centerX - 40, navY + 30, 80, 20)
@ -298,10 +316,36 @@ public class ProviderSelectScreen extends Screen {
if (searchBox != null) { if (searchBox != null) {
searchBox.tick(); searchBox.tick();
} }
if (cnInput != null) {
cnInput.tick();
}
if (needsRefresh) { if (needsRefresh) {
needsRefresh = false; needsRefresh = false;
// 重新构建当前屏幕内容 // 重新构建当前屏幕内容
init(); init();
} }
} }
private void addMappingFromUI() {
String key = query == null ? "" : query.trim();
String val = cnInput == null ? "" : cnInput.getValue().trim();
var player = Minecraft.getInstance().player;
if (key.isEmpty()) {
if (player != null) player.sendSystemMessage(Component.literal("请输入搜索关键字后再添加映射"));
return;
}
if (val.isEmpty()) {
if (player != null) player.sendSystemMessage(Component.literal("请输入中文名称"));
return;
}
boolean ok = com.extendedae_plus.util.ExtendedAEPatternUploadUtil.addOrUpdateAliasMapping(key, val);
if (ok) {
if (player != null) player.sendSystemMessage(Component.literal("已添加/更新映射: " + key + " -> " + val));
// 更新本地过滤显示若名称包含中文可被搜索
applyFilter();
needsRefresh = true;
} else {
if (player != null) player.sendSystemMessage(Component.literal("写入映射失败"));
}
}
} }

View File

@ -131,6 +131,54 @@ public class ExtendedAEPatternUploadUtil {
lastProcessingName = name; lastProcessingName = name;
} }
/**
* 向配置中新增或更新别名 -> 中文映射并刷新内存映射
* 仅用于非原版或希望使用最终搜索关键字场景
*
* @param aliasKey 最终搜索关键字不含冒号大小写不敏感
* @param cnValue 中文名称
* @return 是否写入成功
*/
public static synchronized boolean addOrUpdateAliasMapping(String aliasKey, String cnValue) {
if (aliasKey == null || aliasKey.isBlank() || cnValue == null || cnValue.isBlank()) {
return false;
}
try {
Path cfgDir = FMLPaths.CONFIGDIR.get();
Path cfgPath = cfgDir.resolve(CONFIG_RELATIVE);
if (!Files.exists(cfgPath)) {
// 若文件不存在先创建模板
loadRecipeTypeNames();
}
JsonObject obj;
if (Files.exists(cfgPath)) {
String json = Files.readString(cfgPath);
obj = GSON.fromJson(json, JsonObject.class);
if (obj == null) obj = new JsonObject();
} else {
obj = new JsonObject();
}
String key = aliasKey.trim();
// 仅允许作为别名写入不含冒号如包含冒号仍按原样写入但推荐别名
obj.addProperty(key, cnValue);
Files.createDirectories(cfgPath.getParent());
Files.writeString(cfgPath, GSON.toJson(obj));
// 更新内存映射
if (key.contains(":")) {
try {
ResourceLocation rl = new ResourceLocation(key);
CUSTOM_NAMES.put(rl, cnValue);
} catch (Exception ignored) {}
} else {
CUSTOM_ALIASES.put(key.toLowerCase(), cnValue);
}
return true;
} catch (IOException e) {
return false;
}
}
public static String mapRecipeTypeToCn(Recipe<?> recipe) { public static String mapRecipeTypeToCn(Recipe<?> recipe) {
if (recipe == null) return null; if (recipe == null) return null;
RecipeType<?> type = recipe.getType(); RecipeType<?> type = recipe.getType();

View File

@ -10,5 +10,9 @@
"extendedae_plus.upload_to_matrix.success": "样板已上传到装配矩阵", "extendedae_plus.upload_to_matrix.success": "样板已上传到装配矩阵",
"extendedae_plus.upload_to_matrix.fail_not_crafting": "仅支持上传合成样板,处理样板将被忽略", "extendedae_plus.upload_to_matrix.fail_not_crafting": "仅支持上传合成样板,处理样板将被忽略",
"extendedae_plus.upload_to_matrix.fail_no_matrix": "未在当前网络中找到已成型的装配矩阵", "extendedae_plus.upload_to_matrix.fail_no_matrix": "未在当前网络中找到已成型的装配矩阵",
"extendedae_plus.upload_to_matrix.fail_full": "装配矩阵的样板仓已满或无法插入" "extendedae_plus.upload_to_matrix.fail_full": "装配矩阵的样板仓已满或无法插入",
"extendedae_plus.screen.reload_mapping": "重载映射",
"extendedae_plus.screen.add_mapping": "增加映射",
"extendedae_plus.screen.cn_name": "中文名",
"extendedae_plus.button.choose_provider":"上传样板"
} }