Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

discord: fix party support #2191

Merged
merged 4 commits into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class WebsocketMessage implements Event
{
protected boolean _party;
public String text;

public boolean isParty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import net.runelite.client.ui.overlay.infobox.InfoBoxOverlay;
import net.runelite.client.ui.overlay.tooltip.TooltipOverlay;
import net.runelite.client.ui.overlay.worldmap.WorldMapOverlay;
import net.runelite.client.ws.PartyService;
import org.slf4j.LoggerFactory;

@Singleton
Expand Down Expand Up @@ -161,6 +162,9 @@ public class RuneLite
@Inject
private Provider<ChatboxPanelManager> chatboxPanelManager;

@Inject
private Provider<PartyService> partyService;

@Inject
private Hooks hooks;

Expand Down Expand Up @@ -248,7 +252,6 @@ protected PasswordAuthentication getPasswordAuthentication()
}
}


SentryClient client = Sentry.init("https://fa31d674e44247fa93966c69a903770f@sentry.io/1811856");
client.setRelease(RuneLiteProperties.getPlusVersion());

Expand Down Expand Up @@ -299,7 +302,6 @@ protected PasswordAuthentication getPasswordAuthentication()
true));

injector.getInstance(RuneLite.class).start();

final long end = System.currentTimeMillis();
final RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
final long uptime = rb.getUptime();
Expand Down Expand Up @@ -372,6 +374,7 @@ private void start() throws Exception
xpDropManager.get();
playerManager.get();
chatboxPanelManager.get();
partyService.get();

