mirror of
https://github.com/xzb23/Minecraft-Command-Permission-Control.git
synced 2026-11-25 02:05:25 +08:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac5749a03e
|
||
|
|
d54aded4e0
|
||
|
|
044778fa0d
|
||
|
|
19f292494c
|
||
|
|
18de96c986
|
||
|
|
aae3f57ad4
|
No files matched your search
+1
-1
@@ -32,7 +32,7 @@ mod_name=stopcheat
|
|||||||
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
||||||
mod_license=MIT License
|
mod_license=MIT License
|
||||||
# The mod version. See https://semver.org/
|
# The mod version. See https://semver.org/
|
||||||
mod_version=1.0.11
|
mod_version=1.1
|
||||||
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
||||||
# This should match the base package used for the mod sources.
|
# This should match the base package used for the mod sources.
|
||||||
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import net.minecraft.network.chat.Component;
|
|||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.neoforged.bus.api.SubscribeEvent;
|
import net.neoforged.bus.api.SubscribeEvent;
|
||||||
import net.neoforged.neoforge.event.CommandEvent;
|
import net.neoforged.neoforge.event.CommandEvent;
|
||||||
|
import net.neoforged.neoforge.server.ServerLifecycleHooks;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -21,18 +22,25 @@ public class CommandInterceptor {
|
|||||||
private static final int arg_CHECKINDEX = 1;
|
private static final int arg_CHECKINDEX = 1;
|
||||||
private static final int arg_PARAMETERS = 2;
|
private static final int arg_PARAMETERS = 2;
|
||||||
|
|
||||||
// 从Mod配置获取黑名单列表
|
// 从配置获取黑名单命令和白名单玩家列表
|
||||||
private static List<List<String>> blackList = initblackList(new ArrayList<>(Config.BLACKLIST.get()));
|
private static List<List<String>> blackList;
|
||||||
private static List<String> opUUID = Config.PASSUUID.get().stream()
|
private static List<String> opUUID;
|
||||||
|
private static String cheatMsg;
|
||||||
|
|
||||||
|
public static void applyConfig() {
|
||||||
|
blackList = initblackList(new ArrayList<>(Config.BLACKLIST.get()));
|
||||||
|
opUUID = Config.PASSUUID.get().stream()
|
||||||
.map(s -> s.toLowerCase(Locale.ROOT))
|
.map(s -> s.toLowerCase(Locale.ROOT))
|
||||||
.toList();
|
.toList();
|
||||||
|
cheatMsg = I18n_.i18nDic.get("aza.stopcheat.broadcast.cheat");
|
||||||
|
}
|
||||||
|
|
||||||
public static List<List<String>> initblackList(List<String> commands){
|
public static List<List<String>> initblackList(List<String> commands){
|
||||||
List<List<String>> blackList = new ArrayList<List<String>>();
|
List<List<String>> blackList = new ArrayList<List<String>>();
|
||||||
for (String command: commands) {
|
for (String command: commands) {
|
||||||
List<String> black = new ArrayList<>(Arrays.asList(command.split(CMDSEPARATOR)));
|
List<String> black = new ArrayList<>(Arrays.asList(command.split(CMDSEPARATOR)));
|
||||||
if (black.isEmpty()){
|
if (black.isEmpty()){
|
||||||
LOGGER.warn("{} 配置不可用, 长度错误", command);
|
LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.lan"), command);
|
||||||
} else if (black.size() < 3){
|
} else if (black.size() < 3){
|
||||||
for (int i = 0; black.size() + i < 3; i++){
|
for (int i = 0; black.size() + i < 3; i++){
|
||||||
black.add("0");
|
black.add("0");
|
||||||
@@ -41,7 +49,7 @@ public class CommandInterceptor {
|
|||||||
} else if (black.size() == 3){
|
} else if (black.size() == 3){
|
||||||
blackList.add(black);
|
blackList.add(black);
|
||||||
} else {
|
} else {
|
||||||
LOGGER.warn("{} 配置不可用, 长度错误", command);
|
LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.lan"), command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return blackList;
|
return blackList;
|
||||||
@@ -77,33 +85,34 @@ public class CommandInterceptor {
|
|||||||
try {
|
try {
|
||||||
stopIndex = Integer.parseInt(blackCommands.get(arg_CHECKINDEX)); // 将blackCommands[args_STOPINDEX] 转换为为Integer
|
stopIndex = Integer.parseInt(blackCommands.get(arg_CHECKINDEX)); // 将blackCommands[args_STOPINDEX] 转换为为Integer
|
||||||
}catch (NumberFormatException e){
|
}catch (NumberFormatException e){
|
||||||
LOGGER.warn("{} 配置不可用, args_STOPINDEX错误,重置为0", String.join(CMDSEPARATOR, blackCommands));
|
LOGGER.warn(I18n_.i18nDic.get("aza.stopcheat.warn.config.stopindex"), String.join(CMDSEPARATOR, blackCommands));
|
||||||
stopIndex = arg_blackLISTCOMMANDNAME;
|
stopIndex = arg_blackLISTCOMMANDNAME;
|
||||||
}
|
}
|
||||||
if (stopIndex == 0){ // 禁止整个指令而不是特定参数的指令
|
if (stopIndex == 0){ // 禁止整个指令而不是特定参数的指令
|
||||||
event.setCanceled(true);
|
event.setCanceled(true);
|
||||||
LOGGER.info("{} 使用 {} 命令被拦截", source.getTextName(), raw);
|
LOGGER.info(I18n_.i18nDic.get("aza.stopcheat.log.intercept"), source.getTextName(), raw);
|
||||||
broadcast(source);
|
broadcast(cheatMsg);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
List<String> blackArgs = Arrays.asList(blackCommands.get(arg_PARAMETERS).split(","));
|
List<String> blackArgs = Arrays.asList(blackCommands.get(arg_PARAMETERS).split(","));
|
||||||
if (blackArgs.contains(parts[stopIndex])){ // 禁止特定参数的指令,在配置中定义
|
if (blackArgs.contains(parts[stopIndex])){ // 禁止特定参数的指令,在配置中定义
|
||||||
event.setCanceled(true);
|
event.setCanceled(true);
|
||||||
LOGGER.info("{} 使用 {} 命令被拦截", source.getTextName(), raw);
|
LOGGER.info(I18n_.i18nDic.get("aza.stopcheat.log.intercept"), source.getTextName(), raw);
|
||||||
broadcast(source);
|
broadcast(cheatMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 全服广播作弊
|
// 全服广播
|
||||||
private static void broadcast(CommandSourceStack source) {
|
public static void broadcast(String msg) {
|
||||||
// 从Mod配置获取广播消息
|
var server = ServerLifecycleHooks.getCurrentServer();
|
||||||
String msg = Config.BROADCASTMSG.get();
|
|
||||||
Component component = Component.literal(msg);
|
Component component = Component.literal(msg);
|
||||||
for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) {
|
if (server != null) {
|
||||||
|
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
|
||||||
player.sendSystemMessage(component);
|
player.sendSystemMessage(component);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
package com.ataku.stopcheat;
|
package com.ataku.stopcheat;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import net.neoforged.api.distmarker.Dist;
|
||||||
|
import net.neoforged.fml.ModContainer;
|
||||||
|
import net.neoforged.fml.loading.FMLEnvironment;
|
||||||
|
import net.neoforged.neoforge.client.gui.ConfigurationScreen;
|
||||||
|
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
|
||||||
import net.neoforged.neoforge.common.ModConfigSpec;
|
import net.neoforged.neoforge.common.ModConfigSpec;
|
||||||
|
|
||||||
// An example config class. This is not required, but it's a good idea to have one to keep your config organized.
|
// An example config class. This is not required, but it's a good idea to have one to keep your config organized.
|
||||||
@@ -21,18 +27,17 @@ public class Config {
|
|||||||
.defineList("blacked_commands", List.of("give:0:", "fill:0:", "gamemode:1:creative"), obj -> obj instanceof String);
|
.defineList("blacked_commands", List.of("give:0:", "fill:0:", "gamemode:1:creative"), obj -> obj instanceof String);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static final ModConfigSpec.ConfigValue<List<? extends String>> PASSUUID = BUILDER
|
public static final ModConfigSpec.ConfigValue<List<? extends String>> PASSUUID = BUILDER
|
||||||
.comment("不受约束的玩家UUID列表")
|
.comment("不受约束的玩家UUID列表")
|
||||||
.defineList("pass_users", List.of("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), obj -> obj instanceof String);
|
.defineList("pass_users", List.of("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), obj -> obj instanceof String);
|
||||||
|
|
||||||
|
|
||||||
public static final ModConfigSpec.ConfigValue<String> BROADCASTMSG = BUILDER
|
|
||||||
.comment("有人作弊时的广播内容")
|
|
||||||
.define("broadcastmsg", "§c不准作弊哦(>v0");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static final ModConfigSpec SPEC = BUILDER.build();
|
static final ModConfigSpec SPEC = BUILDER.build();
|
||||||
|
|
||||||
|
public static void configGUI(ModContainer container) {
|
||||||
|
container.registerExtensionPoint(
|
||||||
|
IConfigScreenFactory.class,
|
||||||
|
ConfigurationScreen::new
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.ataku.stopcheat;
|
||||||
|
|
||||||
|
import net.minecraft.client.resources.language.I18n;
|
||||||
|
import net.neoforged.api.distmarker.Dist;
|
||||||
|
import net.neoforged.fml.loading.FMLEnvironment;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class I18n_ {
|
||||||
|
public static Map<String, String> i18nDic = new HashMap<>();
|
||||||
|
|
||||||
|
public static void init_(){
|
||||||
|
if (FMLEnvironment.dist == Dist.DEDICATED_SERVER) {
|
||||||
|
i18nDic.put("aza.stopcheat.warn.config.lan", "{} configuration unavailable, length error");
|
||||||
|
i18nDic.put("aza.stopcheat.warn.config.stopindex", "{} configuration unavailable, args_STOPINDEX error, reset to 0");
|
||||||
|
i18nDic.put("aza.stopcheat.log.intercept", "{} used {} command and was intercepted");
|
||||||
|
i18nDic.put("aza.stopcheat.broadcast.cheat", "§cNo cheating allowed (>v0");
|
||||||
|
i18nDic.put("aza.stopcheat.broadcast.reload", "Configuration reloaded");
|
||||||
|
}else {
|
||||||
|
i18nDic.put("aza.stopcheat.warn.config.lan", I18n.get("aza.stopcheat.warn.config.lan"));
|
||||||
|
i18nDic.put("aza.stopcheat.warn.config.stopindex", I18n.get("aza.stopcheat.warn.config.stopindex"));
|
||||||
|
i18nDic.put("aza.stopcheat.log.intercept", I18n.get("aza.stopcheat.log.intercept"));
|
||||||
|
i18nDic.put("aza.stopcheat.broadcast.cheat", I18n.get("aza.stopcheat.broadcast.cheat"));
|
||||||
|
i18nDic.put("aza.stopcheat.broadcast.reload", I18n.get("aza.stopcheat.broadcast.reload"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
package com.ataku.stopcheat;
|
package com.ataku.stopcheat;
|
||||||
|
|
||||||
|
import net.neoforged.api.distmarker.Dist;
|
||||||
|
import net.neoforged.fml.event.config.ModConfigEvent;
|
||||||
|
import net.neoforged.fml.loading.FMLEnvironment;
|
||||||
|
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import com.mojang.logging.LogUtils;
|
import com.mojang.logging.LogUtils;
|
||||||
@@ -18,17 +22,25 @@ public class stopcheat {
|
|||||||
public static final String MODID = "stopcheat";
|
public static final String MODID = "stopcheat";
|
||||||
// Directly reference a slf4j logger
|
// Directly reference a slf4j logger
|
||||||
public static final Logger LOGGER = LogUtils.getLogger();
|
public static final Logger LOGGER = LogUtils.getLogger();
|
||||||
|
private static boolean firstFlag = true;
|
||||||
|
|
||||||
// The constructor for the mod class is the first code that is run when your mod is loaded.
|
// 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.
|
// FML will recognize some parameter types like IEventBus or ModContainer and pass them in automatically.
|
||||||
public stopcheat(IEventBus modEventBus, ModContainer modContainer) {
|
public stopcheat(IEventBus modEventBus, ModContainer modContainer) {
|
||||||
// 只在专用服务端执行
|
|
||||||
NeoForge.EVENT_BUS.register(CommandInterceptor.class);
|
NeoForge.EVENT_BUS.register(CommandInterceptor.class);
|
||||||
// Register the commonSetup method for modloading
|
// hello
|
||||||
modEventBus.addListener(this::commonSetup);
|
modEventBus.addListener(this::commonSetup);
|
||||||
|
|
||||||
// Register our mod's ModConfigSpec so that FML can create and load the config file for us
|
// 加载配置
|
||||||
|
modEventBus.addListener(this::confUpdate);
|
||||||
|
// L10n
|
||||||
|
NeoForge.EVENT_BUS.addListener(this::loadl10nandconfig);
|
||||||
|
// 注册配置文件
|
||||||
modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
|
modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
|
||||||
|
|
||||||
|
// 游戏内配置界面
|
||||||
|
if (FMLEnvironment.dist == Dist.DEDICATED_SERVER){return;} //专用服务端不注册
|
||||||
|
else {Config.configGUI(modContainer);}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void commonSetup(FMLCommonSetupEvent event) {
|
private void commonSetup(FMLCommonSetupEvent event) {
|
||||||
@@ -36,4 +48,23 @@ public class stopcheat {
|
|||||||
LOGGER.info("HELLO FROM STOPCHEAT");
|
LOGGER.info("HELLO FROM STOPCHEAT");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadl10nandconfig(PlayerEvent.PlayerLoggedInEvent event) {
|
||||||
|
if (!firstFlag){return;} //确保只加载一次
|
||||||
|
I18n_.init_();
|
||||||
|
CommandInterceptor.applyConfig();
|
||||||
|
firstFlag = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int confUpdate(ModConfigEvent event){
|
||||||
|
if (event.getConfig().getSpec() != Config.SPEC){
|
||||||
|
return 1; // 其它Mod Config
|
||||||
|
}
|
||||||
|
if (event instanceof ModConfigEvent.Reloading){
|
||||||
|
LOGGER.info("Loading Config File");
|
||||||
|
CommandInterceptor.applyConfig();
|
||||||
|
CommandInterceptor.broadcast(String.format("§c%s§r %s", stopcheat.MODID, I18n_.i18nDic.get("aza.stopcheat.broadcast.reload")));
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
{
|
{
|
||||||
"itemGroup.stopcheat": "Example Mod Tab",
|
"aza.stopcheat.warn.config.lan": "{} configuration unavailable, length error",
|
||||||
"block.stopcheat.example_block": "Example Block",
|
"aza.stopcheat.warn.config.stopindex": "{} configuration unavailable, args_STOPINDEX error, reset to 0",
|
||||||
"item.stopcheat.example_item": "Example Item",
|
"aza.stopcheat.log.intercept": "{} used {} command and was intercepted",
|
||||||
|
"aza.stopcheat.broadcast.reload": "Configuration reloaded",
|
||||||
|
"aza.stopcheat.broadcast.cheat": "§cNo cheating allowed (>v0",
|
||||||
|
|
||||||
"stopcheat.configuration.title": "stopcheat Configs",
|
"stopcheat.configuration.version": "Config Version",
|
||||||
"stopcheat.configuration.section.stopcheat.common.toml": "stopcheat Configs",
|
"stopcheat.configuration.blacked_commands": "Blacklisted Commands",
|
||||||
"stopcheat.configuration.section.stopcheat.common.toml.title": "stopcheat Configs",
|
"stopcheat.configuration.pass_users": "Bypass Users"
|
||||||
"stopcheat.configuration.items": "Item List",
|
|
||||||
"stopcheat.configuration.logDirtBlock": "Log Dirt Block",
|
|
||||||
"stopcheat.configuration.magicNumberIntroduction": "Magic Number Text",
|
|
||||||
"stopcheat.configuration.magicNumber": "Magic Number"
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"aza.stopcheat.warn.config.lan": "{} 配置不可用, 长度错误",
|
||||||
|
"aza.stopcheat.warn.config.stopindex": "{} 配置不可用, args_STOPINDEX错误,重置为0",
|
||||||
|
"aza.stopcheat.log.intercept": "{} 使用 {} 命令被拦截",
|
||||||
|
"aza.stopcheat.broadcast.reload": "配置重载完成",
|
||||||
|
"aza.stopcheat.broadcast.cheat": "§c不准作弊哦(>v0",
|
||||||
|
|
||||||
|
"stopcheat.configuration.version": "配置文件版本",
|
||||||
|
"stopcheat.configuration.blacked_commands": "禁用指令列表",
|
||||||
|
"stopcheat.configuration.pass_users": "白名单玩家"
|
||||||
|
}
|
||||||
@@ -46,7 +46,7 @@ authors="aza/xz"
|
|||||||
|
|
||||||
# The description text for the mod (multi line!) (#mandatory)
|
# The description text for the mod (multi line!) (#mandatory)
|
||||||
description='''
|
description='''
|
||||||
Example mod description.
|
Command Permission Control.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# The [[mixins]] block allows you to declare your mixin config to FML so that it gets loaded.
|
# The [[mixins]] block allows you to declare your mixin config to FML so that it gets loaded.
|
||||||
|
|||||||
Reference in new issue
Block a user