Skip to content

Commit

Permalink
✨ Added reply section
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Oct 27, 2024
1 parent 3f70d7f commit 16149a7
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 31 deletions.
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);
}
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;
}
}
7 changes: 3 additions & 4 deletions src/main/java/info/itsthesky/disky/core/SkriptUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.log.*;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Color;
import ch.njol.skript.util.ColorRGB;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Getter;
import ch.njol.skript.util.*;
import ch.njol.util.Kleenean;
import info.itsthesky.disky.DiSky;
import info.itsthesky.disky.api.ReflectionUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ public ComponentRow() {
buttons = new ArrayList<>();
}

public ComponentRow(List<Object> components) {
this();

for (Object component : components) {
if (component instanceof final SelectMenu menu)
setMenu(menu);
else if (component instanceof final Button button)
add(button);
else if (component instanceof final TextInput input)
setInput(input);

else if (component instanceof final SelectMenu.Builder<?, ?> menuBuilder)
setMenu(menuBuilder.build());
else if (component instanceof final TextInput.Builder inputBuilder)
setInput(inputBuilder.build());
}
}

public TextInput getInput() {
return input;
}
Expand Down Expand Up @@ -75,4 +93,8 @@ else if (component instanceof TextInput)
setInput((TextInput) component);
}
}

public boolean isEmpty() {
return menu == null && input == null && buttons.isEmpty();
}
}
Loading

0 comments on commit 16149a7

Please sign in to comment.