Skip to content

Commit

Permalink
Allows for multiple return types in ExpressionEntryData (#6873)
Browse files Browse the repository at this point in the history
* Allows for multiple return types in ExpressionEntryData
- Changes the constructors to allow for multiple return types
- Changes the calls to SkriptParser#parseExpression to pass in the array of types

* Changes list to array and redirects existing constructors
- Changes returnTypes field to an array
- Adds 2 new constructors, one which has the flags parameter and the other that defaults to ALL_FLAGS

---------

Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
  • Loading branch information
cheeezburga and sovdeeth authored Aug 15, 2024
1 parent 6da1216 commit f6ae828
Showing 1 changed file with 29 additions and 9 deletions.
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

0 comments on commit f6ae828

Please sign in to comment.