Skip to content

Commit d812e10

Browse files
committed
feat: default host, debug config
1 parent 18700e6 commit d812e10

File tree

4 files changed

+52
-38
lines changed

4 files changed

+52
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bin/
3131
# fabric
3232

3333
run/
34+
run-server/
3435

3536
# java
3637

src/main/java/lol/gito/pingremote/PingRemote.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,43 @@ public void onInitialize() {
2525
LOGGER.info("Ping Remote initialized.");
2626
config = ConfigBuilder.load(PingRemoteConfig.class, MOD_ID);
2727

28-
ServerTickEvents.START_SERVER_TICK.register((serverWorld) -> {
29-
PingRemote.counter++;
30-
});
31-
32-
ServerTickEvents.END_WORLD_TICK.register((serverWorld) -> {
33-
if (counter == config.getTickAmount()) {
34-
counter = 0;
35-
36-
try (HttpClient client = HttpClient.newBuilder()
37-
.version(HttpClient.Version.HTTP_2)
38-
.connectTimeout(Duration.ofSeconds(10))
39-
.build()) {
40-
41-
try {
42-
HttpRequest request = HttpRequest.newBuilder()
43-
.uri(config.getRemoteHost().toURI())
44-
.timeout(Duration.ofMinutes(1))
45-
.build();
46-
47-
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
48-
.thenApply(HttpResponse::body)
49-
.thenAccept(System.out::println);
50-
} catch (URISyntaxException e) {
51-
LOGGER.warn("Cannot ping remote host");
28+
if (config.getRemoteHost() != null) {
29+
ServerTickEvents.START_SERVER_TICK.register((serverWorld) -> {
30+
PingRemote.counter++;
31+
});
32+
33+
ServerTickEvents.END_WORLD_TICK.register((serverWorld) -> {
34+
if (counter == config.getTickAmount()) {
35+
counter = 0;
36+
37+
try (HttpClient client = HttpClient.newBuilder()
38+
.version(HttpClient.Version.HTTP_2)
39+
.connectTimeout(Duration.ofSeconds(10))
40+
.build()) {
41+
42+
try {
43+
HttpRequest request = HttpRequest.newBuilder()
44+
.uri(config.getRemoteHost().toURI())
45+
.timeout(Duration.ofMinutes(1))
46+
.build();
47+
48+
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
49+
.thenApply(HttpResponse::body)
50+
.thenAccept((response) -> {
51+
if (config.isDebug()) {
52+
LOGGER.info("Response body from remote host");
53+
LOGGER.info(response);
54+
}
55+
});
56+
} catch (URISyntaxException e) {
57+
LOGGER.warn("Cannot ping remote host, invalid uri");
58+
}
5259
}
5360
}
54-
}
55-
});
61+
});
62+
} else {
63+
LOGGER.warn("Remote host is not provided");
64+
LOGGER.warn("Skipping mod event registration");
65+
}
5666
}
5767
}

src/main/java/lol/gito/pingremote/config/PingRemoteConfig.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package lol.gito.pingremote.config;
22

3+
import java.net.MalformedURLException;
4+
import java.net.URI;
35
import java.net.URL;
46

57
public class PingRemoteConfig {
6-
private URL remoteHost;
8+
private URL remoteHost = URI.create("https://example.com").toURL();
79
private int tickAmount = 6000;
10+
private boolean debug = false;
11+
12+
public PingRemoteConfig() throws MalformedURLException {
13+
}
814

915
public URL getRemoteHost() {
1016
return remoteHost;
@@ -21,4 +27,12 @@ public int getTickAmount() {
2127
public void setTickAmount(int tickAmount) {
2228
this.tickAmount = tickAmount;
2329
}
30+
31+
public boolean isDebug() {
32+
return debug;
33+
}
34+
35+
public void setDebug(boolean debug) {
36+
this.debug = debug;
37+
}
2438
}

src/main/resources/pingremote.mixins.json

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

0 commit comments

Comments
 (0)