Creating a Condition

Creating a Condition

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

if example is true:
broadcast "this will be called"
else:
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.


package me.limeglass.addon.elements.conditions;

import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

public class CondEntityInsideVehicle extends Condition {

static {
Skript.registerCondition(CondEntityInsideVehicle.class, "%entity% (1¦is|2¦is(n't| not)) inside [a] vehicle");
}

Expression<Entity> entity;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
this.entity = (Expression<Entity>) expressions[0];
return true;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "Entity inside vehicle " + entity.toString(event, debug);
}

@Override
public boolean check(Event arg0) {
//explaining still
return false;
}

}


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.


package me.limeglass.addon.elements.conditions;

import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

public class CondEntityInsideVehicle extends Condition {

static {
Skript.registerCondition(CondEntityInsideVehicle.class, "%entity% (1¦is|2¦is(n't| not)) inside [a] vehicle");
}

Expression<Entity> entity;

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
this.entity = (Expression<Entity>) expressions[0];
//The parser.mark grabs the 1 or the 2 from the pattern.
setNegated(parser.mark == 1);
return true;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "Entity inside vehicle " + entity.toString(event, debug);
}

@Override
public boolean check(Event event) {
//Grabs the entity from the expression.
Entity e = entity.getSingle(event);
//If the entity is null, return false based on the user's input from the (1¦is|2¦is(n't| not))
//Meaning if the user input "is" negated will return true.
//if the user input "isn't", it returns false, makes sense?
if (e == null) return isNegated();
return e.isInsideVehicle() ? isNegated() : !isNegated();
}

}


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