From 5a66c30e8dc6cea4de1557b4584b93fcb7335646 Mon Sep 17 00:00:00 2001 From: brenoepics Date: Sat, 19 Mar 2022 19:34:21 +0000 Subject: [PATCH] Feature Friendlist Categories --- sqlupdates/3_0_0 to 3_0_1.sql | 11 +++ .../habbo/habbohotel/messenger/Messenger.java | 10 +-- .../habbohotel/messenger/MessengerBuddy.java | 6 +- .../messenger/MessengerCategory.java | 31 +++++++ .../eu/habbo/habbohotel/users/HabboInfo.java | 55 ++++++++++++ .../incoming/friends/ChangeRelationEvent.java | 2 +- .../outgoing/friends/FriendsComposer.java | 6 +- .../friends/MessengerInitComposer.java | 19 +++- .../friends/UpdateFriendComposer.java | 88 +++++++++++-------- 9 files changed, 177 insertions(+), 51 deletions(-) create mode 100644 src/main/java/com/eu/habbo/habbohotel/messenger/MessengerCategory.java diff --git a/sqlupdates/3_0_0 to 3_0_1.sql b/sqlupdates/3_0_0 to 3_0_1.sql index 71ab3129..8ae563e1 100644 --- a/sqlupdates/3_0_0 to 3_0_1.sql +++ b/sqlupdates/3_0_0 to 3_0_1.sql @@ -10,3 +10,14 @@ INSERT INTO `emulator_settings` (`key`, `value`) VALUES ('console.mode', '1'); INSERT INTO `emulator_settings` (`key`, `value`) VALUES ('youtube.apikey', ''); INSERT INTO `emulator_settings` (`key`, `value`) VALUES ('hotel.gifts.length.max', '300'); + +-- Add friendship categories table +CREATE TABLE `messenger_categories` ( + `id` int NOT NULL AUTO_INCREMENT, + `name` varchar(25) NOT NULL, + `user_id` int NOT NULL, + UNIQUE KEY `identifier` (`id`) +); + +-- Set an ID (int) from category list items +ALTER TABLE messenger_friendships ADD category int NOT NULL DEFAULT '0' AFTER friends_since; \ No newline at end of file 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 b1d3eb25..fa4603cf 100644 --- a/src/main/java/com/eu/habbo/habbohotel/messenger/Messenger.java +++ b/src/main/java/com/eu/habbo/habbohotel/messenger/Messenger.java @@ -302,7 +302,7 @@ public class Messenger { buddy.setLook(owner.getHabboInfo().getLook()); buddy.setGender(owner.getHabboInfo().getGender()); buddy.setUsername(owner.getHabboInfo().getUsername()); - habbo.getClient().sendResponse(new UpdateFriendComposer(buddy)); + habbo.getClient().sendResponse(new UpdateFriendComposer(habbo, buddy, 0)); } } } @@ -368,12 +368,12 @@ public class Messenger { return; } - habboTo.getClient().sendResponse(new UpdateFriendComposer(to)); - habboFrom.getClient().sendResponse(new UpdateFriendComposer(from)); + habboTo.getClient().sendResponse(new UpdateFriendComposer(habboTo, to, 1)); + habboFrom.getClient().sendResponse(new UpdateFriendComposer(habboFrom, from, 1)); } else if (habboTo != null) { - habboTo.getClient().sendResponse(new UpdateFriendComposer(this.loadFriend(habboTo, userFrom))); + habboTo.getClient().sendResponse(new UpdateFriendComposer(habboTo, this.loadFriend(habboTo, userFrom), 1)); } else if (habboFrom != null) { - habboFrom.getClient().sendResponse(new UpdateFriendComposer(this.loadFriend(habboFrom, userTo))); + habboFrom.getClient().sendResponse(new UpdateFriendComposer(habboFrom, this.loadFriend(habboFrom, userTo), 1)); } } } 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 e68eefb5..d879d004 100644 --- a/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerBuddy.java +++ b/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerBuddy.java @@ -25,6 +25,7 @@ public class MessengerBuddy implements Runnable, ISerialize { private String look = ""; private String motto = ""; private short relation; + private int categoryId = 0; private boolean inRoom; private int userOne = 0; @@ -37,6 +38,7 @@ public class MessengerBuddy implements Runnable, ISerialize { this.motto = set.getString("motto"); this.look = set.getString("look"); this.relation = (short) set.getInt("relation"); + this.categoryId = set.getInt("category"); this.userOne = set.getInt("user_one_id"); this.inRoom = false; if (this.online == 1) { @@ -136,6 +138,8 @@ public class MessengerBuddy implements Runnable, ISerialize { Emulator.getThreading().run(this); } + public int getCategoryId() { return this.categoryId; } + public boolean inRoom() { return this.inRoom; } @@ -181,7 +185,7 @@ public class MessengerBuddy implements Runnable, ISerialize { message.appendBoolean(this.online == 1); message.appendBoolean(this.inRoom); //IN ROOM message.appendString(this.look); - message.appendInt(0); // Friends category ID + message.appendInt(this.categoryId); // Friends category ID message.appendString(this.motto); message.appendString(""); //Last seen as DATETIMESTRING message.appendString(""); // Realname or Facebookame as String diff --git a/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerCategory.java b/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerCategory.java new file mode 100644 index 00000000..b19a43b0 --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/messenger/MessengerCategory.java @@ -0,0 +1,31 @@ +package com.eu.habbo.habbohotel.messenger; + +public class MessengerCategory { + private int user_id; + private String name; + private int id; + + public MessengerCategory(String name, int user_id, int id) { + this.name = name; + this.user_id = user_id; + this.id = id; + } + + public String getName() { + return name; + } + + public int getUserId() { + return user_id; + } + + public int getId() { + return id; + } + + public void setName(String name) { + this.name = name; + } + public void setId(int id) { this.id = id; } +} + diff --git a/src/main/java/com/eu/habbo/habbohotel/users/HabboInfo.java b/src/main/java/com/eu/habbo/habbohotel/users/HabboInfo.java index a7fd28a5..8c166631 100644 --- a/src/main/java/com/eu/habbo/habbohotel/users/HabboInfo.java +++ b/src/main/java/com/eu/habbo/habbohotel/users/HabboInfo.java @@ -4,6 +4,7 @@ import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.catalog.CatalogItem; import com.eu.habbo.habbohotel.games.Game; import com.eu.habbo.habbohotel.games.GamePlayer; +import com.eu.habbo.habbohotel.messenger.MessengerCategory; import com.eu.habbo.habbohotel.navigation.NavigatorSavedSearch; import com.eu.habbo.habbohotel.permissions.Rank; import com.eu.habbo.habbohotel.pets.PetTasks; @@ -55,6 +56,7 @@ public class HabboInfo implements Runnable { private int webPublishTimestamp; private String machineID; private List savedSearches = new ArrayList<>(); + private List messengerCategories = new ArrayList<>(); public HabboInfo(ResultSet set) { try { @@ -88,6 +90,7 @@ public class HabboInfo implements Runnable { this.loadCurrencies(); this.loadSavedSearches(); + this.loadMessengerCategories(); } private void loadCurrencies() { @@ -179,6 +182,56 @@ public class HabboInfo implements Runnable { } } + private void loadMessengerCategories() { + this.messengerCategories = new ArrayList<>(); + + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM messenger_categories WHERE user_id = ?")) { + statement.setInt(1, this.id); + try (ResultSet set = statement.executeQuery()) { + while (set.next()) { + this.messengerCategories.add(new MessengerCategory(set.getString("name"), set.getInt("user_id"), set.getInt("id"))); + } + } + } catch (SQLException e) { + LOGGER.error("Caught SQL exception", e); + } + } + + public void addMessengerCategory(MessengerCategory category) { + this.messengerCategories.add(category); + + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO messenger_categories (name, user_id) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS)) { + statement.setString(1, category.getName()); + statement.setInt(2, this.id); + int affectedRows = statement.executeUpdate(); + + if (affectedRows == 0) { + throw new SQLException("Creating messenger category failed, no rows affected."); + } + + try (ResultSet generatedKeys = statement.getGeneratedKeys()) { + if (generatedKeys.next()) { + category.setId(generatedKeys.getInt(1)); + } else { + throw new SQLException("Creating messenger category failed, no ID found."); + } + } + } catch (SQLException e) { + LOGGER.error("Caught SQL exception", e); + } + } + + public void deleteMessengerCategory(MessengerCategory category) { + this.messengerCategories.remove(category); + + try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("DELETE FROM messenger_categories WHERE id = ?")) { + statement.setInt(1, category.getId()); + statement.execute(); + } catch (SQLException e) { + LOGGER.error("Caught SQL exception", e); + } + } + public int getCurrencyAmount(int type) { return this.currencies.get(type); } @@ -478,6 +531,8 @@ public class HabboInfo implements Runnable { return this.savedSearches; } + public List getMessengerCategories() { return this.messengerCategories; } + @Override public void run() { this.saveCurrencies(); diff --git a/src/main/java/com/eu/habbo/messages/incoming/friends/ChangeRelationEvent.java b/src/main/java/com/eu/habbo/messages/incoming/friends/ChangeRelationEvent.java index cb02842f..d2790194 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/friends/ChangeRelationEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/friends/ChangeRelationEvent.java @@ -16,7 +16,7 @@ public class ChangeRelationEvent extends MessageHandler { UserRelationShipEvent event = new UserRelationShipEvent(this.client.getHabbo(), buddy, relationId); if (!event.isCancelled()) { buddy.setRelation(event.relationShip); - this.client.sendResponse(new UpdateFriendComposer(buddy)); + this.client.sendResponse(new UpdateFriendComposer(this.client.getHabbo(), buddy, 0)); } } } diff --git a/src/main/java/com/eu/habbo/messages/outgoing/friends/FriendsComposer.java b/src/main/java/com/eu/habbo/messages/outgoing/friends/FriendsComposer.java index 2fd69a9a..287e87cb 100644 --- a/src/main/java/com/eu/habbo/messages/outgoing/friends/FriendsComposer.java +++ b/src/main/java/com/eu/habbo/messages/outgoing/friends/FriendsComposer.java @@ -41,10 +41,10 @@ public class FriendsComposer extends MessageComposer { this.response.appendBoolean(row.getOnline() == 1); this.response.appendBoolean(row.inRoom()); //IN ROOM this.response.appendString(row.getOnline() == 1 ? row.getLook() : ""); - this.response.appendInt(0); + this.response.appendInt(row.getCategoryId()); //Friends category this.response.appendString(row.getMotto()); - this.response.appendString(""); - this.response.appendString(""); + this.response.appendString(""); //Last seen as DATETIMESTRING + this.response.appendString(""); //Realname or Facebookame as String this.response.appendBoolean(false); //Offline messaging. this.response.appendBoolean(false); this.response.appendBoolean(false); diff --git a/src/main/java/com/eu/habbo/messages/outgoing/friends/MessengerInitComposer.java b/src/main/java/com/eu/habbo/messages/outgoing/friends/MessengerInitComposer.java index a48e6e65..de3b9206 100644 --- a/src/main/java/com/eu/habbo/messages/outgoing/friends/MessengerInitComposer.java +++ b/src/main/java/com/eu/habbo/messages/outgoing/friends/MessengerInitComposer.java @@ -1,11 +1,15 @@ package com.eu.habbo.messages.outgoing.friends; +import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.messenger.Messenger; +import com.eu.habbo.habbohotel.messenger.MessengerCategory; import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.messages.ServerMessage; import com.eu.habbo.messages.outgoing.MessageComposer; import com.eu.habbo.messages.outgoing.Outgoing; +import java.util.List; + public class MessengerInitComposer extends MessageComposer { private final Habbo habbo; @@ -15,6 +19,7 @@ public class MessengerInitComposer extends MessageComposer { @Override protected ServerMessage composeInternal() { + this.response.init(Outgoing.MessengerInitComposer); if (this.habbo.hasPermission("acc_infinite_friends")) { this.response.appendInt(Integer.MAX_VALUE); @@ -25,9 +30,19 @@ public class MessengerInitComposer extends MessageComposer { this.response.appendInt(1337); this.response.appendInt(Messenger.MAXIMUM_FRIENDS_HC); } + if (!this.habbo.getHabboInfo().getMessengerCategories().isEmpty()) { - //this.response.appendInt(1000); - this.response.appendInt(0); + List messengerCategories = this.habbo.getHabboInfo().getMessengerCategories(); + this.response.appendInt(messengerCategories.size()); + + for (MessengerCategory mc : messengerCategories) { + this.response.appendInt(mc.getId()); + this.response.appendString(mc.getName()); + } + } else { + this.response.appendInt(0); + } return this.response; } } + diff --git a/src/main/java/com/eu/habbo/messages/outgoing/friends/UpdateFriendComposer.java b/src/main/java/com/eu/habbo/messages/outgoing/friends/UpdateFriendComposer.java index 907fa9a0..1be0b1ac 100644 --- a/src/main/java/com/eu/habbo/messages/outgoing/friends/UpdateFriendComposer.java +++ b/src/main/java/com/eu/habbo/messages/outgoing/friends/UpdateFriendComposer.java @@ -1,6 +1,11 @@ package com.eu.habbo.messages.outgoing.friends; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + import com.eu.habbo.habbohotel.messenger.MessengerBuddy; +import com.eu.habbo.habbohotel.messenger.MessengerCategory; import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.HabboGender; import com.eu.habbo.messages.ServerMessage; @@ -8,60 +13,65 @@ import com.eu.habbo.messages.outgoing.MessageComposer; import com.eu.habbo.messages.outgoing.Outgoing; public class UpdateFriendComposer extends MessageComposer { - private MessengerBuddy buddy; + private Collection buddies; private Habbo habbo; + private int action; - public UpdateFriendComposer(MessengerBuddy buddy) { - this.buddy = buddy; + public UpdateFriendComposer(Habbo habbo, MessengerBuddy buddy, Integer action) { + this.habbo = habbo; + this.buddies = Collections.singletonList(buddy); + this.action = action; } - - public UpdateFriendComposer(Habbo habbo) { + public UpdateFriendComposer(Habbo habbo, Collection buddies, Integer action) { this.habbo = habbo; + this.buddies = buddies; + this.action = action; } @Override protected ServerMessage composeInternal() { this.response.init(Outgoing.UpdateFriendComposer); - if (this.buddy != null) { - this.response.appendInt(0); - this.response.appendInt(1); - this.response.appendInt(0); - this.response.appendInt(this.buddy.getId()); - this.response.appendString(this.buddy.getUsername()); - this.response.appendInt(this.buddy.getGender().equals(HabboGender.M) ? 0 : 1); - this.response.appendBoolean(this.buddy.getOnline() == 1); - this.response.appendBoolean(this.buddy.inRoom()); //In room - this.response.appendString(this.buddy.getLook()); - this.response.appendInt(0); - this.response.appendString(this.buddy.getMotto()); - this.response.appendString(""); - this.response.appendString(""); - this.response.appendBoolean(false); - this.response.appendBoolean(false); - this.response.appendBoolean(false); - this.response.appendShort(this.buddy.getRelation()); + if (this.habbo != null && !this.habbo.getHabboInfo().getMessengerCategories().isEmpty()) { + + List messengerCategories = this.habbo.getHabboInfo().getMessengerCategories(); + this.response.appendInt(messengerCategories.size()); + + for (MessengerCategory mc : messengerCategories) { + this.response.appendInt(mc.getId()); + this.response.appendString(mc.getName()); + } } else { this.response.appendInt(0); - this.response.appendInt(1); - this.response.appendInt(0); - this.response.appendInt(-1); - this.response.appendString("Staff Chat"); - this.response.appendInt(0); - this.response.appendBoolean(true); - this.response.appendBoolean(false); //In room - this.response.appendString(this.habbo.getHabboInfo().getLook()); - this.response.appendInt(0); - this.response.appendString(""); - this.response.appendString(""); - this.response.appendString(""); - this.response.appendBoolean(false); - this.response.appendBoolean(false); - this.response.appendBoolean(false); - this.response.appendShort(0); } + + this.response.appendInt(buddies.size()); // totalbuddies + + for(MessengerBuddy buddy : buddies){ + + if (buddy != null) { + this.response.appendInt(this.action); // -1 = removed friendId / 0 = updated friend / 1 = added friend + this.response.appendInt(buddy.getId()); + if (this.action == -1) { + continue; + } + this.response.appendString(buddy.getUsername()); + this.response.appendInt(buddy.getGender().equals(HabboGender.M) ? 0 : 1); + this.response.appendBoolean(buddy.getOnline() == 1); + this.response.appendBoolean(buddy.inRoom()); //In room + this.response.appendString(buddy.getLook()); + this.response.appendInt(buddy.getCategoryId()); + this.response.appendString(buddy.getMotto()); + this.response.appendString(""); //Last seen as DATETIMESTRING + this.response.appendString(""); //Realname or Facebookame as String + this.response.appendBoolean(false); //Offline messaging. + this.response.appendBoolean(false); + this.response.appendBoolean(false); + this.response.appendShort(buddy.getRelation()); + } + } return this.response; } }