支持热重载配置文件 and 修改游戏内广播部分代码

This commit is contained in:
xzb23 committed 2026-08-25 16:53:31 +08:00
1 parent aae3f57ad4
commit 18de96c986
3 files changed
+43 -15

No files matched your search

@@ -7,6 +7,7 @@ import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.event.CommandEvent;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import java.util.ArrayList;
import java.util.Arrays;
@@ -21,11 +22,20 @@ public class CommandInterceptor {
private static final int arg_CHECKINDEX = 1;
private static final int arg_PARAMETERS = 2;
// 从Mod配置获取黑名单列表
private static List<List<String>> blackList = initblackList(new ArrayList<>(Config.BLACKLIST.get()));
private static List<String> opUUID = Config.PASSUUID.get().stream()
.map(s -> s.toLowerCase(Locale.ROOT))
.toList();
// 从配置获取黑名单命令和白名单玩家列表
private static List<List<String>> blackList;
private static List<String> opUUID;
private static String cheatMsg;
public static void applyConfig() {
blackList = initblackList(new ArrayList<>(Config.BLACKLIST.get()));
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()));
}
public static List<List<String>> initblackList(List<String> commands){
List<List<String>> blackList = new ArrayList<List<String>>();
@@ -83,27 +93,28 @@ public class CommandInterceptor {
if (stopIndex == 0){ // 禁止整个指令而不是特定参数的指令
event.setCanceled(true);
LOGGER.info("{} used {} command and was intercepted", source.getTextName(), raw);
broadcast(source);
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);
broadcast(source);
broadcast(cheatMsg);
}
}
}
}
}
// 全服广播作弊
private static void broadcast(CommandSourceStack source) {
// 从Mod配置获取广播消息
String msg = Config.BROADCASTMSG.get();
// 全服广播
private static void broadcast(String msg) {
var server = ServerLifecycleHooks.getCurrentServer();
Component component = Component.literal(msg);
for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) {
player.sendSystemMessage(component);
if (server != null) {
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
player.sendSystemMessage(component);
}
}
}
}
@@ -27,11 +27,14 @@ public class Config {
.defineList("pass_users", List.of("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), obj -> obj instanceof String);
public static final ModConfigSpec.ConfigValue<String> BROADCASTMSG = BUILDER
public static final ModConfigSpec.ConfigValue<String> CHEATBROADCASTMSG = BUILDER
.comment("有人作弊时的广播内容")
.define("broadcastmsg", "§c不准作弊哦(>v0");
.define("cheatmsg", "§c不准作弊哦(>v0");
public static final ModConfigSpec.ConfigValue<String> RELOADBROADCASTMSG = BUILDER
.comment("重载配置时的广播内容")
.define("reloadmsg", "配置重载完成");
static final ModConfigSpec SPEC = BUILDER.build();
@@ -1,5 +1,6 @@
package com.ataku.stopcheat;
import net.neoforged.fml.event.config.ModConfigEvent;
import org.slf4j.Logger;
import com.mojang.logging.LogUtils;
@@ -27,6 +28,8 @@ public class stopcheat {
// Register the commonSetup method for modloading
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
modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
}
@@ -36,4 +39,15 @@ public class stopcheat {
LOGGER.info("HELLO FROM STOPCHEAT");
}
private int confUpdate(ModConfigEvent event){
if (event.getConfig().getSpec() != Config.SPEC){
return 1; // 其它Mod Config
}
if (event instanceof ModConfigEvent.Loading || event instanceof ModConfigEvent.Reloading){
LOGGER.info("Loading Config File");
CommandInterceptor.applyConfig();
}
return 0;
}
}