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:
3 files changed
+32
-54
No files matched your search
@@ -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<String> BLOCKED_COMMANDS = List.of(
|
||||
"give",
|
||||
"fill"
|
||||
);
|
||||
// 从Mod配置获取需要禁用的指令列表
|
||||
private static final List<String> 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);
|
||||
|
||||
@@ -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<List<? extends String>> BLOCKED_COMMANDS = BUILDER
|
||||
.comment("禁用的指令列表")
|
||||
.defineList("blocked_commands", List.of("give", "fill"), obj -> obj instanceof String);
|
||||
|
||||
|
||||
public static final ModConfigSpec.ConfigValue<String> 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<String> 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<List<? extends String>> 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();
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user