Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/main/java/ch/njol/skript/lang/util/SectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,29 @@ private SectionUtils() { }
*/
@SuppressWarnings("JavadocReference")
public static @Nullable Trigger loadLinkedCode(String name, BiFunction<Runnable, Runnable, Trigger> triggerSupplier) {
return loadLinkedCode(name, false, triggerSupplier);
}

/**
* A variant of {@link #loadLinkedCode(String, BiFunction)} that permits delays within the loaded trigger.
* Type hints are still shared with the outer section, but delays inside the body do not cause a parse-time error.
* The caller is responsible for correctly handling any delays encountered while executing the returned trigger
* (e.g. by scheduling execution asynchronously or on a separate tick).
* @see #loadLinkedCode(String, BiFunction)
* @param name The name of the section being loaded.
* @param triggerSupplier A function to load code using a trigger. See {@link #loadLinkedCode(String, BiFunction)}
* for details on the expected behavior of this supplier.
* @return The result of {@code triggerSupplier}, or null if some issue occurred.
*/
public static @Nullable Trigger loadDelayableLinkedCode(String name, BiFunction<Runnable, Runnable, Trigger> triggerSupplier) {
return loadLinkedCode(name, true, triggerSupplier);
}

private static @Nullable Trigger loadLinkedCode(String name, boolean allowDelays,
BiFunction<Runnable, Runnable, Trigger> triggerSupplier) {
AtomicBoolean delayed = new AtomicBoolean(false);
AtomicReference<Backup> hintBackup = new AtomicReference<>();
// Copy hints and ensure no delays
// Copy hints and record whether the body was delayed
Runnable beforeLoading = () -> ParserInstance.get().getHintManager().enterScope(false);
Runnable afterLoading = () -> {
ParserInstance parser = ParserInstance.get();
Expand All @@ -60,7 +80,7 @@ private SectionUtils() { }

Trigger trigger = triggerSupplier.apply(beforeLoading, afterLoading);

if (delayed.get()) {
if (!allowDelays && delayed.get()) {
Skript.error("Delays can't be used within a '" + name + "' section");
return null;
}
Expand Down