Developer API: Making a Condition - BoBoBalloon/InnovativeItemsDOCS GitHub Wiki
This section goes over making a custom condition, this tutorial assumes that you have already read the keyword tutorial. If you have not read the keyword tutorial, please go back and read it.
For this example, I will demonstrate below a condition that checks the gamemode of the target.
To start make a new class that extends the Condition class:
public class IsGamemodeEqualToCondition extends Condition {
}
Implement all methods and build a blank constructor:
public class IsGamemodeEqualToCondition extends Condition {
public IsGamemodeEqualToCondition() {
super();
}
@Override
protected Boolean call(ImmutableList<Object> arguments, RuntimeContext context) {
}
@Override
public boolean isAsync() {
}
}
The constructor of a condition is the exact same as a keyword, if you forget the constructor of a keyword please refer back to the keywords section.
I will complete the constructor below:
public IsGamemodeEqualToCondition() {
super("isgamemodeequalto",
new ExpectedEnum<>(Gamemode.class, "gamemode"));
}
The call() method of a condition is the exact same as the calling() method of a keyword, the only difference is that the call() method returns a boolean while the calling() method returns void. If you forget the calling() method of a keyword please refer back to the keywords section.
I will complete the call() method below:
@Override
protected Boolean call(ImmutableList<Object> arguments, RuntimeContext context) {
Gamemode gamemode = (Gamemode) arguments.get(0);
return context.getPlayer().getGamemode() == gamemode;
}
The condition isAsync() method is the exact same as the keyword isAsync() method. If you forget the isAsync() method of a keyword please refer back to the keywords section.
I will complete the isAsync() method below:
@Override
public boolean isAsync() {
return true;
}
Now we have talked about all the parts of the condition object you must implement, let's take a look at the whole thing together now:
public class IsGamemodeEqualToCondition extends Condition {
public IsGamemodeEqualToCondition() {
super("isgamemodeequalto",
new ExpectedEnum<>(Gamemode.class, "gamemode"));
}
@Override
protected Boolean call(ImmutableList<Object> arguments, RuntimeContext context) {
Gamemode gamemode = (Gamemode) arguments.get(0);
return context.getPlayer().getGamemode() == gamemode;
}
@Override
public boolean isAsync() {
return true;
}
}
Registering the condition is also almost the same as registering a keyword. The only difference is that instead of using the registerKeywords() method, you must use the registerConditions() method. If you forget how to register a keyword please refer back to the keywords section. Just like keywords, this method must be called on the onLoad method in your main class!
@Override
public void onLoad() {
FunctionManager functionManager = InnovativeItemsAPI.getFunctionManager();
functionManager.registerConditions(new IsGamemodeEqualToCondition());
}