diff --git a/src/main/java/expvintl/tools/Tools.java b/src/main/java/expvintl/tools/Tools.java index af1cfa4..8a98847 100644 --- a/src/main/java/expvintl/tools/Tools.java +++ b/src/main/java/expvintl/tools/Tools.java @@ -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 map= (Map) commands.get(CommandHelper.getCommandMap()); + Iterator 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(); } diff --git a/src/main/java/expvintl/tools/utils/CommandHelper.java b/src/main/java/expvintl/tools/utils/CommandHelper.java new file mode 100644 index 0000000..10cd385 --- /dev/null +++ b/src/main/java/expvintl/tools/utils/CommandHelper.java @@ -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; + } +}