Merge branch 'feature/friendrequest-fixes' into 'dev'

Fix offline friendrequests

See merge request morningstar/Arcturus-Community!306
This commit is contained in:
ArpyAge 2021-09-01 14:39:03 +00:00
commit f7ee2ddf72
3 changed files with 55 additions and 49 deletions

View File

@ -65,6 +65,11 @@ public class Messenger {
return users; 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) { public static boolean canFriendRequest(Habbo habbo, String friend) {
Habbo user = Emulator.getGameEnvironment().getHabboManager().getHabbo(friend); Habbo user = Emulator.getGameEnvironment().getHabboManager().getHabbo(friend);
HabboInfo habboInfo; HabboInfo habboInfo;

View File

@ -33,7 +33,7 @@ public class MessengerBuddy implements Runnable, ISerialize {
this.id = set.getInt("id"); this.id = set.getInt("id");
this.username = set.getString("username"); this.username = set.getString("username");
this.gender = HabboGender.valueOf(set.getString("gender")); 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.motto = set.getString("motto");
this.look = set.getString("look"); this.look = set.getString("look");
this.relation = (short) set.getInt("relation"); this.relation = (short) set.getInt("relation");
@ -58,6 +58,7 @@ public class MessengerBuddy implements Runnable, ISerialize {
this.look = set.getString("look"); this.look = set.getString("look");
this.relation = 0; this.relation = 0;
this.userOne = 0; this.userOne = 0;
this.online = set.getInt("online");
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Caught SQL exception", e); LOGGER.error("Caught SQL exception", e);
} }

View File

@ -23,70 +23,70 @@ public class FriendRequestEvent extends MessageHandler {
@Override @Override
public void handle() throws Exception { public void handle() throws Exception {
String username = this.packet.readString(); 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; return;
}
if (Emulator.getPluginManager().fireEvent(new UserRequestFriendshipEvent(this.client.getHabbo(), username, habbo)).isCancelled()) { // TargetHabbo can be null if the Habbo is not online or when the Habbo doesn't exist
this.client.sendResponse(new FriendRequestErrorComposer(2)); Habbo targetHabbo = Emulator.getGameServer().getGameClientManager().getHabbo(username);
return;
}
int id = 0; // If the Habbo is null, we try to get the Habbo from the database.
boolean allowFriendRequests = true; // If the Habbo is still null, the Habbo doesn't exist.
if (targetHabbo == null) {
FriendRequest friendRequest = this.client.getHabbo().getMessenger().findFriendRequest(username); 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")) {
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")) {
statement.setString(1, username); statement.setString(1, username);
try (ResultSet set = statement.executeQuery()) { try (ResultSet set = statement.executeQuery()) {
while (set.next()) { while (set.next()) {
id = set.getInt("id"); targetHabbo = new Habbo(set);
allowFriendRequests = set.getString("block_friendrequests").equalsIgnoreCase("0");
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error("Caught SQL exception", e); LOGGER.error("Caught SQL exception", e);
return; return;
} }
} else {
id = habbo.getHabboInfo().getId();
allowFriendRequests = !habbo.getHabboStats().blockFriendRequests;
if (allowFriendRequests)
habbo.getClient().sendResponse(new FriendRequestComposer(this.client.getHabbo()));
} }
if (id != 0) { if (targetHabbo == null) {
if (!allowFriendRequests) { 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)); this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.TARGET_NOT_ACCEPTING_REQUESTS));
return; 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")) { 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)); this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.FRIEND_LIST_OWN_FULL));
return; return;
} }
if (habbo.getMessenger().getFriends().values().size() >= habbo.getHabboStats().maxFriends && !habbo.hasPermission("acc_infinite_friends")) { // 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)); this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.FRIEND_LIST_TARGET_FULL));
return; return;
} }
Messenger.makeFriendRequest(this.client.getHabbo().getHabboInfo().getId(), id); // Allow plugins to cancel the request
} else { if (Emulator.getPluginManager().fireEvent(new UserRequestFriendshipEvent(this.client.getHabbo(), username, targetHabbo)).isCancelled()) {
this.client.sendResponse(new FriendRequestErrorComposer(FriendRequestErrorComposer.TARGET_NOT_FOUND)); 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);
} }
} }