Add option to toggle friend requests

This commit is contained in:
Jonttuuu 2023-07-30 17:14:13 +03:00
parent a9c0e24378
commit fc947f3145
3 changed files with 28 additions and 2 deletions

View File

@ -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

View File

@ -17,6 +17,7 @@ public final class PlayerFriends {
private final Set<UUID> friends = new HashSet<>();
private final Set<UUID> incomingFriendRequests = new HashSet<>();
private final Set<UUID> 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<JsonObject> 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

View File

@ -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()) {