For when we can't serialize your classes back to Java.
If you got a Config(De)SerializationException explaining that SC-CFG could not (de)serialize your field, this is probably what you are looking for. By creating a type adapter, you tell sc-cfg how to serialize and deserialize a certain type.
By default, sc-cfg comes with some pre-configured type adapters, but you can always provide custom adapters for any type, and if it happens that one of your type adapter clash with the default ones, your type adapter will override the default one, this is already handled, you don't have to do anything.
Automatically register type adapters
Register type adapters for your custom types simply by annotating them with @RegisterTypeAdapter.
importcom.github.secretx33.sccfg.api.annotation.RegisterTypeAdapter;importcom.google.gson.JsonSerializer;// this adapter is automatically registered@RegisterTypeAdapter(YourCustomClass.class)publicclassClassAdapterimplementsJsonSerializer<YourCustomClass>,JsonDeserializer<YourCustomClass> {// ...}
import com.github.secretx33.sccfg.api.annotation.RegisterTypeAdapterimport com.google.gson.JsonSerializer// this adapter is automatically registered@RegisterTypeAdapter(YourCustomClass.class)classClassAdapter : JsonSerializer<YourCustomClass>, JsonDeserializer<YourCustomClass> {// ...}
Manually register type adapters
Sometimes, we have a very specific type that uses parameterized types, which our RegisterTypeAdapter annotation does not support. If that happens to you, worry not! You can still register your type adapters.
importcom.google.gson.reflect.TypeToken;publicclassMyPluginextendsJavaPlugin { @OverridepublicvoidonEnable() {finalType type =newTypeAdapter<YourCustomType<List<String>>>() {}.getType();// register one type adapterConfig.registerTypeAdapter(type,newYourCustomTypeAdapter());// register multiple type adapters at oncefinalMap<?extendsType,Object> typeAdapters =newHashMap<>();typeAdapters.put(SomeClass.class,AdapterForSomeClass());typeAdapters.put(type,YourCustomTypeAdapter());Config.registerTypeAdapters(typeAdapters); }}
import com.github.secretx33.sccfg.*import com.google.gson.reflect.TypeTokenclassMyPlugin : JavaPlugin() {overridefunonEnable() {val type: Type=object : TypeAdapter<YourCustomType<List<String>>>() {}.getType()// register one type adapterregisterTypeAdapter(type, YourCustomTypeAdapter())// register multiple type adapters at onceregisterTypeAdapters(mapOf( SomeClass::class.java to AdapterForSomeClass(), type to YourCustomTypeAdapter(), )) }}