ScriptCraft

ScriptCraft

14.6k Downloads

events.playerInteract not firing when right clicking air

frash23 opened this issue ยท 1 comments

commented

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.

commented

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.