1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Creating a Condition

Aug 8, 2018
Creating a Condition
  • Creating a Condition

    Conditions are what allow a trigger section to be called if it returns true.

    Code (Skript):
    1. if example is true:
    2.     broadcast "this will be called"
    3. else:
    4.     broadcast "this won't be called unless false"
    Now there are two different types of condition classes, a normal Condition and a PropertyCondition, a property condition is similar to a property expression where it takes in a single type and tests if that type matches something, example: checking if an entity is inside a vehicle.

    So lets start with normal Conditions, lets make a condition to test if an entity is inside a vehicle.

    Code (Java):
    1.  
    2. package me.limeglass.addon.elements.conditions;
    3.  
    4. import org.bukkit.entity.Entity;
    5. import org.bukkit.event.Event;
    6. import org.eclipse.jdt.annotation.Nullable;
    7.  
    8. import ch.njol.Skript.Skript;
    9. import ch.njol.Skript.lang.Condition;
    10. import ch.njol.Skript.lang.Expression;
    11. import ch.njol.Skript.lang.SkriptParser.ParseResult;
    12. import ch.njol.util.Kleenean;
    13.  
    14. public class CondEntityInsideVehicle extends Condition {
    15.  
    16.    static {
    17.        Skript.registerCondition(CondEntityInsideVehicle.class, "%entity% (1¦is|2¦is(n't| not)) inside [a] vehicle");
    18.    }
    19.  
    20.    Expression<Entity> entity;
    21.  
    22.    @SuppressWarnings("unchecked")
    23.    @Override
    24.    public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
    25.        this.entity = (Expression<Entity>) expressions[0];
    26.        return true;
    27.    }
    28.  
    29.    @Override
    30.    public String toString(@Nullable Event event, boolean debug) {
    31.        return "Entity inside vehicle " + entity.toString(event, debug);
    32.    }
    33.  
    34.    @Override
    35.    public boolean check(Event arg0) {
    36.        //explaining still
    37.        return false;
    38.    }
    39.  
    40. }
    41.  
    This is similar to expressions, so if you don't understand what the "init" method is, go back and read the expression section.

    So now we have a simple condition, lets add our check, the check is the main beef of the condition.


    Code (Java):
    1. package me.limeglass.addon.elements.conditions;
    2.  
    3. import org.bukkit.entity.Entity;
    4. import org.bukkit.event.Event;
    5. import org.eclipse.jdt.annotation.Nullable;
    6.  
    7. import ch.njol.Skript.Skript;
    8. import ch.njol.Skript.lang.Condition;
    9. import ch.njol.Skript.lang.Expression;
    10. import ch.njol.Skript.lang.SkriptParser.ParseResult;
    11. import ch.njol.util.Kleenean;
    12.  
    13. public class CondEntityInsideVehicle extends Condition {
    14.  
    15.     static {
    16.         Skript.registerCondition(CondEntityInsideVehicle.class, "%entity% (1¦is|2¦is(n't| not)) inside [a] vehicle");
    17.     }
    18.  
    19.     Expression<Entity> entity;
    20.  
    21.     @SuppressWarnings("unchecked")
    22.     @Override
    23.     public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
    24.         this.entity = (Expression<Entity>) expressions[0];
    25.         //The parser.mark grabs the 1 or the 2 from the pattern.
    26.         setNegated(parser.mark == 1);
    27.         return true;
    28.     }
    29.  
    30.     @Override
    31.     public String toString(@Nullable Event event, boolean debug) {
    32.         return "Entity inside vehicle " + entity.toString(event, debug);
    33.     }
    34.  
    35.     @Override
    36.     public boolean check(Event event) {
    37.         //Grabs the entity from the expression.
    38.         Entity e = entity.getSingle(event);
    39.         //If the entity is null, return false based on the user's input from the (1¦is|2¦is(n't| not))
    40.         //Meaning if the user input "is" negated will return true.
    41.         //if the user input "isn't", it returns false, makes sense?
    42.         if (e == null) return isNegated();
    43.         return e.isInsideVehicle() ? isNegated() : !isNegated();
    44.     }
    45.  
    46. }
    The parser.mark grabs the 1 or the 2 from the pattern.

    And that's it, this condition is now completed, so now lets go over what the isNegated is, since I see you looking at why I added that. Njol added a system that allows the developer to include a settable boolean basically, rather than creating a new boolean per class if that makes sense. The setNegated() is mainly used for the users input. If the user used "is" it will set negated to true and if the user used "isn't" it will set the negated to false, which then can be allowed in the check method to return appropriately.

    I don't feel like going over PropertyCondition is worthy of this tutorial, but some say it's proper standards, it's not really the greatest expansion because there could be different condition english statements based off what is being tested, so it's rarely used in certain cases. You can view it here yourself and understand it from there https://github.com/SkriptLang/Skript/blob/master/src/main/java/ch/njol/skript/conditions/base/PropertyCondition.java


    Addon tutorial
    Back | Next