What I want: If a player says brb, he executes the command /afk What I have till now: main (atbmain): Code: package me.Grovert11.AFKtoBRB; import java.util.logging.Logger; import org.bukkit.plugin.java.JavaPlugin; public class atbmain extends JavaPlugin{ Logger log = Logger.getLogger("Minecraft"); @Override public void onDisable() { log.info("|- AFKtoBRB by Grovert11 -|" + " |- :( Disabled ): -|"); } @Override public void onEnable() { getCommand("brb").setExecutor(new atbcommand()); log.info("|- AFKtoBRB by Grovert11 -|" + " |- Enabled -|"); } } atbchat: Code: package me.Grovert11.AFKtoBRB; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerEvent; public class atbchat extends PlayerEvent { public void PlayerChatEvent (Player sender, String msg){ if(msg.equalsIgnoreCase("brb")){ Player player = (Player) sender; sender.sendMessage(ChatColor.AQUA + "Je bent automatisch AFk gezet!"); player.performCommand("afk"); } } } The line getCommand("brb").setExecutor(new atbcommand()); in atbmain is for the command, but has nothing to do with this .
Ok, now I have this in my main class: Code: public void onEnable() { PluginManager pm = getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_CHAT, new atbchat(), Priority.Normal, this); getCommand("brb").setExecutor(new atbcommand()); log.info("|- AFKtoBRB by Grovert11 -|" + " |- Enabled -|"); } And this in the atbchat: Code: public class atbchat extends PlayerListener { public void PlayerChatEvent (Player sender, String msg){ if(msg.equalsIgnoreCase("brb")){ Player player = (Player) sender; sender.sendMessage(ChatColor.AQUA + "Je bent automatisch AFk gezet!"); player.performCommand("afk"); } } } Still an error, what do I do wrong
public void PlayerChatEvent (Player sender, String msg){ <-- this is wrong. Use that: Code: public void onPlayerChat(PlayerChatEvent event) { event.getPlayer().sendMessage("You send a message"); event.getMessage(); // with that you get access to the message }
But then it would say the message, everytime you say something? I want to let them execute a command when they say sth
Code:java @EventHandler(priority = EventPriority.NORMAL)public void onPlayerChat(PlayerChatEvent e) {if(e.isCancelled()||!e.getMessage().equalsIgnoreCase("brb"))return;e.setCancelled(true);e.getPlayer().sendMessage(ChatColor.AQUA + "Je bent automatisch AFK gezet!");e.getPlayer().performCommand("afk");} Dutch, ftw Edit: looks like XF messed up my tabs. :/
Code: public void onPlayerChat(PlayerChatEvent event) { if (event.getMessage().equalsIgnoreCase("brb")) { Player player = event.getPlayer(); player.sendMessage(ChatColor.AQUA + "Je bent automatisch AFk gezet!"); player.performCommand("afk"); event.setCancelled(true); } }
Use the new Event system. Code:java package me.Grovert11.AFKtoBRB; import java.util.logging.Logger; import org.bukkit.plugin.java.JavaPlugin; public class atbmain extends JavaPlugin{ Logger log = Logger.getLogger("Minecraft");@Overridepublic void onDisable() {log.info("|- AFKtoBRB by Grovert11 -|" +" |- :( Disabled ): -|");} @Overridepublic void onEnable() {getCommand("brb").setExecutor(new atbcommand());getServer().getPluginManager().registerEvents(new atbchat, this);log.info("|- AFKtoBRB by Grovert11 -|" +" |- Enabled -|");} } Code:java public class atbchat implements Listener {@EventHandler(priority = EventPriority.NORMAL)public void onPlayerChat (PlayerChatEvent event){if(event.getMessage().equalsIgnoreCase("brb"){event.getPlayer().sendMessage(ChatColor.AQUA + "Je bent automatisch AFk gezet!");event.getPlayer.performCommand("afk");}}}
Thank you very much, but Eclipse still gives an error (and it's not false, tested it) on new atbchat in the main class. "atbchat cannot be resolved to a variable" and "Syntax error on token "new", delete this token". In the atbchat I get an error on @EventHandler, because it says I need to add an attribute defining event. Whith what shouuld it be defined there? (Btw, thanks to the others who helped me )
Oh sorry, at Code:java getServer().getPluginManager().registerEvents(new atbchat, this); Change it to: Code:java getServer().getPluginManager().registerEvents(new atbchat(), this);
Thank you VERY much! Do you know if there's a possibility to execute the command and say the message after the chat is done? Now it tells the message, executes the command and then does the chat. And what do I use for "if(...) or if(...){}" EDIT: I found the "or-thing". It's || Maybe The only way to do this is first let the sender say it, and at the end cancel it?
That would create an infinite loop. Try this: Code:java public class atbchat implements Listener { @EventHandler(priority = EventPriority.NORMAL) public void onPlayerChat (final PlayerChatEvent event){ if(event.getMessage().equalsIgnoreCase("brb"){ event.getPlayer().sendMessage(ChatColor.AQUA + "Je bent automatisch AFk gezet!"); event.getPlayer().getServer().getScheduler().scheduleSyncDelayedTask( event.getPlayer().getServer().getPlugin("Your plugin"), new Runnable { public void run(){ event.getPlayer().performCommand("afk"); } } , 20L) } }}