-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for subclass conversions to config manager
- Subclasses of config-mappable classes can now be converted by storing the type in config - Base commands registered to the command map now correctly report their usage - Removed old/deprecated config manager
- Loading branch information
Showing
24 changed files
with
141 additions
and
1,154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: RedLib | ||
main: redempt.redlib.RedLib | ||
version: 2022-01-11 18:34 | ||
version: 2022-01-16 05:50 | ||
author: Redempt | ||
api-version: 1.13 | ||
load: STARTUP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/redempt/redlib/config/annotations/ConfigSubclassable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package redempt.redlib.config.annotations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Indicates that this type can be | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@Documented | ||
@Inherited | ||
public @interface ConfigSubclassable { | ||
} |
9 changes: 0 additions & 9 deletions
9
src/redempt/redlib/config/conversion/StaticRootConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/redempt/redlib/config/conversion/SubclassConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package redempt.redlib.config.conversion; | ||
|
||
import redempt.redlib.config.ConfigManager; | ||
import redempt.redlib.config.ConfigType; | ||
import redempt.redlib.config.data.DataHolder; | ||
|
||
/** | ||
* A converter which can convert subclasses of mappable classes | ||
* @author Redempt | ||
*/ | ||
public class SubclassConverter { | ||
|
||
/** | ||
* Creates a TypeConverter that can convert subclasses | ||
* @param manager The ConfigManager handling the data | ||
* @param clazz The class to handle subclasses of | ||
* @param isAbstract Whether the class is abstract or an interface | ||
* @param <T> The type | ||
* @return The converter | ||
*/ | ||
public static <T> TypeConverter<T> create(ConfigManager manager, Class<T> clazz, boolean isAbstract) { | ||
TypeConverter<T> parent = !isAbstract ? ObjectConverter.create(manager, new ConfigType<>(clazz)) : null; | ||
return new TypeConverter<T>() { | ||
@Override | ||
public T loadFrom(DataHolder section, String path, T currentValue) { | ||
String typeName = section.getSubsection(path).getString("=type"); | ||
if (typeName == null) { | ||
throw new IllegalStateException("Could not determine subclass for object with path " + path); | ||
} | ||
Class<?> type = manager.loadClass(typeName); | ||
if (!clazz.isAssignableFrom(type)) { | ||
throw new IllegalStateException(type + " is not a subclass of " + clazz); | ||
} | ||
TypeConverter<T> converter = type.equals(clazz) ? parent : (TypeConverter<T>) manager.getConverter(new ConfigType<>(type)); | ||
return converter.loadFrom(section, path, currentValue); | ||
} | ||
|
||
@Override | ||
public void saveTo(T t, DataHolder section, String path) { | ||
Class<?> type = t.getClass(); | ||
if (!clazz.isAssignableFrom(type)) { | ||
throw new IllegalStateException(type + " is not a subclass of " + clazz); | ||
} | ||
TypeConverter<T> converter = type.equals(clazz) ? parent : (TypeConverter<T>) manager.getConverter(new ConfigType<>(type)); | ||
converter.saveTo(t, section, path); | ||
section.getSubsection(path).set("=type", type.getName()); | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.