163 lines
4.6 KiB
Java
163 lines
4.6 KiB
Java
// PLACE THIS FILE AT:
|
|
// src/client/java/de/fullrisk/citrusDev/client/PlaytimeTracker.java
|
|
//
|
|
// NOT under src/main/java — MinecraftClient and client.player only exist
|
|
// on the client source set. That is the actual cause of the
|
|
// "Cannot resolve symbol" errors, not a mistake in this code.
|
|
|
|
package de.fullrisk.citrusDev.client;
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
import java.net.URI;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpRequest;
|
|
import java.net.http.HttpResponse;
|
|
import java.time.Duration;
|
|
import java.util.UUID;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
|
|
public class PlaytimeTracker {
|
|
|
|
//ticks zu backend schicken
|
|
|
|
private static final String BACKEND_URL =
|
|
"http://62.116.42.223:25501/api/heartbeat";
|
|
|
|
private static final HttpClient HTTP =
|
|
HttpClient.newBuilder()
|
|
.connectTimeout(Duration.ofSeconds(10))
|
|
.build();
|
|
|
|
private static int ticks = 0;
|
|
private static int currentPlaytimeSeconds = 0;
|
|
|
|
public static void tick() {
|
|
ticks++;
|
|
|
|
if (ticks % 100 == 0) {
|
|
System.out.println("Ticking: " + ticks);
|
|
}
|
|
|
|
if (ticks >= 60) {
|
|
ticks = 0;
|
|
sendHeartbeat();
|
|
getPlaytime(); // <-- add this
|
|
|
|
}
|
|
}
|
|
|
|
private static void sendHeartbeat() {
|
|
Verifier.sendTokenForVerification();
|
|
|
|
Minecraft client =
|
|
Minecraft.getInstance();
|
|
|
|
if (client.player == null) {
|
|
System.out.println("No player yet");
|
|
return;
|
|
}
|
|
|
|
UUID uuid = client.player.getUUID();
|
|
|
|
String json = """
|
|
{
|
|
"uuid": "%s"
|
|
}
|
|
""".formatted(uuid);
|
|
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
.uri(URI.create(BACKEND_URL))
|
|
.timeout(Duration.ofSeconds(10))
|
|
.header("Content-Type", "application/json")
|
|
.POST(HttpRequest.BodyPublishers.ofString(json))
|
|
.build();
|
|
|
|
HTTP.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
|
.thenAccept(response -> {
|
|
if (response.statusCode() != 200) {
|
|
System.out.println("Heartbeat failed: " + response.statusCode());
|
|
}
|
|
})
|
|
.exceptionally(error -> {
|
|
error.printStackTrace();
|
|
return null;
|
|
});
|
|
|
|
}
|
|
|
|
|
|
//playtime vom server holen
|
|
|
|
private static final String PLAYTIME_URL =
|
|
"http://62.116.42.223:25501/api/player/%s/playtime";
|
|
|
|
|
|
public static void getPlaytime() {
|
|
|
|
Minecraft client = Minecraft.getInstance();
|
|
|
|
if (client.player == null) {
|
|
return;
|
|
}
|
|
|
|
UUID uuid = client.player.getUUID();
|
|
|
|
HttpRequest request = HttpRequest.newBuilder()
|
|
.uri(URI.create(
|
|
PLAYTIME_URL.formatted(uuid)
|
|
))
|
|
.GET()
|
|
.build();
|
|
|
|
|
|
HTTP.sendAsync(
|
|
request,
|
|
HttpResponse.BodyHandlers.ofString()
|
|
)
|
|
.thenAccept(response -> {
|
|
|
|
if (response.statusCode() == 200) {
|
|
|
|
String json = response.body();
|
|
|
|
JsonObject data =
|
|
JsonParser.parseString(json)
|
|
.getAsJsonObject();
|
|
|
|
|
|
currentPlaytimeSeconds =
|
|
data.get("playtime_seconds")
|
|
.getAsInt();
|
|
|
|
|
|
// Compare playtime here
|
|
if (currentPlaytimeSeconds >= 3600) {
|
|
System.out.println(
|
|
"Player has more than 1 hour!"
|
|
);
|
|
}
|
|
else {
|
|
System.out.println(
|
|
"Player needs more time"
|
|
);
|
|
}
|
|
|
|
|
|
System.out.println(
|
|
"Seconds played: " + currentPlaytimeSeconds
|
|
);
|
|
|
|
} else {
|
|
System.out.println(
|
|
"Failed: " + response.statusCode()
|
|
);
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
public static int getCurrentPlaytimeSeconds() {
|
|
return currentPlaytimeSeconds;
|
|
}
|
|
} |