增加添加映射按钮,方便操作
This commit is contained in:
parent
f636a984b8
commit
f8fa9bac0e
|
|
@ -39,6 +39,8 @@ public class ProviderSelectScreen extends Screen {
|
|||
|
||||
// 搜索框
|
||||
private EditBox searchBox;
|
||||
// 中文名输入框(用于添加映射)
|
||||
private EditBox cnInput;
|
||||
private String query = "";
|
||||
private boolean needsRefresh = false;
|
||||
|
||||
|
|
@ -131,6 +133,22 @@ public class ProviderSelectScreen extends Screen {
|
|||
.build();
|
||||
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())
|
||||
.bounds(centerX - 40, navY + 30, 80, 20)
|
||||
|
|
@ -298,10 +316,36 @@ public class ProviderSelectScreen extends Screen {
|
|||
if (searchBox != null) {
|
||||
searchBox.tick();
|
||||
}
|
||||
if (cnInput != null) {
|
||||
cnInput.tick();
|
||||
}
|
||||
if (needsRefresh) {
|
||||
needsRefresh = false;
|
||||
// 重新构建当前屏幕内容
|
||||
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("写入映射失败"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,6 +131,54 @@ public class ExtendedAEPatternUploadUtil {
|
|||
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) {
|
||||
if (recipe == null) return null;
|
||||
RecipeType<?> type = recipe.getType();
|
||||
|
|
|
|||
|
|
@ -10,5 +10,9 @@
|
|||
"extendedae_plus.upload_to_matrix.success": "样板已上传到装配矩阵",
|
||||
"extendedae_plus.upload_to_matrix.fail_not_crafting": "仅支持上传合成样板,处理样板将被忽略",
|
||||
"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":"上传样板"
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user