Skip to content

Tasks (Prototype 1) #7462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: dev/feature
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.skriptlang.skript.lang.converter.Converter;
import org.skriptlang.skript.lang.converter.Converters;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.util.Cancellable;

public class DefaultConverters {

Expand Down Expand Up @@ -257,6 +258,20 @@ public void setAmount(Number amount) {
return null;
});

// Cancellable (task to bukkit) - for checking whether things are cancelled
Converters.registerConverter(Cancellable.class, org.bukkit.event.Cancellable.class, cancellable -> new org.bukkit.event.Cancellable() {
@Override
public boolean isCancelled() {
return cancellable.isCancelled();
}

@Override
public void setCancelled(boolean cancel) {
if (cancel)
cancellable.cancel();
}
});

// Enchantment - EnchantmentType
Converters.registerConverter(Enchantment.class, EnchantmentType.class, e -> new EnchantmentType(e, -1));

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import ch.njol.skript.util.visual.VisualEffect;
import ch.njol.skript.util.visual.VisualEffects;
import ch.njol.yggdrasil.Fields;
import org.bukkit.event.Cancellable;
import org.skriptlang.skript.lang.util.SkriptQueue;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.util.Completable;
import org.skriptlang.skript.util.Executable;
import org.skriptlang.skript.util.Task;

import java.io.File;
import java.io.StreamCorruptedException;
Expand Down Expand Up @@ -846,6 +849,29 @@ public String toVariableNameString(final Script script) {
.examples("run {_function} with arguments 1 and true")
.since("2.10"));

Classes.registerClass(new ClassInfo<>(Completable.class, "completable")
.user("completables?")
.name("Completable")
.description("Something that can be completed (e.g. a task).")
.examples("complete the current task", "{task} is completed")
.since("INSERT VERSION"));

Classes.registerClass(new ClassInfo<>(Cancellable.class, "cancellable")
.user("cancellables?")
.name("Cancellable")
.description("Something that can be cancelled: an event, a task, a timer.")
.examples("cancel {_task}")
.since("INSERT VERSION"));

Classes.registerClass(new ClassInfo<>(Task.class, "task")
.user("tasks?")
.name("Task")
.description("A task is an executable section of code. Other triggers can wait for its completion.")
.examples("run {_task}")
.since("INSERT VERSION")
.serializer(new YggdrasilSerializer<>())
);

Classes.registerClass(new ClassInfo<>(DynamicFunctionReference.class, "function")
.user("functions?")
.name("Function")
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondActive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ch.njol.skript.conditions;

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.skript.registrations.Feature;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.util.Completable;
import org.skriptlang.skript.util.Task;

// todo doc
public class CondActive extends Condition {

static {
Skript.registerCondition(CondActive.class,
"%completable% is (running|active|:incomplete)",
"%completable% (is not|isn't) (running|active|:incomplete)",
"%completable% is inactive"
);
}

private Expression<Completable> completable;
private boolean incomplete;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
this.setNegated(matchedPattern > 0);
if (matchedPattern > 1 && !this.getParser().hasExperiment(Feature.TASKS))
return false;
//noinspection unchecked
this.completable = (Expression<Completable>) exprs[0];
this.incomplete = parseResult.hasTag("incomplete");
return true;
}

@Override
public boolean check(Event event) {
Completable thing = completable.getSingle(event);
if (thing == null)
return false;
// If it's a task, running/active also means not cancelled
if (thing instanceof Task task && !incomplete)
return (task.isCancelled() || thing.isComplete()) ^ isNegated();
return thing.isComplete() ^ isNegated();
}

@Override
public String toString(@Nullable Event event, boolean debug) {
if (incomplete)
return completable.toString(event, debug) + (isNegated() ? " is not incomplete" : " is incomplete");
return completable.toString(event, debug) + (isNegated() ? " is not active" : " is active");
}

}
39 changes: 28 additions & 11 deletions src/main/java/ch/njol/skript/conditions/CondCancelled.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,57 @@
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.registrations.Feature;
import ch.njol.util.Kleenean;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Event Cancelled")
@Description("Checks whether or not the event is cancelled.")
@Name("Is Cancelled")
@Description("Checks whether or not the event or a task is cancelled.")
@Examples({"on click:",
"\tif event is cancelled:",
"\t\tbroadcast \"no clicks allowed!\""
})
@Since("2.2-dev36")
@Since("2.2-dev36, INSERT VERSION (tasks: experimental)")
public class CondCancelled extends Condition {

static {
Skript.registerCondition(CondCancelled.class,
"[the] event is cancel[l]ed",
"[the] event (is not|isn't) cancel[l]ed"
);
"[the] event is cancel[l]ed",
"[the] event (is not|isn't) cancel[l]ed",
"%cancellable% is cancel[l]ed",
"%cancellable% (is not|isn't) cancel[l]ed"
);
}


private @Nullable Expression<Cancellable> cancellable;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setNegated(matchedPattern == 1);
this.setNegated(matchedPattern % 2 != 0);
if (matchedPattern > 1 && !this.getParser().hasExperiment(Feature.TASKS))
return false;
if (matchedPattern > 1)
this.cancellable = (Expression<Cancellable>) exprs[0];
return true;
}

@Override
public boolean check(Event e) {
return (e instanceof Cancellable && ((Cancellable) e).isCancelled()) ^ isNegated();
public boolean check(Event event) {
if (cancellable != null) {
Cancellable single = cancellable.getSingle(event);
if (single != null)
return single.isCancelled() ^ isNegated();
return false;
}
return (event instanceof Cancellable cancellable && cancellable.isCancelled() ^ isNegated());
}

@Override
public String toString(@Nullable Event e, boolean debug) {
public String toString(@Nullable Event event, boolean debug) {
if (cancellable != null)
return cancellable.toString(event, debug) + (isNegated() ? " is not cancelled" : " is cancelled");
return isNegated() ? "event is not cancelled" : "event is cancelled";
}

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondComplete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ch.njol.skript.conditions;

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.skript.registrations.Feature;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.util.Completable;

// todo doc
public class CondComplete extends Condition {

static {
Skript.registerCondition(CondComplete.class,
"%completable% is (finished|complete[d])",
"%completable% (is not|isn't) (finished|complete[d])"
);
}

private Expression<Completable> completable;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
this.setNegated(matchedPattern == 1);
if (matchedPattern > 1 && !this.getParser().hasExperiment(Feature.TASKS))
return false;
//noinspection unchecked
this.completable = (Expression<Completable>) exprs[0];
return true;
}

@Override
public boolean check(Event event) {
Completable task = completable.getSingle(event);
if (task == null)
return false;
return task.isComplete() ^ isNegated();
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return completable.toString(event, debug) + (isNegated() ? " is not complete" : " is complete");
}

}
Loading
Loading