summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-01-28 23:16:50 +0500
committerilotterytea <iltsu@alright.party>2025-01-28 23:16:50 +0500
commit9a07311b35c82a5e4565bed5abfcc770afab06ec (patch)
treed30ad12415d84b498373c1d25f25f064ea1f803b
parent835c2fdb6c1f1064ebb0eece8816cd4edfee3ec8 (diff)
feat: server properties
-rw-r--r--.gitignore3
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java9
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/properties/ServerProperties.java81
3 files changed, 93 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index fc300f7..76fda6b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -163,3 +163,6 @@ Thumbs.db
## You could also add that configuration to the text in nativeimage.gradle .
## You should delete or comment out the next line if you have configuration in a different resource-config.json .
**/resource-config.json
+
+## Frogartha
+server.properties
diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java
index d7f6a44..eb55161 100644
--- a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java
+++ b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java
@@ -5,6 +5,7 @@ import com.github.czyzby.websocket.serialization.impl.ManualSerializer;
import kz.ilotterytea.frogartha.domain.actions.*;
import kz.ilotterytea.frogartha.domain.events.ServerOptionsEvent;
import kz.ilotterytea.frogartha.exceptions.PlayerKickException;
+import kz.ilotterytea.frogartha.server.properties.ServerProperties;
import kz.ilotterytea.frogartha.utils.Logger;
import kz.ilotterytea.frogartha.utils.SerializerUtils;
import org.java_websocket.WebSocket;
@@ -36,6 +37,14 @@ public class FrogarthaServer extends WebSocketServer {
this.serializer = new ManualSerializer();
SerializerUtils.registerTypes(serializer);
+
+ ServerProperties properties = new ServerProperties();
+ try {
+ properties.create();
+ properties.load();
+ } catch (Exception e) {
+ log.error("An exception was thrown while processing server.properties", e);
+ }
}
@Override
diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/properties/ServerProperties.java b/server/src/main/java/kz/ilotterytea/frogartha/server/properties/ServerProperties.java
new file mode 100644
index 0000000..0189e2e
--- /dev/null
+++ b/server/src/main/java/kz/ilotterytea/frogartha/server/properties/ServerProperties.java
@@ -0,0 +1,81 @@
+package kz.ilotterytea.frogartha.server.properties;
+
+import kz.ilotterytea.frogartha.FrogarthaConstants;
+import kz.ilotterytea.frogartha.utils.Logger;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+public class ServerProperties extends Properties {
+ private final Logger log;
+ private final File file;
+
+ public ServerProperties() {
+ super();
+ this.log = new Logger(ServerProperties.class);
+
+ String path = "server.properties";
+ this.file = new File(path);
+
+ defaultValues();
+ }
+
+ public void create() throws IOException {
+ if (!file.exists()) {
+ if (file.createNewFile()) {
+ FileOutputStream fos = new FileOutputStream(file);
+ store(fos, FrogarthaConstants.Game.APP_NAME + " server properties");
+ fos.close();
+ log.log("Created server.properties file");
+ } else {
+ log.log("Failed to create a server.properties file");
+ }
+ }
+ }
+
+ public void load() throws IOException {
+ FileInputStream fis = new FileInputStream(file);
+ load(fis);
+ fis.close();
+ log.log("Loaded properties");
+
+ apply();
+ }
+
+ private void defaultValues() {
+ setProperty("player-max-jump-strength", FrogarthaConstants.Player.MAX_JUMP_STRENGTH);
+ setProperty("player-max-jump-height", FrogarthaConstants.Player.MAX_JUMP_HEIGHT);
+ setProperty("player-jump-speed", FrogarthaConstants.Player.JUMP_SPEED);
+
+ setProperty("chat-max-message-length", FrogarthaConstants.Chat.MAX_MESSAGE_LENGTH);
+ setProperty("chat-message-per-milliseconds", FrogarthaConstants.Chat.MESSAGE_PER_MS);
+ }
+
+ private void apply() {
+ FrogarthaConstants.Player.MAX_JUMP_STRENGTH = getProperty("player-max-jump-strength", FrogarthaConstants.Player.MAX_JUMP_STRENGTH);
+ FrogarthaConstants.Player.MAX_JUMP_HEIGHT = getProperty("player-max-jump-height", FrogarthaConstants.Player.MAX_JUMP_HEIGHT);
+ FrogarthaConstants.Player.JUMP_SPEED = getProperty("player-jump-speed", FrogarthaConstants.Player.JUMP_SPEED);
+
+ FrogarthaConstants.Chat.MAX_MESSAGE_LENGTH = getProperty("chat-max-message-length", FrogarthaConstants.Chat.MAX_MESSAGE_LENGTH);
+ FrogarthaConstants.Chat.MESSAGE_PER_MS = getProperty("chat-message-per-milliseconds", FrogarthaConstants.Chat.MESSAGE_PER_MS);
+ }
+
+ public float getProperty(String key, float defaultValue) {
+ return Float.parseFloat(super.getProperty(key, String.valueOf(defaultValue)));
+ }
+
+ public int getProperty(String key, int defaultValue) {
+ return Integer.parseInt(super.getProperty(key, String.valueOf(defaultValue)));
+ }
+
+ public synchronized void setProperty(String key, float value) {
+ super.setProperty(key, String.valueOf(value));
+ }
+
+ public synchronized void setProperty(String key, int value) {
+ super.setProperty(key, String.valueOf(value));
+ }
+}