SpectatorPlus

SpectatorPlus

34.1k Downloads

Adding new event handlers without breaking on older Minecraft versions

pgmann opened this issue ยท 3 comments

commented

It would be good to make use of this new event from Minecraft 1.9.4, but it would require breaking changes to SpectatorPlus - i.e. we could only support 1.9 and above.

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerSwitchHands(PlayerSwapHandsItemEvent ev) {
    ev.setCancelled(true); // of course, a check would come first
}

It would be good to have a kind of API in zLib for 'soft-using' an event if it's available. Do you think this is possible, for example using reflection? (Or maybe there's a way I can do this easily I haven't thought of!)

commented

Note, perhaps a Custom Event could be used in some way.

commented

I got you covered.

@FutureEventHandler (name = "player.PlayerSwapHandItemsEvent", priority = EventPriority.HIGHEST)
public void onPlayerSwapHand(WrappedEvent ev)
{
    if (SP.getPlayerData(((PlayerEvent) ev.getEvent()).getPlayer()).isSpectating()) // or whatever the condition is
        // Warning: throws an UnsupportedOperationException if the event (retrieved at runtime) is not cancellable!
        ev.setCancelled(true);

    // Reflective operations can also be made via ev.getEvent().
}

Javadoc of the annotation, FutureEvents class used to register them (see the registerFutureEvents method documentation) and WrappedEvent.

commented

That's great, it works! Thanks ;)