支持I18n/L10n

This commit is contained in:
xzb23 committed 2026-08-25 20:30:33 +08:00
1 parent 18de96c986
commit 19f292494c
6 files changed
+65 -24

No files matched your search

@@ -32,9 +32,7 @@ public class CommandInterceptor {
opUUID = Config.PASSUUID.get().stream()
.map(s -> s.toLowerCase(Locale.ROOT))
.toList();
cheatMsg = Config.CHEATBROADCASTMSG.get();
broadcast(String.format("§c%s§r %s", stopcheat.MODID, Config.RELOADBROADCASTMSG.get()));
cheatMsg = I18n_.i18nDic.get("aza.stopcheat.broadcast.cheat");
}
public static List<List<String>> initblackList(List<String> commands){
@@ -42,7 +40,7 @@ public class CommandInterceptor {
for (String command: commands) {
List<String> black = new ArrayList<>(Arrays.asList(command.split(CMDSEPARATOR)));
if (black.isEmpty()){
LOGGER.warn("{} configuration unavailable, length error", command);
LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.lan"), command);
} else if (black.size() < 3){
for (int i = 0; black.size() + i < 3; i++){
black.add("0");
@@ -51,7 +49,7 @@ public class CommandInterceptor {
} else if (black.size() == 3){
blackList.add(black);
} else {
LOGGER.warn("{} configuration unavailable, length error", command);
LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.lan"), command);
}
}
return blackList;
@@ -87,19 +85,19 @@ public class CommandInterceptor {
try {
stopIndex = Integer.parseInt(blackCommands.get(arg_CHECKINDEX)); // 将blackCommands[args_STOPINDEX] 转换为为Integer
}catch (NumberFormatException e){
LOGGER.warn("{} configuration unavailable, args_STOPINDEX error, reset to 0", String.join(CMDSEPARATOR, blackCommands));
LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.stopindex"), String.join(CMDSEPARATOR, blackCommands));
stopIndex = arg_blackLISTCOMMANDNAME;
}
if (stopIndex == 0){ // 禁止整个指令而不是特定参数的指令
event.setCanceled(true);
LOGGER.info("{} used {} command and was intercepted", source.getTextName(), raw);
LOGGER.info(I18n_.i18nDic.get("aza.stopcheat.log.intercept"), source.getTextName(), raw);
broadcast(cheatMsg);
}
else {
List<String> blackArgs = Arrays.asList(blackCommands.get(arg_PARAMETERS).split(","));
if (blackArgs.contains(parts[stopIndex])){ // 禁止特定参数的指令,在配置中定义
event.setCanceled(true);
LOGGER.info("{} used {} command and was intercepted", source.getTextName(), raw);
LOGGER.info(I18n_.i18nDic.get("aza.stopcheat.log.intercept"), source.getTextName(), raw);
broadcast(cheatMsg);
}
}
@@ -108,7 +106,7 @@ public class CommandInterceptor {
}
// 全服广播
private static void broadcast(String msg) {
public static void broadcast(String msg) {
var server = ServerLifecycleHooks.getCurrentServer();
Component component = Component.literal(msg);
if (server != null) {
@@ -21,21 +21,10 @@ public class Config {
.defineList("blacked_commands", List.of("give:0:", "fill:0:", "gamemode:1:creative"), obj -> obj instanceof String);
public static final ModConfigSpec.ConfigValue<List<? extends String>> PASSUUID = BUILDER
.comment("不受约束的玩家UUID列表")
.defineList("pass_users", List.of("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), obj -> obj instanceof String);
public static final ModConfigSpec.ConfigValue<String> CHEATBROADCASTMSG = BUILDER
.comment("有人作弊时的广播内容")
.define("cheatmsg", "§c不准作弊哦(>v0");
public static final ModConfigSpec.ConfigValue<String> RELOADBROADCASTMSG = BUILDER
.comment("重载配置时的广播内容")
.define("reloadmsg", "配置重载完成");
static final ModConfigSpec SPEC = BUILDER.build();
}
@@ -0,0 +1,29 @@
package com.ataku.stopcheat;
import net.minecraft.client.resources.language.I18n;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.loading.FMLEnvironment;
import java.util.HashMap;
import java.util.Map;
public class I18n_ {
public static Map<String, String> i18nDic = new HashMap<>();
public static void init_(){
if (FMLEnvironment.dist == Dist.DEDICATED_SERVER) {
i18nDic.put("aza.stopcheat.warn.config.lan", "{} configuration unavailable, length error");
i18nDic.put("aza.stopcheat.warn.config.stopindex", "{} configuration unavailable, args_STOPINDEX error, reset to 0");
i18nDic.put("aza.stopcheat.log.intercept", "{} used {} command and was intercepted");
i18nDic.put("aza.stopcheat.broadcast.cheat", "§cNo cheating allowed (>v0");
i18nDic.put("aza.stopcheat.broadcast.reload", "Configuration reloaded");
}else {
i18nDic.put("aza.stopcheat.warn.config.lan", I18n.get("aza.stopcheat.warn.config.lan"));
i18nDic.put("aza.stopcheat.warn.config.stopindex", I18n.get("aza.stopcheat.warn.config.stopindex"));
i18nDic.put("aza.stopcheat.log.intercept", I18n.get("aza.stopcheat.log.intercept"));
i18nDic.put("aza.stopcheat.broadcast.cheat", I18n.get("aza.stopcheat.broadcast.cheat"));
i18nDic.put("aza.stopcheat.broadcast.reload", I18n.get("aza.stopcheat.broadcast.reload"));
}
}
}
@@ -1,6 +1,7 @@
package com.ataku.stopcheat;
import net.neoforged.fml.event.config.ModConfigEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import org.slf4j.Logger;
import com.mojang.logging.LogUtils;
@@ -19,18 +20,20 @@ public class stopcheat {
public static final String MODID = "stopcheat";
// Directly reference a slf4j logger
public static final Logger LOGGER = LogUtils.getLogger();
private static boolean firstFlag = true;
// The constructor for the mod class is the first code that is run when your mod is loaded.
// FML will recognize some parameter types like IEventBus or ModContainer and pass them in automatically.
public stopcheat(IEventBus modEventBus, ModContainer modContainer) {
// 只在专用服务端执行
NeoForge.EVENT_BUS.register(CommandInterceptor.class);
// Register the commonSetup method for modloading
// hello
modEventBus.addListener(this::commonSetup);
// 加载配置
modEventBus.addListener(this::confUpdate);
// Register our mod's ModConfigSpec so that FML can create and load the config file for us
// L10n
NeoForge.EVENT_BUS.addListener(this::loadl10nandconfig);
// 注册配置文件
modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
}
@@ -39,13 +42,21 @@ public class stopcheat {
LOGGER.info("HELLO FROM STOPCHEAT");
}
private void loadl10nandconfig(PlayerEvent.PlayerLoggedInEvent event) {
if (!firstFlag){return;} //确保只加载一次
I18n_.init_();
CommandInterceptor.applyConfig();
firstFlag = false;
}
private int confUpdate(ModConfigEvent event){
if (event.getConfig().getSpec() != Config.SPEC){
return 1; // 其它Mod Config
}
if (event instanceof ModConfigEvent.Loading || event instanceof ModConfigEvent.Reloading){
if (event instanceof ModConfigEvent.Reloading){
LOGGER.info("Loading Config File");
CommandInterceptor.applyConfig();
CommandInterceptor.broadcast(String.format("§c%s§r %s", stopcheat.MODID, I18n_.i18nDic.get("aza.stopcheat.broadcast.reload")));
}
return 0;
}
@@ -0,0 +1,7 @@
{
"aza.stopcheat.warn.config.lan": "{} configuration unavailable, length error",
"aza.stopcheat.warn.config.stopindex": "{} configuration unavailable, args_STOPINDEX error, reset to 0",
"aza.stopcheat.log.intercept": "{} used {} command and was intercepted",
"aza.stopcheat.broadcast.reload": "Configuration reloaded",
"aza.stopcheat.broadcast.cheat": "§cNo cheating allowed (>v0"
}
@@ -0,0 +1,7 @@
{
"aza.stopcheat.warn.config.lan": "{} 配置不可用, 长度错误",
"aza.stopcheat.warn.config.stopindex": "{} 配置不可用, args_STOPINDEX错误,重置为0",
"aza.stopcheat.log.intercept": "{} 使用 {} 命令被拦截",
"aza.stopcheat.broadcast.reload": "配置重载完成",
"aza.stopcheat.broadcast.cheat": "§c不准作弊哦(>v0"
}