Skip to content

Commit

Permalink
track player session data (server ip, start time, duration)
Browse files Browse the repository at this point in the history
  • Loading branch information
PepperLola committed Jun 15, 2021
1 parent 7a21986 commit 2130311
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;

public class DiscordRichPresenceMod extends Module {

Expand All @@ -38,6 +41,8 @@ public class DiscordRichPresenceMod extends Module {
int updateTicks = 0;
private static IPCClient client;

public static long START_TIME = -1;

public DiscordRichPresenceMod() {
super("discordRPC", "Discord RPC", "Enabled Discord Rich Presence", ModuleType.MISC, null, null);
}
Expand Down Expand Up @@ -70,7 +75,7 @@ public void onReady(IPCClient client) {
builder.setState(DiscordState.getDisplayString(discordState))
.setLargeImage(serverIp.contains("hypixel") ? currentGame : "minecraft", Minecraft.getMinecraft().getSession().getUsername())
.setSmallImage("playerinfo", PlayerInfo.MODID + " v" + PlayerInfo.VERSION)
.setStartTimestamp(OffsetDateTime.now());
.setStartTimestamp(OffsetDateTime.ofInstant(Instant.ofEpochMilli(START_TIME), ZoneId.systemDefault()));
client.sendRichPresence(builder.build());
}).start();
}
Expand Down Expand Up @@ -131,9 +136,17 @@ public void onServerJoin(ServerJoinEvent event) {
System.out.printf("CONNECTED TO: %s%n", event.getServer());
setDiscordState(DiscordState.MULTIPLAYER);
serverIp = event.getServer();
START_TIME = System.currentTimeMillis();
updateDiscord();
}

@SubscribeEvent
public void onServerLeave(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) {
System.out.println("DISCONNECTED FROM " + serverIp);
START_TIME = -1;
serverIp = "";
}

/**
* Updates on scoreboard change to update Hypixel minigame in presence
* @param event
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/palight/playerinfo/util/ApiUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.palight.playerinfo.util;

import com.palight.playerinfo.PlayerInfo;
import com.palight.playerinfo.modules.impl.misc.DiscordRichPresenceMod;
import net.minecraft.client.Minecraft;
import org.apache.http.Header;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -45,7 +47,23 @@ public static void authenticate(String username, String password) {
}

public static void sendOnline() {
String data = String.format("{\"minecraft_uuid\": \"%s\"}", Minecraft.getSessionInfo().get("X-Minecraft-UUID"));
String data = String.format(
"{\"minecraft_uuid\": \"%s\"}",
Minecraft.getSessionInfo().get("X-Minecraft-UUID")
);

if (DiscordRichPresenceMod.START_TIME != -1 && !DiscordRichPresenceMod.serverIp.isEmpty()) {
JSONObject obj = new JSONObject()
.put("minecraft_uuid", Minecraft.getSessionInfo().get("X-Minecraft-UUID"))
.put("start_time", DiscordRichPresenceMod.START_TIME)
.put("server_ip", DiscordRichPresenceMod.serverIp)
.put("mod_version", PlayerInfo.VERSION);

data = obj.toString();
}



Map<String, String> HEADERS = new HashMap<>();
HEADERS.put("Content-Type", "application/json");
HttpUtil.httpPut(API_URL + "/user/online", HEADERS, data, response -> {
Expand Down

0 comments on commit 2130311

Please sign in to comment.