events.playerInteract not firing when right clicking air · Issue #256 · walterhiggins/ScriptCraft · GitHub
Skip to content

events.playerInteract not firing when right clicking air #256

Closed

Description

@frash23

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.

Activity

  1. frash23 commented on Jul 1, 2015

    @frash23
    Author

    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.

  2. added 2 commits that reference this issue on Mar 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions