AsyncPlayerChatEvent is not working

Discussion in 'Spigot Plugin Development' started by theminerrick, Jun 2, 2015.

  1. hello guys i am making a anticurse plugin only the asyncplayerchatevent is not working i tryed to debug it but with no results. here is my code:

    Code (Text):
    package eu.thenlterms.chathelper;

    import java.util.HashMap;

    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import eu.thenlterms.chathelper.configs.BlackListedWords;

    public class CurseListener implements Listener {

        BlackListedWords b = BlackListedWords.getInstance();
       
        HashMap<String, String> lastmessage = new HashMap<String, String>();

       
        public Main plugin;
        public CurseListener(Main instance) {
            this.plugin=instance;
        }
       

        @EventHandler
        public void oncurse(AsyncPlayerChatEvent e){
            if(plugin.getConfig().getBoolean("togglechat")==true){
                if(!e.getPlayer().hasPermission("chathelper.chatstate.bypass")){
            if(ChatStates.isState(ChatStates.disabled)){
                e.setCancelled(true);
                String message  = plugin.getConfig().getString("disabledchat");
                e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', message));
            }}}
            if(!e.getPlayer().hasPermission("chathelper.anticurse.bypass")){
                if(plugin.getConfig().getBoolean("anticurse") ==true){
                    for (String word : e.getMessage().split(" ")){
                    if(b.getwords().getStringList("badwords").contains(word)){
                        e.setCancelled(true);
                        String message  = plugin.getConfig().getString("forbiddenword");
                        e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', message));
                       
                    }
                       
                    }
                   
                }
               
            }
            if(!e.getPlayer().hasPermission("chathelper.antispam.bypass")){
                if(plugin.getConfig().getBoolean("antispam")==true){
               
                if(lastmessage.containsKey(e.getPlayer().getUniqueId()) && lastmessage.containsValue(e.getMessage())){
                    e.setCancelled(true);
                    String message  = plugin.getConfig().getString("antispammessage");
                    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', message));
                   
                }
               
            }
           
        }
       
    }}
     
    someone any idea's how i can fix it
     
  2. clip

    Benefactor

    Did you register the event onEnable?
     
  3. Yes i did.
     
  4. ==true. wat.

    On topic:
    Are you getting any stacktraces in console?
     
  5. nope i don't get any stacktraces
     
  6. Do you know if the function is called at all?
    Like if you add a debug message at the start of the method.
     
  7. i tried but i didn't get the message
     
  8. Then there must be something wrong with registering your events.
     
  9. i don't really know what can be the fault
    here is my main code:

    Code (Text):
    package eu.thenlterms.chathelper;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import eu.thenlterms.chathelper.configs.BlackListedWords;

    public class Main extends JavaPlugin{

        public Logger log = Logger.getLogger("Minecraft");

        @Override
        public void onEnable() {
        log.info("[ChatHelper] " + this.getDescription().getVersion() + " Has been loaded.");
        BlackListedWords.getInstance().setup(this);
        PluginManager pm = Bukkit.getServer().getPluginManager();
        pm.registerEvents(new CurseListener(this), this);
        getCommand("togglechat").setExecutor(new CommandToggleChat(this));
        saveDefaultConfig();
        }
        @Override
        public void onDisable() {
            log.info("[ChatHelper] " + this.getDescription().getVersion() + " Has been Unloaded.");
        }
       
    }
     
  10. Do you have a plugin.yml that points to the correct main class?
     
  11. yes
     
  12. log.info("[ChatHelper] " + this.getDescription().getVersion() + " Has been loaded.");
    And this does get triggered?
     
  13. yes
     
  14. Is togglechat enabled in your config?

    Edit:
    and are you OP?
     
  15. yes and i tried with op and without op
     
  16. Alright, I want you to try something,
    Delete everything, or comment it out of that function, and replace it with this.
    And tell me if it shows up in your console:

    Code (Text):
    @EventHandler
    public void oncurse(AsyncPlayerChatEvent e) {
    System.out.println("test");
    }
     
  17. this works
     
  18. Alright, so the onCurse function does get triggered. That's good.
    We're getting somewhere.

    What I should try to do, is debug every thing you are testing, see what returns false.
    System.out.println(plugin.getConfig().getBoolean("togglechat"))
    System.out.println(!e.getPlayer().hasPermission("chathelper.chatstate.bypass"))
    System.out.println(ChatStates.isState(ChatStates.disabled))

    Just check which one returns false ;)
     
  19. the last one returns false also when run /togglechat
     
  20. Well, then I should start looking at the ChatStates.isState method ;)