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:
Norbi Peti 2019-03-08 22:17:49 +01:00
parent 7fd422136b
commit 138259412f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 20 additions and 6 deletions

View file

@ -17,6 +17,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
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.
* @param key The key to use
* @param defaultProvider A mapping between config paths and config generators
* @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();
var cs=c.getConfigurationSection(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())));
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() {

View file

@ -5,6 +5,8 @@ import lombok.RequiredArgsConstructor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
@ -74,12 +76,14 @@ public class ConfigData<T> { //TODO: Save after a while
else if (def instanceof Double)
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;
}
public void set(T value) {
Object val;
if (setter != null)
if (setter != null && value != null)
val = setter.apply(value);
else val = value;
if (config != null)

View file

@ -130,7 +130,7 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
}
}
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();
String param = commandline.substring(pj, j);
if (cl == String.class) {

View file

@ -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
*/
public final String getCommandPath() {
public String getCommandPath() {
return path;
}

View file

@ -78,7 +78,7 @@ public class ButtonProcessor extends AbstractProcessor {
cs.set("params", ((ExecutableElement) targetcl).getParameters().stream().skip(1).map(p -> {
//String tn=p.asType().toString();
//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)
return "[" + p.getSimpleName() + "]";
return "<" + p.getSimpleName() + ">";