more config abstraction

This commit is contained in:
Erik Bročko 2018-09-04 22:41:52 +02:00
parent a4e32ee605
commit 6482b03c2f
7 changed files with 259 additions and 14 deletions

View File

@ -0,0 +1,50 @@
package cz.marwland.mc.core.config;
import org.bukkit.configuration.file.FileConfiguration;
public class ConfigEntry<T> {
private final String subpath;
private IValueValidator<T> validator;
public ConfigEntry(String subpath) {
this.subpath = subpath;
}
public ConfigEntry(String subpath, IValueValidator<T> validator) {
this.subpath = subpath;
this.validator = validator;
}
public void setValidator(IValueValidator<T> validator) {
this.validator = validator;
}
public String getSubpath() {
return this.subpath;
}
public T validate(String s) {
if (validator == null)
return null;
return (T) validator.validate(s);
}
public boolean setIn(FileConfiguration cfg, String parent, String value) {
T o = validate(value);
if (o == null)
return false;
cfg.set(parent + "." + subpath, o);
return true;
}
public T getFrom(FileConfiguration cfg, String parent) {
return (T) validate(cfg.getString(parent + "." + subpath));
}
// TODO:
// should be IValueExecuter<T>, but we cannot know the type at compile-time.
public void doStuff(FileConfiguration cfg, String parent, IValueExecuter<Object> iValueExecuter) {
iValueExecuter.doStuff(this.getFrom(cfg, parent));
}
}

View File

@ -0,0 +1,35 @@
package cz.marwland.mc.core.config;
import java.util.HashMap;
import java.util.Set;
public class EntryManager {
public HashMap<String, ConfigEntry<?>> entries = new HashMap<>();
public EntryManager() {
}
public EntryManager(ConfigEntry<?>... es) {
for (ConfigEntry<?> e : es) {
this.addEntry(e);
}
}
public void addEntry(ConfigEntry<?> e) {
entries.put(e.getSubpath(), e);
}
public ConfigEntry<?> getEntry(String subpath) {
return entries.get(subpath);
}
public Set<String> getKeys() {
return entries.keySet();
}
public boolean hasKey(String subpath) {
return entries.get(subpath) == null;
}
}

View File

@ -0,0 +1,7 @@
package cz.marwland.mc.core.config;
public interface IValueExecuter<T> {
public void doStuff(T val);
}

View File

@ -0,0 +1,7 @@
package cz.marwland.mc.core.config;
public interface IValueValidator<T> {
public T validate(String str);
}

View File

@ -0,0 +1,90 @@
package cz.marwland.mc.core.config;
import org.bukkit.util.Vector;
import cz.marwland.mc.core.util.LocationUtil;
public class ValueValidator {
public static final IValueValidator<Byte> BYTE = new IValueValidator<>() {
@Override
public Byte validate(String str) {
try {
return Byte.parseByte(str);
} catch (NumberFormatException ex) {
return null;
}
}
};
public static final IValueValidator<Short> SHORT = new IValueValidator<>() {
@Override
public Short validate(String str) {
try {
return Short.parseShort(str);
} catch (NumberFormatException ex) {
return null;
}
}
};
public static final IValueValidator<Integer> INTEGER = new IValueValidator<>() {
@Override
public Integer validate(String str) {
try {
return Integer.parseInt(str);
} catch (NumberFormatException ex) {
return null;
}
}
};
public static final IValueValidator<Long> LONG = new IValueValidator<>() {
@Override
public Long validate(String str) {
try {
return Long.parseLong(str);
} catch (NumberFormatException ex) {
return null;
}
}
};
public static final IValueValidator<Float> FLOAT = new IValueValidator<>() {
@Override
public Float validate(String str) {
try {
return Float.parseFloat(str);
} catch (NumberFormatException ex) {
return null;
}
}
};
public static final IValueValidator<Double> DOUBLE = new IValueValidator<>() {
@Override
public Double validate(String str) {
try {
return Double.parseDouble(str);
} catch (NumberFormatException ex) {
return null;
}
}
};
public static final IValueValidator<Boolean> BOOLEAN = new IValueValidator<>() {
@Override
public Boolean validate(String str) {
try {
return Boolean.parseBoolean(str);
} catch (NumberFormatException ex) {
return null;
}
}
};
public static final IValueValidator<String> STRING = new IValueValidator<>() {
@Override
public String validate(String str) {
return str;
}
};
public static final IValueValidator<Vector> VECTOR = new IValueValidator<>() {
@Override
public Vector validate(String str) {
return LocationUtil.deserializeVector(str);
}
};
}

View File

@ -3,7 +3,6 @@ package cz.marwland.mc.core.features;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.command.CommandMap;
import org.bukkit.command.defaults.BukkitCommand;
@ -16,7 +15,6 @@ public abstract class Feature implements Listener {
protected final MarwCore plugin;
private String name = "";
private ArrayList<BukkitCommand> commands = new ArrayList<>();
private HashMap<String, String> configs = new HashMap<>();
private static CommandMap cmap;
public Feature(MarwCore plugin, String name) {
@ -39,8 +37,6 @@ public abstract class Feature implements Listener {
}
}
plugin.getServer().getPluginManager().registerEvents(this, plugin);
//configs.forEach((k, v) -> plugin.getConfigManager().registerConfig(k, v));
}
public void onDisable() {
@ -51,7 +47,6 @@ public abstract class Feature implements Listener {
}
}
public void onReload() {
}
@ -72,7 +67,6 @@ public abstract class Feature implements Listener {
}
protected void registerConfig(String template, String path) {
//this.configs.put(template, path);
plugin.getConfigManager().registerConfig(template, path);
}

