Config and command fixes/improvements
Default value for config maps added Added support for array types in configs and handling null in setting Added support for TextArg annotation Allowing to override the command path Fixed optional argument detection in the annotation processor ("Optional" --> "OptionalArg")
This commit is contained in:
parent
7fd422136b
commit
138259412f
5 changed files with 20 additions and 6 deletions
|
@ -17,6 +17,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -227,14 +228,23 @@ public abstract class Component<TP extends JavaPlugin> {
|
||||||
/**
|
/**
|
||||||
* Returns a map of configs that are under the given key.
|
* Returns a map of configs that are under the given key.
|
||||||
* @param key The key to use
|
* @param key The key to use
|
||||||
|
* @param defaultProvider A mapping between config paths and config generators
|
||||||
* @return A map containing configs
|
* @return A map containing configs
|
||||||
*/
|
*/
|
||||||
protected Map<String, IHaveConfig> getConfigMap(String key) {
|
protected Map<String, IHaveConfig> getConfigMap(String key, Map<String, Consumer<IHaveConfig>> defaultProvider) {
|
||||||
val c=getConfig().getConfig();
|
val c=getConfig().getConfig();
|
||||||
var cs=c.getConfigurationSection(key);
|
var cs=c.getConfigurationSection(key);
|
||||||
if(cs==null) cs=c.createSection(key);
|
if(cs==null) cs=c.createSection(key);
|
||||||
return cs.getValues(false).entrySet().stream().filter(e->e.getValue() instanceof ConfigurationSection)
|
val res = cs.getValues(false).entrySet().stream().filter(e -> e.getValue() instanceof ConfigurationSection)
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, kv -> new IHaveConfig((ConfigurationSection) kv.getValue())));
|
.collect(Collectors.toMap(Map.Entry::getKey, kv -> new IHaveConfig((ConfigurationSection) kv.getValue())));
|
||||||
|
if (res.size() == 0) {
|
||||||
|
for (val entry : defaultProvider.entrySet()) {
|
||||||
|
val conf = new IHaveConfig(cs.createSection(entry.getKey()));
|
||||||
|
entry.getValue().accept(conf);
|
||||||
|
res.put(entry.getKey(), conf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getClassName() {
|
private String getClassName() {
|
||||||
|
|
|
@ -5,6 +5,8 @@ import lombok.RequiredArgsConstructor;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@ -74,12 +76,14 @@ public class ConfigData<T> { //TODO: Save after a while
|
||||||
else if (def instanceof Double)
|
else if (def instanceof Double)
|
||||||
val = ((Number) val).doubleValue();
|
val = ((Number) val).doubleValue();
|
||||||
}
|
}
|
||||||
|
if (val instanceof List && def.getClass().isArray())
|
||||||
|
val = ((List<T>) val).toArray((T[]) Array.newInstance(def.getClass().getComponentType(), 0));
|
||||||
return (T) val;
|
return (T) val;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set(T value) {
|
public void set(T value) {
|
||||||
Object val;
|
Object val;
|
||||||
if (setter != null)
|
if (setter != null && value != null)
|
||||||
val = setter.apply(value);
|
val = setter.apply(value);
|
||||||
else val = value;
|
else val = value;
|
||||||
if (config != null)
|
if (config != null)
|
||||||
|
|
|
@ -130,7 +130,7 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
j = commandline.indexOf(' ', j + 1); //End index
|
j = commandline.indexOf(' ', j + 1); //End index
|
||||||
if (j == -1) //Last parameter
|
if (j == -1 || paramArr[i1].isAnnotationPresent(TextArg.class)) //Last parameter
|
||||||
j = commandline.length();
|
j = commandline.length();
|
||||||
String param = commandline.substring(pj, j);
|
String param = commandline.substring(pj, j);
|
||||||
if (cl == String.class) {
|
if (cl == String.class) {
|
||||||
|
|
|
@ -59,7 +59,7 @@ public abstract class ICommand2<TP extends Command2Sender> {
|
||||||
*
|
*
|
||||||
* @return The command path, <i>which is the command class name by default</i> (removing any "command" from it) - Change via the {@link CommandClass} annotation
|
* @return The command path, <i>which is the command class name by default</i> (removing any "command" from it) - Change via the {@link CommandClass} annotation
|
||||||
*/
|
*/
|
||||||
public final String getCommandPath() {
|
public String getCommandPath() {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ public class ButtonProcessor extends AbstractProcessor {
|
||||||
cs.set("params", ((ExecutableElement) targetcl).getParameters().stream().skip(1).map(p -> {
|
cs.set("params", ((ExecutableElement) targetcl).getParameters().stream().skip(1).map(p -> {
|
||||||
//String tn=p.asType().toString();
|
//String tn=p.asType().toString();
|
||||||
//return tn.substring(tn.lastIndexOf('.')+1)+" "+p.getSimpleName();
|
//return tn.substring(tn.lastIndexOf('.')+1)+" "+p.getSimpleName();
|
||||||
boolean optional = p.getAnnotationMirrors().stream().anyMatch(am -> am.getAnnotationType().toString().endsWith("Optional"));
|
boolean optional = p.getAnnotationMirrors().stream().anyMatch(am -> am.getAnnotationType().toString().endsWith("OptionalArg"));
|
||||||
if (optional)
|
if (optional)
|
||||||
return "[" + p.getSimpleName() + "]";
|
return "[" + p.getSimpleName() + "]";
|
||||||
return "<" + p.getSimpleName() + ">";
|
return "<" + p.getSimpleName() + ">";
|
||||||
|
|
Loading…
Reference in a new issue