Skip to content

Commit 0a1578e

Browse files
committed
added TemplateManager and started with api
1 parent d3ed85c commit 0a1578e

File tree

12 files changed

+415
-44
lines changed

12 files changed

+415
-44
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ target/
3636
# test #
3737
data/
3838
lobby/
39+
templates/
40+
*.json
41+
*.toml
42+
*.db

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>servermanager-launcher</module>
1515
<module>servermanager-node</module>
1616
<module>servermanager-worker</module>
17+
<module>servermanager-api</module>
1718
</modules>
1819

1920
</project>

servermanager-api/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.nexoscript</groupId>
8+
<artifactId>servermanager-parent</artifactId>
9+
<version>0.2.0</version>
10+
</parent>
11+
12+
<artifactId>servermanager-api</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>23</maven.compiler.source>
16+
<maven.compiler.target>23</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-jar-plugin</artifactId>
25+
<version>3.4.2</version>
26+
<configuration>
27+
<archive>
28+
<manifest>
29+
<addClasspath>true</addClasspath>
30+
<mainClass>com.nexoscript.servermanager.launcher.ServerManagerLauncher</mainClass>
31+
</manifest>
32+
<manifestEntries>
33+
<Created-By>ezTxmMC &amp; DragonRex</Created-By>
34+
</manifestEntries>
35+
</archive>
36+
</configuration>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<version>3.13.0</version>
42+
<configuration>
43+
<source>17</source>
44+
<target>17</target>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
</project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.nexoscript.servermanger.api;
2+
3+
public interface IServerManager {
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.nexoscript.servermanger.api.server;
2+
3+
import java.util.Scanner;
4+
5+
public interface IServer {
6+
void start();
7+
void stop();
8+
void console(Scanner scanner);
9+
}

servermanager-node/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@
4545
</plugins>
4646
</build>
4747

48+
<dependencies>
49+
<dependency>
50+
<groupId>com.nexoscript</groupId>
51+
<artifactId>servermanager-api</artifactId>
52+
<version>0.2.0</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.json</groupId>
56+
<artifactId>json</artifactId>
57+
<version>20241224</version>
58+
</dependency>
59+
</dependencies>
60+
4861
</project>

servermanager-node/src/main/java/com/nexoscript/servermanager/node/ServerManagerNode.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,43 @@
22

33
import com.nexoscript.servermanager.node.console.Console;
44
import com.nexoscript.servermanager.node.server.ServerActionRunner;
5+
import com.nexoscript.servermanager.node.template.TemplateManager;
6+
import com.nexoscript.servermanger.api.IServerManager;
57

6-
public class ServerManagerNode {
7-
private static ServerActionRunner actionRunner;
8-
private static Console console;
8+
public class ServerManagerNode implements IServerManager {
9+
private final TemplateManager templateManager;
10+
private final ServerActionRunner actionRunner;
11+
12+
private final Console console;
913

1014
public static void main(String[] args) {
11-
actionRunner = new ServerActionRunner();
12-
shutdownHook();
13-
console = new Console(actionRunner);
14-
console.start();
15+
new ServerManagerNode();
16+
}
17+
18+
public ServerManagerNode() {
19+
this.templateManager = new TemplateManager();
20+
this.actionRunner = new ServerActionRunner(this);
21+
this.shutdownHook();
22+
this.console = new Console(this.templateManager, this.actionRunner);
23+
this.console.start();
1524
}
1625

17-
public static void shutdownHook() {
26+
public void shutdownHook() {
1827
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
1928
System.out.println("\nProgramm wird beendet. Stoppe alle Server...");
20-
actionRunner.shutdownAllServers();
29+
this.actionRunner.shutdownAllServers();
2130
}));
2231
}
2332

24-
public static Console getConsole() {
25-
return console;
33+
public Console getConsole() {
34+
return this.console;
35+
}
36+
37+
public ServerActionRunner getActionRunner() {
38+
return this.actionRunner;
2639
}
2740

28-
public static ServerActionRunner getActionRunner() {
29-
return actionRunner;
41+
public TemplateManager getTemplateManager() {
42+
return templateManager;
3043
}
3144
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.nexoscript.servermanager.node.config;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
6+
import java.io.File;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
12+
public class JsonConfig {
13+
private Path configFile;
14+
private JSONObject jsonObject;
15+
16+
public JsonConfig(String configName) {
17+
this.create(".", configName);
18+
}
19+
20+
public JsonConfig(String path, String configName) {
21+
this.create(path, configName);
22+
}
23+
24+
private void create(String path, String configName) {
25+
this.configFile = Path.of(path, configName);
26+
File file = new File(this.configFile.toString());
27+
try {
28+
if (!file.exists()) {
29+
if (file.createNewFile()) {
30+
this.jsonObject = new JSONObject();
31+
this.save();
32+
return;
33+
}
34+
throw new IOException("File can't created. " + file.getAbsolutePath());
35+
}
36+
this.jsonObject = new JSONObject(new String(Files.readAllBytes(this.configFile)));
37+
} catch (IOException e) {
38+
throw new RuntimeException(e);
39+
}
40+
}
41+
42+
public void addDefault(String key, Object value) {
43+
if (this.get(key) == null) {
44+
this.jsonObject.put(key, value);
45+
this.save();
46+
}
47+
}
48+
49+
public boolean set(String key, Object value) {
50+
try {
51+
this.jsonObject.put(key, value);
52+
this.save();
53+
return true;
54+
} catch (JSONException e) {
55+
return false;
56+
}
57+
}
58+
59+
public boolean remove(String key) {
60+
try {
61+
this.jsonObject.remove(key);
62+
this.save();
63+
return true;
64+
} catch (JSONException ignored) {
65+
return false;
66+
}
67+
}
68+
69+
public Object get(String key) {
70+
try {
71+
return this.jsonObject.get(key);
72+
} catch (JSONException e) {
73+
return null;
74+
}
75+
}
76+
77+
public void save() {
78+
File file = new File(this.configFile.toString());
79+
try (FileWriter fileWriter = new FileWriter(file)) {
80+
fileWriter.write(this.jsonObject.toString());
81+
fileWriter.flush();
82+
} catch (IOException e) {
83+
throw new RuntimeException(e);
84+
}
85+
}
86+
87+
public Path getConfigFile() {
88+
return configFile;
89+
}
90+
}

servermanager-node/src/main/java/com/nexoscript/servermanager/node/console/Console.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import java.util.Scanner;
44

55
import com.nexoscript.servermanager.node.server.ServerActionRunner;
6+
import com.nexoscript.servermanager.node.template.TemplateManager;
67

78
public class Console {
9+
private final TemplateManager templateManager;
810
private final ServerActionRunner actionRunner;
911
private final Scanner scanner;
1012

1113
private boolean running;
1214

13-
public Console(ServerActionRunner actionRunner) {
15+
public Console(TemplateManager templateManager, ServerActionRunner actionRunner) {
16+
this.templateManager = templateManager;
1417
this.actionRunner = actionRunner;
1518
this.scanner = new Scanner(System.in);
1619
this.running = true;
@@ -46,6 +49,30 @@ public void start() {
4649
}
4750
this.actionRunner.openConsole(this.scanner, commandParts[1]);
4851
}
52+
case "create-template" -> {
53+
if (commandParts.length < 2) {
54+
System.out.println("Verwendung: create-template <name>");
55+
break;
56+
}
57+
this.templateManager.createTemplate(commandParts[1]);
58+
System.out.println("Template " + commandParts[1] + " erstellt.");
59+
}
60+
case "rename-template" -> {
61+
if (commandParts.length < 3) {
62+
System.out.println("Verwendung: rename-template <name> <newName>");
63+
break;
64+
}
65+
this.templateManager.renameTemplate(commandParts[1], commandParts[2]);
66+
System.out.println("Template " + commandParts[1] + " zu " + commandParts[2] + " umbenannt.");
67+
}
68+
case "delete-template" -> {
69+
if (commandParts.length < 2) {
70+
System.out.println("Verwendung: delete-template <name>");
71+
break;
72+
}
73+
this.templateManager.deleteTemplate(commandParts[1]);
74+
System.out.println("Template " + commandParts[1] + " gelöscht.");
75+
}
4976
case "exit" -> this.running = false;
5077
default -> System.out.println("Unbekannter Befehl: " + command);
5178
}

0 commit comments

Comments
 (0)