8 changed files with 144 additions and 61 deletions

No files matched your search

+1 -1
View File
@@ -32,7 +32,7 @@ mod_name=stopcheat
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=MIT License mod_license=MIT License
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=1.0.11 mod_version=1.1
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html
@@ -7,6 +7,7 @@ import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.event.CommandEvent; import net.neoforged.neoforge.event.CommandEvent;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -16,35 +17,42 @@ import java.util.Locale;
public class CommandInterceptor { public class CommandInterceptor {
public static final org.slf4j.Logger LOGGER = LogUtils.getLogger(); public static final org.slf4j.Logger LOGGER = LogUtils.getLogger();
private static final int arg_blackLISTCOMMANDNAME = 0; private static final int ARG_BLOCKLIST_COMMAND_NAME = 0;
private static final String CMDSEPARATOR = ":"; private static final String CMDSEPARATOR = ":";
private static final int arg_CHECKINDEX = 1; private static final int ARG_CHECKINDEX = 1;
private static final int arg_PARAMETERS = 2; private static final int ARG_PARAMETERS = 2;
// 从Mod配置获取黑名单列表 // 从配置获取黑名单命令和白名单玩家列表
private static List<List<String>> blackList = initblackList(new ArrayList<>(Config.BLACKLIST.get())); private static List<List<String>> blockList;
private static List<String> opUUID = Config.PASSUUID.get().stream() private static List<String> opUUID;
private static String cheatMsg;
public static void applyConfig() {
blockList = initBlockList(new ArrayList<>(Config.BLOCKLIST.get()));
opUUID = Config.PASSUUID.get().stream()
.map(s -> s.toLowerCase(Locale.ROOT)) .map(s -> s.toLowerCase(Locale.ROOT))
.toList(); .toList();
cheatMsg = I18n_.i18nDic.get("aza.stopcheat.broadcast.cheat");
}
public static List<List<String>> initblackList(List<String> commands){ public static List<List<String>> initBlockList(List<String> commands){
List<List<String>> blackList = new ArrayList<List<String>>(); List<List<String>> blockList = new ArrayList<List<String>>();
for (String command: commands) { for (String command: commands) {
List<String> black = new ArrayList<>(Arrays.asList(command.split(CMDSEPARATOR))); List<String> blockedCommand = new ArrayList<>(Arrays.asList(command.split(CMDSEPARATOR)));
if (black.isEmpty()){ if (blockedCommand.isEmpty()){
LOGGER.warn("{} 配置不可用, 长度错误", command); LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.lan"), command);
} else if (black.size() < 3){ } else if (blockedCommand.size() < 3){
for (int i = 0; black.size() + i < 3; i++){ for (int i = 0; blockedCommand.size() + i < 3; i++){
black.add("0"); blockedCommand.add("0");
} }
blackList.add(black); blockList.add(blockedCommand);
} else if (black.size() == 3){ } else if (blockedCommand.size() == 3){
blackList.add(black); blockList.add(blockedCommand);
} else { } else {
LOGGER.warn("{} 配置不可用, 长度错误", command); LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.lan"), command);
} }
} }
return blackList; return blockList;
} }
public static boolean checkPlayer(CommandSourceStack source){ public static boolean checkPlayer(CommandSourceStack source){
@@ -71,39 +79,40 @@ public class CommandInterceptor {
String raw = parseResults.getReader().getString(); String raw = parseResults.getReader().getString();
String[] parts = raw.trim().split("\\s+"); String[] parts = raw.trim().split("\\s+");
for (List<String> blackCommands: blackList){ for (List<String> blockedCommand: blockList){
if (parts[0].equals(blackCommands.get(arg_blackLISTCOMMANDNAME))){ // 输入的指令在黑名单中 if (parts[0].equals(blockedCommand.get(ARG_BLOCKLIST_COMMAND_NAME))){ // 输入的指令在黑名单中
int stopIndex; int stopIndex;
try { try {
stopIndex = Integer.parseInt(blackCommands.get(arg_CHECKINDEX)); // 将blackCommands[args_STOPINDEX] 转换为为Integer stopIndex = Integer.parseInt(blockedCommand.get(ARG_CHECKINDEX)); // blockedCommand[ARG_STOPINDEX] 转换为为Integer
}catch (NumberFormatException e){ }catch (NumberFormatException e){
LOGGER.warn("{} 配置不可用, args_STOPINDEX错误,重置为0", String.join(CMDSEPARATOR, blackCommands)); LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.stopindex"), String.join(CMDSEPARATOR, blockedCommand));
stopIndex = arg_blackLISTCOMMANDNAME; stopIndex = ARG_BLOCKLIST_COMMAND_NAME;
} }
if (stopIndex == 0){ // 禁止整个指令而不是特定参数的指令 if (stopIndex == 0){ // 禁止整个指令而不是特定参数的指令
event.setCanceled(true); event.setCanceled(true);
LOGGER.info("{} 使用 {} 命令被拦截", source.getTextName(), raw); LOGGER.info(I18n_.i18nDic.get("aza.stopcheat.log.intercept"), source.getTextName(), raw);
broadcast(source); broadcast(cheatMsg);
} }
else { else {
List<String> blackArgs = Arrays.asList(blackCommands.get(arg_PARAMETERS).split(",")); List<String> blockedArgs = Arrays.asList(blockedCommand.get(ARG_PARAMETERS).split(","));
if (blackArgs.contains(parts[stopIndex])){ // 禁止特定参数的指令,在配置中定义 if (blockedArgs .contains(parts[stopIndex])){ // 禁止特定参数的指令,在配置中定义
event.setCanceled(true); event.setCanceled(true);
LOGGER.info("{} 使用 {} 命令被拦截", source.getTextName(), raw); LOGGER.info(I18n_.i18nDic.get("aza.stopcheat.log.intercept"), source.getTextName(), raw);
broadcast(source); broadcast(cheatMsg);
} }
} }
} }
} }
} }
// 全服广播作弊 // 全服广播
private static void broadcast(CommandSourceStack source) { public static void broadcast(String msg) {
// 从Mod配置获取广播消息 var server = ServerLifecycleHooks.getCurrentServer();
String msg = Config.BROADCASTMSG.get();
Component component = Component.literal(msg); Component component = Component.literal(msg);
for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) { if (server != null) {
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
player.sendSystemMessage(component); player.sendSystemMessage(component);
} }
} }
} }
}
+16 -11
View File
@@ -1,6 +1,12 @@
package com.ataku.stopcheat; package com.ataku.stopcheat;
import java.util.List; import java.util.List;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.client.gui.ConfigurationScreen;
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
import net.neoforged.neoforge.common.ModConfigSpec; import net.neoforged.neoforge.common.ModConfigSpec;
// An example config class. This is not required, but it's a good idea to have one to keep your config organized. // An example config class. This is not required, but it's a good idea to have one to keep your config organized.
@@ -13,13 +19,12 @@ public class Config {
.defineInRange("version", 1, 0, Integer.MAX_VALUE); .defineInRange("version", 1, 0, Integer.MAX_VALUE);
public static final ModConfigSpec.ConfigValue<List<? extends String>> BLACKLIST = BUILDER public static final ModConfigSpec.ConfigValue<List<? extends String>> BLOCKLIST = BUILDER
.comment("禁用的指令列表") .comment("禁用的指令列表")
.comment("格式为{cmd}:{checkIndex}:{arg1[,arg2,...]}") .comment("格式为{cmd}:{checkIndex}:{arg1[,arg2,...]}")
.comment("格式为{指令}:{禁用第几个参数}:禁用的参数1[,禁用的参数2,...]}") .comment("格式为{指令}:{禁用第几个参数}:{禁用的参数1[,禁用的参数2,...]}")
.comment("其中{checkIndex}填0代表禁用整个指令") .comment("其中{checkIndex}填0代表禁用整个指令")
.defineList("blacked_commands", List.of("give:0:", "fill:0:", "gamemode:1:creative"), obj -> obj instanceof String); .defineList("blocked_commands", List.of("give:0:", "fill:0:", "gamemode:1:creative"), obj -> obj instanceof String);
public static final ModConfigSpec.ConfigValue<List<? extends String>> PASSUUID = BUILDER public static final ModConfigSpec.ConfigValue<List<? extends String>> PASSUUID = BUILDER
@@ -27,12 +32,12 @@ public class Config {
.defineList("pass_users", List.of("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), obj -> obj instanceof String); .defineList("pass_users", List.of("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), obj -> obj instanceof String);
public static final ModConfigSpec.ConfigValue<String> BROADCASTMSG = BUILDER
.comment("有人作弊时的广播内容")
.define("broadcastmsg", "§c不准作弊哦(>v0");
static final ModConfigSpec SPEC = BUILDER.build(); static final ModConfigSpec SPEC = BUILDER.build();
public static void configGUI(ModContainer container) {
container.registerExtensionPoint(
IConfigScreenFactory.class,
ConfigurationScreen::new
);
}
} }
@@ -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,5 +1,9 @@
package com.ataku.stopcheat; package com.ataku.stopcheat;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.fml.event.config.ModConfigEvent;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import org.slf4j.Logger; import org.slf4j.Logger;
import com.mojang.logging.LogUtils; import com.mojang.logging.LogUtils;
@@ -18,17 +22,25 @@ public class stopcheat {
public static final String MODID = "stopcheat"; public static final String MODID = "stopcheat";
// Directly reference a slf4j logger // Directly reference a slf4j logger
public static final Logger LOGGER = LogUtils.getLogger(); 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. // 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. // FML will recognize some parameter types like IEventBus or ModContainer and pass them in automatically.
public stopcheat(IEventBus modEventBus, ModContainer modContainer) { public stopcheat(IEventBus modEventBus, ModContainer modContainer) {
// 只在专用服务端执行
NeoForge.EVENT_BUS.register(CommandInterceptor.class); NeoForge.EVENT_BUS.register(CommandInterceptor.class);
// Register the commonSetup method for modloading // hello
modEventBus.addListener(this::commonSetup); modEventBus.addListener(this::commonSetup);
// Register our mod's ModConfigSpec so that FML can create and load the config file for us // 加载配置
modEventBus.addListener(this::confUpdate);
// L10n
NeoForge.EVENT_BUS.addListener(this::loadl10nandconfig);
// 注册配置文件
modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC); modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
// 游戏内配置界面
if (FMLEnvironment.dist == Dist.DEDICATED_SERVER){return;} //专用服务端不注册
else {Config.configGUI(modContainer);}
} }
private void commonSetup(FMLCommonSetupEvent event) { private void commonSetup(FMLCommonSetupEvent event) {
@@ -36,4 +48,23 @@ public class stopcheat {
LOGGER.info("HELLO FROM 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.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;
}
} }
@@ -1,13 +1,11 @@
{ {
"itemGroup.stopcheat": "Example Mod Tab", "aza.stopcheat.warn.config.lan": "{} configuration unavailable, length error",
"block.stopcheat.example_block": "Example Block", "aza.stopcheat.warn.config.stopindex": "{} configuration unavailable, ARG_STOPINDEX error, reset to 0",
"item.stopcheat.example_item": "Example Item", "aza.stopcheat.log.intercept": "{} used {} command and was intercepted",
"aza.stopcheat.broadcast.reload": "Configuration reloaded",
"aza.stopcheat.broadcast.cheat": "§cNo cheating allowed (>v0",
"stopcheat.configuration.title": "stopcheat Configs", "stopcheat.configuration.version": "Config Version",
"stopcheat.configuration.section.stopcheat.common.toml": "stopcheat Configs", "stopcheat.configuration.blocked_commands": "Blacklisted Commands",
"stopcheat.configuration.section.stopcheat.common.toml.title": "stopcheat Configs", "stopcheat.configuration.pass_users": "Bypass Users"
"stopcheat.configuration.items": "Item List",
"stopcheat.configuration.logDirtBlock": "Log Dirt Block",
"stopcheat.configuration.magicNumberIntroduction": "Magic Number Text",
"stopcheat.configuration.magicNumber": "Magic Number"
} }
@@ -0,0 +1,11 @@
{
"aza.stopcheat.warn.config.lan": "{} 配置不可用, 长度错误",
"aza.stopcheat.warn.config.stopindex": "{} 配置不可用, ARG_STOPINDEX错误,重置为0",
"aza.stopcheat.log.intercept": "{} 使用 {} 命令被拦截",
"aza.stopcheat.broadcast.reload": "配置重载完成",
"aza.stopcheat.broadcast.cheat": "§c不准作弊哦(>v0",
"stopcheat.configuration.version": "配置文件版本",
"stopcheat.configuration.blocked_commands": "禁用指令列表",
"stopcheat.configuration.pass_users": "白名单玩家"
}
@@ -46,7 +46,7 @@ authors="aza/xz"
# The description text for the mod (multi line!) (#mandatory) # The description text for the mod (multi line!) (#mandatory)
description=''' description='''
Example mod description. Command Permission Control.
''' '''
# The [[mixins]] block allows you to declare your mixin config to FML so that it gets loaded. # The [[mixins]] block allows you to declare your mixin config to FML so that it gets loaded.