From 95a59befa6c82c83f56d9929bdcdb7a5d33f3b28 Mon Sep 17 00:00:00 2001 From: xz Date: Fri, 14 Aug 2026 18:33:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A6=81=E6=AD=A2=E4=BD=9C=E5=BC=8A=E5=B9=B6?= =?UTF-8?q?=E5=B9=BF=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../ataku/stopcheat/CommandInterceptor.java | 45 +++++++++++++++++++ .../java/com/ataku/stopcheat/stopcheat.java | 43 +++++++++--------- .../com/ataku/stopcheat/stopcheatClient.java | 31 ------------- 4 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 src/main/java/com/ataku/stopcheat/CommandInterceptor.java delete mode 100644 src/main/java/com/ataku/stopcheat/stopcheatClient.java diff --git a/gradle.properties b/gradle.properties index 1bc3e4d..7946c32 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,7 +18,7 @@ minecraft_version=1.21.1 # as they do not follow standard versioning conventions. minecraft_version_range=[1.21.1] # The Neo version must agree with the Minecraft version to get a valid artifact -neo_version=21.1.248 +neo_version=21.1.247 # The loader version range can only use the major version of FML as bounds loader_version_range=[1,) diff --git a/src/main/java/com/ataku/stopcheat/CommandInterceptor.java b/src/main/java/com/ataku/stopcheat/CommandInterceptor.java new file mode 100644 index 0000000..c50a4ca --- /dev/null +++ b/src/main/java/com/ataku/stopcheat/CommandInterceptor.java @@ -0,0 +1,45 @@ +package com.ataku.stopcheat; + +import com.mojang.brigadier.ParseResults; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.network.chat.Component; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.neoforge.event.CommandEvent; + +import java.util.List; +import java.util.Locale; +public class CommandInterceptor { + + // 要禁用的命令列表(不含斜杠) + private static final List BLOCKED_COMMANDS = List.of( + "give", // 禁用 /give + "gamemode", // 禁用 /gamemode + "fill" // 禁用 /fill + ); + + @SubscribeEvent + public static void onCommandExecution(CommandEvent event) { + ParseResults parseResults = event.getParseResults(); + + // 获取命令的根节点名称 + String commandName = parseResults.getContext().getNodes().isEmpty() + ? "" + : parseResults.getContext().getNodes().get(0).getNode().getName(); + + // 检查是否在禁用列表中 + if (BLOCKED_COMMANDS.contains(commandName.toLowerCase(Locale.ROOT))) { + // 取消命令执行 + event.setCanceled(true); + CommandSourceStack source = parseResults.getContext().getSource(); + + // 可选:向执行者发送提示 + 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 + ); + } + } + } +} diff --git a/src/main/java/com/ataku/stopcheat/stopcheat.java b/src/main/java/com/ataku/stopcheat/stopcheat.java index 69be9e9..6e554d8 100644 --- a/src/main/java/com/ataku/stopcheat/stopcheat.java +++ b/src/main/java/com/ataku/stopcheat/stopcheat.java @@ -1,5 +1,6 @@ package com.ataku.stopcheat; +import net.neoforged.fml.loading.FMLEnvironment; import org.slf4j.Logger; import com.mojang.logging.LogUtils; @@ -66,26 +67,28 @@ public class stopcheat { // 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) { - // Register the commonSetup method for modloading - modEventBus.addListener(this::commonSetup); - - // Register the Deferred Register to the mod event bus so blocks get registered - BLOCKS.register(modEventBus); - // Register the Deferred Register to the mod event bus so items get registered - ITEMS.register(modEventBus); - // Register the Deferred Register to the mod event bus so tabs get registered - CREATIVE_MODE_TABS.register(modEventBus); - - // Register ourselves for server and other game events we are interested in. - // Note that this is necessary if and only if we want *this* class (stopcheat) to respond directly to events. - // Do not add this line if there are no @SubscribeEvent-annotated functions in this class, like onServerStarting() below. - NeoForge.EVENT_BUS.register(this); - - // Register the item to a creative tab - modEventBus.addListener(this::addCreative); - - // Register our mod's ModConfigSpec so that FML can create and load the config file for us - modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC); + // 只在专用服务端执行 + NeoForge.EVENT_BUS.register(CommandInterceptor.class); +// // Register the commonSetup method for modloading +// modEventBus.addListener(this::commonSetup); +// +// // Register the Deferred Register to the mod event bus so blocks get registered +// BLOCKS.register(modEventBus); +// // Register the Deferred Register to the mod event bus so items get registered +// ITEMS.register(modEventBus); +// // Register the Deferred Register to the mod event bus so tabs get registered +// CREATIVE_MODE_TABS.register(modEventBus); +// +// // Register ourselves for server and other game events we are interested in. +// // Note that this is necessary if and only if we want *this* class (stopcheat) to respond directly to events. +// // Do not add this line if there are no @SubscribeEvent-annotated functions in this class, like onServerStarting() below. +// NeoForge.EVENT_BUS.register(this); +// +// // Register the item to a creative tab +// modEventBus.addListener(this::addCreative); +// +// // Register our mod's ModConfigSpec so that FML can create and load the config file for us +// modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC); } private void commonSetup(FMLCommonSetupEvent event) { diff --git a/src/main/java/com/ataku/stopcheat/stopcheatClient.java b/src/main/java/com/ataku/stopcheat/stopcheatClient.java deleted file mode 100644 index 0dc07d0..0000000 --- a/src/main/java/com/ataku/stopcheat/stopcheatClient.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.ataku.stopcheat; - -import net.minecraft.client.Minecraft; -import net.neoforged.api.distmarker.Dist; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.ModContainer; -import net.neoforged.fml.common.EventBusSubscriber; -import net.neoforged.fml.common.Mod; -import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; -import net.neoforged.neoforge.client.gui.ConfigurationScreen; -import net.neoforged.neoforge.client.gui.IConfigScreenFactory; - -// This class will not load on dedicated servers. Accessing client side code from here is safe. -@Mod(value = stopcheat.MODID, dist = Dist.CLIENT) -// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent -@EventBusSubscriber(modid = stopcheat.MODID, value = Dist.CLIENT) -public class stopcheatClient { - public stopcheatClient(ModContainer container) { - // Allows NeoForge to create a config screen for this mod's configs. - // The config screen is accessed by going to the Mods screen > clicking on your mod > clicking on config. - // Do not forget to add translations for your config options to the en_us.json file. - container.registerExtensionPoint(IConfigScreenFactory.class, ConfigurationScreen::new); - } - - @SubscribeEvent - static void onClientSetup(FMLClientSetupEvent event) { - // Some client setup code - stopcheat.LOGGER.info("HELLO FROM CLIENT SETUP"); - stopcheat.LOGGER.info("MINECRAFT NAME >> {}", Minecraft.getInstance().getUser().getName()); - } -}