Skip to content

Commit 4be16a6

Browse files
committed
Add smart cast method for utility.
1 parent 32a0741 commit 4be16a6

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.byteskript</groupId>
88
<artifactId>byteskript</artifactId>
9-
<version>1.0.23</version>
9+
<version>1.0.24</version>
1010
<name>ByteSkript</name>
1111
<description>A compiled JVM implementation of the Skript language.</description>
1212

src/main/java/org/byteskript/skript/api/SyntaxElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ default void writeCall(final MethodBuilder builder, final Method method, final C
143143
else sub = context.getBuilder().addMatching(target);
144144
sub.setModifiers(0x00000002 | 0x00000008 | 0x00001000 | 0x00000040);
145145
int index = 0;
146-
for (Type input : inputs) {
146+
for (final Type input : inputs) {
147147
sub.writeCode(loadObject(index));
148148
sub.writeCode(cast(input));
149149
index++;

src/main/java/org/byteskript/skript/runtime/Skript.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,29 @@ public static Skript currentInstance() {
284284
return skript;
285285
}
286286

287-
public <From, To> void registerConverter(Class<From> from, Class<To> to, Converter<From, To> converter) {
288-
final Converter.Data data = new Converter.Data(from, to);
289-
this.converters.put(data, converter);
290-
}
291-
287+
/**
288+
* A utility method to handle converting types for syntax.
289+
* This will throw an error if conversion is impossible.
290+
*/
291+
public static <To> To convert(Object from, Class<To> to) {
292+
return convert(from, to, true);
293+
}
294+
295+
/**
296+
* A utility method to handle converting types for syntax.
297+
* If the fail parameter is true, this will throw an error, otherwise returning null.
298+
*/
299+
@SuppressWarnings("unchecked")
300+
public static <From, To> To convert(From from, Class<To> to, boolean fail) {
301+
if (to.isInstance(from)) return to.cast(from);
302+
final Skript instance = findInstance();
303+
final Converter<From, To> converter = (Converter<From, To>) instance.getConverter(from.getClass(), to);
304+
if (converter != null) return converter.convert(from);
305+
if (fail) throw new ScriptRuntimeError("Unable convert '" + from + "' to type " + to.getSimpleName() + ".");
306+
else return null;
307+
}
308+
309+
@SuppressWarnings("unchecked")
292310
public <From, To> Converter<From, To> getConverter(Class<From> from, Class<To> to) {
293311
final Converter.Data data = new Converter.Data(from, to);
294312
if (converters.containsKey(data)) return (Converter<From, To>) converters.get(data);
@@ -299,6 +317,11 @@ public <From, To> Converter<From, To> getConverter(Class<From> from, Class<To> t
299317
return null;
300318
}
301319

320+
public <From, To> void registerConverter(Class<From> from, Class<To> to, Converter<From, To> converter) {
321+
final Converter.Data data = new Converter.Data(from, to);
322+
this.converters.put(data, converter);
323+
}
324+
302325
@Description("""
303326
Gets the parent class-loader attached to this Skript runtime.
304327
This is used to search available libraries and scripts for classes.

0 commit comments

Comments
 (0)