Developer API: Making an Ability Trigger: Advanced - BoBoBalloon/InnovativeItemsDOCS GitHub Wiki

Intro

This section goes over making a more advanced type of ability trigger, this tutorial assumes that you have already read the first ability trigger tutorial. If you have not read the keyword tutorial, please go back and read it.

Manually Registering Events

As explained in the first tutorial, bukkit events are automatically registered by the api so you don't have to. While this feature is convenient, it might get in the way when you intend to make more complex triggers. The api currently only supports one bukkit event per ability trigger, in some cases this might not be enough and it might get in your way. To combat this, you can use the ManuallyRegister annotation on your ability trigger's class header to inform the api that you would not like it to register any bukkit events for you.

Keep in mind that because of this, the api will never call the fromEvent() or trigger() methods. So you can simply throw errors and or return null respectively.

@ManuallyRegister
public class TestTrigger extends AbilityTrigger<Event, RuntimeContext> { //in this case, both the generic types are meaningless so you can make them whatever you want, you still need a valid identifier and regex though.
    public TestTrigger() {
        super("test", null, Event.class, RuntimeContext.class, (event, player) -> Collections.emptyList(), null);
    }

    @Override
    @NotNull
    public Player fromEvent(@NotNull Event event) {
        throw new UnsupportedOperationException("Congrats, you managed to execute an event that wasn't even registered! Yikes...");
    }

    @Nullable
    @Override
    public RuntimeContext trigger(@NotNull Event event, @NotNull CustomItem item, @NotNull Ability ability) {
        return null;
    }
}

Ability Management

There are some cases when developing an ability trigger, that you don't even need events at all, whether it be because it is simply inefficient due to the sheer amount of times the method is being invoked. Because of this the ability trigger class has a method with a blank implementation that you are free to override if you would like (completely optional). This method will be called every time a new ability is initialized with your ability trigger.

The method is called init and can be overridden like so:

@ManuallyRegister
public class TestTrigger extends AbilityTrigger<Event, RuntimeContext> { //in this case, both the generic types are meaningless so you can make them whatever you want, you still need a valid identifier and regex though.
    public TestTrigger() {
        super("test", null, Event.class, RuntimeContext.class, (event, player) -> Collections.emptyList(), null);
    }

    @Override
    public void init(@NotNull Ability ability) {
        Bukkit.brodcastMessage("Ability with the identifier " + ability.getIdentifier() + " was registered with the ability trigger of test!");
        //you can use the ability.getProvidedTriggerIdentifier() method to parse regular expressions (if you had info in the name like the timer trigger that you need to parse)
    }

    @Override
    @NotNull
    public Player fromEvent(@NotNull Event event) {
        throw new UnsupportedOperationException("Congrats, you managed to execute an event that wasn't even registered! Yikes...");
    }

    @Nullable
    @Override
    public RuntimeContext trigger(@NotNull Event event, @NotNull CustomItem item, @NotNull Ability ability) {
        return null;
    }
}