From 445a2addcda371e90ac7346f01fa1a71eb25240c Mon Sep 17 00:00:00 2001 From: xz Date: Wed, 19 Aug 2026 15:11:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81=20and=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ataku/stopcheat/CommandInterceptor.java | 31 +++++++++---------- src/main/java/com/ataku/stopcheat/Config.java | 26 ++++++++-------- .../java/com/ataku/stopcheat/stopcheat.java | 29 +++-------------- 3 files changed, 32 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/ataku/stopcheat/CommandInterceptor.java b/src/main/java/com/ataku/stopcheat/CommandInterceptor.java index b9a3aa7..2fe28a7 100644 --- a/src/main/java/com/ataku/stopcheat/CommandInterceptor.java +++ b/src/main/java/com/ataku/stopcheat/CommandInterceptor.java @@ -8,20 +8,15 @@ import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.CommandEvent; +import java.util.ArrayList; import java.util.List; import java.util.Locale; public class CommandInterceptor { - // 这里只放完全禁用的指令,gamemode 单独处理 - private static final List BLOCKED_COMMANDS = List.of( - "give", - "fill" - ); + // 从Mod配置获取需要禁用的指令列表 + private static final List BLOCKED_COMMANDS = new ArrayList<>(Config.BLOCKED_COMMANDS.get()); - public static void register() { - NeoForge.EVENT_BUS.register(CommandInterceptor.class); - } @SubscribeEvent public static void onCommandExecution(CommandEvent event) { @@ -29,23 +24,24 @@ public class CommandInterceptor { if (parseResults.getContext().getNodes().isEmpty()) return; CommandSourceStack source = parseResults.getContext().getSource(); + // 获取玩家输入的指令名 String commandName = parseResults.getContext().getNodes() - .get(0).getNode().getName() + .getFirst().getNode().getName() .toLowerCase(Locale.ROOT); String raw = parseResults.getReader().getString(); - // ========== /gamemode 只拦截 creative ========== + // 对 /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 / c / 1 全部拦截 - if (List.of("creative", "c", "1").contains(modeArg)) { + // 仅拦截creative + if (List.of("creative", "1").contains(modeArg)) { event.setCanceled(true); - broadcast(source, "§c不准作弊哦(∠·ω< )⌒★"); + broadcast(source); return; } } @@ -53,14 +49,17 @@ public class CommandInterceptor { return; } - // ========== 拦截其他禁用指令 ========== + // 拦截列表中的指令 if (BLOCKED_COMMANDS.contains(commandName)) { event.setCanceled(true); - broadcast(source, "§c不准作弊哦(∠·ω< )⌒★"); + broadcast(source); } } - private static void broadcast(CommandSourceStack source, String msg) { + // 全服广播作弊 + private static void broadcast(CommandSourceStack source) { + // 从Mod配置获取广播消息 + String msg = Config.BROADCASTMSG.get(); Component component = Component.literal(msg); for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) { player.sendSystemMessage(component); diff --git a/src/main/java/com/ataku/stopcheat/Config.java b/src/main/java/com/ataku/stopcheat/Config.java index 6dd31cc..ec69a35 100644 --- a/src/main/java/com/ataku/stopcheat/Config.java +++ b/src/main/java/com/ataku/stopcheat/Config.java @@ -17,22 +17,22 @@ import net.neoforged.neoforge.common.ModConfigSpec; public class Config { private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder(); - public static final ModConfigSpec.BooleanValue LOG_DIRT_BLOCK = BUILDER - .comment("Whether to log the dirt block on common setup") - .define("logDirtBlock", true); + public static final ModConfigSpec.IntValue VERSION = BUILDER + .comment("Config Version") + .defineInRange("version", 1, 0, Integer.MAX_VALUE); + + + public static final ModConfigSpec.ConfigValue> BLOCKED_COMMANDS = BUILDER + .comment("禁用的指令列表") + .defineList("blocked_commands", List.of("give", "fill"), obj -> obj instanceof String); + + + public static final ModConfigSpec.ConfigValue BROADCASTMSG = BUILDER + .comment("有人作弊时的广播内容") + .define("broadcastmsg", "§c不准作弊哦(>v0"); - public static final ModConfigSpec.IntValue MAGIC_NUMBER = BUILDER - .comment("A magic number") - .defineInRange("magicNumber", 42, 0, Integer.MAX_VALUE); - public static final ModConfigSpec.ConfigValue MAGIC_NUMBER_INTRODUCTION = BUILDER - .comment("What you want the introduction message to be for the magic number") - .define("magicNumberIntroduction", "The magic number is... "); - // a list of strings that are treated as resource locations for items - public static final ModConfigSpec.ConfigValue> ITEM_STRINGS = BUILDER - .comment("A list of items to log on common setup.") - .defineListAllowEmpty("items", List.of("minecraft:iron_ingot"), () -> "", Config::validateItemName); static final ModConfigSpec SPEC = BUILDER.build(); diff --git a/src/main/java/com/ataku/stopcheat/stopcheat.java b/src/main/java/com/ataku/stopcheat/stopcheat.java index 6e554d8..40d5847 100644 --- a/src/main/java/com/ataku/stopcheat/stopcheat.java +++ b/src/main/java/com/ataku/stopcheat/stopcheat.java @@ -70,7 +70,7 @@ public class stopcheat { // 只在专用服务端执行 NeoForge.EVENT_BUS.register(CommandInterceptor.class); // // Register the commonSetup method for modloading -// modEventBus.addListener(this::commonSetup); + modEventBus.addListener(this::commonSetup); // // // Register the Deferred Register to the mod event bus so blocks get registered // BLOCKS.register(modEventBus); @@ -88,33 +88,12 @@ public class stopcheat { // 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); + modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC); } private void commonSetup(FMLCommonSetupEvent event) { - // Some common setup code - LOGGER.info("HELLO FROM COMMON SETUP"); - - if (Config.LOG_DIRT_BLOCK.getAsBoolean()) { - LOGGER.info("DIRT BLOCK >> {}", BuiltInRegistries.BLOCK.getKey(Blocks.DIRT)); - } - - LOGGER.info("{}{}", Config.MAGIC_NUMBER_INTRODUCTION.get(), Config.MAGIC_NUMBER.getAsInt()); - - Config.ITEM_STRINGS.get().forEach((item) -> LOGGER.info("ITEM >> {}", item)); + // hello + LOGGER.info("HELLO FROM STOPCHEAT"); } - // Add the example block item to the building blocks tab - private void addCreative(BuildCreativeModeTabContentsEvent event) { - if (event.getTabKey() == CreativeModeTabs.BUILDING_BLOCKS) { - event.accept(EXAMPLE_BLOCK_ITEM); - } - } - - // You can use SubscribeEvent and let the Event Bus discover methods to call - @SubscribeEvent - public void onServerStarting(ServerStartingEvent event) { - // Do something when the server starts - LOGGER.info("HELLO from server starting"); - } }