Skip to content

Commit

Permalink
feat(server-properties): Creation of ServerProperties System (#18)
Browse files Browse the repository at this point in the history
* feat(server-properties): creation of a ServerProperties system with enums

* feat(server-properties): delete "on" and "off"

* feat(server-properties): add "SPAWN_ANIMALS" and "SPAWN_MONSTERS"
  • Loading branch information
AzaleeX authored Aug 21, 2024
1 parent 5e0c451 commit d6b0650
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 25 deletions.
34 changes: 9 additions & 25 deletions src/main/java/org/sculk/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.apache.logging.log4j.Logger;
import org.cloudburstmc.protocol.bedrock.packet.PlayerListPacket;
import org.sculk.config.Config;
import org.sculk.config.ServerProperties;
import org.sculk.config.ServerPropertiesKeys;
import org.sculk.console.TerminalConsole;
import org.sculk.event.EventManager;
import org.sculk.network.BedrockInterface;
Expand Down Expand Up @@ -58,7 +60,7 @@ public class Server {
private final Path dataPath;
private final Path pluginDataPath;

private final Config properties;
private final ServerProperties properties;
private final Config config;
private final Config operators;
private final Config whitelist;
Expand Down Expand Up @@ -97,27 +99,9 @@ public Server(Logger logger, String dataPath) {
this.config = new Config(this.dataPath + "/sculk.yml");

logger.info("Loading {}...", TextFormat.AQUA + "server.properties" + TextFormat.WHITE);
this.properties = new Config(this.dataPath.resolve("server.properties").toString(), Config.PROPERTIES);
if(!this.properties.exists("server-port")) {
this.properties.set("language", "English");
this.properties.set("motd", "A Sculk Server Software");
this.properties.set("sub-motd", "Powered by Sculk");
this.properties.set("server-port", 19132);
this.properties.set("server-ip", "0.0.0.0");
this.properties.set("white-list", false);
this.properties.set("max-players", 20);
this.properties.set("gamemode", "Survival");
this.properties.set("pvp", true);
this.properties.set("difficulty", 1);
this.properties.set("level-name", "world");
this.properties.set("level-seed", "");
this.properties.set("level-type", "DEFAULT");
this.properties.set("auto-save", true);
this.properties.set("xbox-auth", true);
this.properties.save();
}
this.motd = this.properties.getString("motd");
this.submotd = this.properties.getString("sub-motd");
this.properties = new ServerProperties(this.dataPath);
this.motd = this.properties.get(ServerPropertiesKeys.MOTD, "A Sculk Server Software");
this.submotd = this.properties.get(ServerPropertiesKeys.SUB_MOTD, "Powered by Sculk");

this.injector = Guice.createInjector(Stage.PRODUCTION, new SculkModule(this));
this.eventManager = injector.getInstance(EventManager.class);
Expand All @@ -128,7 +112,7 @@ public Server(Logger logger, String dataPath) {
this.banByName = new Config(this.dataPath.resolve("banned-players.txt").toString(), Config.ENUM);
this.banByIp = new Config(this.dataPath.resolve("banned-ip.txt").toString(), Config.ENUM);

logger.info("Selected {} as the base language", this.properties.getString("language"));
logger.info("Selected {} as the base language", this.properties.get(ServerPropertiesKeys.LANGUAGE, "English"));
logger.info("Starting Minecraft: Bedrock Edition server version {}", TextFormat.AQUA + Sculk.MINECRAFT_VERSION + TextFormat.WHITE);

this.console = new TerminalConsole(this);
Expand All @@ -151,7 +135,7 @@ public void start() {
shutdown();
}

if(this.properties.getBoolean("xbox-auth")) {
if(this.properties.get(ServerPropertiesKeys.XBOX_AUTH, true)) {
logger.info("Online mode is enable. The server will verify that players are authenticated to XboxLive.");
} else {
logger.info("{}Online mode is not enabled. The server no longer checks if players are authenticated to XboxLive.", TextFormat.RED);
Expand Down Expand Up @@ -216,7 +200,7 @@ public Map<UUID, Player> getOnlinePlayers() {
return Collections.unmodifiableMap(playerList);
}

public Config getProperties() {
public ServerProperties getProperties() {
return properties;
}

Expand Down
124 changes: 124 additions & 0 deletions src/main/java/org/sculk/config/ServerProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.sculk.config;

import java.io.File;
import java.nio.file.Path;

public class ServerProperties {
private final Config properties;

public ServerProperties(Path dataPath) {
File file = new File(dataPath + "/server.properties");
if (!file.exists()) {
ConfigSection defaults = getDefaultValues();
new Config(file.getPath(), Config.PROPERTIES, defaults).save();
}
this.properties = new Config(dataPath + "/server.properties", Config.PROPERTIES, getDefaultValues());
}

private ConfigSection getDefaultValues() {
ConfigSection defaults = new ConfigSection();
defaults.put(ServerPropertiesKeys.LANGUAGE.toString(), "English");
defaults.put(ServerPropertiesKeys.MOTD.toString(), "A Sculk Server Software");
defaults.put(ServerPropertiesKeys.SUB_MOTD.toString(), "Powered by Sculk");
defaults.put(ServerPropertiesKeys.SERVER_IP.toString(), "0.0.0.0");
defaults.put(ServerPropertiesKeys.SERVER_PORT.toString(), 19132);
defaults.put(ServerPropertiesKeys.WHITELIST.toString(), "off");
defaults.put(ServerPropertiesKeys.MAX_PLAYERS.toString(), 20);
defaults.put(ServerPropertiesKeys.GAMEMODE.toString(), 0);
defaults.put(ServerPropertiesKeys.PVP.toString(), "on");
defaults.put(ServerPropertiesKeys.DIFFICULTY.toString(), 1);
defaults.put(ServerPropertiesKeys.LEVEL_NAME.toString(), "world");
defaults.put(ServerPropertiesKeys.LEVEL_SEED.toString(), "");
defaults.put(ServerPropertiesKeys.LEVEL_TYPE.toString(), "DEFAULT");
defaults.put(ServerPropertiesKeys.SPAWN_ANIMALS.toString(), "on");
defaults.put(ServerPropertiesKeys.SPAWN_MONSTERS.toString(), "on");
defaults.put(ServerPropertiesKeys.AUTO_SAVE.toString(), "on");
defaults.put(ServerPropertiesKeys.XBOX_AUTH.toString(), "on");
return defaults;
}

public ConfigSection getProperties() {
return this.properties.getRootSection();
}

public Integer get(ServerPropertiesKeys key, Integer defaultValue) {
Object value = this.properties.get(key.toString());
if (value instanceof String) {
try {
return Integer.parseInt((String) value);
} catch (NumberFormatException e) {
return defaultValue;
}
} else if (value instanceof Integer) {
return (Integer) value;
} else {
return defaultValue;
}
}

public String get(ServerPropertiesKeys key, String defaultValue) {
Object value = this.properties.get(key.toString());
if (value instanceof String) {
return (String) value;
} else {
return defaultValue;
}
}

public Boolean get(ServerPropertiesKeys key, Boolean defaultValue) {
Object value = this.properties.get(key.toString());
if (value instanceof String) {
String stringValue = ((String) value).toLowerCase();
if (stringValue.equals("on")) {
return true;
} else if (stringValue.equals("off")) {
return false;
}
} else if (value instanceof Boolean) {
return (Boolean) value;
}
return defaultValue;
}

public Long get(ServerPropertiesKeys key, Long defaultValue) {
Object value = this.properties.get(key.toString());
if (value instanceof String stringValue) {
if (!stringValue.isEmpty()) {
try {
return Long.parseLong(stringValue);
} catch (NumberFormatException e) {
return defaultValue;
}
} else {
return defaultValue;
}
} else if (value instanceof Long) {
return (Long) value;
} else {
return defaultValue;
}
}

public void set(String key, Object value) {
if (value instanceof Boolean) {
value = (Boolean) value ? "on" : "off";
}
this.properties.set(key, value);
}

public void remove(String key) {
this.properties.remove(key);
}

public boolean exists(String key) {
return this.properties.exists(key);
}

public void save() {
this.properties.save();
}

public void reload() {
this.properties.reload();
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/sculk/config/ServerPropertiesKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.sculk.config;

public enum ServerPropertiesKeys {
LANGUAGE("language"),
MOTD("motd"),
SUB_MOTD("sub-motd"),
SERVER_PORT("server-port"),
SERVER_IP("server-ip"),
WHITELIST("white-list"),
MAX_PLAYERS("max-players"),
GAMEMODE("gamemode"),
PVP("pvp"),
DIFFICULTY("difficulty"),
LEVEL_NAME("level-name"),
LEVEL_SEED("level-seed"),
LEVEL_TYPE("level-type"),
SPAWN_ANIMALS("spawn-animals"),
SPAWN_MONSTERS("spawn-monsters"),
AUTO_SAVE("auto-save"),
XBOX_AUTH("xbox-auth");

private final String key;

ServerPropertiesKeys(String key) {
this.key = key;
}

@Override
public String toString() {
return key;
}
}

0 comments on commit d6b0650

Please sign in to comment.