-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
297 additions
and
31 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/info/itsthesky/disky/api/skript/AsyncEffectSection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package info.itsthesky.disky.api.skript; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.effects.Delay; | ||
import ch.njol.skript.lang.EffectSection; | ||
import ch.njol.skript.lang.Trigger; | ||
import ch.njol.skript.lang.TriggerItem; | ||
import ch.njol.skript.timings.SkriptTimings; | ||
import ch.njol.skript.variables.Variables; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Represents an effect section that runs asynchronously. | ||
* Made by Sky, inspired by Skript's {@link ch.njol.skript.util.AsyncEffect} & {@link ch.njol.skript.lang.EffectSection} | ||
*/ | ||
public abstract class AsyncEffectSection extends EffectSection { | ||
|
||
@Override | ||
@Nullable | ||
protected TriggerItem walk(@NotNull Event e) { | ||
debug(e, true); | ||
|
||
Delay.addDelayedEvent(e); // Mark this event as delayed | ||
Object localVars = Variables.removeLocals(e); // Back up local variables | ||
|
||
if (!Skript.getInstance().isEnabled()) // See https://github.com/SkriptLang/Skript/issues/3702 | ||
return null; | ||
|
||
Bukkit.getScheduler().runTaskAsynchronously(Skript.getInstance(), () -> { | ||
// Re-set local variables | ||
if (localVars != null) | ||
Variables.setLocalVariables(e, localVars); | ||
|
||
execute(e); // Execute this effect | ||
|
||
if (getNext() != null) { | ||
Bukkit.getScheduler().runTask(Skript.getInstance(), () -> { // Walk to next item synchronously | ||
Object timing = null; | ||
if (SkriptTimings.enabled()) { // getTrigger call is not free, do it only if we must | ||
Trigger trigger = getTrigger(); | ||
if (trigger != null) { | ||
timing = SkriptTimings.start(trigger.getDebugLabel()); | ||
} | ||
} | ||
|
||
TriggerItem.walk(getNext(), e); | ||
|
||
Variables.removeLocals(e); // Clean up local vars, we may be exiting now | ||
|
||
SkriptTimings.stop(timing); // Stop timing if it was even started | ||
}); | ||
} else { | ||
Variables.removeLocals(e); | ||
} | ||
}); | ||
return null; | ||
} | ||
|
||
protected abstract void execute(Event e); | ||
} |
103 changes: 103 additions & 0 deletions
103
src/main/java/info/itsthesky/disky/api/skript/BetterExpressionEntryData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package info.itsthesky.disky.api.skript; | ||
|
||
import ch.njol.skript.ScriptLoader; | ||
import ch.njol.skript.config.Node; | ||
import ch.njol.skript.config.SectionNode; | ||
import ch.njol.skript.config.SimpleNode; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.ParseContext; | ||
import ch.njol.skript.lang.SkriptParser; | ||
import ch.njol.skript.localization.Message; | ||
import ch.njol.skript.log.ErrorQuality; | ||
import ch.njol.skript.log.ParseLogHandler; | ||
import info.itsthesky.disky.DiSky; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.entry.EntryData; | ||
import org.skriptlang.skript.lang.entry.EntryValidator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* An {@link org.skriptlang.skript.lang.entry.util.ExpressionEntryData} that allows multiple expressions to be defined in a single entry. | ||
* It also let the developer write the different expressions in a section. | ||
* @param <T> the return type of the expressions | ||
* @author Sky | ||
*/ | ||
public class BetterExpressionEntryData<T> extends EntryData<List<Expression<? extends T>>> { | ||
|
||
private static final Message M_IS = new Message("is"); | ||
private final Class<T> returnType; | ||
private final int flags; | ||
|
||
public BetterExpressionEntryData(String key, @Nullable List<Expression<? extends T>> defaultValue, boolean optional, | ||
Class<T> returnType, int flags) { | ||
super(key, defaultValue, optional); | ||
|
||
this.returnType = returnType; | ||
this.flags = flags; | ||
} | ||
|
||
public BetterExpressionEntryData(String key, @Nullable List<Expression<? extends T>> defaultValue, | ||
boolean optional, Class<T> returnType) { | ||
this(key, defaultValue, optional, returnType, SkriptParser.ALL_FLAGS); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public List<Expression<? extends T>> getValue(@NotNull Node node) { | ||
if (node instanceof final SectionNode sectionNode) { | ||
final List<Expression<? extends T>> expressions = new ArrayList<>(); | ||
for (Node subNode : sectionNode) { | ||
final String value = subNode.getKey(); | ||
final Expression<? extends T> expression = parseExpression(value); | ||
if (expression != null) | ||
expressions.add(expression); | ||
} | ||
return expressions; | ||
} else if (node instanceof final SimpleNode simpleNode) { | ||
final String key = simpleNode.getKey(); | ||
if (key == null) | ||
return null; | ||
|
||
final String value = ScriptLoader.replaceOptions(key).substring(getKey().length() + EntryValidator.EntryValidatorBuilder.DEFAULT_ENTRY_SEPARATOR.length()); | ||
final Expression<? extends T> expression = parseExpression(value); | ||
if (expression != null) | ||
return List.of(expression); | ||
|
||
return null; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private Expression<? extends T> parseExpression(String value) { | ||
Expression<? extends T> expression; | ||
try (ParseLogHandler log = new ParseLogHandler().start()) { | ||
expression = new SkriptParser(value, flags, ParseContext.DEFAULT) | ||
.parseExpression(returnType); | ||
if (expression == null) // print an error if it couldn't parse | ||
log.printError( | ||
"'" + value + "' " + M_IS + " " + SkriptParser.notOfType(returnType), | ||
ErrorQuality.NOT_AN_EXPRESSION | ||
); | ||
} | ||
return expression; | ||
} | ||
|
||
@Override | ||
public boolean canCreateWith(@NotNull Node node) { | ||
if (node instanceof SectionNode) { | ||
return true; | ||
} else if (node instanceof SimpleNode) { | ||
String key = node.getKey(); | ||
if (key == null) | ||
return false; | ||
key = ScriptLoader.replaceOptions(key); | ||
return key.startsWith(getKey() + EntryValidator.EntryValidatorBuilder.DEFAULT_ENTRY_SEPARATOR); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.