eventBus.subscribe(GameStateChanged.class, this, hooks::onGameStateChanged);
eventBus.subscribe(ScriptCallbackEvent.class, this, hooks::onScriptCallbackEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected void startUp()
{
partyService.setUsername(discordService.getCurrentUser().username + "#" + discordService.getCurrentUser().discriminator);
}

wsClient.unregisterMessage(DiscordUserInfo.class);
wsClient.registerMessage(DiscordUserInfo.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@Value
@EqualsAndHashCode(callSuper = true)
class DiscordUserInfo extends PartyMemberMessage
public class DiscordUserInfo extends PartyMemberMessage
{
private final String userId;
private final String avatarId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package net.runelite.client.plugins.party;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.inject.Binder;
import com.google.inject.Provides;
import java.awt.Color;
Expand Down Expand Up @@ -60,12 +62,14 @@
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.chat.QueuedMessage;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.EventBus;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.ConfigChanged;
import net.runelite.client.events.OverlayMenuClicked;
import net.runelite.client.events.PartyChanged;
import net.runelite.client.input.KeyListener;
import net.runelite.client.input.KeyManager;
import net.runelite.client.plugins.discord.DiscordUserInfo;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.PluginType;
Expand All @@ -82,6 +86,10 @@
import net.runelite.client.ws.PartyMember;
import net.runelite.client.ws.PartyService;
import net.runelite.client.ws.WSClient;
import net.runelite.http.api.ws.WebsocketMessage;
import net.runelite.http.api.ws.messages.party.Join;
import net.runelite.http.api.ws.messages.party.PartyChatMessage;
import net.runelite.http.api.ws.messages.party.PartyMemberMessage;
import net.runelite.http.api.ws.messages.party.UserJoin;
import net.runelite.http.api.ws.messages.party.UserPart;
import net.runelite.http.api.ws.messages.party.UserSync;
Expand All @@ -95,49 +103,37 @@
@Singleton
public class PartyPlugin extends Plugin implements KeyListener
{
@Getter(AccessLevel.PACKAGE)
private final Map<UUID, PartyData> partyDataMap = Collections.synchronizedMap(new HashMap<>());
@Getter(AccessLevel.PACKAGE)
private final List<PartyTilePingData> pendingTilePings = Collections.synchronizedList(new ArrayList<>());
@Inject
@Named("developerMode")
boolean developerMode;
@Inject
private Client client;

@Inject
private PartyService party;

@Inject
private WSClient ws;

@Inject
private OverlayManager overlayManager;

@Inject
private PartyStatsOverlay partyStatsOverlay;

@Inject
private PartyPingOverlay partyPingOverlay;

@Inject
private KeyManager keyManager;

@Inject
private WSClient wsClient;

@Inject
private WorldMapPointManager worldMapManager;

@Inject
private PartyConfig config;

@Inject
private ChatMessageManager chatMessageManager;

@Inject
@Named("developerMode")
boolean developerMode;

@Getter(AccessLevel.PACKAGE)
private final Map<UUID, PartyData> partyDataMap = Collections.synchronizedMap(new HashMap<>());

@Getter(AccessLevel.PACKAGE)
private final List<PartyTilePingData> pendingTilePings = Collections.synchronizedList(new ArrayList<>());

private EventBus eventBus;
private int lastHp, lastPray;
private boolean hotkeyDown, doSync;
private boolean sendAlert;
Expand Down Expand Up @@ -488,6 +484,83 @@ private void onCommandExecuted(CommandExecuted commandExecuted)
}
}

@Subscribe
private void onPartyMemberMessage(PartyMemberMessage event)
{
JsonObject jobj = new Gson().fromJson(event.text, JsonObject.class);
if (jobj.get("type").getAsString().equals("SkillUpdate"))
{
Skill skillToUpdate = Skill.valueOf(jobj.get("skill").getAsString());
SkillUpdate skillUpdateEvent = new SkillUpdate(skillToUpdate, jobj.get("value").getAsInt(), jobj.get("max").getAsInt());
skillUpdateEvent.setMemberId(event.getMemberId());
eventBus.post(SkillUpdate.class, skillUpdateEvent);
return;
}

if (jobj.get("type").getAsString().equals("LocationUpdate"))
{
WorldPoint worldPoint = new WorldPoint(jobj.get("worldPoint").getAsJsonObject().get("x").getAsInt(), jobj.get("worldPoint").getAsJsonObject().get("y").getAsInt(), jobj.get("worldPoint").getAsJsonObject().get("plane").getAsInt());
LocationUpdate locationUpdate = new LocationUpdate(worldPoint);
locationUpdate.setMemberId(event.getMemberId());
eventBus.post(LocationUpdate.class, locationUpdate);
return;
}

if (jobj.get("type").getAsString().equals("DiscordUserInfo"))
{
DiscordUserInfo info = new DiscordUserInfo(jobj.get("userId").getAsString(), jobj.get("avatarId").getAsString());
info.setMemberId(event.getMemberId());
eventBus.post(DiscordUserInfo.class, info);
return;
}
}

@Subscribe
private void onWebsocketMessage(WebsocketMessage event)
{
JsonObject jobj = new Gson().fromJson(event.text, JsonObject.class);
if (jobj.get("type").getAsString().equals("UserJoin"))
{
UserJoin joinEvent = new UserJoin(UUID.fromString(jobj.get("memberId").getAsString()), UUID.fromString(jobj.get("partyId").getAsString()), jobj.get("name").getAsString());
eventBus.post(UserJoin.class, joinEvent);
return;
}
if (jobj.get("type").getAsString().equals("Join"))
{
Join joinEvent = new Join(UUID.fromString(jobj.get("partyId").getAsString()), jobj.get("name").getAsString());
eventBus.post(Join.class, joinEvent);
return;
}
if (jobj.get("type").getAsString().equals("PartyChatMessage"))
{
PartyChatMessage partyChatMessageEvent = new PartyChatMessage(jobj.get("value").getAsString());
eventBus.post(PartyChatMessage.class, partyChatMessageEvent);
return;
}
if (jobj.get("type").getAsString().equals("UserPart"))
{
UserPart userPartEvent = new UserPart(UUID.fromString(jobj.get("memberId").getAsString()));
eventBus.post(UserPart.class, userPartEvent);
return;
}
if (jobj.get("type").getAsString().equals("UserSync"))
{
UserSync userPartEvent = new UserSync();
eventBus.post(UserSync.class, userPartEvent);
return;
}
if (jobj.get("type").getAsString().equals("TilePing"))
{
WorldPoint worldPoint = new WorldPoint(jobj.get("worldPoint").getAsJsonObject().get("x").getAsInt(), jobj.get("worldPoint").getAsJsonObject().get("y").getAsInt(), jobj.get("worldPoint").getAsJsonObject().get("plane").getAsInt());
TilePing tilePing = new TilePing(worldPoint);
eventBus.post(TilePing.class, tilePing);
return;
}

log.debug("Unhandled WS event: {}", event.text);

}

@Nullable
PartyData getPartyData(final UUID uuid)
{
Expand Down
39 changes: 20 additions & 19 deletions runelite-client/src/main/java/net/runelite/client/ws/WSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import net.runelite.http.api.ws.WebsocketGsonFactory;
import net.runelite.http.api.ws.WebsocketMessage;
import net.runelite.http.api.ws.messages.Handshake;
import net.runelite.http.api.ws.messages.party.PartyMessage;
import net.runelite.http.api.ws.messages.party.PartyMemberMessage;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
Expand Down Expand Up @@ -148,9 +148,17 @@ public void close()
}

@Override
public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response)
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason)
{
log.info("Websocket {} opened", webSocket);
log.info("Websocket {} closed: {}/{}", webSocket, code, reason);
this.webSocket = null;
}

@Override
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, Response response)
{
log.warn("Error in websocket {}:{}", response, t);
this.webSocket = null;
}

@Override
Expand All @@ -167,28 +175,21 @@ public void onMessage(@NotNull WebSocket webSocket, @NotNull String text)
log.debug("Failed to deserialize message", e);
return;
}
message.text = text;

if (message.isParty() && !(message instanceof PartyMessage))
if (message instanceof PartyMemberMessage)
{
// spoofed message?
return;
eventBus.post(PartyMemberMessage.class, message);
}
else
{
eventBus.post(WebsocketMessage.class, message);
}

log.debug("Got: {}", text);
eventBus.post(PartyMessage.class, message);
}

@Override
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason)
{
log.info("Websocket {} closed: {}/{}", webSocket, code, reason);
this.webSocket = null;
}

@Override
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, Response response)
public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response)
{
log.warn("Error in websocket {}:{}", response, t);
this.webSocket = null;
log.info("Websocket {} opened", webSocket);
}
}