-1

此代码应该等待.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);
            }
      }
}
4

3 回答 3

5

我想你想要@EventHandler注释:

@EventHandler public void onChat(PlayerChatEvent event) {
    ...
}
于 2012-05-30T19:25:24.600 回答
4

你错过了 3 件事:

  • 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);
        }
    }
}
于 2013-10-05T15:30:52.597 回答
1

我只需要添加到我的代码中,它就完美运行了!

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

并且@EventHandler在前面onChat()

完成的代码:

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);
        }
  }
}
于 2012-05-31T00:37:06.460 回答