Overview of the Section API + Examples #4559
TheLimeGlass
started this conversation in
General
Replies: 1 comment
-
A few corrections:
import ch.njol.skript.Skript;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.lang.EffectSection;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.Section;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.TriggerSection;
import ch.njol.skript.variables.Variables;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.eclipse.jdt.annotation.Nullable;
import java.util.List;
public class SectionExample {
public static class DummyEvent extends Event {
@Override
public HandlerList getHandlers() {
//* no need to implement handlers when event will never be called
throw new IllegalStateException("DummyEvent mustn't be called");
}
}
// Please use EffSec for prefixes
public static class EffSecShade extends EffectSection {
static {
Skript.registerSection(EffSecShade.class, "open an umbrella for %players%");
}
private Expression<Player> players;
// *No need to store next TriggerItem, super classes take care of this
private Trigger trigger;
@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult,
@Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
// Check if they're using it as a section or an effect
// *alternative: sectionNode != null
if (hasSection())
// Create our trigger here and define DummyEvent as the event we're changing it too.
trigger = loadCode(sectionNode, "open umbaska for bae", DummyEvent.class);
players = (Expression<Player>) exprs[0];
return true;
}
@Override
@Nullable
protected TriggerItem walk(Event event) {
if (!players.check(event, player -> player.getName().equalsIgnoreCase("BaeFell"))) {
// Example continuing.
debug(event, false);
// *you should always call debug in the walk method,
// second param defines whether the section ran
// (though it's not important if you forget)
return getNext();
}
if (hasSection()) {
// *Copy local variables to our new event.
// Required when switching events in a section.
Object localVars = Variables.copyLocalVariables(event);
DummyEvent dummy = new DummyEvent();
Variables.setLocalVariables(dummy, localVars);
// Runs the section.
trigger.execute(dummy);
}
debug(event, true);
// I believe you can also do super.walk(event, false)
// *super.walk(event, false) works too, but getNext is easier
return getNext();
}
@Override
public String toString(@Nullable Event event, boolean debug) {
// toString should always include parameters, in most cases you don't need to check event or debug arguments
return "opening an umbaska for players " + players.toString(event, debug);
}
}
// Please use Sec for prefixes
public static class SecAlone extends Section {
static {
Skript.registerSection(SecAlone.class, "while baefell and gatt are alone");
}
// *you usually don't need to override setNext, save the actual next TriggerItem, or set a fake next TriggerItem
// it's only needed here because this section is repeating (after it is done, it should run again)
@Nullable
private TriggerItem actualNext;
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult,
SectionNode sectionNode, List<TriggerItem> triggerItems) {
loadCode(sectionNode);
super.setNext(this);
return true;
}
@Override
@Nullable
protected TriggerItem walk(Event event) {
// A fail safe is good to add onto this, like on a reload or something, but this is an example
// *that'd just for this specific example, most sections would probably only run the trigger once on walk and move on
if (Bukkit.getOnlinePlayers().stream()
.allMatch(player -> player.getName().equalsIgnoreCase("BaeFell") ||
player.getName().equalsIgnoreCase("Gatt"))) {
// Repeat the section while condition is set.
// *when you call walk, you don't have to call the debug method yourself as it is done in walk
return walk(event, true);
} else {
// If you want to add debug support, this method exists. Set your config.sk verbose to debug.
debug(event, false);
return actualNext;
}
}
@Override
public TriggerSection setNext(@Nullable TriggerItem next) {
actualNext = next;
return this;
}
@Override
public String toString(@Nullable Event event, boolean debug) {
return "while baefell and gatt are alone";
}
}
} Script usage:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Skript's own Sections for reviewing here https://github.com/SkriptLang/Skript/tree/master/src/main/java/ch/njol/skript/sections
There is Currently
Section
EffectSection
The Section class requires the section to be filled like an if statement.
The EffectSection allows you to make the syntax an effect or a section, the user doesn't need to make it a section. Example being Skript's spawn EffectSection syntax. If it's defined as an effect, it's used like it always has, but it can now be a section too.
Once you pick your required section function; in your init method, for EffectSections you need to save a Trigger instance field for the class, set it in the init using any of the
loadCode
methods provided in the Section classes. There are different usages, but you can just pass the sectionNode which is provided in the init parameters if you're not changing the event to be used in the section. Yes you can change the event, this is how in the example the spawn event can be changed over to a SpawnCreatureEvent and you can then use event-entity. This loadCode method is where you define the event you're changing it to.Then in the
walk
method, walk your trigger when you want. Then when you want to continue on with the rest of the code outside of your section, you just return the Section'ssuper.walk(e, false)
field, or in my examples, you can use the providedsetNext
method.Oh and if you want to copy the local variables into your section when you change the event, you can use
Object localVars = Variables.copyLocalVariables(e);
before you walk your trigger, and then in your triggerVariables.setLocalVariables(yourChangedEvent, localVars);
Simple examples of Sections:
Beta Was this translation helpful? Give feedback.
All reactions