change port, config and version mechanisms

This commit is contained in:
Sen 2025-05-26 13:27:03 +02:00
parent f879b060e8
commit adc81d56d2
14 changed files with 122 additions and 74 deletions

View file

@ -0,0 +1,11 @@
package common;
import common.util.ReleaseType;
public abstract class Version {
public static final String NAME = "TCR";
public static final ReleaseType RELEASE = ReleaseType.DEV;
public static final int MAJOR = 2;
public static final int MINOR = 2;
public static final int PATCH = 1;
}

View file

@ -23,11 +23,13 @@ public abstract class Config {
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;
@ -41,6 +43,7 @@ public abstract class Config {
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;
@ -59,7 +62,7 @@ public abstract class Config {
// throw new RuntimeException(e);
// }
// }
this.setValue(this.def);
this.setValue(this.def, this.noDef);
}
public String getValue() {
@ -82,7 +85,7 @@ public abstract class Config {
}
}
private void setValue(String value) {
private void setValue(String value, boolean noClamp) {
try {
switch(this.type) {
case STRING:
@ -98,7 +101,7 @@ public abstract class Config {
}
catch(NumberFormatException e) {
}
this.field.setInt(null, ExtMath.clampi(inum, (int)this.min, (int)this.max));
this.field.setInt(null, noClamp ? inum : ExtMath.clampi(inum, (int)this.min, (int)this.max));
break;
case FLOAT:
float fnum = 0.0f;
@ -107,7 +110,8 @@ public abstract class Config {
}
catch(NumberFormatException e) {
}
fnum = ExtMath.clampf(fnum, this.min, this.max);
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;
@ -118,12 +122,6 @@ public abstract class Config {
}
}
}
public static final int PROTOCOL = 666;
public static final int PORT = 26666;
public static final String NAME = "TCR";
public static final String VERSION = "v2.2.1-alpha";
public static final String CLIENT_VERSION = NAME + " Client " + VERSION;
public static final Map<String, Config.Value> VARS = new TreeMap();
@ -439,6 +437,8 @@ public abstract class Config {
public static int timeout = 30;
@Var(name = "passwordMinLength", min = 1, max = 32)
public static int minPassLength = 8;
@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;
@ -479,7 +479,7 @@ public abstract class Config {
public static void clear() {
for(Config.Value value : VARS.values()) {
value.setValue(value.def);
value.setValue(value.def, value.noDef);
}
}
@ -492,7 +492,7 @@ public abstract class Config {
public static void set(String key, String value, boolean update) {
Config.Value vl = VARS.get(key);
if(vl != null) {
vl.setValue(value);
vl.setValue(value, false);
if(update && vl.callback != null)
vl.callback.run();
}

View file

@ -0,0 +1,24 @@
package common.util;
public enum ReleaseType {
DEV("dev", 3),
ALPHA("alpha", 2),
BETA("beta", 1),
STABLE(null, 0);
private final String suffix;
private final int id;
private ReleaseType(String suffix, int id) {
this.suffix = suffix == null ? "" : "-" + suffix;
this.id = id;
}
public String toString() {
return this.suffix;
}
public int getId() {
return this.id;
}
}

View file

@ -7,13 +7,16 @@ import java.util.function.Function;
import javax.swing.JOptionPane;
import common.Version;
import common.collect.Lists;
import common.collect.Maps;
import common.log.Log;
public abstract class Util {
public static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");
private static final long START = getTime();
public static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");
public static final int PROTOCOL = Version.MAJOR << 16 | Version.MINOR << 8 | Version.PATCH;
public static final String VERSION = "v" + Version.MAJOR + "." + Version.MINOR + "." + Version.PATCH + Version.RELEASE;
public static String strip(String str, int offset, int len, char newl, char tab, char unk) {
StringBuilder sb = new StringBuilder();
@ -397,4 +400,12 @@ int utf_len(const char *str) {
public static String getRegionName(int x, int z) {
return String.format("r.%c%X%c%X.rgn", x < 0 ? 'n' : 'p', ((x < 0) ? -x : x) >> 3, z < 0 ? 'n' : 'p', ((z < 0) ? -z : z) >> 3);
}
public static String repeatString(Object obj, int count) {
StringBuilder sb = new StringBuilder();
for(int z = 0; z < count; z++) {
sb.append(obj);
}
return sb.toString();
}
}