mirror of
https://github.com/Nan1t/NanoLimbo.git
synced 2025-07-15 13:40:14 +02:00
Rewrite confguration and serializers to new config library
This commit is contained in:
parent
168faf9eea
commit
8ba8760925
@ -1,13 +1,17 @@
|
||||
package ru.nanit.limbo.configuration;
|
||||
|
||||
import napi.configurate.Configuration;
|
||||
import napi.configurate.source.ConfigSources;
|
||||
import napi.configurate.yaml.YamlConfiguration;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurationOptions;
|
||||
import org.spongepowered.configurate.serialize.TypeSerializerCollection;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
import ru.nanit.limbo.server.data.*;
|
||||
import ru.nanit.limbo.util.Colors;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public final class LimboConfig {
|
||||
|
||||
@ -39,34 +43,62 @@ public final class LimboConfig {
|
||||
}
|
||||
|
||||
public void load() throws Exception {
|
||||
Configuration conf = YamlConfiguration.builder()
|
||||
.source(ConfigSources.resource("/settings.yml", this).copyTo(root))
|
||||
ConfigurationOptions options = ConfigurationOptions.defaults().serializers(getSerializers());
|
||||
YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
|
||||
.source(this::getReader)
|
||||
.defaultOptions(options)
|
||||
.build();
|
||||
|
||||
conf.reload();
|
||||
ConfigurationNode conf = loader.load();
|
||||
|
||||
address = conf.getNode("bind").getValue(SocketAddress.class);
|
||||
maxPlayers = conf.getNode("maxPlayers").getInt();
|
||||
pingData = conf.getNode("ping").getValue(PingData.class);
|
||||
dimensionType = conf.getNode("dimension").getString();
|
||||
spawnPosition = conf.getNode("spawnPosition").getValue(Position.class);
|
||||
gameMode = conf.getNode("gameMode").getInt();
|
||||
useJoinMessage = conf.getNode("joinMessage", "enable").getBoolean();
|
||||
useBossBar = conf.getNode("bossBar", "enable").getBoolean();
|
||||
address = conf.node("bind").get(SocketAddress.class);
|
||||
maxPlayers = conf.node("maxPlayers").getInt();
|
||||
pingData = conf.node("ping").get(PingData.class);
|
||||
dimensionType = conf.node("dimension").getString();
|
||||
spawnPosition = conf.node("spawnPosition").get(Position.class);
|
||||
gameMode = conf.node("gameMode").getInt();
|
||||
useJoinMessage = conf.node("joinMessage", "enable").getBoolean();
|
||||
useBossBar = conf.node("bossBar", "enable").getBoolean();
|
||||
|
||||
if (useJoinMessage)
|
||||
joinMessage = Colors.of(conf.getNode("joinMessage", "text").getString());
|
||||
joinMessage = Colors.of(conf.node("joinMessage", "text").getString(""));
|
||||
|
||||
if (useBossBar)
|
||||
bossBar = conf.getNode("bossBar").getValue(BossBar.class);
|
||||
bossBar = conf.node("bossBar").get(BossBar.class);
|
||||
|
||||
infoForwarding = conf.getNode("infoForwarding").getValue(InfoForwarding.class);
|
||||
readTimeout = conf.getNode("readTimeout").getLong();
|
||||
debugLevel = conf.getNode("debugLevel").getInt();
|
||||
infoForwarding = conf.node("infoForwarding").get(InfoForwarding.class);
|
||||
readTimeout = conf.node("readTimeout").getLong();
|
||||
debugLevel = conf.node("debugLevel").getInt();
|
||||
|
||||
useEpoll = conf.getNode("netty", "useEpoll").getBoolean(true);
|
||||
bossGroupSize = conf.getNode("netty", "threads", "bossGroup").getInt(1);
|
||||
workerGroupSize = conf.getNode("netty", "threads", "workerGroup").getInt(4);
|
||||
useEpoll = conf.node("netty", "useEpoll").getBoolean(true);
|
||||
bossGroupSize = conf.node("netty", "threads", "bossGroup").getInt(1);
|
||||
workerGroupSize = conf.node("netty", "threads", "workerGroup").getInt(4);
|
||||
}
|
||||
|
||||
private BufferedReader getReader() throws IOException {
|
||||
String name = "settings.yml";
|
||||
Path filePath = Paths.get(root.toString(), name);
|
||||
|
||||
if (!Files.exists(filePath)) {
|
||||
InputStream stream = getClass().getResourceAsStream( "/" + name);
|
||||
|
||||
if (stream == null)
|
||||
throw new FileNotFoundException("Cannot find settings resource file");
|
||||
|
||||
Files.copy(stream, filePath);
|
||||
}
|
||||
|
||||
return Files.newBufferedReader(filePath);
|
||||
}
|
||||
|
||||
private TypeSerializerCollection getSerializers() {
|
||||
return TypeSerializerCollection.builder()
|
||||
.register(SocketAddress.class, new SocketAddressSerializer())
|
||||
.register(InfoForwarding.class, new InfoForwarding.Serializer())
|
||||
.register(PingData.class, new PingData.Serializer())
|
||||
.register(BossBar.class, new BossBar.Serializer())
|
||||
.register(Position.class, new Position.Serializer())
|
||||
.build();
|
||||
}
|
||||
|
||||
public SocketAddress getAddress() {
|
||||
|
@ -1,18 +1,20 @@
|
||||
package ru.nanit.limbo.configuration;
|
||||
|
||||
import napi.configurate.data.ConfigNode;
|
||||
import napi.configurate.serializing.NodeSerializer;
|
||||
import napi.configurate.serializing.NodeSerializingException;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.serialize.SerializationException;
|
||||
import org.spongepowered.configurate.serialize.TypeSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
|
||||
public class SocketAddressSerializer implements NodeSerializer<SocketAddress> {
|
||||
public class SocketAddressSerializer implements TypeSerializer<SocketAddress> {
|
||||
|
||||
@Override
|
||||
public SocketAddress deserialize(ConfigNode node) {
|
||||
String ip = node.getNode("ip").getString();
|
||||
int port = node.getNode("port").getInt();
|
||||
public SocketAddress deserialize(Type type, ConfigurationNode node) {
|
||||
String ip = node.node("ip").getString();
|
||||
int port = node.node("port").getInt();
|
||||
SocketAddress address;
|
||||
|
||||
if (ip == null || ip.isEmpty()){
|
||||
@ -25,7 +27,7 @@ public class SocketAddressSerializer implements NodeSerializer<SocketAddress> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(SocketAddress socketAddress, ConfigNode configNode) throws NodeSerializingException {
|
||||
public void serialize(Type type, @Nullable SocketAddress obj, ConfigurationNode node) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -10,16 +10,12 @@ import io.netty.channel.epoll.EpollServerSocketChannel;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.util.ResourceLeakDetector;
|
||||
import napi.configurate.serializing.NodeSerializers;
|
||||
import ru.nanit.limbo.configuration.LimboConfig;
|
||||
import ru.nanit.limbo.configuration.SocketAddressSerializer;
|
||||
import ru.nanit.limbo.connection.ClientChannelInitializer;
|
||||
import ru.nanit.limbo.connection.ClientConnection;
|
||||
import ru.nanit.limbo.server.data.*;
|
||||
import ru.nanit.limbo.util.Logger;
|
||||
import ru.nanit.limbo.world.DimensionRegistry;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -51,12 +47,6 @@ public final class LimboServer {
|
||||
|
||||
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.DISABLED);
|
||||
|
||||
NodeSerializers.register(SocketAddress.class, new SocketAddressSerializer());
|
||||
NodeSerializers.register(InfoForwarding.class, new InfoForwarding.Serializer());
|
||||
NodeSerializers.register(PingData.class, new PingData.Serializer());
|
||||
NodeSerializers.register(BossBar.class, new BossBar.Serializer());
|
||||
NodeSerializers.register(Position.class, new Position.Serializer());
|
||||
|
||||
config = new LimboConfig(Paths.get("./"));
|
||||
config.load();
|
||||
|
||||
|
@ -1,10 +1,13 @@
|
||||
package ru.nanit.limbo.server.data;
|
||||
|
||||
import napi.configurate.data.ConfigNode;
|
||||
import napi.configurate.serializing.NodeSerializer;
|
||||
import napi.configurate.serializing.NodeSerializingException;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.serialize.SerializationException;
|
||||
import org.spongepowered.configurate.serialize.TypeSerializer;
|
||||
import ru.nanit.limbo.util.Colors;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class BossBar {
|
||||
|
||||
private String text;
|
||||
@ -84,35 +87,35 @@ public class BossBar {
|
||||
}
|
||||
}
|
||||
|
||||
public static class Serializer implements NodeSerializer<BossBar>{
|
||||
public static class Serializer implements TypeSerializer<BossBar> {
|
||||
|
||||
@Override
|
||||
public BossBar deserialize(ConfigNode node) throws NodeSerializingException {
|
||||
public BossBar deserialize(Type type, ConfigurationNode node) throws SerializationException {
|
||||
BossBar bossBar = new BossBar();
|
||||
|
||||
bossBar.setText(Colors.of(node.getNode("text").getString()));
|
||||
bossBar.setHealth(node.getNode("health").getFloat());
|
||||
bossBar.setText(Colors.of(node.node("text").getString("")));
|
||||
bossBar.setHealth(node.node("health").getFloat());
|
||||
|
||||
if (bossBar.getHealth() < 0 || bossBar.getHealth() > 1)
|
||||
throw new NodeSerializingException("BossBar health value must be between 0.0 and 1.0");
|
||||
throw new SerializationException("BossBar health value must be between 0.0 and 1.0");
|
||||
|
||||
try {
|
||||
bossBar.setColor(Color.valueOf(node.getNode("color").getString().toUpperCase()));
|
||||
bossBar.setColor(Color.valueOf(node.node("color").getString("").toUpperCase()));
|
||||
} catch (IllegalArgumentException e){
|
||||
throw new NodeSerializingException("Invalid bossbar color");
|
||||
throw new SerializationException("Invalid bossbar color");
|
||||
}
|
||||
|
||||
try {
|
||||
bossBar.setDivision(Division.valueOf(node.getNode("division").getString().toUpperCase()));
|
||||
bossBar.setDivision(Division.valueOf(node.node("division").getString("").toUpperCase()));
|
||||
} catch (IllegalArgumentException e){
|
||||
throw new NodeSerializingException("Invalid bossbar division");
|
||||
throw new SerializationException("Invalid bossbar division");
|
||||
}
|
||||
|
||||
return bossBar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(BossBar bossBar, ConfigNode configNode) {
|
||||
public void serialize(Type type, @Nullable BossBar obj, ConfigurationNode node) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package ru.nanit.limbo.server.data;
|
||||
|
||||
import napi.configurate.data.ConfigNode;
|
||||
import napi.configurate.serializing.NodeSerializer;
|
||||
import napi.configurate.serializing.NodeSerializingException;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.serialize.SerializationException;
|
||||
import org.spongepowered.configurate.serialize.TypeSerializer;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@ -37,27 +38,27 @@ public class InfoForwarding {
|
||||
MODERN
|
||||
}
|
||||
|
||||
public static class Serializer implements NodeSerializer<InfoForwarding> {
|
||||
public static class Serializer implements TypeSerializer<InfoForwarding> {
|
||||
|
||||
@Override
|
||||
public InfoForwarding deserialize(ConfigNode node) throws NodeSerializingException {
|
||||
public InfoForwarding deserialize(java.lang.reflect.Type type, ConfigurationNode node) throws SerializationException {
|
||||
InfoForwarding forwarding = new InfoForwarding();
|
||||
|
||||
try {
|
||||
forwarding.type = Type.valueOf(node.getNode("type").getString().toUpperCase());
|
||||
forwarding.type = Type.valueOf(node.node("type").getString("").toUpperCase());
|
||||
} catch (IllegalArgumentException e){
|
||||
throw new NodeSerializingException("Undefined info forwarding type");
|
||||
throw new SerializationException("Undefined info forwarding type");
|
||||
}
|
||||
|
||||
if (forwarding.type == Type.MODERN){
|
||||
forwarding.secretKey = node.getNode("secret").getString().getBytes(StandardCharsets.UTF_8);
|
||||
forwarding.secretKey = node.node("secret").getString("").getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
return forwarding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(InfoForwarding infoForwarding, ConfigNode configNode) {
|
||||
public void serialize(java.lang.reflect.Type type, @Nullable InfoForwarding obj, ConfigurationNode node) throws SerializationException {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
package ru.nanit.limbo.server.data;
|
||||
|
||||
import napi.configurate.data.ConfigNode;
|
||||
import napi.configurate.serializing.NodeSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.serialize.TypeSerializer;
|
||||
import ru.nanit.limbo.util.Colors;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class PingData {
|
||||
|
||||
private String version;
|
||||
@ -25,18 +28,18 @@ public class PingData {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public static class Serializer implements NodeSerializer<PingData> {
|
||||
public static class Serializer implements TypeSerializer<PingData> {
|
||||
|
||||
@Override
|
||||
public PingData deserialize(ConfigNode node) {
|
||||
public PingData deserialize(Type type, ConfigurationNode node) {
|
||||
PingData pingData = new PingData();
|
||||
pingData.setDescription(Colors.of(node.getNode("description").getString()));
|
||||
pingData.setVersion(Colors.of(node.getNode("version").getString()));
|
||||
pingData.setDescription(Colors.of(node.node("description").getString("")));
|
||||
pingData.setVersion(Colors.of(node.node("version").getString("")));
|
||||
return pingData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(PingData pingData, ConfigNode configNode) {
|
||||
public void serialize(Type type, @Nullable PingData obj, ConfigurationNode node) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package ru.nanit.limbo.server.data;
|
||||
|
||||
import napi.configurate.data.ConfigNode;
|
||||
import napi.configurate.serializing.NodeSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.serialize.TypeSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class Position {
|
||||
|
||||
@ -51,21 +54,21 @@ public class Position {
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public static class Serializer implements NodeSerializer<Position> {
|
||||
public static class Serializer implements TypeSerializer<Position> {
|
||||
|
||||
@Override
|
||||
public Position deserialize(ConfigNode node) {
|
||||
public Position deserialize(Type type, ConfigurationNode node) {
|
||||
Position position = new Position();
|
||||
position.setX(node.getNode("x").getDouble());
|
||||
position.setY(node.getNode("y").getDouble());
|
||||
position.setZ(node.getNode("z").getDouble());
|
||||
position.setYaw(node.getNode("yaw").getFloat());
|
||||
position.setPitch(node.getNode("pitch").getFloat());
|
||||
position.setX(node.node("x").getDouble());
|
||||
position.setY(node.node("y").getDouble());
|
||||
position.setZ(node.node("z").getDouble());
|
||||
position.setYaw(node.node("yaw").getFloat());
|
||||
position.setPitch(node.node("pitch").getFloat());
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Position position, ConfigNode configNode) {
|
||||
public void serialize(Type type, @Nullable Position obj, ConfigurationNode node) {
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user