Rewrite confguration and serializers to new config library

This commit is contained in:
Nanit 2021-10-28 20:53:46 +03:00
parent 168faf9eea
commit 8ba8760925
7 changed files with 113 additions and 79 deletions

View File

@ -1,13 +1,17 @@
package ru.nanit.limbo.configuration; package ru.nanit.limbo.configuration;
import napi.configurate.Configuration; import org.spongepowered.configurate.ConfigurationNode;
import napi.configurate.source.ConfigSources; import org.spongepowered.configurate.ConfigurationOptions;
import napi.configurate.yaml.YamlConfiguration; import org.spongepowered.configurate.serialize.TypeSerializerCollection;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import ru.nanit.limbo.server.data.*; import ru.nanit.limbo.server.data.*;
import ru.nanit.limbo.util.Colors; import ru.nanit.limbo.util.Colors;
import java.io.*;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
public final class LimboConfig { public final class LimboConfig {
@ -39,34 +43,62 @@ public final class LimboConfig {
} }
public void load() throws Exception { public void load() throws Exception {
Configuration conf = YamlConfiguration.builder() ConfigurationOptions options = ConfigurationOptions.defaults().serializers(getSerializers());
.source(ConfigSources.resource("/settings.yml", this).copyTo(root)) YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
.source(this::getReader)
.defaultOptions(options)
.build(); .build();
conf.reload(); ConfigurationNode conf = loader.load();
address = conf.getNode("bind").getValue(SocketAddress.class); address = conf.node("bind").get(SocketAddress.class);
maxPlayers = conf.getNode("maxPlayers").getInt(); maxPlayers = conf.node("maxPlayers").getInt();
pingData = conf.getNode("ping").getValue(PingData.class); pingData = conf.node("ping").get(PingData.class);
dimensionType = conf.getNode("dimension").getString(); dimensionType = conf.node("dimension").getString();
spawnPosition = conf.getNode("spawnPosition").getValue(Position.class); spawnPosition = conf.node("spawnPosition").get(Position.class);
gameMode = conf.getNode("gameMode").getInt(); gameMode = conf.node("gameMode").getInt();
useJoinMessage = conf.getNode("joinMessage", "enable").getBoolean(); useJoinMessage = conf.node("joinMessage", "enable").getBoolean();
useBossBar = conf.getNode("bossBar", "enable").getBoolean(); useBossBar = conf.node("bossBar", "enable").getBoolean();
if (useJoinMessage) if (useJoinMessage)
joinMessage = Colors.of(conf.getNode("joinMessage", "text").getString()); joinMessage = Colors.of(conf.node("joinMessage", "text").getString(""));
if (useBossBar) if (useBossBar)
bossBar = conf.getNode("bossBar").getValue(BossBar.class); bossBar = conf.node("bossBar").get(BossBar.class);
infoForwarding = conf.getNode("infoForwarding").getValue(InfoForwarding.class); infoForwarding = conf.node("infoForwarding").get(InfoForwarding.class);
readTimeout = conf.getNode("readTimeout").getLong(); readTimeout = conf.node("readTimeout").getLong();
debugLevel = conf.getNode("debugLevel").getInt(); debugLevel = conf.node("debugLevel").getInt();
useEpoll = conf.getNode("netty", "useEpoll").getBoolean(true); useEpoll = conf.node("netty", "useEpoll").getBoolean(true);
bossGroupSize = conf.getNode("netty", "threads", "bossGroup").getInt(1); bossGroupSize = conf.node("netty", "threads", "bossGroup").getInt(1);
workerGroupSize = conf.getNode("netty", "threads", "workerGroup").getInt(4); 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() { public SocketAddress getAddress() {

View File

@ -1,18 +1,20 @@
package ru.nanit.limbo.configuration; package ru.nanit.limbo.configuration;
import napi.configurate.data.ConfigNode; import org.checkerframework.checker.nullness.qual.Nullable;
import napi.configurate.serializing.NodeSerializer; import org.spongepowered.configurate.ConfigurationNode;
import napi.configurate.serializing.NodeSerializingException; import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
public class SocketAddressSerializer implements NodeSerializer<SocketAddress> { public class SocketAddressSerializer implements TypeSerializer<SocketAddress> {
@Override @Override
public SocketAddress deserialize(ConfigNode node) { public SocketAddress deserialize(Type type, ConfigurationNode node) {
String ip = node.getNode("ip").getString(); String ip = node.node("ip").getString();
int port = node.getNode("port").getInt(); int port = node.node("port").getInt();
SocketAddress address; SocketAddress address;
if (ip == null || ip.isEmpty()){ if (ip == null || ip.isEmpty()){
@ -25,7 +27,7 @@ public class SocketAddressSerializer implements NodeSerializer<SocketAddress> {
} }
@Override @Override
public void serialize(SocketAddress socketAddress, ConfigNode configNode) throws NodeSerializingException { public void serialize(Type type, @Nullable SocketAddress obj, ConfigurationNode node) {
} }
} }

View File

@ -10,16 +10,12 @@ import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.ResourceLeakDetector; import io.netty.util.ResourceLeakDetector;
import napi.configurate.serializing.NodeSerializers;
import ru.nanit.limbo.configuration.LimboConfig; import ru.nanit.limbo.configuration.LimboConfig;
import ru.nanit.limbo.configuration.SocketAddressSerializer;
import ru.nanit.limbo.connection.ClientChannelInitializer; import ru.nanit.limbo.connection.ClientChannelInitializer;
import ru.nanit.limbo.connection.ClientConnection; import ru.nanit.limbo.connection.ClientConnection;
import ru.nanit.limbo.server.data.*;
import ru.nanit.limbo.util.Logger; import ru.nanit.limbo.util.Logger;
import ru.nanit.limbo.world.DimensionRegistry; import ru.nanit.limbo.world.DimensionRegistry;
import java.net.SocketAddress;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -51,12 +47,6 @@ public final class LimboServer {
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.DISABLED); 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 = new LimboConfig(Paths.get("./"));
config.load(); config.load();

View File

@ -1,10 +1,13 @@
package ru.nanit.limbo.server.data; package ru.nanit.limbo.server.data;
import napi.configurate.data.ConfigNode; import org.checkerframework.checker.nullness.qual.Nullable;
import napi.configurate.serializing.NodeSerializer; import org.spongepowered.configurate.ConfigurationNode;
import napi.configurate.serializing.NodeSerializingException; import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import ru.nanit.limbo.util.Colors; import ru.nanit.limbo.util.Colors;
import java.lang.reflect.Type;
public class BossBar { public class BossBar {
private String text; 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 @Override
public BossBar deserialize(ConfigNode node) throws NodeSerializingException { public BossBar deserialize(Type type, ConfigurationNode node) throws SerializationException {
BossBar bossBar = new BossBar(); BossBar bossBar = new BossBar();
bossBar.setText(Colors.of(node.getNode("text").getString())); bossBar.setText(Colors.of(node.node("text").getString("")));
bossBar.setHealth(node.getNode("health").getFloat()); bossBar.setHealth(node.node("health").getFloat());
if (bossBar.getHealth() < 0 || bossBar.getHealth() > 1) 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 { try {
bossBar.setColor(Color.valueOf(node.getNode("color").getString().toUpperCase())); bossBar.setColor(Color.valueOf(node.node("color").getString("").toUpperCase()));
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e){
throw new NodeSerializingException("Invalid bossbar color"); throw new SerializationException("Invalid bossbar color");
} }
try { try {
bossBar.setDivision(Division.valueOf(node.getNode("division").getString().toUpperCase())); bossBar.setDivision(Division.valueOf(node.node("division").getString("").toUpperCase()));
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e){
throw new NodeSerializingException("Invalid bossbar division"); throw new SerializationException("Invalid bossbar division");
} }
return bossBar; return bossBar;
} }
@Override @Override
public void serialize(BossBar bossBar, ConfigNode configNode) { public void serialize(Type type, @Nullable BossBar obj, ConfigurationNode node) {
} }
} }

View File

@ -1,8 +1,9 @@
package ru.nanit.limbo.server.data; package ru.nanit.limbo.server.data;
import napi.configurate.data.ConfigNode; import org.checkerframework.checker.nullness.qual.Nullable;
import napi.configurate.serializing.NodeSerializer; import org.spongepowered.configurate.ConfigurationNode;
import napi.configurate.serializing.NodeSerializingException; import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -37,27 +38,27 @@ public class InfoForwarding {
MODERN MODERN
} }
public static class Serializer implements NodeSerializer<InfoForwarding> { public static class Serializer implements TypeSerializer<InfoForwarding> {
@Override @Override
public InfoForwarding deserialize(ConfigNode node) throws NodeSerializingException { public InfoForwarding deserialize(java.lang.reflect.Type type, ConfigurationNode node) throws SerializationException {
InfoForwarding forwarding = new InfoForwarding(); InfoForwarding forwarding = new InfoForwarding();
try { try {
forwarding.type = Type.valueOf(node.getNode("type").getString().toUpperCase()); forwarding.type = Type.valueOf(node.node("type").getString("").toUpperCase());
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e){
throw new NodeSerializingException("Undefined info forwarding type"); throw new SerializationException("Undefined info forwarding type");
} }
if (forwarding.type == Type.MODERN){ 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; return forwarding;
} }
@Override @Override
public void serialize(InfoForwarding infoForwarding, ConfigNode configNode) { public void serialize(java.lang.reflect.Type type, @Nullable InfoForwarding obj, ConfigurationNode node) throws SerializationException {
} }
} }

View File

@ -1,9 +1,12 @@
package ru.nanit.limbo.server.data; package ru.nanit.limbo.server.data;
import napi.configurate.data.ConfigNode; import org.checkerframework.checker.nullness.qual.Nullable;
import napi.configurate.serializing.NodeSerializer; import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.TypeSerializer;
import ru.nanit.limbo.util.Colors; import ru.nanit.limbo.util.Colors;
import java.lang.reflect.Type;
public class PingData { public class PingData {
private String version; private String version;
@ -25,18 +28,18 @@ public class PingData {
this.description = description; this.description = description;
} }
public static class Serializer implements NodeSerializer<PingData> { public static class Serializer implements TypeSerializer<PingData> {
@Override @Override
public PingData deserialize(ConfigNode node) { public PingData deserialize(Type type, ConfigurationNode node) {
PingData pingData = new PingData(); PingData pingData = new PingData();
pingData.setDescription(Colors.of(node.getNode("description").getString())); pingData.setDescription(Colors.of(node.node("description").getString("")));
pingData.setVersion(Colors.of(node.getNode("version").getString())); pingData.setVersion(Colors.of(node.node("version").getString("")));
return pingData; return pingData;
} }
@Override @Override
public void serialize(PingData pingData, ConfigNode configNode) { public void serialize(Type type, @Nullable PingData obj, ConfigurationNode node) {
} }
} }

View File

@ -1,7 +1,10 @@
package ru.nanit.limbo.server.data; package ru.nanit.limbo.server.data;
import napi.configurate.data.ConfigNode; import org.checkerframework.checker.nullness.qual.Nullable;
import napi.configurate.serializing.NodeSerializer; import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
public class Position { public class Position {
@ -51,21 +54,21 @@ public class Position {
this.pitch = pitch; this.pitch = pitch;
} }
public static class Serializer implements NodeSerializer<Position> { public static class Serializer implements TypeSerializer<Position> {
@Override @Override
public Position deserialize(ConfigNode node) { public Position deserialize(Type type, ConfigurationNode node) {
Position position = new Position(); Position position = new Position();
position.setX(node.getNode("x").getDouble()); position.setX(node.node("x").getDouble());
position.setY(node.getNode("y").getDouble()); position.setY(node.node("y").getDouble());
position.setZ(node.getNode("z").getDouble()); position.setZ(node.node("z").getDouble());
position.setYaw(node.getNode("yaw").getFloat()); position.setYaw(node.node("yaw").getFloat());
position.setPitch(node.getNode("pitch").getFloat()); position.setPitch(node.node("pitch").getFloat());
return position; return position;
} }
@Override @Override
public void serialize(Position position, ConfigNode configNode) { public void serialize(Type type, @Nullable Position obj, ConfigurationNode node) {
} }
} }