_Tutorial Coding Triggers - Eniripsa96/SkillAPI GitHub Wiki

Step 1 - Extend CustomTrigger

To begin, you need to make a java class that implements CustomTrigger. This is the interface provided by SkillAPI and defines the needed methods to work as a trigger.

public class MyTrigger implements CustomTrigger

Step 2 - Implement Methods

Now that you're implementing the interface, you need to add all the required functions. Your IDE may auto-generate these for you. The methods needed are:

/**
 * @return unique key for your component (what is used in skill .yml files)
 */
String getKey();

/**
 * @return class of the event related to the trigger
 */
Class<E> getEvent();

/**
 * @return A description for your trigger that's shown in the editor
 */
String getDescription();

/**
 * Defines all the user-configurable options in the editor. You can use
 * any of these option types:
 *   - NUMBER > a numeric value with options for base and per level increments
 *   - STRING > a freeform bit of text the user can enter
 *   - DROPDOWN > allows the user to select one item from a predefined list of options
 *   - LIST > allows the user to select one or more items from a predefined list of options 
 * 
 * More details can be seen in the JavaDocs <a href="http://eniripsa96.github.io/SkillAPI/javadoc/?com.sucy.skill.dynamic.custom:EditorOption">here</a>
 *
 * @return settings to show in the editor
 */
List<EditorOption> getOptions();

/**
 * Uses information from the event and settings to determine whether or not the
 * trigger is applicable to the given occurrence. For example, a trigger handling
 * when something is damaged may only return true if the amount of damage dealt
 * is higher than a certain threshold which could come from the skill settings.
 *
 * @param event - the event your trigger handles
 * @param level - the level of the owning skill
 * @param settings - settings defined by the owning skill
 * @return true if the skill should activate, false otherwise
 */
boolean shouldTrigger(final E event, final int level, final Settings settings);

/**
 * Reads data from the event and provides values to the caster's value data. This can be used within
 * skills for more flexible effects. An example of this in base triggers is the Launch trigger providing
 * the speed a projectile was launched so mechanics can replace it with equally-fast projectiles.
 *
 * @param event event details
 * @param data caster's value data to populate
 */
void setValues(final E event, final Map<String, Object> data);

/**
 * Determines who would be considered the caster based on the event
 *
 * @param event event details
 * @return the one to apply the trigger for
 */
LivingEntity getCaster(final E event);

/**
 * Fetches who would be the target based on the event
 *
 * @param event event details
 * @param settings skill settings
 * @return the one being affected by the trigger (initial target)
 */
LivingEntity getTarget(final E event, final Settings settings);

Step 3 - Include the trigger in your SkillPlugin method

See Coding Plugins

Step 4 - Test

Start up your server after finishing the above and your trigger should be loaded into SkillAPI. Check the file at plugins/SkillAPI/tool-config.json and your trigger along with its settings should be in the file. Drop that JSON file into the dynamic editor and your trigger should now be an option in the tool. Make a skill with your trigger and try it out!

Example

Here is a recreation of the Skill Damage trigger with all of its options:

import com.google.common.collect.ImmutableList;
import com.sucy.skill.api.Settings;
import com.sucy.skill.api.event.SkillDamageEvent;
import com.sucy.skill.dynamic.ComponentType;
import com.sucy.skill.dynamic.custom.CustomTrigger;
import com.sucy.skill.dynamic.custom.EditorOption;
import org.bukkit.entity.LivingEntity;

import java.util.List;
import java.util.Map;

public class CustomSkillDealtTrigger implements CustomTrigger<SkillDamageEvent> {

    @Override
    public String getKey() {
        return "SKILL_DAMAGE";
    }

    @Override
    public ComponentType getType() {
        return ComponentType.TRIGGER;
    }

    @Override
    public String getDescription() {
        return "Applies skill effects when a player deals damage with a skill.";
    }

    @Override
    public List<EditorOption> getOptions() {
        return ImmutableList.of(
                EditorOption.dropdown(
                        "target",
                        "Target Caster",
                        "True makes children target the caster. False makes children target the entity.",
                        ImmutableList.of("True", "False")),
                EditorOption.number(
                        "dmg-min",
                        "Min Damage",
                        "The minimum damage that needs to be dealt",
                        0,
                        0),
                EditorOption.number(
                        "dmg-max",
                        "Max Damage",
                        "The maximum damage that can be dealt",
                        999,
                        0),
                EditorOption.text(
                        "category", 
                        "Category", 
                        "The type of skill damage to apply for. Leave this empty to apply to all skill damage",
                        "default")
        );
    }

    @Override
    public Class<SkillDamageEvent> getEvent() {
        return SkillDamageEvent.class;
    }

    @Override
    public boolean shouldTrigger(final SkillDamageEvent event, final int level, final Settings settings) {
        final double min = settings.getDouble("dmg-min", 0);
        final double max = settings.getDouble("dmg-max", 999);
        final List<String> types = settings.getStringList("category");
        final boolean empty = types.isEmpty() || types.get(0).isEmpty();

        return event.getDamage() >= min && event.getDamage() <= max &&
                (empty || types.contains(event.getClassification()));
    }

    @Override
    public LivingEntity getCaster(final SkillDamageEvent event) {
        return event.getDamager();
    }

    @Override
    public LivingEntity getTarget(final SkillDamageEvent event, final Settings settings) {
        final boolean isUsingTarget = settings.getString("target", "true").equalsIgnoreCase("false");

        return isUsingTarget ? event.getTarget() : event.getDamager();
    }

    @Override
    public void setValues(final SkillDamageEvent event, final Map<String, Object> data) {
        data.put("api-dealt", event.getDamage());
    }
}
⚠️ **GitHub.com Fallback** ⚠️