Created
November 20, 2015 00:59
-
-
Save Deamon5550/4dea99d6d21c822e0244 to your computer and use it in GitHub Desktop.
Event Filtering Idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package filter; | |
import java.util.Collection; | |
import filter.CauseFilters.All; | |
import filter.CauseFilters.First; | |
import filter.DataFilters.HasDouble; | |
import filter.DataFilters.HasInt; | |
import filter.SubtypeFilters.Exclude; | |
public class Example { | |
//The Current methods | |
@Listener | |
public void onSubEvent(SubEvent event) { | |
// Fairly typical pattern | |
Optional<Player> player = event.getCause().first(Player.class); | |
if(player.isPresent()) { | |
//do something | |
} | |
} | |
//========================================================================== | |
//The proposed methods | |
@Listener | |
public void onSubEvent2(SubEvent event, @First Player player) { | |
//will only be called if there is a player | |
} | |
@Listener | |
public void onSubEvent2(SubEvent event, @First(name=NamedCause.NOTIFIER) Player player) { | |
//will only be called if there is a player notifier present | |
} | |
@Listener | |
public void onSubEvent3(SubEvent event, @First @HasDouble(key="health", value=5.0) Player player) { | |
//will only be called if there is a player and they have 5 health | |
// (at least thats the idea, still working on how to make data based filtering sane) | |
} | |
@Listener | |
public void onSubEvent3(SubEvent event, @First @HasString(key="displayName", value="gabizou") Player player) { | |
//will only be called if its gabizou | |
} | |
@Listener | |
public void onSubEvent3(SubEvent event, @First @Supports(HealthData.class) Entity entity) { | |
//will only be called if there is an entity and it has health | |
// (sure you could just use '@First Living entity' but its an example damn it) | |
} | |
@Listener | |
public void onSubEvent3(SubEvent event, @First @Has(ChargedData.class) Creeper entity) { | |
//will only be called if there is a creeper and its charged | |
// (this will be a bit of special handling to check the boolean state of the data as well as its existance) | |
} | |
@Listener | |
public void onSubEvent2(SubEvent event, @All Player[] players) { | |
//do something with all players | |
//this will not be called if there are no players | |
} | |
@Listener | |
public void onSubEvent2(SubEvent event, @All(allowEmpty=true) Collection<Player> players) { | |
//do something with all players | |
//this will be called regardless of if there are any players | |
} | |
@Listener | |
public void onSubSub2And3(@Exclude(SubSubEvent1.class) SubEvent event) { | |
// runs for SubSubEvent2 and SubSubEvent3 but not SubSubEvent1 | |
} | |
@Listener | |
public void onSubSub2And3(@Include({SubSubEvent1.class, SubSubEvent2.class}) SubEvent event) { | |
// runs for SubSubEvent1 and SubSubEvent2 but not SubSubEvent3 | |
} | |
@Listener | |
public void onCancelled(@IsCancelled SubEvent event) { | |
// will only run if the event has been cancelled by a previous listener | |
} | |
static interface Event { } | |
static interface SubEvent extends Event { } | |
static interface SubSubEvent1 extends SubEvent { } | |
static interface SubSubEvent2 extends SubEvent { } | |
static interface SubSubEvent3 extends SubEvent { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment