PluginManager.callEvent(T event) returns before event is handled

Discussion in 'BungeeCord Plugin Development' started by wea_ondara, May 10, 2013.

  1. Got this serious issue multiple times in my bungeecord plugins.

    I checked if any subscribe method starts any seperate threads causing this, BUT nothing like that.
    I added console logs and found out that callEvent return before any eventhandler logged anything.
    Does anybody have an idea? Really need it.

    Example 1: PermissionCheckEvent is triggered after perm check

    Code (Text):
    @Subscribe
        public void onPluginMessage(PluginMessageEvent e)
        {
            if(!e.getTag().equalsIgnoreCase(this.channel))
            {
                return;
            }
            String msg=new String(e.getData());
            ProxiedPlayer pp=(ProxiedPlayer) e.getReceiver();
            server.getPluginManager().dispatchCommand(pp, msg); //perm gets checked -> no perm although it's granted by a plugin; works when entering the command by a player
            e.setCancelled(true);
        }

    Example 2: own events return before triggered

    Code (Text):
    @Subscribe
        public void onChat(net.md_5.bungee.api.event.ChatEvent e)
        {
                e.setCancelled(true);
                chat(sender, player.getWriteChannel(), e.getMessage());
        }
        public void chat(ProxiedPlayer sender, Channel c, String msg)
        {
            //chatevent
            ChatEvent ce=new ChatEvent(sender,msg);  //my own event not bungeecord
            ce=server.getPluginManager().callEvent(ce);
            debug.log("event returned"); //FAILS HERE: appears before other logs issued in subscribe methods
            debug.log(ce.isCancelled());  //always false because event triggered afterwards
            if(ce.isCancelled())
            {
                return;
            }
            msg=ce.getMessage();
            String formattedMessage=formatMessage(c,sender,msg);
            //...
        }
     
    private String formatMessage(Channel channel, ProxiedPlayer pp, String message)
        {
            String ret=format;
            ChatFormatInfoEvent cfie=new ChatFormatInfoEvent(pp);
            cfie=server.getPluginManager().callEvent(cfie); //event is triggered afterwards
            //....
            //do formating and return
        }
     
     
  2. So the problem is that your event handler methods aren't ever called ? If so, are you sure you :
    1) Implemented Listener
    2) Registered that listener in the pluginmanager

    Also, maybe the callEvent method returns but the eventhandlers are called after ? (Asynchronously ?) Not too sure about this one.
     
  3. Yes the events are definitely called ... but afterwards (don't know why)

    the subscribe methods are called after the callevent method returns .. confusing
     
  4. md_5

    Administrator Developer

    I saw someone else having this issue ages back, but could never reproduce.
     
    • Agree Agree x 1
  5. Of course, there were 2 issues on github #200 and #202.

    You can try this for example:

    Connect to a bungeecord server with an acoount without permission to access /server. The permission will/should be granted by PermissionCheckEvent, but isn't.

    Code (Text):

        @Subscribe
        public void onPluginMessage(PluginMessageEvent e)
        {
            if(!e.getTag().equalsIgnoreCase(this.channel))  //some channel
            {
                return;
            }
            String msg=new String(e.getData());  //data contains "server main"
            ProxiedPlayer pp=(ProxiedPlayer) e.getReceiver();  //msg is send by a spigot server to bungeecord
            server.getPluginManager().dispatchCommand(pp, msg); //perm gets checked -> no perm although it's granted
            e.setCancelled(true);
        }
     
        @Subscribe
        public void onPermissionCheck(PermissionCheckEvent e)
        {
            e.setHasPermission(true); //grant every permission to the command sender; just for this test case
        }
     
  6. maybe events cannot be called in event handlers (subscribe methods) ...
    does the eventbus support handling multiple events at the same time?
     
  7. md_5

    Administrator Developer

    So the issue here is that events cannot be nested?
    Can you maybe do a simple test case for that?
     
  8. so, made a simple test case ...

    i prepared myself a local bungeecord and 2 spigot servers named lobby and main

    plugins:
    i put the bungeecord plugin into the bungeecord plugins folder
    and the bukkit plugin into the plugins folder of the lobby server; main doesn't need the plugin

    permissions:
    i remove the permissions from all the ranks; the permissions will be granted by the plugin

    let's start:
    i started all the servers a joined my local bungeecord
    i spawned on the lobby server -> everything worked as it should

    so here comes the problem: i type in /start to do some work on the bukkit/spigot server
    once this has finished i should be moved form the lobby to main server -> bukkit/spigot sends a plugin message to bungeecord using the player who typed the /start command. everything works fine for now.
    the plugin message contains "server main", the command that will be dispatched by the bungeecord plugin with the player. the permission for the /server command should be granted by the PermissioCheckEvent but isn't.
    and the question is WHY?

    The source and the jars of both plugins is available below:

    the Bukkit/Spigot part:
    plugin: http://pastebin.com/B0jgfSss
    plugin.yml: http://pastebin.com/zaVsxgGM
    jar: http://www.dropbox.com/s/cjra29rt2dxutoo/EventBugBukkit.jar

    the BungeeCord part:
    plugin: http://pastebin.com/uwPcnYxG
    plugin.yml: http://pastebin.com/HsvCbMVi
    jar: http://www.dropbox.com/s/08bamw7y59a6c2c/EventBugBungee.jar
     
  9. md_5

    Administrator Developer

    The good news is you are not crazy!
    Commited a failing unit test to BungeeCord just now. Check out the source on github.

    EDIT: Pushed what should be a fix
     
    • Friendly Friendly x 1
  10. #414 worked with the test plugins
    will try out if all the similar problems are fix with this
     
  11. tested it all ... and everything worked fine
    finally got rid of all the silly errors

    thx @md5
     
    • Like Like x 1