PlayerChatEvent

Discussion in 'Plugin Development' started by Grovert11, Jan 22, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Grovert11

    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 ;).
     
  2. You must extend the PlayerListener class, and register it in your main class "atbmain"
     
  3. Offline

    Grovert11

    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 :(
     
  4. Offline

    Seadragon91

    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
    }
     
  5. Offline

    Grovert11

    But then it would say the message, everytime you say something?
    I want to let them execute a command when they say sth
     
  6. Offline

    Darkman2412

    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onPlayerChat(PlayerChatEvent e) {
    3. if(e.isCancelled()||!e.getMessage().equalsIgnoreCase("brb"))
    4. return;
    5. e.setCancelled(true);
    6. e.getPlayer().sendMessage(ChatColor.AQUA + "Je bent automatisch AFK gezet!");
    7. e.getPlayer().performCommand("afk");
    8. }

    Dutch, ftw :p

    Edit: looks like XF messed up my tabs. :/
     
  7. Offline

    Seadragon91

    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);
                    }
                }
     
  8. Offline

    Technius

    Use the new Event system.

    Code:java
    1.  
    2. package me.Grovert11.AFKtoBRB;
    3.  
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class atbmain extends JavaPlugin{
    9.  
    10. Logger log = Logger.getLogger("Minecraft");
    11. @Override
    12. public void onDisable() {
    13. log.info("|- AFKtoBRB by Grovert11 -|" +
    14. " |- :( Disabled ): -|");
    15. }
    16.  
    17. @Override
    18. public void onEnable() {
    19. getCommand("brb").setExecutor(new atbcommand());
    20. getServer().getPluginManager().registerEvents(new atbchat, this);
    21. log.info("|- AFKtoBRB by Grovert11 -|" +
    22. " |- Enabled -|");
    23. }
    24.  
    25. }
    26.  

    Code:java
    1.  
    2. public class atbchat implements Listener {
    3. @EventHandler(priority = EventPriority.NORMAL)
    4. public void onPlayerChat (PlayerChatEvent event){
    5. if(event.getMessage().equalsIgnoreCase("brb"){
    6. event.getPlayer().sendMessage(ChatColor.AQUA + "Je bent automatisch AFk gezet!");
    7. event.getPlayer.performCommand("afk");
    8. }
    9. }
    10. }
    11.  
     
  9. Offline

    Grovert11

    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 ;) )
     
  10. Offline

    Technius

    Oh sorry, at
    Code:java
    1.  
    2. getServer().getPluginManager().registerEvents(new atbchat, this);
    3.  

    Change it to:
    Code:java
    1.  
    2. getServer().getPluginManager().registerEvents(new atbchat(), this);
    3.  
     
  11. Offline

    Grovert11

    Could have thought of that myself, stupid noobness xD.
    What do I have to do with the @EventHandler?
     
  12. Offline

    Technius

    Code:java
    1.  
    2. @EventHandler(event = PlayerChatEvent.class, priority = EventPriority.NORMAL)
    3.  
     
  13. Offline

    Grovert11

    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 || :D
    Maybe The only way to do this is first let the sender say it, and at the end cancel it?
     
  14. Offline

    Technius

    That would create an infinite loop. Try this:
    Code:java
    1.  
    2. public class atbchat implements Listener {
    3. @EventHandler(priority = EventPriority.NORMAL)
    4. public void onPlayerChat (final PlayerChatEvent event){
    5. if(event.getMessage().equalsIgnoreCase("brb"){
    6. event.getPlayer().sendMessage(ChatColor.AQUA + "Je bent automatisch AFk gezet!");
    7. event.getPlayer().getServer().getScheduler().scheduleSyncDelayedTask(
    8. event.getPlayer().getServer().getPlugin("Your plugin"), new Runnable {
    9. public void run(){
    10. event.getPlayer().performCommand("afk");
    11. }
    12. } , 20L)
    13. }
    14. }
    15. }
    16.  
     
  15. Offline

    Grovert11

    Will do that tomorrow!
     
Thread Status:
Not open for further replies.

Share This Page