change svar implementation

This commit is contained in:
Sen 2025-05-31 22:56:43 +02:00
parent 367e6f5bfd
commit 126ce64a24
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
7 changed files with 171 additions and 214 deletions

View file

@ -1,130 +1,8 @@
package common.init;
import static java.lang.annotation.ElementType.FIELD;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.TreeMap;
import common.util.ExtMath;
import common.util.Var;
public abstract class Config {
public static enum ValueType {
STRING, BOOLEAN, INTEGER, FLOAT;
}
@Target(FIELD)
@Retention(value = RetentionPolicy.RUNTIME)
private static @interface Var {
String name();
float min() default (float)Integer.MIN_VALUE;
float max() default (float)Integer.MAX_VALUE;
boolean nonDefault() default false;
}
public static class Value {
public final ValueType type;
public final String def;
public final boolean noDef;
private final Field field;
private final float min;
private final float max;
private Runnable callback;
private Value(Field field, Var value) {
this.type = field.getType() == int.class ? ValueType.INTEGER : (field.getType() == boolean.class ? ValueType.BOOLEAN :
(field.getType() == String.class ? ValueType.STRING : (field.getType() == float.class ? ValueType.FLOAT : null)));
if(this.type == null)
throw new IllegalArgumentException(value.name() + ": Unbekannter Variablen-Typ - " + field.getType());
this.field = field;
this.def = this.getValue();
this.noDef = value.nonDefault();
// Clamped clamp = this.field.getAnnotation(Clamped.class);
this.min = (this.type == ValueType.INTEGER || this.type == ValueType.FLOAT)
? value.min() : 0;
this.max = (this.type == ValueType.INTEGER || this.type == ValueType.FLOAT)
? value.max() : (this.type == ValueType.BOOLEAN ? 1 : 0);
// Update update = this.field.getAnnotation(Update.class);
// if(value.callback() == Callback.class) {
// this.callback = null;
// }
// else {
// try {
// this.callback = value.callback().getConstructor().newInstance();
// }
// catch(InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
// | NoSuchMethodException e) {
// throw new RuntimeException(e);
// }
// }
this.setValue(this.def, this.noDef);
}
public String getValue() {
try {
return "" + this.field.get(null);
// switch(this.type) {
// case STRING:
// default:
// return (String)this.field.get(null);
// case BOOLEAN:
// return "" + this.field.getBoolean(null);
// case INTEGER:
// return "" + this.field.getInt(null);
// case FLOAT:
// return "" + this.field.getFloat(null);
// }
}
catch(IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private void setValue(String value, boolean noClamp) {
try {
switch(this.type) {
case STRING:
this.field.set(null, value);
break;
case BOOLEAN:
this.field.setBoolean(null, Boolean.parseBoolean(value));
break;
case INTEGER:
int inum = 0;
try {
inum = Integer.parseInt(value);
}
catch(NumberFormatException e) {
}
this.field.setInt(null, noClamp ? inum : ExtMath.clampi(inum, (int)this.min, (int)this.max));
break;
case FLOAT:
float fnum = 0.0f;
try {
fnum = Float.parseFloat(value);
}
catch(NumberFormatException e) {
}
if(!noClamp)
fnum = ExtMath.clampf(fnum, this.min, this.max);
int round = (int)(fnum * 1000.0f);
this.field.setFloat(null, (float)round / 1000.0f);
break;
}
}
catch(IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
public static final Map<String, Config.Value> VARS = new TreeMap();
@Var(name = "fireTick")
public static boolean fire = true;
@Var(name = "mobGriefing")
@ -205,8 +83,6 @@ public abstract class Config {
public static boolean mergeInfinite = true;
@Var(name = "infighting")
public static boolean infight = true;
// @Var(name = "infightSameType")
// public static boolean infightSame = true;
@Var(name = "damageFall")
public static boolean damageFall = true;
@Var(name = "damageFire")
@ -450,15 +326,6 @@ public abstract class Config {
@Var(name = "port", min = 1024, max = 32767, nonDefault = true)
public static int port = -1;
// @Var(name = "spawnX", min = -World.MAX_SIZE + 1, max = World.MAX_SIZE - 1)
// public static int spawnX = 0;
// @Var(name = "spawnY", min = -1, max = 511)
// public static int spawnY = 64;
// @Var(name = "spawnZ", min = -World.MAX_SIZE + 1, max = World.MAX_SIZE - 1)
// public static int spawnZ = 0;
// @Var(name = "spawnDim")
// public static int spawnDim = 0;
@Var(name = "gravity")
public static float gravity = 1.0f;
@Var(name = "knockback")
@ -468,40 +335,4 @@ public abstract class Config {
public static String password = "";
@Var(name = "baseSeed", nonDefault = true)
public static String seed = "";
static {
for(Field field : Config.class.getDeclaredFields()) {
if(field.isAnnotationPresent(Var.class)) {
if(!Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()))
throw new IllegalArgumentException("Feld für Variable " + field + " muss statisch und änderbar sein!");
Var value = field.getAnnotation(Var.class);
if(value.name().isEmpty())
throw new IllegalArgumentException("Variablenname von " + field + " kann nicht leer sein!");
if(VARS.containsKey(value.name()))
throw new IllegalArgumentException("Variable " + value.name() + " existiert bereits!");
VARS.put(value.name(), new Config.Value(field, value));
}
}
}
public static void clear() {
for(Config.Value value : VARS.values()) {
value.setValue(value.def, value.noDef);
}
}
public static void setCallback(Runnable callback, String ... vars) {
for(String key : vars) {
VARS.get(key).callback = callback;
}
}
public static void set(String key, String value, boolean update) {
Config.Value vl = VARS.get(key);
if(vl != null) {
vl.setValue(value, false);
if(update && vl.callback != null)
vl.callback.run();
}
}
}

View file

@ -0,0 +1,16 @@
package common.util;
import static java.lang.annotation.ElementType.FIELD;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(FIELD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Var {
String name();
float min() default (float)Integer.MIN_VALUE;
float max() default (float)Integer.MAX_VALUE;
boolean nonDefault() default false;
}