Added support for config map gen
#59 Hopefully Also added support for setting the path automatically
This commit is contained in:
parent
ce29422177
commit
4dade43f82
4 changed files with 54 additions and 25 deletions
|
@ -42,7 +42,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
|
||||||
@Override
|
@Override
|
||||||
public final void onEnable() {
|
public final void onEnable() {
|
||||||
loadConfig();
|
loadConfig();
|
||||||
IHaveConfig.pregenConfig(this, iConfig);
|
IHaveConfig.pregenConfig(this, null);
|
||||||
try {
|
try {
|
||||||
pluginEnable();
|
pluginEnable();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -85,7 +85,7 @@ public abstract class Component<TP extends JavaPlugin> {
|
||||||
}
|
}
|
||||||
component.plugin = plugin;
|
component.plugin = plugin;
|
||||||
updateConfig(plugin, component);
|
updateConfig(plugin, component);
|
||||||
IHaveConfig.pregenConfig(component, component.config);
|
IHaveConfig.pregenConfig(component, null);
|
||||||
component.register(plugin);
|
component.register(plugin);
|
||||||
components.put(component.getClass(), component);
|
components.put(component.getClass(), component);
|
||||||
if (plugin instanceof ButtonPlugin)
|
if (plugin instanceof ButtonPlugin)
|
||||||
|
|
|
@ -5,7 +5,7 @@ import buttondevteam.lib.ThorpeUtils;
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.Setter;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.configuration.Configuration;
|
import org.bukkit.configuration.Configuration;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
@ -22,7 +22,6 @@ import java.util.function.Function;
|
||||||
* Use the getter/setter constructor if {@link T} isn't a primitive type or String.<br>
|
* Use the getter/setter constructor if {@link T} isn't a primitive type or String.<br>
|
||||||
* Use {@link Component#getConfig()} or {@link ButtonPlugin#getIConfig()} then {@link IHaveConfig#getData(String, Object)} to get an instance.
|
* Use {@link Component#getConfig()} or {@link ButtonPlugin#getIConfig()} then {@link IHaveConfig#getData(String, Object)} to get an instance.
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
|
|
||||||
//@AllArgsConstructor(access = AccessLevel.PACKAGE)
|
//@AllArgsConstructor(access = AccessLevel.PACKAGE)
|
||||||
public class ConfigData<T> {
|
public class ConfigData<T> {
|
||||||
private static final HashMap<Configuration, SaveTask> saveTasks = new HashMap<>();
|
private static final HashMap<Configuration, SaveTask> saveTasks = new HashMap<>();
|
||||||
|
@ -31,7 +30,8 @@ public class ConfigData<T> {
|
||||||
*/
|
*/
|
||||||
private final ConfigurationSection config;
|
private final ConfigurationSection config;
|
||||||
@Getter
|
@Getter
|
||||||
private final String path;
|
@Setter(AccessLevel.PACKAGE)
|
||||||
|
private String path;
|
||||||
private final T def;
|
private final T def;
|
||||||
private final Object primitiveDef;
|
private final Object primitiveDef;
|
||||||
private final Runnable saveAction;
|
private final Runnable saveAction;
|
||||||
|
@ -54,7 +54,7 @@ public class ConfigData<T> {
|
||||||
private boolean saved = false;
|
private boolean saved = false;
|
||||||
|
|
||||||
//This constructor is needed because it sets the getter and setter
|
//This constructor is needed because it sets the getter and setter
|
||||||
public ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter, Runnable saveAction) {
|
ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter, Runnable saveAction) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.def = def;
|
this.def = def;
|
||||||
|
@ -64,6 +64,15 @@ public class ConfigData<T> {
|
||||||
this.saveAction = saveAction;
|
this.saveAction = saveAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@java.beans.ConstructorProperties({"config", "path", "def", "primitiveDef", "saveAction"})
|
||||||
|
ConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Runnable saveAction) {
|
||||||
|
this.config = config;
|
||||||
|
this.path = path;
|
||||||
|
this.def = def;
|
||||||
|
this.primitiveDef = primitiveDef;
|
||||||
|
this.saveAction = saveAction;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ConfigData{" +
|
return "ConfigData{" +
|
||||||
|
|
|
@ -6,9 +6,12 @@ import lombok.Getter;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import javax.annotation.Nullable;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.util.*;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A config system
|
* A config system
|
||||||
|
@ -122,21 +125,38 @@ public final class IHaveConfig {
|
||||||
* Generates the config YAML.
|
* Generates the config YAML.
|
||||||
*
|
*
|
||||||
* @param obj The object which has config methods
|
* @param obj The object which has config methods
|
||||||
* @param config The config that holds the values
|
* @param configMap The result from {@link Component#getConfigMap(String, Map)}. May be null.
|
||||||
*/
|
*/
|
||||||
public static void pregenConfig(Object obj, IHaveConfig config) {
|
public static void pregenConfig(Object obj, @Nullable Map<String, IHaveConfig> configMap) {
|
||||||
val ms = obj.getClass().getDeclaredMethods();
|
val ms = obj.getClass().getDeclaredMethods();
|
||||||
for (val m : ms) {
|
for (val m : ms) {
|
||||||
if (!m.getReturnType().getName().equals(ConfigData.class.getName())) continue;
|
if (!m.getReturnType().getName().equals(ConfigData.class.getName())) continue;
|
||||||
try {
|
try {
|
||||||
if (m.getParameterCount() == 0) {
|
|
||||||
m.setAccessible(true);
|
m.setAccessible(true);
|
||||||
ConfigData<?> c = (ConfigData<?>) m.invoke(obj);
|
List<ConfigData<?>> configList;
|
||||||
if (!c.getPath().equals(m.getName()))
|
if (m.getParameterCount() == 0) {
|
||||||
MainPlugin.Instance.getLogger().warning("Config name does not match: " + c.getPath() + " instead of " + m.getName()); //TODO: Set it here
|
configList = Collections.singletonList((ConfigData<?>) m.invoke(obj));
|
||||||
|
} else if (m.getParameterCount() == 1 && m.getParameterTypes()[0] == IHaveConfig.class) {
|
||||||
|
if (configMap == null) continue; //Hope it will get called with the param later
|
||||||
|
configList = configMap.entrySet().stream().map(kv ->
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return (ConfigData<?>) m.invoke(obj, kv.getValue());
|
||||||
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||||
|
TBMCCoreAPI.SendException("Failed to pregenerate " + m.getName() + " for " + obj + " using config " + kv.getKey() + "!", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}).filter(Objects::nonNull).collect(Collectors.toList());
|
||||||
|
} else {
|
||||||
|
MainPlugin.Instance.getLogger().warning("Method " + m.getName() + " returns a config but its parameters are unknown: " + Arrays.toString(m.getParameterTypes()));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (val c : configList) {
|
||||||
|
if (c.getPath().length() == 0)
|
||||||
|
c.setPath(m.getName());
|
||||||
|
else if (!c.getPath().equals(m.getName()))
|
||||||
|
MainPlugin.Instance.getLogger().warning("Config name does not match: " + c.getPath() + " instead of " + m.getName());
|
||||||
c.get(); //Saves the default value if needed - also checks validity
|
c.get(); //Saves the default value if needed - also checks validity
|
||||||
} else if (m.getParameterCount() == 1) {
|
|
||||||
//TODO: Config map
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException("Failed to pregenerate " + m.getName() + " for " + obj + "!", e);
|
TBMCCoreAPI.SendException("Failed to pregenerate " + m.getName() + " for " + obj + "!", e);
|
||||||
|
|
Loading…
Reference in a new issue