View File

@ -1,7 +1,6 @@
package cz.marwland.mc.essentials.features;
import java.util.Arrays;
import java.util.List;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
@ -10,23 +9,72 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.util.Vector;
import cz.marwland.mc.core.MarwCore;
import cz.marwland.mc.core.config.ConfigEntry;
import cz.marwland.mc.core.config.EntryManager;
import cz.marwland.mc.core.config.IValueExecuter;
import cz.marwland.mc.core.config.ValueValidator;
import cz.marwland.mc.core.features.Feature;
import cz.marwland.mc.core.util.LocationUtil;
import cz.marwland.mc.core.util.Region;
import net.md_5.bungee.api.ChatColor;
public class Borders extends Feature {
FileConfiguration cfg;
EntryManager emgr = new EntryManager();
public Borders(MarwCore plugin) {
super(plugin, "heil");
// String name, String description, String usageMessage, List<String> aliases)
this.addCommand(new BukkitCommand("sieg", "Heil the Führer!", "/sieg", Arrays.asList()) {
this.addCommand(new BukkitCommand(
"worldborder",
"Modify the world's borders",
"/border [world] (key) (value)",
Arrays.asList() ) {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
sender.sendMessage("xddd");
if (args.length < 2)
return false;
World w = plugin.getServer().getWorld(args[0]);
if (w == null) {
sender.sendMessage(ChatColor.RED + "Invalid world!");
return true;
}
ConfigEntry<?> entry = emgr.getEntry(args[1]);
if (entry == null) {
sender.sendMessage(
ChatColor.RED + "Invalid key '" +
ChatColor.YELLOW + args[1] +
ChatColor.RED + "'! Available: " +
ChatColor.GRAY + String.join(", ", emgr.getKeys()));
return false;
}
if (args.length == 2) { // get
sender.sendMessage(
ChatColor.YELLOW + args[1] +
ChatColor.GRAY + " @ " +
ChatColor.AQUA + w.getName() +
ChatColor.GRAY + ": " + entry.getFrom(cfg, w.getName()));
} else { // set
String newval = String.join(" ", args).substring(args[0].length() + args[1].length() + 2);
if (entry.setIn(cfg, w.getName(), newval)) {
sender.sendMessage(
ChatColor.RED + "Invalid value for " +
ChatColor.YELLOW + args[1] +
ChatColor.RED + ": " +
ChatColor.GRAY + newval);
}
}
return true;
}
});
emgr.addEntry(new ConfigEntry<>("center", ValueValidator.VECTOR));
emgr.addEntry(new ConfigEntry<>("size", ValueValidator.DOUBLE));
emgr.addEntry(new ConfigEntry<>("radius", ValueValidator.INTEGER));
emgr.addEntry(new ConfigEntry<>("warningdist", ValueValidator.INTEGER));
emgr.addEntry(new ConfigEntry<>("warningtime", ValueValidator.INTEGER));
emgr.addEntry(new ConfigEntry<>("damage", ValueValidator.DOUBLE));
emgr.addEntry(new ConfigEntry<>("buffer", ValueValidator.DOUBLE));
this.registerConfig("borders.yml");
}
@ -49,6 +97,20 @@ public class Borders extends Feature {
if (!cfg.contains(wname))
continue;
plugin.getLogger().info("Loading borders for " + wname);
emgr.getEntry("radius").doStuff(cfg, wname, new IValueExecuter<Object>() {
@Override
public void doStuff(Object val) {
w.getWorldBorder().setSize((int) val);
}
});
emgr.getEntry("center").doStuff(cfg, wname, new IValueExecuter<Object>() {
@Override
public void doStuff(Object val) {
Vector v = (Vector) val;
w.getWorldBorder().setCenter(v.getX(), v.getZ());
}
});
/*
Region r = new Region();
if (cfg.contains(wname + ".center") && cfg.contains(wname + ".size")) {
plugin.getLogger().info("should i?");
@ -85,8 +147,8 @@ public class Borders extends Feature {
Vector c = r.getCenter();
w.getWorldBorder().setCenter(c.getX(), c.getZ());
}
if (cfg.contains(wname + ".radius"))
*/
/*if (cfg.contains(wname + ".radius"))
w.getWorldBorder().setSize(cfg.getInt(wname + ".radius"));
if (cfg.contains(wname + ".warningdist"))
w.getWorldBorder().setWarningDistance(cfg.getInt(wname + ".warningdist"));
@ -95,7 +157,7 @@ public class Borders extends Feature {
if (cfg.contains(wname + ".damage"))
w.getWorldBorder().setDamageAmount(cfg.getDouble(wname + ".damage"));
if (cfg.contains(wname + ".buffer"))
w.getWorldBorder().setDamageBuffer(cfg.getDouble(wname + ".buffer"));
w.getWorldBorder().setDamageBuffer(cfg.getDouble(wname + ".buffer"));*/
}
}