borders: finally works

This commit is contained in:
Erik Bročko 2018-09-05 01:08:25 +02:00
parent 6482b03c2f
commit 3bcccbcd2a
5 changed files with 130 additions and 40 deletions

View File

@ -1,10 +1,14 @@
package cz.marwland.mc.core.config;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.util.Vector;
import cz.marwland.mc.core.util.LocationUtil;
public class ConfigEntry<T> {
private final String subpath;
private IValueValidator<T> validator;
private T defval;
public ConfigEntry(String subpath) {
this.subpath = subpath;
@ -15,6 +19,12 @@ public class ConfigEntry<T> {
this.validator = validator;
}
public ConfigEntry(String subpath, IValueValidator<T> validator, T defval) {
this.subpath = subpath;
this.validator = validator;
this.defval = defval;
}
public void setValidator(IValueValidator<T> validator) {
this.validator = validator;
}
@ -23,6 +33,10 @@ public class ConfigEntry<T> {
return this.subpath;
}
public T getDefaultValue() {
return this.defval;
}
public T validate(String s) {
if (validator == null)
return null;
@ -33,7 +47,7 @@ public class ConfigEntry<T> {
T o = validate(value);
if (o == null)
return false;
cfg.set(parent + "." + subpath, o);
cfg.set(parent + "." + subpath, (o instanceof Vector) ? LocationUtil.serializeVector((Vector) o) : o);
return true;
}
@ -44,7 +58,10 @@ public class ConfigEntry<T> {
// 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));
Object val = this.getFrom(cfg, parent);
if (val == null)
return;
iValueExecuter.doStuff(val);
}
}

View File

@ -1,5 +1,6 @@
package cz.marwland.mc.core.config;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
@ -28,6 +29,9 @@ public class EntryManager {
return entries.keySet();
}
public Collection<ConfigEntry<?>> getEntries() {
return entries.values();
}
public boolean hasKey(String subpath) {
return entries.get(subpath) == null;
}

View File

@ -58,6 +58,8 @@ public class ValueValidator {
public static final IValueValidator<Double> DOUBLE = new IValueValidator<>() {
@Override
public Double validate(String str) {
if (str == null)
return null;
try {
return Double.parseDouble(str);
} catch (NumberFormatException ex) {
@ -84,6 +86,8 @@ public class ValueValidator {
public static final IValueValidator<Vector> VECTOR = new IValueValidator<>() {
@Override
public Vector validate(String str) {
if (str == null)
return null;
return LocationUtil.deserializeVector(str);
}
};

View File

@ -22,4 +22,12 @@ public class LocationUtil {
}
return new Vector(vals[0], vals[1], vals[2]);
}
public static String serializeVector(Vector vec) {
return vec.getX() + " " + vec.getY() + " " + vec.getZ();
}
public static String serialize2DVector(Vector vec) {
return vec.getX() + " " + vec.getZ();
}
}

View File

@ -1,11 +1,15 @@
package cz.marwland.mc.essentials.features;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.util.StringUtil;
import org.bukkit.util.Vector;
import cz.marwland.mc.core.MarwCore;
@ -31,8 +35,13 @@ public class Borders extends Feature {
Arrays.asList() ) {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (args.length < 2)
return false;
if (args.length < 2) {
sender.sendMessage(
ChatColor.YELLOW + "Format: " +
ChatColor.GRAY + ChatColor.ITALIC + "/" + commandLabel +
ChatColor.YELLOW + ChatColor.GRAY + " (world) (key) [value]");
return true;
}
World w = plugin.getServer().getWorld(args[0]);
if (w == null) {
sender.sendMessage(ChatColor.RED + "Invalid world!");
@ -42,22 +51,44 @@ public class Borders extends Feature {
ConfigEntry<?> entry = emgr.getEntry(args[1]);
if (entry == null) {
sender.sendMessage(
ChatColor.RED + "Invalid key '" +
ChatColor.RED + "Invalid key " +
ChatColor.YELLOW + args[1] +
ChatColor.RED + "'! Available: " +
ChatColor.RED + "! Available: " +
ChatColor.GRAY + String.join(", ", emgr.getKeys()));
return false;
return true;
}
if (args.length == 2) { // get
if (!cfg.contains(w.getName())) {
sender.sendMessage(
ChatColor.RED + "No border specified for " +
ChatColor.YELLOW + args[0] +
ChatColor.RED + "!");
return true;
}
Object val = entry.getFrom(cfg, w.getName());
System.out.println("sending");
sender.sendMessage(
ChatColor.YELLOW + args[1] +
ChatColor.GRAY + " @ " +
ChatColor.AQUA + w.getName() +
ChatColor.GRAY + ": " + entry.getFrom(cfg, w.getName()));
ChatColor.GRAY + ": " + (val == null ? ChatColor.ITALIC + "[" + entry.getDefaultValue() + "]": val));
} else { // set
String oldval = "" + entry.getFrom(cfg, w.getName());
String newval = String.join(" ", args).substring(args[0].length() + args[1].length() + 2);
if (entry.setIn(cfg, w.getName(), newval)) {
plugin.getConfigManager().save();
cfg = getConfig("borders.yml");
loadWorlds();
sender.sendMessage(
ChatColor.GREEN + "Changed " +
ChatColor.YELLOW + entry.getSubpath() +
ChatColor.GRAY + " @ " +
ChatColor.AQUA + w.getName() +
ChatColor.GRAY + ": " + oldval +
ChatColor.GOLD + " => " +
ChatColor.GRAY + newval);
} else {
sender.sendMessage(
ChatColor.RED + "Invalid value for " +
ChatColor.YELLOW + args[1] +
@ -67,47 +98,84 @@ public class Borders extends Feature {
}
return true;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
ArrayList<String> hints = new ArrayList<String>();
if (args.length < 1)
return hints;
String lastWord = args[args.length - 1];
if (args.length == 1) {
for (World w : plugin.getServer().getWorlds()) {
if (StringUtil.startsWithIgnoreCase(w.getName(), lastWord))
hints.add(w.getName());
}
} else if(args.length == 2) {
for (String key : emgr.getKeys()) {
if (StringUtil.startsWithIgnoreCase(key, lastWord))
hints.add(key);
}
}
Collections.sort(hints, String.CASE_INSENSITIVE_ORDER);
return hints;
}
});
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));
emgr.addEntry(new ConfigEntry<>("center", ValueValidator.VECTOR, new Vector(0, 0, 0)));
// emgr.addEntry(new ConfigEntry<>("size", ValueValidator.DOUBLE));
emgr.addEntry(new ConfigEntry<>("radius", ValueValidator.INTEGER, 60000000));
emgr.addEntry(new ConfigEntry<>("warningdist", ValueValidator.INTEGER, 5));
emgr.addEntry(new ConfigEntry<>("warningtime", ValueValidator.INTEGER, 15));
emgr.addEntry(new ConfigEntry<>("damage", ValueValidator.DOUBLE, 0.2d));
emgr.addEntry(new ConfigEntry<>("buffer", ValueValidator.DOUBLE, 5d));
this.registerConfig("borders.yml");
}
@Override
public void onEnable() {
public void onEnable() {
super.onEnable();
cfg = getConfig("borders.yml");
this.loadWorlds();
}
@Override
public void onReload() {
cfg = getConfig("borders.yml");
this.loadWorlds();
}
public void loadWorlds() {
for(World w : plugin.getServer().getWorlds()) {
final String wname = w.getName();
if (!cfg.contains(wname))
if (!cfg.contains(wname)) {
w.getWorldBorder().reset();
continue;
plugin.getLogger().info("Loading borders for " + wname);
}
emgr.getEntry("center").doStuff(cfg, wname, new IValueExecuter<Object>() {
public void doStuff(Object val) {
Vector v = (Vector) val;
w.getWorldBorder().setCenter(v.getX(), v.getZ());
}
});
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
emgr.getEntry("warningdist").doStuff(cfg, wname, new IValueExecuter<Object>() {
public void doStuff(Object val) {
Vector v = (Vector) val;
w.getWorldBorder().setCenter(v.getX(), v.getZ());
w.getWorldBorder().setWarningDistance((int) val);
}
});
emgr.getEntry("warningtime").doStuff(cfg, wname, new IValueExecuter<Object>() {
public void doStuff(Object val) {
w.getWorldBorder().setWarningTime((int) val);
}
});
emgr.getEntry("damage").doStuff(cfg, wname, new IValueExecuter<Object>() {
public void doStuff(Object val) {
w.getWorldBorder().setDamageAmount((double) val);
}
});
emgr.getEntry("buffer").doStuff(cfg, wname, new IValueExecuter<Object>() {
public void doStuff(Object val) {
w.getWorldBorder().setDamageBuffer((double) val);
}
});
/*
@ -148,17 +216,6 @@ public class Borders extends Feature {
w.getWorldBorder().setCenter(c.getX(), c.getZ());
}
*/
/*if (cfg.contains(wname + ".radius"))
w.getWorldBorder().setSize(cfg.getInt(wname + ".radius"));
if (cfg.contains(wname + ".warningdist"))
w.getWorldBorder().setWarningDistance(cfg.getInt(wname + ".warningdist"));
if (cfg.contains(wname + ".warningtime"))
w.getWorldBorder().setWarningTime(cfg.getInt(wname + ".warningtime"));
if (cfg.contains(wname + ".damage"))
w.getWorldBorder().setDamageAmount(cfg.getDouble(wname + ".damage"));
if (cfg.contains(wname + ".buffer"))
w.getWorldBorder().setDamageBuffer(cfg.getDouble(wname + ".buffer"));*/
}
}