onChat()没有被调用

时间:2012-05-30 19:19:35

标签: java bukkit

此代码应等待.help在聊天中输入,然后发送一条消息"Help text."。 没有任何事情发生。

package testplugin.HelpMe;
import java.util.logging.Logger;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.player.PlayerChatEvent;

public class HelpMe extends JavaPlugin implements {

    Logger log;

    public void onEnable(){
        log = this.getLogger();
        log.info("Your plugin has been enabled!");
    }

    public void onDisable(){
        log.info("Your plugin has been disabled.");
    }

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        return false;


      }
      public void onChat(PlayerChatEvent event) {
            if (event.getMessage().startsWith(".help"))
            {
              event.getPlayer().sendMessage("Help text.");
              event.setCancelled(true);
            }
      }
}

3 个答案:

答案 0 :(得分:5)

我想你想要@EventHandler注释:

@EventHandler public void onChat(PlayerChatEvent event) {
    ...
}

答案 1 :(得分:4)

你缺少三件事:

  • implements Listener(你错过了你提供的代码,复制粘贴错误?)
  • @EventHandler方法
  • 上的onChat注释
  • 实际注册您的插件以侦听事件的代码,getServer().getPluginManager().registerEvents(this, this);
  • 中的onEnable

因此,最终结果如下:

package testplugin.HelpMe;
import java.util.logging.Logger;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.Listener;
import org.bukkit.event.EventHandler;

public class HelpMe extends JavaPlugin implements Listener {

    Logger log;

    public void onEnable(){
        log = this.getLogger();
        log.info("Your plugin has been enabled!");
        //Tell the server that this plugin provides an event listener (which is this class), and to invoke event handlers in that class
        getServer().getPluginManager().registerEvents(this, this);
    }

    public void onDisable(){
        log.info("Your plugin has been disabled.");
    }

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        return false;
    }

    //Marks that this method handles the PlayerChatEvent
    @EventHandler
    public void onChat(PlayerChatEvent event) {
        if (event.getMessage().startsWith(".help"))
        {
          event.getPlayer().sendMessage("Help text.");
          event.setCancelled(true);
        }
    }
}

答案 2 :(得分:1)

我需要添加的是我的代码,它完美无缺!

public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
}

@EventHandleronChat()

完成的代码:

package testplugin.HelpMe;
import java.util.logging.Logger;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.player.PlayerChatEvent;

public class HelpMe extends JavaPlugin implements {

Logger log;

public void onEnable(){
    log = this.getLogger();
    log.info("Your plugin has been enabled!");
    getServer().getPluginManager().registerEvents(this, this);
}

public void onDisable(){
    log.info("Your plugin has been disabled.");
}

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    return false;


  }
  @EventHandler public void onChat(PlayerChatEvent event) {
        if (event.getMessage().startsWith(".help"))
        {
          event.getPlayer().sendMessage("Help text.");
          event.setCancelled(true);
        }
  }
}