Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events.playerInteract not firing when right clicking air #256

Closed
frash23 opened this issue Jun 21, 2015 · 1 comment
Closed

events.playerInteract not firing when right clicking air #256

frash23 opened this issue Jun 21, 2015 · 1 comment

Comments

@frash23
Copy link

frash23 commented Jun 21, 2015

events.playerInteract doesn't seem to fire when right clicking air, despite bukkit behaving like this by default.

I am aware the client does not send any packets when right clicking air while not holding anything, but it doesn't fire despite holding items.

EDIT: May be related to the fact playerInteractEvent is cancelled by default, should still allow us to do stuff with it. I'm not a java developer, so I wouldn't know.

@frash23
Copy link
Author

frash23 commented Jul 1, 2015

I've learned a lot about the Bukkit API since opening this, and I managed to write my own version of your events.on function which does not have this issue:

exports.listen = function(eventType, func, priority) {
    var scPlugin = scPlugin || server.getPluginManager().getPlugin('scriptcraft');
    var RegisteredListener = org.bukkit.plugin.RegisteredListener;
    var EventExecutor = org.bukkit.plugin.EventExecutor;
    var EventPriority = org.bukkit.event.EventPriority;

    priority = EventPriority[ priority || 'NORMAL' ];

    // We have to use this method of getting the .getHandlerList
    // method because nashorn is dumb.
    var eventClass = eventType['class'];
    var handlerList = eventClass.getMethod('getHandlerList').invoke(null);

    // This map is used for assigning extra variables to the
    // listener's execute() function's `this` scope.
    var extraBinds = {};

    // Construct the lsitener's executor.
    var executor = new EventExecutor({
        execute: function(plugin, e) {

            // Let's create a function for safely canceling any
            // cancel-able event
            extraBinds.cancel = function() {
                if(e instanceof org.bukkit.event.Cancellable) e.setCancelled(true);
            };

            // We'll assign this map to `func`'s `this` scope
            var bind = {};
            // Append extra variables from outside this scope
            // (currently just this.unregister & this.cancel)
            for(var key in extraBinds) bind[key] = extraBinds[key];

            func.call(bind, e);
        }
    });

    var listener = new RegisteredListener(scPlugin, executor, priority, scPlugin, false);
    handlerList.register(listener);

    // Pass an unregister event to the listener
    extraBinds.unregister = function() {
        handlerList.unregister(listener);
    };

    return extraBinds;
};

Seems like the trick was your version was passing true as the last argument to the org.bukkit.plugin.RegisteredListener constructor. I'll admit I don't know exactly what this means, so I'm not sure if this should be changed in master as well, I'll let you decide that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants