Add config.yml support for port, host and MOTD
Some checks failed
Build JStom / build (push) Has been cancelled
Some checks failed
Build JStom / build (push) Has been cancelled
This commit is contained in:
@@ -21,6 +21,9 @@ dependencies {
|
||||
|
||||
// Logging
|
||||
implementation("org.slf4j:slf4j-simple:2.0.9")
|
||||
|
||||
// Config
|
||||
implementation("org.yaml:snakeyaml:2.2")
|
||||
}
|
||||
|
||||
application {
|
||||
|
||||
4
config.yml
Normal file
4
config.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
# JStom Configuration
|
||||
host: "0.0.0.0"
|
||||
port: 25565
|
||||
motd: "A JStom Server"
|
||||
48
src/main/java/net/jstom/Config.java
Normal file
48
src/main/java/net/jstom/Config.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package net.jstom;
|
||||
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
public class Config {
|
||||
public static int PORT = 25565;
|
||||
public static String MOTD = "A JStom Server";
|
||||
public static String HOST = "0.0.0.0";
|
||||
|
||||
private static final String CONFIG_FILE = "config.yml";
|
||||
|
||||
public static void load() {
|
||||
File file = new File(CONFIG_FILE);
|
||||
Yaml yaml = new Yaml();
|
||||
|
||||
if (!file.exists()) {
|
||||
saveDefault();
|
||||
}
|
||||
|
||||
try (InputStream inputStream = new FileInputStream(file)) {
|
||||
Map<String, Object> data = yaml.load(inputStream);
|
||||
if (data != null) {
|
||||
if (data.containsKey("port")) PORT = (int) data.get("port");
|
||||
if (data.containsKey("motd")) MOTD = (String) data.get("motd");
|
||||
if (data.containsKey("host")) HOST = (String) data.get("host");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Could not load config.yml: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveDefault() {
|
||||
try (FileWriter writer = new FileWriter(CONFIG_FILE)) {
|
||||
writer.write("# JStom Configuration\n");
|
||||
writer.write("host: \"0.0.0.0\"\n");
|
||||
writer.write("port: 25565\n");
|
||||
writer.write("motd: \"A JStom Server\"\n");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,12 +11,17 @@ import net.minestom.server.instance.InstanceContainer;
|
||||
import net.minestom.server.instance.InstanceManager;
|
||||
import net.minestom.server.instance.LightingChunk;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.ping.Status;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Load Config
|
||||
Config.load();
|
||||
|
||||
// Initialize server
|
||||
MinecraftServer minecraftServer = MinecraftServer.init();
|
||||
|
||||
@@ -36,6 +41,13 @@ public class Main {
|
||||
event.setSpawningInstance(instanceContainer);
|
||||
player.setRespawnPoint(new Pos(0, 42, 0));
|
||||
});
|
||||
|
||||
// MOTD
|
||||
globalEventHandler.addListener(ServerListPingEvent.class, event -> {
|
||||
event.setStatus(Status.builder(event.getStatus())
|
||||
.description(Component.text(Config.MOTD))
|
||||
.build());
|
||||
});
|
||||
|
||||
// Initialize Script Manager
|
||||
ScriptManager scriptManager = new ScriptManager(new File("scripts"));
|
||||
@@ -55,6 +67,9 @@ public class Main {
|
||||
System.out.println("Reloading " + fileName + "...");
|
||||
scriptManager.reload(fileName);
|
||||
} else {
|
||||
// Reload config too
|
||||
Config.load();
|
||||
System.out.println("Configuration reloaded.");
|
||||
System.out.println("Reloading all scripts...");
|
||||
scriptManager.reload();
|
||||
}
|
||||
@@ -65,10 +80,10 @@ public class Main {
|
||||
}
|
||||
}).start();
|
||||
|
||||
System.out.println("Server starting on port 25565");
|
||||
System.out.println("Type 'reload' to reload all scripts, or 'reload <filename.js>' for a specific file.");
|
||||
System.out.println("Server starting on " + Config.HOST + ":" + Config.PORT);
|
||||
System.out.println("Type 'reload' to reload scripts and config, or 'reload <filename.js>' for a specific file.");
|
||||
|
||||
// Start the server
|
||||
minecraftServer.start("0.0.0.0", 25565);
|
||||
minecraftServer.start(Config.HOST, Config.PORT);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user