Java Bukkit/Spigot - Block Specified Commands

时间:2016-07-11 22:46:21

标签: java plugins server minecraft bukkit

i'm trying to make a plugin, it must block a specified commands setted by config. i've maked this but it doesn't block any command.

Code:

@EventHandler(priority = EventPriority.HIGHEST)
  public void onPreprocess(PlayerCommandPreprocessEvent event)
  {
    Player player = event.getPlayer();
    String command = event.getMessage();
    List<String> bCmds = this.plugin.cfg.getStringList("blocked-commands");

    for (String bCmd : bCmds) 
    {
        if(command.equalsIgnoreCase(bCmd))
        {
            event.setCancelled(true);
        }
    }
  }

Config:

blocked-commands:
- /pl
- /op
- /sp
- /gravityblock

PS: I've tried to use:

String command = event.getMessage().subString(1);

Thanks for Help... :)

2 个答案:

答案 0 :(得分:0)

注册活动?实施了清单?

此外,您的代码将无法使用命令中的其他参数。 如果它包含空格,则用“”拆分它并获取第一个元素以获取命令

if(cmd.contains(“”))cmd = cmd.split(“”)[0];

答案 1 :(得分:0)

Bukkit's wiiki所述,优先级按以下顺序调用:

  • EventPriority.LOWEST
  • EventPriority.LOW
  • EventPriority.NORMAL
  • EventPriority.HIGH
  • EventPriority.HIGHEST
  • EventPriority.MONITOR

也许您可以尝试使用最低优先级,因此在处理命令之前会取消事件。

我也相信命令可能有参数,所以它可能不等于提供的字符串,你也应该尝试

String command = event.getMessage();


if (command.toLowerCase().startsWith("/command") ) {
    //cancel
}