此代码应等待.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);
}
}
}
答案 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);
}
@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);
}
}
}