Skip to content
Permalink
Browse files

Merge pull request #553 from PseudoKnight/master

Ignore fake player entities for events
  • Loading branch information
PseudoKnight committed Mar 1, 2020
2 parents 533b5ab + 75f9234 commit ce840aadf0f885fb344711b99a884db958e3813c
@@ -39,6 +39,17 @@ public void cancel(BindableEvent e, boolean state) {
}
}

@Override
public boolean shouldFire(BindableEvent event) {
Object e = event._GetObject();
if(e instanceof PlayerEvent) {
return !((PlayerEvent) e).getPlayer().hasMetadata("NPC");
} else if(e instanceof EntityEvent && ((EntityEvent) e).getEntity() instanceof Player) {
return !((EntityEvent) e).getEntity().hasMetadata("NPC");
}
return true;
}

@Override
public Map<String, Mixed> evaluate_helper(BindableEvent event) throws EventException {
Map<String, Mixed> map = new HashMap<>();
@@ -49,6 +49,17 @@ public final void setAbstractEventMixin(EventMixinInterface mixin) {
this.mixin = mixin;
}

/**
* This function should return true if the event code should be run, based on implementation specific conditions
* for the BindableEvent.
*
* @param e The bindable event itself
* @return {@code true} if the event code should be run
*/
public boolean shouldFire(BindableEvent e) {
return this.mixin.shouldFire(e);
}

/**
* If the event needs to run special code when a player binds the event, it can be done here. By default, an
* UnsupportedOperationException is thrown, but is caught and ignored.
@@ -27,6 +27,17 @@
*/
public boolean isCancellable(BindableEvent o);

/**
* Return true if the event should be fired.
* This occurs before prefiltering to allow the implementation to have a say.
*
* @param e The bindable event itself
* @return {@code true} if the event should fire
*/
default boolean shouldFire(BindableEvent e) {
return true;
}

/**
* This constructs the common elements in an event.
*
@@ -290,7 +290,7 @@ public static void TriggerListener(Driver type, String eventName, BindableEvent
Event driver = EventList.getEvent(type, eventName);
if(driver == null) {
throw ConfigRuntimeException.CreateUncatchableException("Tried to fire an unknown event: " + eventName, Target.UNKNOWN);
} else {
} else if(!(driver instanceof AbstractEvent) || ((AbstractEvent) driver).shouldFire(e)) {
FireListeners(GetMatchingEvents(type, eventName, e, driver), driver, e);
}
}

0 comments on commit ce840aa

Please sign in to comment.
You can’t perform that action at this time.