禁止作弊并广播

This commit is contained in:
xzb23 committed 2026-08-14 18:33:00 +08:00
1 parent 891f574221
commit 95a59befa6
4 files changed
+69 -52

No files matched your search

+1 -1
View File
@@ -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,)
@@ -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<String> BLOCKED_COMMANDS = List.of(
"give", // 禁用 /give
"gamemode", // 禁用 /gamemode
"fill" // 禁用 /fill
);
@SubscribeEvent
public static void onCommandExecution(CommandEvent event) {
ParseResults<CommandSourceStack> 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
);
}
}
}
}
@@ -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) {
@@ -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());
}
}