remove bukkit commands

This commit is contained in:
expvintl 2023-09-12 23:26:49 +08:00
parent ec65481f20
commit da94e4448d
2 changed files with 58 additions and 1 deletions

View File

@ -1,17 +1,21 @@
package expvintl.tools;
import expvintl.tools.listeners.LoginHandler;
import expvintl.tools.utils.CommandHelper;
import org.bukkit.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Logger;
@ -66,10 +70,41 @@ public final class Tools extends JavaPlugin {
logger.warning(e.getMessage());
}
}
private void DisableBukkitCommands(){
try {
Field commands=SimpleCommandMap.class.getDeclaredField("knownCommands");
commands.setAccessible(true);
Map<String, Command> map= (Map<String, Command>) commands.get(CommandHelper.getCommandMap());
Iterator<String> it=map.keySet().iterator();
//取消注册下列命令
while(it.hasNext()) {
String cmd= it.next();
//移除bukkit命名空间中的命令
if (cmd.startsWith("bukkit:")) {
it.remove();
}
//移除直接执行的命令
switch (cmd) {
case "ver":
case "version":
case "pl":
case "plugins":
case "about":
it.remove();
break;
}
}
commands.setAccessible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onEnable() {
// Plugin startup logic
logger.info("expvintl:Tools ²å¼þÒÑÆôÓÃ!");
DisableBukkitCommands();
initConfigs();
}

View File

@ -0,0 +1,22 @@
package expvintl.tools.utils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import java.lang.reflect.Field;
public class CommandHelper {
private static CommandMap commandMap=null;
//获取命令Map并缓存
public static CommandMap getCommandMap(){
try {
Field map=Bukkit.getServer().getClass().getDeclaredField("commandMap");
map.setAccessible(true);
if(commandMap==null) commandMap=(CommandMap)map.get(Bukkit.getServer());
map.setAccessible(false);
} catch (Exception e) {
Bukkit.getLogger().warning("无法获取CommandMap:\n"+e.getMessage());
}
return commandMap;
}
}