aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/RichPresenceManager.java
blob: 0d11a8f27f285eaec7cb6a99d1b6116048a166e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package kr.syeyoung.dungeonsguide;

import kr.syeyoung.dungeonsguide.events.SkyblockJoinedEvent;
import kr.syeyoung.dungeonsguide.events.SkyblockLeftEvent;
import kr.syeyoung.dungeonsguide.features.FeatureRegistry;
import kr.syeyoung.dungeonsguide.party.PartyInviteViewer;
import kr.syeyoung.dungeonsguide.party.PartyJoinRequest;
import kr.syeyoung.dungeonsguide.party.PartyManager;
import kr.syeyoung.dungeonsguide.stomp.StompHeader;
import kr.syeyoung.dungeonsguide.stomp.StompPayload;
import net.arikia.dev.drpc.*;
import net.arikia.dev.drpc.callbacks.*;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.json.JSONObject;

import java.io.*;
import java.net.URL;
import java.time.Instant;

public class RichPresenceManager implements Runnable {
    public static RichPresenceManager INSTANCE = new RichPresenceManager();

    public RichPresenceManager() {
        setup();
        new Thread(this).start();
        Runtime.getRuntime().addShutdownHook(new Thread(DiscordRPC::discordShutdown));
    }
    public void setup() {
        DiscordRPC.discordInitialize("816298079732498473", new DiscordEventHandlers.Builder()
                .setReadyEventHandler(new ReadyCallback() {
                    @Override
                    public void apply(DiscordUser user) {
                        updatePresence();
                    }
                }).setJoinRequestEventHandler(request -> {
                    PartyJoinRequest partyJoinRequest = new PartyJoinRequest();
                    partyJoinRequest.setDiscordUser(request);
                    partyJoinRequest.setTime(Instant.now());

                    PartyInviteViewer.INSTANCE.joinRequests.add(partyJoinRequest);
                }).setJoinGameEventHandler(joinSecret -> {
                    e.getDungeonsGuide().getStompConnection().send(new StompPayload().method(StompHeader.SEND)
                            .header("destination", "/app/party.askedtojoin")
                            .payload(new JSONObject().put("token", joinSecret).toString()));
                }).setErroredEventHandler((errorCode, message) -> {
                    System.out.println("ERROR! "+errorCode+ " - "+message);
                    setup();
                }).setDisconnectedEventHandler((errorCode, message) -> {
                    System.out.println("ERROR! "+errorCode+ " - "+message);
                    setup();
                }).build(), true);
    }

    private SkyblockStatus skyblockStatus = e.getDungeonsGuide().getSkyblockStatus();


    public void updatePresence() {
        if (!skyblockStatus.isOnHypixel() || !FeatureRegistry.ADVANCED_RICHPRESENCE.isEnabled()) {
            DiscordRPC.discordClearPresence();
        } else {
            DiscordRichPresence.Builder richPresenceBuilder = new DiscordRichPresence.Builder(skyblockStatus.getDungeonName());
            richPresenceBuilder.setBigImage("mort", "mort")
                    .setParty(PartyManager.INSTANCE.getPartyID(), 1, 5);

            if (skyblockStatus.getContext() != null) {
                long init = skyblockStatus.getContext().getInit();
                richPresenceBuilder.setStartTimestamps(init);
            } else {
                if (PartyManager.INSTANCE.isAllowAskToJoin())
                    richPresenceBuilder.setSecrets(PartyManager.INSTANCE.getAskToJoinSecret(), null);
            }
            richPresenceBuilder.setDetails("Dungeons Guide RichPresence Test");
            DiscordRPC.discordUpdatePresence(richPresenceBuilder.build());
        }
    }


    @SubscribeEvent
    public void joinSkyblock(SkyblockJoinedEvent skyblockJoinedEvent) {
        updatePresence();
    }
    @SubscribeEvent
    public void leaveSkyblock(SkyblockLeftEvent skyblockLeftEvent) {
        updatePresence();
    }

    private String lastLoc = "";

    @SubscribeEvent
    public void tick(TickEvent.ClientTickEvent clientTickEvent) {
    }
    @Override
    public void run() {
        while(true) {
            try {
                DiscordRPC.discordRunCallbacks();
                if (skyblockStatus.isOnSkyblock() && !lastLoc.equalsIgnoreCase(skyblockStatus.getDungeonName())) {
                    lastLoc = skyblockStatus.getDungeonName()+"";
                }
                updatePresence();
                Thread.sleep(1000L);
            } catch (Exception e) {e.printStackTrace();}
        }
    }
}