Class Converters

java.lang.Object
org.skriptlang.skript.lang.converter.Converters

public final class Converters extends Object
Converters are used to provide Skript with specific instructions for converting an object to a different type.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <From, To> To[]
    convert(From[] from, Class<To> toType, Converter<? super From,? extends To> converter)
    A method for bulk-converting objects of a specific type using a specific Converter.
    static <From, To> To
    convert(From from, Class<? extends To>[] toTypes)
    A method for converting an object into one of several provided types.
    static <From, To> To
    convert(From from, Class<To> toType)
    Standard method for converting an object into a different type.
    static <To> To[]
    convert(Object @Nullable [] from, Class<? extends To>[] toTypes, Class<To> superType)
    A method for bulk-conversion of objects into one of several provided types.
    static <To> To[]
    convert(Object @Nullable [] from, Class<To> toType)
    Standard method for bulk-conversion of objects into a different type.
    static boolean
    converterExists(Class<?> fromType, Class<?> toType)
    A method for determining whether a Converter of fromType to toType exists.
    static boolean
    converterExists(Class<?> fromType, Class<?>... toTypes)
    A method for determining whether a direct Converter of fromType to one of the provided toTypes exists.
    static <To> To[]
    convertStrictly(Object[] from, Class<To> toType)
    A method for bulk-conversion that guarantees objects of toType are returned.
    static <To> To
    convertStrictly(Object from, Class<To> toType)
    A method that guarantees an object of toType is returned.
    static <From, To> To[]
    convertUnsafe(From[] from, Class<?> toType, Converter<? super From,? extends To> converter)
    A method for bulk-converting objects of a specific type using a specific Converter.
    static <F, M, T> void
    This method is to be called after Skript has finished registration.
    static boolean
    exactConverterExists(Class<?> fromType, Class<?> toType)
    A method for determining whether a direct Converter of fromType to toType exists.
    static <F, T> @Nullable Converter<F,T>
    getConverter(Class<F> fromType, Class<T> toType)
    A method for obtaining a Converter that can convert an object of fromType into an object of toType.
    static <F, T> @Nullable ConverterInfo<F,T>
    getConverterInfo(Class<F> fromType, Class<T> toType)
    A method for obtaining the ConverterInfo of a Converter that can convert an object of fromType into an object of toType.
    static @Unmodifiable List<ConverterInfo<?,?>>
     
    static <F, T> void
    registerConverter(Class<F> fromType, Class<T> toType, Converter<F,T> converter)
    Registers a new Converter with Skript's collection of Converters.
    static <F, T> void
    registerConverter(Class<F> fromType, Class<T> toType, Converter<F,T> converter, int flags)
    Registers a new Converter with Skript's collection of Converters.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getConverterInfos

      public static @Unmodifiable List<ConverterInfo<?,?>> getConverterInfos()
      Returns:
      An unmodifiable list containing all registered ConverterInfos. Please note that this does not include any special Converters resolved by Skript during runtime. This method ONLY returns converters explicitly registered during registration. Thus, it is recommended to use getConverter(Class, Class).
    • registerConverter

      public static <F, T> void registerConverter(Class<F> fromType, Class<T> toType, Converter<F,T> converter)
      Registers a new Converter with Skript's collection of Converters.
      Parameters:
      fromType - The type to convert from.
      toType - The type to convert to.
      converter - A Converter for converting objects of fromType to toType.
    • registerConverter

      public static <F, T> void registerConverter(Class<F> fromType, Class<T> toType, Converter<F,T> converter, int flags)
      Registers a new Converter with Skript's collection of Converters.
      Parameters:
      fromType - The type to convert from.
      toType - The type to convert to.
      converter - A Converter for converting objects of fromType to toType.
      flags - Flags to set for the Converter. Flags can be found under Converter.
    • createChainedConverters

      public static <F, M, T> void createChainedConverters()
      This method is to be called after Skript has finished registration. It allows ChainedConverters to be created so that Skript may do more complex conversions involving multiple converters.
    • exactConverterExists

      public static boolean exactConverterExists(Class<?> fromType, Class<?> toType)
      A method for determining whether a direct Converter of fromType to toType exists. Unlike other methods of this class, it is not the case that Skript.isAcceptRegistrations() must return false for this method to be used.
      Parameters:
      fromType - The type to convert from.
      toType - The type to convert to.
      Returns:
      Whether a direct Converter of fromType to toType exists.
    • converterExists

      public static boolean converterExists(Class<?> fromType, Class<?> toType)
      A method for determining whether a Converter of fromType to toType exists.
      Parameters:
      fromType - The type to convert from.
      toType - The type to convert to.
      Returns:
      Whether a Converter of fromType to toType exists.
    • converterExists

      public static boolean converterExists(Class<?> fromType, Class<?>... toTypes)
      A method for determining whether a direct Converter of fromType to one of the provided toTypes exists.
      Parameters:
      fromType - The type to convert from.
      toTypes - The types to attempt converting to.
      Returns:
      Whether a Converter of fromType to one of the provided toTypes exists.
    • getConverter

      @Nullable public static <F, T> @Nullable Converter<F,T> getConverter(Class<F> fromType, Class<T> toType)
      A method for obtaining a Converter that can convert an object of fromType into an object of toType.
      Parameters:
      fromType - The type to convert from.
      toType - The type to convert to.
      Returns:
      A Converter capable of converting an object of fromType into an object of toType. Will return null if no such Converter exists.
    • getConverterInfo

      @Nullable public static <F, T> @Nullable ConverterInfo<F,T> getConverterInfo(Class<F> fromType, Class<T> toType)
      A method for obtaining the ConverterInfo of a Converter that can convert an object of fromType into an object of toType.
      Parameters:
      fromType - The type to convert from.
      toType - The type to convert to.
      Returns:
      The ConverterInfo of a Converter capable of converting an object of fromType into an object of toType. Will return null if no such Converter exists.
    • convert

      @Nullable public static <From, To> To convert(@Nullable From from, Class<To> toType)
      Standard method for converting an object into a different type.
      Parameters:
      from - The object to convert.
      toType - The type that from should be converted into.
      Returns:
      An object of toType, or null if from couldn't be successfully converted.
    • convert

      @Nullable public static <From, To> To convert(@Nullable From from, Class<? extends To>[] toTypes)
      A method for converting an object into one of several provided types.
      Parameters:
      from - The object to convert.
      toTypes - A list of types that should be tried for converting from.
      Returns:
      An object of one of the provided toTypes, or null if from couldn't successfully be converted.
    • convert

      public static <To> To[] convert(Object @Nullable [] from, Class<To> toType)
      Standard method for bulk-conversion of objects into a different type.
      Parameters:
      from - The objects to convert.
      toType - The type that from should be converted into.
      Returns:
      Objects of toType. Will return null if from is null. Please note that the returned array may not be the same size as from. This can happen if an object contained within from is not successfully converted.
    • convert

      public static <To> To[] convert(Object @Nullable [] from, Class<? extends To>[] toTypes, Class<To> superType)
      A method for bulk-conversion of objects into one of several provided types.
      Parameters:
      from - The objects to convert.
      toTypes - A list of types that should be tried for converting each object.
      superType - A parent type of all provided toTypes.
      Returns:
      Objects of superType. Will return any empty array if from is null. Please note that the returned array may not be the same size as from. This can happen if an object contained within from is not successfully converted. And, of course, the returned array may contain objects of a different type.
    • convert

      public static <From, To> To[] convert(From[] from, Class<To> toType, Converter<? super From,? extends To> converter)
      A method for bulk-converting objects of a specific type using a specific Converter.
      Parameters:
      from - The objects to convert.
      toType - The type to convert into.
      converter - The converter to use for conversion.
      Returns:
      Objects of toType. Please note that the returned array may not be the same size as from. This can happen if an object contained within from is not successfully converted.
    • convertStrictly

      public static <To> To convertStrictly(Object from, Class<To> toType)
      A method that guarantees an object of toType is returned.
      Parameters:
      from - The object to convert.
      toType - The type to convert into.
      Returns:
      An object of toType.
      Throws:
      ClassCastException - If from cannot be converted.
    • convertStrictly

      public static <To> To[] convertStrictly(Object[] from, Class<To> toType)
      A method for bulk-conversion that guarantees objects of toType are returned.
      Parameters:
      from - The object to convert.
      toType - The type to convert into.
      Returns:
      Objects of toType. The returned array will be the same size as from.
      Throws:
      ClassCastException - If any of the provided objects cannot be converted.
    • convertUnsafe

      public static <From, To> To[] convertUnsafe(From[] from, Class<?> toType, Converter<? super From,? extends To> converter)
      A method for bulk-converting objects of a specific type using a specific Converter.
      Parameters:
      from - The objects to convert.
      toType - A superclass for all objects to be converted.
      converter - The converter to use for conversion.
      Returns:
      Objects of toType. Please note that the returned array may not be the same size as from. This can happen if an object contained within from is not successfully converted.
      Throws:
      ArrayStoreException - If toType is not a superclass of all objects returned by the converter.
      ClassCastException - If toType is not of To.