mirror of
https://github.com/xzb23/Minecraft-Command-Permission-Control.git
synced 2026-11-22 00:34:36 +08:00
可以通过配置文件仅禁用特定命令的特定参数了
This commit is contained in:
2 files changed
+58
-27
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package com.ataku.stopcheat;
|
||||
|
||||
import com.mojang.brigadier.ParseResults;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
@@ -9,50 +10,76 @@ import net.neoforged.neoforge.common.NeoForge;
|
||||
import net.neoforged.neoforge.event.CommandEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class CommandInterceptor {
|
||||
public static final org.slf4j.Logger LOGGER = LogUtils.getLogger();
|
||||
|
||||
// 从Mod配置获取需要禁用的指令列表
|
||||
private static final List<String> BLOCKED_COMMANDS = new ArrayList<>(Config.BLOCKED_COMMANDS.get());
|
||||
private static final int arg_blackLISTCOMMANDNAME = 0;
|
||||
private static final String CMDSEPARATOR = ":";
|
||||
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.BLOCKLIST.get()));
|
||||
public static List<List<String>> initblackList(List<String> commands){
|
||||
List<List<String>> blackList = new ArrayList<List<String>>();
|
||||
for (String command: commands) {
|
||||
List<String> black = new ArrayList<>(Arrays.asList(command.split(CMDSEPARATOR)));
|
||||
if (black.size() < 1){
|
||||
LOGGER.warn("{} 配置不可用, 长度错误", command);
|
||||
} else if (black.size() < 3){
|
||||
for (int i = 0; black.size() + i < 3; i++){
|
||||
black.add("0");
|
||||
}
|
||||
blackList.add(black);
|
||||
} else if (black.size() == 3){
|
||||
blackList.add(black);
|
||||
} else {
|
||||
LOGGER.warn("{} 配置不可用, 长度错误", command);
|
||||
}
|
||||
}
|
||||
return blackList;
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onCommandExecution(CommandEvent event) {
|
||||
public static void checkCommand(CommandEvent event) {
|
||||
ParseResults<CommandSourceStack> parseResults = event.getParseResults();
|
||||
if (parseResults.getContext().getNodes().isEmpty()) return;
|
||||
|
||||
// 获取执行指令的玩家
|
||||
CommandSourceStack source = parseResults.getContext().getSource();
|
||||
// 获取玩家输入的指令名
|
||||
String commandName = parseResults.getContext().getNodes()
|
||||
.getFirst().getNode().getName()
|
||||
.toLowerCase(Locale.ROOT);
|
||||
|
||||
// 获取玩家输入的指令
|
||||
String raw = parseResults.getReader().getString();
|
||||
String[] parts = raw.trim().split("\\s+");
|
||||
|
||||
// 对 /gamemode 进行单独处理
|
||||
if ("gamemode".equals(commandName)) {
|
||||
String[] parts = raw.substring(1).trim().split("\\s+");
|
||||
|
||||
if (parts.length >= 2) {
|
||||
String modeArg = parts[1].toLowerCase(Locale.ROOT);
|
||||
|
||||
// 仅拦截creative
|
||||
if (List.of("creative", "1").contains(modeArg)) {
|
||||
for (List<String> blackCommands: blackList){
|
||||
if (parts[0].equals(blackCommands.get(arg_blackLISTCOMMANDNAME))){ // 输入的指令在黑名单中
|
||||
int stopIndex;
|
||||
try {
|
||||
stopIndex = Integer.parseInt(blackCommands.get(arg_CHECKINDEX)); // 将blackCommands[args_STOPINDEX] 转换为为Integer
|
||||
}catch (NumberFormatException e){
|
||||
LOGGER.warn("{} 配置不可用, args_STOPINDEX错误,重置为0", String.join(CMDSEPARATOR, blackCommands));
|
||||
stopIndex = arg_blackLISTCOMMANDNAME;
|
||||
}
|
||||
if (stopIndex == 0){ // 禁止整个指令而不是特定参数的指令
|
||||
event.setCanceled(true);
|
||||
LOGGER.info("{} 使用 {} 命令被拦截", source.getTextName(), raw);
|
||||
broadcast(source);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
List<String> blackArgs = Arrays.asList(blackCommands.get(arg_PARAMETERS).split(","));
|
||||
if (blackArgs.contains(parts[stopIndex])){ // 禁止特定参数的指令,在配置中定义
|
||||
event.setCanceled(true);
|
||||
LOGGER.info("{} 使用 {} 命令被拦截", source.getTextName(), raw);
|
||||
broadcast(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 其他模式(survival/adventure/spectator)或无参数,直接放行
|
||||
return;
|
||||
}
|
||||
|
||||
// 拦截列表中的指令
|
||||
if (BLOCKED_COMMANDS.contains(commandName)) {
|
||||
event.setCanceled(true);
|
||||
broadcast(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,9 +22,13 @@ public class Config {
|
||||
.defineInRange("version", 1, 0, Integer.MAX_VALUE);
|
||||
|
||||
|
||||
public static final ModConfigSpec.ConfigValue<List<? extends String>> BLOCKED_COMMANDS = BUILDER
|
||||
public static final ModConfigSpec.ConfigValue<List<? extends String>> BLOCKLIST = BUILDER
|
||||
.comment("禁用的指令列表")
|
||||
.defineList("blocked_commands", List.of("give", "fill"), obj -> obj instanceof String);
|
||||
.comment("格式为{cmd/指令}:{checkIndex/禁用第几个参数}:{arg1[,arg2,...]/禁用的参数1[,禁用的参数2,...]}")
|
||||
.comment("其中{checkIndex}填0代表禁用整个指令")
|
||||
.defineList("blocked_commands", List.of("give:0:", "fill:0:", "gamemode:1:creative"), obj -> obj instanceof String);
|
||||
|
||||
|
||||
|
||||
|
||||
public static final ModConfigSpec.ConfigValue<String> BROADCASTMSG = BUILDER
|
||||
|
||||
Reference in new issue
Block a user