diff --git a/src/cz/marwland/mc/core/config/ConfigEntry.java b/src/cz/marwland/mc/core/config/ConfigEntry.java new file mode 100644 index 0000000..00855e4 --- /dev/null +++ b/src/cz/marwland/mc/core/config/ConfigEntry.java @@ -0,0 +1,50 @@ +package cz.marwland.mc.core.config; + +import org.bukkit.configuration.file.FileConfiguration; + +public class ConfigEntry { + private final String subpath; + private IValueValidator validator; + + public ConfigEntry(String subpath) { + this.subpath = subpath; + } + + public ConfigEntry(String subpath, IValueValidator validator) { + this.subpath = subpath; + this.validator = validator; + } + + public void setValidator(IValueValidator 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, but we cannot know the type at compile-time. + public void doStuff(FileConfiguration cfg, String parent, IValueExecuter iValueExecuter) { + iValueExecuter.doStuff(this.getFrom(cfg, parent)); + } + +} \ No newline at end of file diff --git a/src/cz/marwland/mc/core/config/EntryManager.java b/src/cz/marwland/mc/core/config/EntryManager.java new file mode 100644 index 0000000..65daf5e --- /dev/null +++ b/src/cz/marwland/mc/core/config/EntryManager.java @@ -0,0 +1,35 @@ +package cz.marwland.mc.core.config; + +import java.util.HashMap; +import java.util.Set; + +public class EntryManager { + + public HashMap> 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 getKeys() { + return entries.keySet(); + } + + public boolean hasKey(String subpath) { + return entries.get(subpath) == null; + } + +} diff --git a/src/cz/marwland/mc/core/config/IValueExecuter.java b/src/cz/marwland/mc/core/config/IValueExecuter.java new file mode 100644 index 0000000..d7427fb --- /dev/null +++ b/src/cz/marwland/mc/core/config/IValueExecuter.java @@ -0,0 +1,7 @@ +package cz.marwland.mc.core.config; + +public interface IValueExecuter { + + public void doStuff(T val); + +} diff --git a/src/cz/marwland/mc/core/config/IValueValidator.java b/src/cz/marwland/mc/core/config/IValueValidator.java new file mode 100644 index 0000000..2635d1c --- /dev/null +++ b/src/cz/marwland/mc/core/config/IValueValidator.java @@ -0,0 +1,7 @@ +package cz.marwland.mc.core.config; + +public interface IValueValidator { + + public T validate(String str); + +} diff --git a/src/cz/marwland/mc/core/config/ValueValidator.java b/src/cz/marwland/mc/core/config/ValueValidator.java new file mode 100644 index 0000000..0ff34ce --- /dev/null +++ b/src/cz/marwland/mc/core/config/ValueValidator.java @@ -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 = new IValueValidator<>() { + @Override + public Byte validate(String str) { + try { + return Byte.parseByte(str); + } catch (NumberFormatException ex) { + return null; + } + } + }; + public static final IValueValidator SHORT = new IValueValidator<>() { + @Override + public Short validate(String str) { + try { + return Short.parseShort(str); + } catch (NumberFormatException ex) { + return null; + } + } + }; + public static final IValueValidator INTEGER = new IValueValidator<>() { + @Override + public Integer validate(String str) { + try { + return Integer.parseInt(str); + } catch (NumberFormatException ex) { + return null; + } + } + }; + public static final IValueValidator LONG = new IValueValidator<>() { + @Override + public Long validate(String str) { + try { + return Long.parseLong(str); + } catch (NumberFormatException ex) { + return null; + } + } + }; + public static final IValueValidator FLOAT = new IValueValidator<>() { + @Override + public Float validate(String str) { + try { + return Float.parseFloat(str); + } catch (NumberFormatException ex) { + return null; + } + } + }; + public static final IValueValidator DOUBLE = new IValueValidator<>() { + @Override + public Double validate(String str) { + try { + return Double.parseDouble(str); + } catch (NumberFormatException ex) { + return null; + } + } + }; + public static final IValueValidator BOOLEAN = new IValueValidator<>() { + @Override + public Boolean validate(String str) { + try { + return Boolean.parseBoolean(str); + } catch (NumberFormatException ex) { + return null; + } + } + }; + public static final IValueValidator STRING = new IValueValidator<>() { + @Override + public String validate(String str) { + return str; + } + }; + public static final IValueValidator VECTOR = new IValueValidator<>() { + @Override + public Vector validate(String str) { + return LocationUtil.deserializeVector(str); + } + }; +} diff --git a/src/cz/marwland/mc/core/features/Feature.java b/src/cz/marwland/mc/core/features/Feature.java index 6ed1e71..ca2a1b2 100644 --- a/src/cz/marwland/mc/core/features/Feature.java +++ b/src/cz/marwland/mc/core/features/Feature.java @@ -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 commands = new ArrayList<>(); - private HashMap 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() { @@ -50,7 +46,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); } diff --git a/src/cz/marwland/mc/essentials/features/Borders.java b/src/cz/marwland/mc/essentials/features/Borders.java index c042c07..44585a8 100644 --- a/src/cz/marwland/mc/essentials/features/Borders.java +++ b/src/cz/marwland/mc/essentials/features/Borders.java @@ -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 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() { + @Override + public void doStuff(Object val) { + w.getWorldBorder().setSize((int) val); + } + }); + emgr.getEntry("center").doStuff(cfg, wname, new IValueExecuter() { + @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"));*/ } }