From fc947f314534dfafa46031a3b78729d7103557ff Mon Sep 17 00:00:00 2001 From: Jonttuuu <50843099+Jonttuuu@users.noreply.github.com> Date: Sun, 30 Jul 2023 17:14:13 +0300 Subject: [PATCH] Add option to toggle friend requests --- .../java/fi/flexplex/lib/FlexLibAdapter.java | 5 +++++ .../java/fi/flexplex/lib/PlayerFriends.java | 17 +++++++++++++++-- .../java/fi/flexplex/lib/WebSocketClient.java | 8 ++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/fi/flexplex/lib/FlexLibAdapter.java b/src/main/java/fi/flexplex/lib/FlexLibAdapter.java index 461853c..75077c7 100644 --- a/src/main/java/fi/flexplex/lib/FlexLibAdapter.java +++ b/src/main/java/fi/flexplex/lib/FlexLibAdapter.java @@ -12,6 +12,11 @@ public interface FlexLibAdapter { */ public default void onWebSocketDisconnect() {} + /** + * Called when player friend requests are toggle on/off + */ + public default void onFriendRequestsToggle(final String player, final boolean newValue) {} + /** * Called when player friend list or friend requests are updated * @param player diff --git a/src/main/java/fi/flexplex/lib/PlayerFriends.java b/src/main/java/fi/flexplex/lib/PlayerFriends.java index ef8945f..a704244 100644 --- a/src/main/java/fi/flexplex/lib/PlayerFriends.java +++ b/src/main/java/fi/flexplex/lib/PlayerFriends.java @@ -17,6 +17,7 @@ public final class PlayerFriends { private final Set friends = new HashSet<>(); private final Set incomingFriendRequests = new HashSet<>(); private final Set outcomingFriendRequests = new HashSet<>(); + private boolean friendRequestsEnabled; private long lastUsed = System.currentTimeMillis(); protected PlayerFriends(final GraphQLApi graphQLApi, final String token, final String player) throws IOException { @@ -36,8 +37,9 @@ public final class PlayerFriends { final Optional result = this.graphQLApi.execute( "query{" + "player(uuid:\"" + this.player + "\"){" + - "friends{uuid}" + - "incomingFriendRequests(token:\"" + this.token + "\"){uuid}" + + "acceptFriendRequests," + + "friends{uuid}," + + "incomingFriendRequests(token:\"" + this.token + "\"){uuid}," + "outcomingFriendRequests(token:\"" + this.token + "\"){uuid}" + "}" + "}" @@ -48,6 +50,8 @@ public final class PlayerFriends { final JsonObject playerObject = result.get().get("player").getAsJsonObject(); + this.friendRequestsEnabled = playerObject.get("acceptFriendRequests").getAsBoolean(); + this.friends.clear(); for (final JsonElement element : playerObject.get("friends").getAsJsonArray()) { this.friends.add(UUID.fromString(element.getAsJsonObject().get("uuid").getAsString())); @@ -93,6 +97,15 @@ public final class PlayerFriends { return this.outcomingFriendRequests; } + /** + * Check if friend requests is enabled + * @return is friend requests enabled + */ + public boolean isFriendRequestsEnabled() { + this.lastUsed = System.currentTimeMillis(); + return friendRequestsEnabled; + } + /** * Create new friend request * @param player from diff --git a/src/main/java/fi/flexplex/lib/WebSocketClient.java b/src/main/java/fi/flexplex/lib/WebSocketClient.java index ab2c442..8478403 100644 --- a/src/main/java/fi/flexplex/lib/WebSocketClient.java +++ b/src/main/java/fi/flexplex/lib/WebSocketClient.java @@ -48,6 +48,14 @@ public final class WebSocketClient { public void onTextMessage(final WebSocket ws, final String message) { final String[] args = message.split(" "); switch (args[0]) { + case "FRIEND_REQUEST_TOGGLE": + if (args.length == 3) { + for (final FlexLibAdapter listener : flexLib.getEventListeners()) { + listener.onFriendRequestsToggle(args[1], args[2].equals("TRUE")); + listener.onFriendsUpdate(args[1]); + } + } + break; case "FRIEND_REQUEST": if (args.length == 3) { for (final FlexLibAdapter listener : flexLib.getEventListeners()) {