From b46c3403305e8eb32d0f3b55a08cb43df8576548 Mon Sep 17 00:00:00 2001
From: cheeezburga <47320303+cheeezburga@users.noreply.github.com>
Date: Sat, 6 Jul 2024 23:16:59 +1000
Subject: [PATCH 1/2] 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
---
.../lang/entry/util/ExpressionEntryData.java | 26 +++++++++++--------
1 file changed, 15 insertions(+), 11 deletions(-)
diff --git a/src/main/java/org/skriptlang/skript/lang/entry/util/ExpressionEntryData.java b/src/main/java/org/skriptlang/skript/lang/entry/util/ExpressionEntryData.java
index 42e964918f9..fc666fb7c39 100644
--- a/src/main/java/org/skriptlang/skript/lang/entry/util/ExpressionEntryData.java
+++ b/src/main/java/org/skriptlang/skript/lang/entry/util/ExpressionEntryData.java
@@ -27,6 +27,9 @@
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.entry.KeyValueEntryData;
+import java.util.Arrays;
+import java.util.List;
+
/**
* A type of {@link KeyValueEntryData} designed to parse its value as an {@link Expression}.
* This data CAN return null if expression parsing fails.
@@ -36,31 +39,31 @@ public class ExpressionEntryData extends KeyValueEntryData returnType;
+ private final List> returnTypes;
private final int flags;
/**
- * @param returnType The expected return type of the matched expression.
+ * @param returnTypes The expected return types of the matched expression.
*/
+ @SafeVarargs
public ExpressionEntryData(
- String key, @Nullable Expression defaultValue, boolean optional,
- Class returnType
+ String key, @Nullable Expression defaultValue, boolean optional, Class... returnTypes
) {
- this(key, defaultValue, optional, returnType, SkriptParser.ALL_FLAGS);
+ this(key, defaultValue, optional, SkriptParser.ALL_FLAGS, returnTypes);
}
/**
- * @param returnType The expected return type of the matched expression.
+ * @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 defaultValue, boolean optional,
- Class returnType, int flags
+ String key, @Nullable Expression defaultValue, boolean optional, int flags, Class... returnTypes
) {
super(key, defaultValue, optional);
- this.returnType = returnType;
+ this.returnTypes = Arrays.asList(returnTypes);
this.flags = flags;
}
@@ -69,12 +72,13 @@ public ExpressionEntryData(
@SuppressWarnings("unchecked")
protected Expression extends T> getValue(String value) {
Expression extends T> expression;
+ Class extends T>[] returnTypesArray = returnTypes.toArray(new Class[0]);
try (ParseLogHandler log = new ParseLogHandler().start()) {
expression = new SkriptParser(value, flags, ParseContext.DEFAULT)
- .parseExpression(returnType);
+ .parseExpression(returnTypesArray);
if (expression == null) // print an error if it couldn't parse
log.printError(
- "'" + value + "' " + M_IS + " " + SkriptParser.notOfType(returnType),
+ "'" + value + "' " + M_IS + " " + SkriptParser.notOfType(returnTypesArray),
ErrorQuality.NOT_AN_EXPRESSION
);
}
From 90d6251df0f3de0774f101a2c3e5647b80ea6f78 Mon Sep 17 00:00:00 2001
From: cheeezburga <47320303+cheeezburga@users.noreply.github.com>
Date: Sat, 6 Jul 2024 23:45:00 +1000
Subject: [PATCH 2/2] 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
---
.../lang/entry/util/ExpressionEntryData.java | 32 ++++++++++++++-----
1 file changed, 24 insertions(+), 8 deletions(-)
diff --git a/src/main/java/org/skriptlang/skript/lang/entry/util/ExpressionEntryData.java b/src/main/java/org/skriptlang/skript/lang/entry/util/ExpressionEntryData.java
index fc666fb7c39..356a8e848dd 100644
--- a/src/main/java/org/skriptlang/skript/lang/entry/util/ExpressionEntryData.java
+++ b/src/main/java/org/skriptlang/skript/lang/entry/util/ExpressionEntryData.java
@@ -27,9 +27,6 @@
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.entry.KeyValueEntryData;
-import java.util.Arrays;
-import java.util.List;
-
/**
* A type of {@link KeyValueEntryData} designed to parse its value as an {@link Expression}.
* This data CAN return null if expression parsing fails.
@@ -39,10 +36,30 @@ public class ExpressionEntryData extends KeyValueEntryData> returnTypes;
+ private final Class[] returnTypes;
private final int flags;
+ /**
+ * @param returnType The expected return type of the matched expression.
+ */
+ public ExpressionEntryData(
+ String key, @Nullable Expression defaultValue, boolean optional, Class returnType
+ ) {
+ this(key, defaultValue, optional, SkriptParser.ALL_FLAGS, returnType);
+ }
+
+ /**
+ * @param returnType The expected return type of the matched expression.
+ * @param flags Parsing flags. See {@link SkriptParser#SkriptParser(String, int, ParseContext)}
+ * javadoc for more details.
+ */
+ public ExpressionEntryData(
+ String key, @Nullable Expression defaultValue, boolean optional, Class returnType, int flags
+ ) {
+ this(key, defaultValue, optional, flags, returnType);
+ }
+
/**
* @param returnTypes The expected return types of the matched expression.
*/
@@ -63,7 +80,7 @@ public ExpressionEntryData(
String key, @Nullable Expression defaultValue, boolean optional, int flags, Class... returnTypes
) {
super(key, defaultValue, optional);
- this.returnTypes = Arrays.asList(returnTypes);
+ this.returnTypes = returnTypes;
this.flags = flags;
}
@@ -72,13 +89,12 @@ public ExpressionEntryData(
@SuppressWarnings("unchecked")
protected Expression extends T> getValue(String value) {
Expression extends T> expression;
- Class extends T>[] returnTypesArray = returnTypes.toArray(new Class[0]);
try (ParseLogHandler log = new ParseLogHandler().start()) {
expression = new SkriptParser(value, flags, ParseContext.DEFAULT)
- .parseExpression(returnTypesArray);
+ .parseExpression(returnTypes);
if (expression == null) // print an error if it couldn't parse
log.printError(
- "'" + value + "' " + M_IS + " " + SkriptParser.notOfType(returnTypesArray),
+ "'" + value + "' " + M_IS + " " + SkriptParser.notOfType(returnTypes),
ErrorQuality.NOT_AN_EXPRESSION
);
}