Skip to content

Commit 0011243

Browse files
committed
Refactor
1 parent 3eed5e3 commit 0011243

File tree

12 files changed

+147
-157
lines changed

12 files changed

+147
-157
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dependencies {
4141

4242
implementation(include('org.java-websocket:Java-WebSocket:1.6.0'))
4343
implementation(include('org.json:json:20250107'))
44-
implementation(include("org.yaml:snakeyaml:2.2"))
44+
implementation(include('org.yaml:snakeyaml:2.2'))
4545
}
4646

4747
processResources {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
org.gradle.jvmargs=-Xmx1G
33
# Fabric Properties
44
# check these on https://modmuss50.me/fabric.html
5-
minecraft_version=1.21.4
5+
minecraft_version=1.21.5
66
yarn_mappings=1.21.4+build.8
77
loader_version=0.16.10
88
# Mod Properties

src/main/java/dev/loat/web_socket_console/config/files/WebSocketConsoleConfigFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ public class WebSocketConsoleConfigFile {
2121
- "WARN"
2222
- "ERROR"
2323
""")
24-
public String logLevel = LogLevel.DEBUG;
25-
}
24+
public String logLevel = LogLevel.INFO;
25+
}

src/main/java/dev/loat/web_socket_console/config/parser/YamlSerializer.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import org.yaml.snakeyaml.nodes.Tag;
99
import org.yaml.snakeyaml.representer.Representer;
1010

11-
import java.io.FileWriter;
1211
import java.io.InputStream;
1312
import java.lang.reflect.Field;
1413
import java.nio.file.Files;
14+
import java.nio.file.Path;
1515
import java.nio.file.Paths;
1616
import java.util.ArrayList;
17+
import java.util.HashMap;
1718
import java.util.List;
1819
import java.util.Map;
1920

@@ -33,6 +34,12 @@ public YamlSerializer(String filePath, Class<ConfigClass> configClass) {
3334
* @throws Exception If an error occurs while writing the file.
3435
*/
3536
public void serialize(ConfigClass config) throws Exception {
37+
Path path = Paths.get(filePath);
38+
if (!Files.exists(path)) {
39+
Files.createDirectories(path.getParent());
40+
Files.createFile(path);
41+
}
42+
3643
DumperOptions options = new DumperOptions();
3744
options.setIndent(2);
3845
options.setPrettyFlow(true);
@@ -43,49 +50,43 @@ public void serialize(ConfigClass config) throws Exception {
4350

4451
Yaml yaml = new Yaml(representer, options);
4552

46-
String tempFile = filePath + ".tmp";
47-
try (FileWriter writer = new FileWriter(tempFile)) {
48-
yaml.dump(config, writer);
49-
}
50-
51-
Map<String, Object> yamlData;
52-
try (InputStream inputStream = Files.newInputStream(Paths.get(tempFile))) {
53-
yamlData = new Yaml().load(inputStream);
54-
}
53+
String yamlString = yaml.dump(config);
54+
String[] lines = yamlString.split("\n");
5555

56-
List<String> modifiedLines = new ArrayList<>();
56+
// Kommentare für Felder mit @Comment-Annotation sammeln
5757
Field[] fields = configClass.getDeclaredFields();
58-
58+
Map<String, List<String>> commentsMap = new HashMap<>();
5959
for (Field field : fields) {
6060
Comment comment = field.getAnnotation(Comment.class);
61-
String fieldName = field.getName();
62-
6361
if (comment != null) {
6462
String[] commentLines = comment.value().split("\n");
63+
List<String> formattedComments = new ArrayList<>();
6564
for (String commentLine : commentLines) {
66-
String trimmedLine = commentLine.trim();
67-
if (!trimmedLine.isEmpty()) {
68-
modifiedLines.add("# " + trimmedLine);
65+
String trimmed = commentLine.trim();
66+
if (!trimmed.isEmpty()) {
67+
formattedComments.add("# " + trimmed);
6968
} else {
70-
modifiedLines.add("#");
69+
formattedComments.add("#");
7170
}
7271
}
72+
commentsMap.put(field.getName(), formattedComments);
7373
}
74+
}
7475

75-
if (yamlData != null && yamlData.containsKey(fieldName)) {
76-
Object value = yamlData.get(fieldName);
77-
String yamlValue = yaml.dump(value).trim();
78-
if (yamlValue.contains("\n")) {
79-
yamlValue = yamlValue.substring(0, yamlValue.indexOf('\n'));
76+
// YAML-Zeilen mit Kommentaren anreichern
77+
List<String> modifiedLines = new ArrayList<>();
78+
for (String line : lines) {
79+
for (String fieldName : commentsMap.keySet()) {
80+
if (line.startsWith(fieldName + ":")) {
81+
modifiedLines.addAll(commentsMap.get(fieldName));
82+
break;
8083
}
81-
modifiedLines.add(fieldName + ": " + yamlValue);
82-
} else {
83-
modifiedLines.add(fieldName + ": null");
8484
}
85+
modifiedLines.add(line);
8586
}
8687

87-
Files.write(Paths.get(filePath), modifiedLines);
88-
Files.deleteIfExists(Paths.get(tempFile));
88+
// Ergebnis in die Zieldatei schreiben
89+
Files.write(path, modifiedLines);
8990
}
9091

9192
/**
@@ -95,7 +96,15 @@ public void serialize(ConfigClass config) throws Exception {
9596
* @throws Exception If an error occurs while reading the file or casting.
9697
*/
9798
public ConfigClass parse() throws Exception {
98-
try (InputStream inputStream = Files.newInputStream(Paths.get(filePath))) {
99+
Path path = Paths.get(filePath);
100+
if (!Files.exists(path)) {
101+
// Erstelle die Ordnerstruktur und Datei, falls sie nicht existieren
102+
Files.createDirectories(path.getParent());
103+
Files.createFile(path);
104+
throw new IllegalStateException("YAML file was newly created and is empty: " + filePath);
105+
}
106+
107+
try (InputStream inputStream = Files.newInputStream(path)) {
99108
LoaderOptions loaderOptions = new LoaderOptions();
100109
loaderOptions.setAllowDuplicateKeys(false);
101110
Constructor constructor = new Constructor(configClass, loaderOptions);
@@ -107,4 +116,4 @@ public ConfigClass parse() throws Exception {
107116
return configClass.cast(result);
108117
}
109118
}
110-
}
119+
}

src/main/java/dev/loat/web_socket_console/console/LogAppender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.loat.web_socket_console.console;
22

3-
import dev.loat.web_socket_console.web_socket.LogMessage;
3+
import dev.loat.web_socket_console.web_socket.send.LogMessage;
44
import dev.loat.web_socket_console.web_socket.WebSocketConsoleServer;
55
import org.apache.logging.log4j.core.LogEvent;
66
import org.apache.logging.log4j.core.appender.AbstractAppender;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dev.loat.web_socket_console.http;
2+
3+
import java.net.URI;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Objects;
9+
10+
public class URLParameters {
11+
private final String query;
12+
13+
/**
14+
* @param url The url to get parameters from
15+
*/
16+
public URLParameters(URI url) {
17+
18+
var query = url.getQuery();
19+
this.query = Objects.requireNonNullElse(query, "");
20+
}
21+
22+
public Map<String, List<String>> getParameters() {
23+
var parameters = new HashMap<String, List<String>>();
24+
25+
if (this.query.isEmpty()) {
26+
return parameters;
27+
}
28+
29+
for (var parameter : this.query.split("&")) {
30+
var keyValue = parameter.split("=", 2);
31+
32+
var values = parameters.computeIfAbsent(keyValue[0], key -> new ArrayList<>());
33+
34+
if (keyValue.length == 1) {
35+
values.add(null);
36+
} else {
37+
values.add(keyValue[1]);
38+
}
39+
}
40+
41+
return parameters;
42+
}
43+
44+
public List<String> getParameter(String key) {
45+
return getParameters().get(key);
46+
}
47+
}

src/main/java/dev/loat/web_socket_console/web_socket/WebSocketConsoleServer.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package dev.loat.web_socket_console.web_socket;
22

3+
import dev.loat.web_socket_console.http.URLParameters;
34
import dev.loat.web_socket_console.logging.Logger;
4-
import dev.loat.web_socket_console.web_socket.client.WebSocketClientList;
5+
import dev.loat.web_socket_console.web_socket.send.LogMessage;
56
import net.minecraft.server.MinecraftServer;
67
import org.java_websocket.WebSocket;
78
import org.java_websocket.handshake.ClientHandshake;
89
import org.java_websocket.server.WebSocketServer;
910

1011
import java.net.InetSocketAddress;
11-
import java.util.Collections;
12-
import java.util.HashSet;
13-
import java.util.Set;
14-
import java.util.concurrent.Executors;
15-
import java.util.concurrent.ScheduledExecutorService;
12+
import java.net.URI;
13+
import java.net.URISyntaxException;
14+
import java.util.*;
1615

1716
public class WebSocketConsoleServer extends WebSocketServer {
18-
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1917
private final Set<WebSocket> clients = Collections.synchronizedSet(new HashSet<>());
2018
private final MinecraftServer serverInstance;
2119
private final int port;
@@ -44,6 +42,35 @@ public void onOpen(
4442
ClientHandshake clientHandshake
4543
) {
4644
this.clients.add(connection);
45+
Map<String, List<String>> parameters;
46+
47+
Logger.info("http://localhost" + clientHandshake.getResourceDescriptor());
48+
try {
49+
var uri = new URI("http://localhost" + clientHandshake.getResourceDescriptor());
50+
uri.getPath();
51+
parameters = new URLParameters(uri).getParameters();
52+
} catch (URISyntaxException e) {
53+
throw new RuntimeException(e);
54+
}
55+
56+
// try {
57+
// new URI(connection.getRemoteSocketAddress().getHostString() + connection.getResourceDescriptor()).toURL().getQuery();
58+
// } catch (URISyntaxException e) {
59+
// throw new RuntimeException(e);
60+
// }
61+
62+
63+
/*
64+
localhost:8080/log?channel=debug -> DEBUG Channel
65+
localhost:8080/log?channel=info -> INFO Channel
66+
localhost:8080/log?channel=warn -> WARN Channel
67+
localhost:8080/log?channel=error -> ERROR Channel
68+
69+
localhost:8080/log -> localhost:8080/log?channel=debug&channel=info&channel=warn&channel=error -> All Channel
70+
71+
localhost:8080/log?channel=warn&channel=error -> WARN, ERROR Channel
72+
73+
*/
4774

4875
Logger.info("Client connected: {}", connection.getRemoteSocketAddress());
4976
}
@@ -69,7 +96,7 @@ public void onMessage(
6996
WebSocket connection,
7097
String message
7198
) {
72-
// Logger.info(message);
99+
Logger.debug(message);
73100

74101
if (this.serverInstance != null) {
75102
serverInstance.execute(() -> serverInstance.getCommandManager().executeWithPrefix(

src/main/java/dev/loat/web_socket_console/web_socket/client/WebSocketClient.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/java/dev/loat/web_socket_console/web_socket/client/WebSocketClientList.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/main/java/dev/loat/web_socket_console/web_socket/LogMessage.java renamed to src/main/java/dev/loat/web_socket_console/web_socket/send/LogMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.loat.web_socket_console.web_socket;
1+
package dev.loat.web_socket_console.web_socket.send;
22

33
import org.apache.logging.log4j.Level;
44
import org.apache.logging.log4j.ThreadContext;

0 commit comments

Comments
 (0)