@@ -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