Created
November 3, 2017 21:40
-
-
Save TheMasteredPanda/36724b600989dc91d4bd175d564f62e8 to your computer and use it in GitHub Desktop.
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
/** | |
* Same as BaseFile#populate but allows for formatting to be automatically done. | |
* @param clazz - the class. | |
*/ | |
@Override | |
public void populate(Class<?> clazz, Object instance) | |
{ | |
this.lib.debug(this, "Attempting to populate class " + clazz.getName() + "."); | |
for (Field f : clazz.getFields()) { | |
this.lib.debug(this, "Iteration landed at " + f.getName() + "."); | |
if (!f.isAnnotationPresent(ConfigPopulate.class)) { | |
this.lib.debug(this, f.getName() + " does not have the correct annotation."); | |
continue; | |
} | |
this.lib.debug(this, f.getName() + " does have the correct annotation."); | |
ConfigPopulate annotation = f.getAnnotation(ConfigPopulate.class); | |
String value = this.getConfiguration().getString(annotation.value(), null); | |
if (value == null) { | |
this.lib.debug(this, "Value is null in annotation over " + f.getName() + "."); | |
throw new DeveloperException("Key " + annotation.value() + ". Was not found in file " + this.getName() + "."); | |
} | |
if (!GenericUtil.caseable(value, f.getType())) { | |
this.lib.debug(this, "Can't cast field type " + f.getName() + " do the value in the config."); | |
throw new DeveloperException("Value corresponding to key " + annotation.value() + " could not be assigned to field " + f.getName() + " as it's type, " + f.getType().getName() + " could not be casted to the value " + value.toString() + "."); | |
} | |
this.lib.debug(this, "Setting the field " + f.getName() + " accessible."); | |
f.setAccessible(true); | |
if (f.getType().equals(String.class) && annotation.format()) { | |
this.lib.debug(this, "Setting the value as a String, formatted, to field " + f.getName() + ". Type of field: " + f.getType().getName() + "."); | |
TryUtil.sneaky(() -> f.set(instance, Format.color(this.PLUGIN_MESSAGE_FORMAT.replace("{prefix}", this.PLUGIN_PREFIX).replace("{message}", value)))); | |
continue; | |
} else if (f.getType().equals(String.class) && annotation.color()) { | |
this.lib.debug(this, "Setting the value as a string, colored, to field " + f.getName() + "."); | |
TryUtil.sneaky(() -> f.set(instance, Format.color(value))); | |
continue; | |
} | |
this.lib.debug(this, "Setting field " + f.getName() + "."); | |
TryUtil.sneaky(() -> f.set(instance, value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment