diff options
author | syeyoung <cyong06@naver.com> | 2021-03-06 09:04:04 +0900 |
---|---|---|
committer | syeyoung <cyong06@naver.com> | 2021-03-06 09:04:04 +0900 |
commit | d07cfe08841302ad1ce1e29902d06c4c439a589f (patch) | |
tree | 20f4b7d5f6fea746a9298a4c356f6eb6e497fd1c /src/main/java/kr/syeyoung/dungeonsguide/RichPresenceManager.java | |
parent | 55e475a31c722a3e9e3bd3216bc0e0d396b6f359 (diff) | |
download | Skyblock-Dungeons-Guide-d07cfe08841302ad1ce1e29902d06c4c439a589f.tar.gz Skyblock-Dungeons-Guide-d07cfe08841302ad1ce1e29902d06c4c439a589f.tar.bz2 Skyblock-Dungeons-Guide-d07cfe08841302ad1ce1e29902d06c4c439a589f.zip |
ssl better
Diffstat (limited to 'src/main/java/kr/syeyoung/dungeonsguide/RichPresenceManager.java')
-rw-r--r-- | src/main/java/kr/syeyoung/dungeonsguide/RichPresenceManager.java | 118 |
1 files changed, 73 insertions, 45 deletions
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/RichPresenceManager.java b/src/main/java/kr/syeyoung/dungeonsguide/RichPresenceManager.java index f3b75d41..422d15e1 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/RichPresenceManager.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/RichPresenceManager.java @@ -1,67 +1,61 @@ package kr.syeyoung.dungeonsguide; -import com.jagrosh.discordipc.IPCClient; -import com.jagrosh.discordipc.IPCListener; -import com.jagrosh.discordipc.entities.Packet; -import com.jagrosh.discordipc.entities.RichPresence; -import com.jagrosh.discordipc.entities.User; -import com.jagrosh.discordipc.exceptions.NoDiscordClientException; import kr.syeyoung.dungeonsguide.events.SkyblockJoinedEvent; import kr.syeyoung.dungeonsguide.events.SkyblockLeftEvent; +import kr.syeyoung.dungeonsguide.features.FeatureRegistry; +import kr.syeyoung.dungeonsguide.party.PartyManager; +import kr.syeyoung.dungeonsguide.stomp.StompHeader; +import kr.syeyoung.dungeonsguide.stomp.StompPayload; +import net.arikia.dev.drpc.DiscordEventHandlers; +import net.arikia.dev.drpc.DiscordRPC; +import net.arikia.dev.drpc.DiscordRichPresence; +import net.arikia.dev.drpc.DiscordUser; +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.time.OffsetDateTime; - -public class RichPresenceManager implements IPCListener { +public class RichPresenceManager implements JoinRequestCallback, JoinGameCallback, ErroredCallback, DisconnectedCallback { public static RichPresenceManager INSTANCE = new RichPresenceManager(); - private IPCClient ipcClient; - public void setup() throws NoDiscordClientException { - ipcClient = new IPCClient(816298079732498473L); - ipcClient.setListener(this); - ipcClient.connect(); - } - - @Override - public void onReady(IPCClient client) { - updatePresence(); - } - - @Override - public void onActivityJoinRequest(IPCClient client, String secret, User user) { - System.out.println(user.getAsMention()+" wanna join"); - } - - @Override - public void onPacketReceived(IPCClient client, Packet packet) { - System.out.println("Packet recv from rpc "+packet); - } - - @Override - public void onDisconnect(IPCClient client, Throwable t) { - t.printStackTrace(); - try { - setup(); - } catch (NoDiscordClientException e) { - e.printStackTrace(); - } + public void setup() { + DiscordRPC.discordInitialize("816298079732498473", new DiscordEventHandlers.Builder() + .setReadyEventHandler(new ReadyCallback() { + @Override + public void apply(DiscordUser user) { + updatePresence(); + } + }) + .setJoinRequestEventHandler(this) + .setJoinGameEventHandler(this) + .setErroredEventHandler(this) + .setDisconnectedEventHandler(this).build(), true); } private SkyblockStatus skyblockStatus = e.getDungeonsGuide().getSkyblockStatus(); public void updatePresence() { - if (!skyblockStatus.isOnHypixel()) { - ipcClient.sendRichPresence(null); + nextUpdate= System.currentTimeMillis() + 10000L; + if (!skyblockStatus.isOnHypixel() || !FeatureRegistry.ADVANCED_RICHPRESENCE.isEnabled()) { + DiscordRPC.discordClearPresence(); } else { - ipcClient.sendRichPresence(new RichPresence.Builder() - .setJoinSecret("")); + 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()); } } - private String JOIN_SECRET; - @SubscribeEvent public void joinSkyblock(SkyblockJoinedEvent skyblockJoinedEvent) { @@ -71,4 +65,38 @@ public class RichPresenceManager implements IPCListener { public void leaveSkyblock(SkyblockLeftEvent skyblockLeftEvent) { updatePresence(); } + + private String lastLoc = ""; + private long nextUpdate = System.currentTimeMillis() + 10000L; + @SubscribeEvent + public void tick(TickEvent.ClientTickEvent clientTickEvent) { + try { + if (skyblockStatus.isOnSkyblock() && !lastLoc.equalsIgnoreCase(skyblockStatus.getDungeonName())) { + lastLoc = skyblockStatus.getDungeonName()+""; + updatePresence(); + } else if (nextUpdate < System.currentTimeMillis()) { + updatePresence(); + } + } catch (Exception e) {e.printStackTrace();} + } + + @Override + public void apply(int errorCode, String message) { + System.out.println("ERROR! "+errorCode+ " - "+message); + setup(); + } + + @Override + public void apply(String joinSecret) { + System.out.println("OK JOINNNNNNNNNNNNNNNNNN "+joinSecret); + e.getDungeonsGuide().getStompConnection().send(new StompPayload().method(StompHeader.SEND) + .header("destination", "/app/party.askedtojoin") + .payload(new JSONObject().put("token", joinSecret).toString())); + } + + @Override + public void apply(DiscordUser user) { + System.out.println(user.username+" wants to join"); + DiscordRPC.discordRespond(user.userId, DiscordRPC.DiscordReply.YES); + } } |