Help with the AsyncPlayerChatEvent event?

Discussion in 'Plugin Development' started by Tagtart, May 16, 2015.

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

    Tagtart

    Hello, I'm struggling to understand exactly how the AsyncPlayerChatEvent event works, could anyone explain it to me? In developing a new plugin, ConsoleSender, which will send a command from the console if the '/' in the message is replaced with a '!', e.g: !op <player>, which runs that from the console.

    Here is my code, and I have tried it both with the @EventHandler there with no tags, with tags, and without it at all. My mistake is probably a dumb one, please don't rage if it ends up I have missed something simple. Is it anything to do with the event.setCancelled(true);

    Remember I'm not stupid, this aint the entire class...

    Code:
        @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onExclaimationCommand(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
            String s = event.getMessage();
    
    /* This code here was to test if the event was even being called, which it is not */
            String pn = p.getName();
            Bukkit.broadcastMessage(pn);
            Bukkit.broadcastMessage(s);
    /* This code here was to test if the event was even being called, which it is not */
    
            if (s.startsWith("!")) {
                if (p.hasPermission("console")) {
                    event.setCancelled(true);
                    s = s.replaceFirst(s, "!");
                    getServer().getConsoleSender().sendMessage(s);
                    String[] msg = {"The following command has been run by the console:", s};
                    p.sendMessage(msg);
                } else {
                    event.setCancelled(true);
                    p.sendMessage("§c§lNo permission!");
                }
            }
        }
     
    Last edited: May 16, 2015
  2. Online

    timtower Administrator Administrator Moderator

    @Tagtart Event might be canceled, did you register it in the onEnable?
     
  3. Offline

    Tagtart

  4. Offline

    Ungemonstert

    @Tagtart you tested in your
    Code:
    if (s.startsWith("!"))
    if the msg starts with an "!".
    If it is and p has the permission, you replaced the first char with a "!". But the first char is already a "!".

    For example:

    p writes "!hello".
    your plugin tests if it starts with "!"
    true
    p has the permission "console"
    true
    your plugin replace the message "!hello" with the message "!hello".

    I think you can delete the line
    Code:
    s = s.replaceFirst(s, "!");
    ;)
     
  5. Offline

    Tagtart

    @Ungemonstert Ah, I do believe I made an error with that. Although the plugin is working fine, here is its 1 class code:

    Code:
    package net.minecrash.console;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ConsoleSender extends JavaPlugin implements Listener {
        String v = "1.1";
        public void onEnable() {
            getLogger().info("Enabled ConsoleSender v"+v);
            getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
            getLogger().info("Disabled ConsoleSender v"+v);
        }
       
        @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
        public void onCommand(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
            String s = event.getMessage();
            if (s.startsWith("!")) {
                if (p.hasPermission("console")) {
                    s = s.replaceFirst("!", "");
                    getServer().dispatchCommand(getServer().getConsoleSender(), s);
                    String[] msg = {"The following command has been run by the console:", s};
                    p.sendMessage(msg);
                    event.setCancelled(true);
                } else {
                    p.sendMessage("§c§lNo permission!");
                    event.setCancelled(true);
                }
            }
        }
    }
    I was supposed to replace "!" with "", instead of s with "!"
     
Thread Status:
Not open for further replies.

Share This Page