refactoring: Config
YAML -> HOCON
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
package mc.server.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
@Accessors(fluent = true)
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public class Config {
|
||||
|
||||
private final Server server = new Server();
|
||||
private final Players players = new Players();
|
||||
private final World world = new World();
|
||||
|
||||
private String motd;
|
||||
private String disconnectReason;
|
||||
private Path iconPath;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public static class Server {
|
||||
private String host;
|
||||
private int port;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public static class Players {
|
||||
private final FakeOnline fakeOnline = new FakeOnline();
|
||||
|
||||
private int maxOnlile;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public static class FakeOnline {
|
||||
private boolean enable;
|
||||
private int value;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public static class World {
|
||||
private int viewDistance;
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package mc.server.di;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.server.config.Config;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Module
|
||||
@RequiredArgsConstructor
|
||||
public class ConfigModule {
|
||||
|
||||
private final Path configPath;
|
||||
|
||||
@Provides
|
||||
Config provideConfig() {
|
||||
Config config = new Config();
|
||||
Map<String, Object> map = new Yaml().load(readConfigAsString());
|
||||
|
||||
config.server().host(fromYamlPath("server/host", map, "127.0.0.1"));
|
||||
config.server().port(fromYamlPath("server/port", map, 25565));
|
||||
|
||||
config.motd(fromYamlPath("motd", map, ""));
|
||||
config.disconnectReason(fromYamlPath("disconnect-reason", map, ""));
|
||||
|
||||
config.players().maxOnlile(fromYamlPath("players/max-online", map, 0));
|
||||
config.players().fakeOnline().enable(fromYamlPath("players/fake-online/enable", map, false));
|
||||
config.players().fakeOnline().value(fromYamlPath("players/fake-online/value", map, 0));
|
||||
|
||||
config.world().viewDistance(fromYamlPath("world/view-distance", map, 0));
|
||||
|
||||
if (Boolean.TRUE.equals(fromYamlPath("icon/enable", map, false))) {
|
||||
config.iconPath(Paths.get(fromYamlPath("icon/path", map, "favicon.png")));
|
||||
}
|
||||
|
||||
map.clear();
|
||||
return config;
|
||||
}
|
||||
|
||||
private String readConfigAsString() {
|
||||
try {
|
||||
return Files.readString(configPath);
|
||||
} catch (IOException e) {
|
||||
log.error("Can't load config from '{}'", configPath.toAbsolutePath(), e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T fromYamlPath(String mapPath, Map<String, Object> map, T defaultValue) {
|
||||
String[] keys = mapPath.split("/", 2);
|
||||
|
||||
if (map.containsKey(keys[0])) {
|
||||
Object object = map.get(keys[0]);
|
||||
if (keys.length > 1) {
|
||||
return fromYamlPath(keys[1], (Map<String, Object>) object, defaultValue);
|
||||
} else {
|
||||
return (T) object;
|
||||
}
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
server:
|
||||
host: 127.0.0.1
|
||||
port: 25565
|
||||
|
||||
motd: |
|
||||
&bmc-project &8:: &4ZERO
|
||||
&8develop by &7DmitriyMX
|
||||
|
||||
disconnect-reason: '&4Server is not available.'
|
||||
|
||||
players:
|
||||
max-online: 0
|
||||
fake-online:
|
||||
enable: false
|
||||
value: 0
|
||||
|
||||
# Размер значка: 64x64 px
|
||||
icon:
|
||||
enable: false
|
||||
path: favicon.png
|
||||
|
||||
world:
|
||||
view-distance: 1
|
||||
Reference in New Issue
Block a user