Skip to content
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

Allows for multiple return types in ExpressionEntryData #6873

Merged
merged 4 commits into from
Aug 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ public class ExpressionEntryData<T> extends KeyValueEntryData<Expression<? exten

private static final Message M_IS = new Message("is");

private final Class<T> returnType;
private final Class<T>[] returnTypes;

private final int flags;

/**
* @param returnType The expected return type of the matched expression.
*/
public ExpressionEntryData(
String key, @Nullable Expression<T> defaultValue, boolean optional,
Class<T> returnType
String key, @Nullable Expression<T> defaultValue, boolean optional, Class<T> returnType
) {
this(key, defaultValue, optional, returnType, SkriptParser.ALL_FLAGS);
this(key, defaultValue, optional, SkriptParser.ALL_FLAGS, returnType);
}

/**
Expand All @@ -56,11 +55,32 @@ public ExpressionEntryData(
* javadoc for more details.
*/
public ExpressionEntryData(
String key, @Nullable Expression<T> defaultValue, boolean optional,
Class<T> returnType, int flags
String key, @Nullable Expression<T> defaultValue, boolean optional, Class<T> returnType, int flags
) {
this(key, defaultValue, optional, flags, returnType);
}

/**
* @param returnTypes The expected return types of the matched expression.
*/
@SafeVarargs
public ExpressionEntryData(
String key, @Nullable Expression<T> defaultValue, boolean optional, Class<T>... returnTypes
) {
this(key, defaultValue, optional, SkriptParser.ALL_FLAGS, returnTypes);
}

/**
* @param returnTypes The expected return types of the matched expression.
* @param flags Parsing flags. See {@link SkriptParser#SkriptParser(String, int, ParseContext)}
* javadoc for more details.
*/
@SafeVarargs
public ExpressionEntryData(
String key, @Nullable Expression<T> defaultValue, boolean optional, int flags, Class<T>... returnTypes
) {
super(key, defaultValue, optional);
this.returnType = returnType;
this.returnTypes = returnTypes;
this.flags = flags;
}

Expand All @@ -71,10 +91,10 @@ protected Expression<? extends T> getValue(String value) {
Expression<? extends T> expression;
try (ParseLogHandler log = new ParseLogHandler().start()) {
expression = new SkriptParser(value, flags, ParseContext.DEFAULT)
.parseExpression(returnType);
.parseExpression(returnTypes);
if (expression == null) // print an error if it couldn't parse
log.printError(
"'" + value + "' " + M_IS + " " + SkriptParser.notOfType(returnType),
"'" + value + "' " + M_IS + " " + SkriptParser.notOfType(returnTypes),
ErrorQuality.NOT_AN_EXPRESSION
);
}
Expand Down