From f77693d2e5427766c6540c22e514f9b151c36167 Mon Sep 17 00:00:00 2001 From: Remco Date: Wed, 1 Sep 2021 14:39:03 +0000 Subject: [PATCH] Fix offline friendrequests --- .../habbo/habbohotel/messenger/Messenger.java | 5 + .../habbohotel/messenger/MessengerBuddy.java | 3 +- .../incoming/friends/FriendRequestEvent.java | 96 +++++++++---------- 3 files changed, 55 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/messenger/Messenger.java b/src/main/java/com/eu/habbo/habbohotel/messenger/Messenger.java index db3c357c..b1d3eb25 100644 --- a/src/main/java/com/eu/habbo/habbohotel/messenger/Messenger.java +++ b/src/main/java/com/eu/habbo/habbohotel/messenger/Messenger.java @@ -65,6 +65,11 @@ public class Messenger { return users; } + /** + * @deprecated + * This method is no longer used and is only kept to avoid breaking any plugins + */ + @Deprecated public static boolean canFriendRequest(Habbo habbo, String friend) { Habbo user = Emulator.getGameEnvironment().getHabboManager().getHabbo(friend); HabboInfo habboInfo; diff --git a/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerBuddy.java b/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerBuddy.java index 4a292b87..65487f3f 100644 --- a/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerBuddy.java +++ b/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerBuddy.java @@ -33,7 +33,7 @@ public class MessengerBuddy implements Runnable, ISerialize { this.id = set.getInt("id"); this.username = set.getString("username"); this.gender = HabboGender.valueOf(set.getString("gender")); - this.online = Integer.valueOf(set.getString("online")); + this.online = set.getInt("online"); this.motto = set.getString("motto"); this.look = set.getString("look"); this.relation = (short) set.getInt("relation"); @@ -58,6 +58,7 @@ public class MessengerBuddy implements Runnable, ISerialize { this.look = set.getString("look"); this.relation = 0; this.userOne = 0; + this.online = set.getInt("online"); } catch (SQLException e) { LOGGER.error("Caught SQL exception", e); } diff --git a/src/main/java/com/eu/habbo/messages/incoming/friends/FriendRequestEvent.java b/src/main/java/com/eu/habbo/messages/incoming/friends/FriendRequestEvent.java index 758ca1b4..95b363d7 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/friends/FriendRequestEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/friends/FriendRequestEvent.java @@ -23,70 +23,70 @@ public class FriendRequestEvent extends MessageHandler { @Override public void handle() throws Exception { String username = this.packet.readString(); - Habbo habbo = Emulator.getGameServer().getGameClientManager().getHabbo(username); - if (habbo.getHabboInfo().getId() == this.client.getHabbo().getHabboInfo().getId()) { + if (this.client == null || username == null || username.isEmpty()) return; - } - if (Emulator.getPluginManager().fireEvent(new UserRequestFriendshipEvent(this.client.getHabbo(), username, habbo)).isCancelled()) { - this.client.sendResponse(new FriendRequestErrorComposer(2)); - return; - } + // TargetHabbo can be null if the Habbo is not online or when the Habbo doesn't exist + Habbo targetHabbo = Emulator.getGameServer().getGameClientManager().getHabbo(username); - int id = 0; - boolean allowFriendRequests = true; - - FriendRequest friendRequest = this.client.getHabbo().getMessenger().findFriendRequest(username); - if (friendRequest != null) { - this.client.getHabbo().getMessenger().acceptFriendRequest(friendRequest.getId(), this.client.getHabbo().getHabboInfo().getId()); - return; - } - - if (!Messenger.canFriendRequest(this.client.getHabbo(), username)) { - this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.TARGET_NOT_FOUND)); - return; - } - - if (habbo == null) { - try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT users_settings.block_friendrequests, users.id FROM users INNER JOIN users_settings ON users.id = users_settings.user_id WHERE username = ? LIMIT 1")) { + // If the Habbo is null, we try to get the Habbo from the database. + // If the Habbo is still null, the Habbo doesn't exist. + if (targetHabbo == null) { + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT users.*, users_settings.block_friendrequests FROM users INNER JOIN users_settings ON users.id = users_settings.user_id WHERE username = ? LIMIT 1")) { statement.setString(1, username); try (ResultSet set = statement.executeQuery()) { while (set.next()) { - id = set.getInt("id"); - allowFriendRequests = set.getString("block_friendrequests").equalsIgnoreCase("0"); + targetHabbo = new Habbo(set); } } } catch (SQLException e) { LOGGER.error("Caught SQL exception", e); return; } - } else { - id = habbo.getHabboInfo().getId(); - allowFriendRequests = !habbo.getHabboStats().blockFriendRequests; - if (allowFriendRequests) - habbo.getClient().sendResponse(new FriendRequestComposer(this.client.getHabbo())); } - if (id != 0) { - if (!allowFriendRequests) { - this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.TARGET_NOT_ACCEPTING_REQUESTS)); - return; - } - - if (this.client.getHabbo().getMessenger().getFriends().values().size() >= this.client.getHabbo().getHabboStats().maxFriends && !this.client.getHabbo().hasPermission("acc_infinite_friends")) { - this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.FRIEND_LIST_OWN_FULL)); - return; - } - - if (habbo.getMessenger().getFriends().values().size() >= habbo.getHabboStats().maxFriends && !habbo.hasPermission("acc_infinite_friends")) { - this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.FRIEND_LIST_TARGET_FULL)); - return; - } - - Messenger.makeFriendRequest(this.client.getHabbo().getHabboInfo().getId(), id); - } else { + if (targetHabbo == null) { this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.TARGET_NOT_FOUND)); + return; } + + int targetId = targetHabbo.getHabboInfo().getId(); + boolean targetBlocksFriendRequests = targetHabbo.getHabboStats().blockFriendRequests; + + // Making friends with yourself would be very pathetic, we try to avoid that + if (targetId == this.client.getHabbo().getHabboInfo().getId()) + return; + + // Target Habbo exists + // Check if Habbo is accepting friend requests + if (targetBlocksFriendRequests) { + this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.TARGET_NOT_ACCEPTING_REQUESTS)); + return; + } + + // You can only have x friends + if (this.client.getHabbo().getMessenger().getFriends().values().size() >= this.client.getHabbo().getHabboStats().maxFriends && !this.client.getHabbo().hasPermission("acc_infinite_friends")) { + this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.FRIEND_LIST_OWN_FULL)); + return; + } + + // Check if targets friendlist is full + if (targetHabbo.getMessenger().getFriends().values().size() >= targetHabbo.getHabboStats().maxFriends && !targetHabbo.hasPermission("acc_infinite_friends")) { + this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.FRIEND_LIST_TARGET_FULL)); + return; + } + + // Allow plugins to cancel the request + if (Emulator.getPluginManager().fireEvent(new UserRequestFriendshipEvent(this.client.getHabbo(), username, targetHabbo)).isCancelled()) { + this.client.sendResponse(new FriendRequestErrorComposer(2)); + return; + } + + if(targetHabbo.isOnline()) { + targetHabbo.getClient().sendResponse(new FriendRequestComposer(this.client.getHabbo())); + } + + Messenger.makeFriendRequest(this.client.getHabbo().getHabboInfo().getId(), targetId); } }