borders: finally works

This commit is contained in:
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();
}
}