mirror of
https://github.com/xzb23/Minecraft-Command-Permission-Control.git
synced 2026-11-22 00:34:36 +08:00
允许修改游戏模式到非创造模式 and 修改了提示文本
This commit is contained in:
1 file changed
+45
-21
@@ -3,43 +3,67 @@ package com.ataku.stopcheat;
|
||||
import com.mojang.brigadier.ParseResults;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.common.NeoForge;
|
||||
import net.neoforged.neoforge.event.CommandEvent;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class CommandInterceptor {
|
||||
|
||||
// 要禁用的命令列表(不含斜杠)
|
||||
// 这里只放完全禁用的指令,gamemode 单独处理
|
||||
private static final List<String> BLOCKED_COMMANDS = List.of(
|
||||
"give", // 禁用 /give
|
||||
"gamemode", // 禁用 /gamemode
|
||||
"fill" // 禁用 /fill
|
||||
"give",
|
||||
"fill"
|
||||
);
|
||||
|
||||
public static void register() {
|
||||
NeoForge.EVENT_BUS.register(CommandInterceptor.class);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onCommandExecution(CommandEvent event) {
|
||||
ParseResults<CommandSourceStack> parseResults = event.getParseResults();
|
||||
if (parseResults.getContext().getNodes().isEmpty()) return;
|
||||
|
||||
// 获取命令的根节点名称
|
||||
String commandName = parseResults.getContext().getNodes().isEmpty()
|
||||
? ""
|
||||
: parseResults.getContext().getNodes().get(0).getNode().getName();
|
||||
CommandSourceStack source = parseResults.getContext().getSource();
|
||||
String commandName = parseResults.getContext().getNodes()
|
||||
.get(0).getNode().getName()
|
||||
.toLowerCase(Locale.ROOT);
|
||||
|
||||
// 检查是否在禁用列表中
|
||||
if (BLOCKED_COMMANDS.contains(commandName.toLowerCase(Locale.ROOT))) {
|
||||
// 取消命令执行
|
||||
event.setCanceled(true);
|
||||
CommandSourceStack source = parseResults.getContext().getSource();
|
||||
String raw = parseResults.getReader().getString();
|
||||
|
||||
// 可选:向执行者发送提示
|
||||
if (parseResults.getContext().getSource().getEntity() instanceof net.minecraft.server.level.ServerPlayer player) {
|
||||
player.sendSystemMessage(net.minecraft.network.chat.Component.literal("§c不准作弊哦(>v0"));
|
||||
source.getServer().getPlayerList().broadcastSystemMessage(
|
||||
Component.literal("§c不准作弊哦(>v0"),
|
||||
false
|
||||
);
|
||||
// ========== /gamemode 只拦截 creative ==========
|
||||
if ("gamemode".equals(commandName)) {
|
||||
String[] parts = raw.substring(1).trim().split("\\s+");
|
||||
|
||||
if (parts.length >= 2) {
|
||||
String modeArg = parts[1].toLowerCase(Locale.ROOT);
|
||||
|
||||
// creative / c / 1 全部拦截
|
||||
if (List.of("creative", "c", "1").contains(modeArg)) {
|
||||
event.setCanceled(true);
|
||||
broadcast(source, "§c不准作弊哦(∠·ω< )⌒★");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 其他模式(survival/adventure/spectator)或无参数,直接放行
|
||||
return;
|
||||
}
|
||||
|
||||
// ========== 拦截其他禁用指令 ==========
|
||||
if (BLOCKED_COMMANDS.contains(commandName)) {
|
||||
event.setCanceled(true);
|
||||
broadcast(source, "§c不准作弊哦(∠·ω< )⌒★");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void broadcast(CommandSourceStack source, String msg) {
|
||||
Component component = Component.literal(msg);
|
||||
for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) {
|
||||
player.sendSystemMessage(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user