From be3e7122932aa034a93289c346512a0c50c31ac2 Mon Sep 17 00:00:00 2001 From: Dominic Bridge Date: Fri, 8 Dec 2023 16:57:53 +0000 Subject: [PATCH 1/2] room unit bot manager --- .../com/eu/habbo/habbohotel/bots/Bot.java | 2 +- .../eu/habbo/habbohotel/bots/BotManager.java | 2 +- .../catalog/layouts/RoomBundleLayout.java | 4 +- .../habbohotel/commands/list/BotsCommand.java | 4 +- .../interactions/InteractionEffectTile.java | 2 +- .../WiredConditionFurniHaveRoom.java | 2 +- .../WiredConditionNotFurniHaveRoom.java | 2 +- .../wired/effects/WiredEffectBotClothes.java | 2 +- .../effects/WiredEffectBotFollowRoom.java | 2 +- .../effects/WiredEffectBotGiveHandItem.java | 2 +- .../wired/effects/WiredEffectBotTalk.java | 2 +- .../effects/WiredEffectBotTalkToRoom.java | 2 +- .../wired/effects/WiredEffectBotTeleport.java | 4 +- .../effects/WiredEffectBotWalkToFurni.java | 2 +- .../triggers/WiredTriggerBotReachedFurni.java | 2 +- .../triggers/WiredTriggerBotReachedRoom.java | 2 +- .../com/eu/habbo/habbohotel/rooms/Room.java | 14 +- .../eu/habbo/habbohotel/rooms/RoomLayout.java | 2 +- .../habbo/habbohotel/rooms/RoomManager.java | 8 +- .../habbohotel/rooms/RoomUnitManager.java | 248 +-------------- .../habbohotel/rooms/bots/RoomBotManager.java | 288 ++++++++++++++++++ .../types => bots/entities}/RoomBot.java | 3 +- .../rooms/entities/units/RoomUnit.java | 2 +- .../units/types/RoomUnitSubManager.java | 14 + .../rooms/items/RoomItemManager.java | 10 +- .../rooms/items/entities/RoomItem.java | 6 +- .../com/eu/habbo/habbohotel/users/Habbo.java | 2 +- .../incoming/navigator/DeleteRoomEvent.java | 2 +- .../incoming/rooms/bots/CommandBotEvent.java | 2 +- .../GetBotCommandConfigurationDataEvent.java | 2 +- .../incoming/rooms/bots/PlaceBotEvent.java | 2 +- .../rooms/bots/RemoveBotFromFlatEvent.java | 4 +- .../threading/runnables/RoomUnitTeleport.java | 2 +- 33 files changed, 368 insertions(+), 281 deletions(-) create mode 100644 src/main/java/com/eu/habbo/habbohotel/rooms/bots/RoomBotManager.java rename src/main/java/com/eu/habbo/habbohotel/rooms/{entities/units/types => bots/entities}/RoomBot.java (97%) create mode 100644 src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomUnitSubManager.java diff --git a/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java b/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java index e446892b..a99f0d6d 100644 --- a/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java +++ b/src/main/java/com/eu/habbo/habbohotel/bots/Bot.java @@ -5,7 +5,7 @@ import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.habbohotel.rooms.RoomChatMessage; import com.eu.habbo.habbohotel.rooms.constants.RoomChatMessageBubbles; import com.eu.habbo.habbohotel.rooms.constants.RoomUserAction; -import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomBot; +import com.eu.habbo.habbohotel.rooms.bots.entities.RoomBot; import com.eu.habbo.habbohotel.units.type.Avatar; import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.HabboGender; diff --git a/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java b/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java index f14b26f6..27033576 100644 --- a/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/bots/BotManager.java @@ -107,7 +107,7 @@ public class BotManager { } bot.onPickUp(habbo, room); - room.getRoomUnitManager().removeBot(bot); + room.getRoomUnitManager().getRoomBotManager().removeBot(bot); bot.setFollowingHabboId(0); bot.setOwnerId(botOwnerInfo.getId()); bot.setOwnerName(botOwnerInfo.getUsername()); diff --git a/src/main/java/com/eu/habbo/habbohotel/catalog/layouts/RoomBundleLayout.java b/src/main/java/com/eu/habbo/habbohotel/catalog/layouts/RoomBundleLayout.java index 60baa9f8..3e3b75f2 100644 --- a/src/main/java/com/eu/habbo/habbohotel/catalog/layouts/RoomBundleLayout.java +++ b/src/main/java/com/eu/habbo/habbohotel/catalog/layouts/RoomBundleLayout.java @@ -190,10 +190,10 @@ public class RoomBundleLayout extends SingleBundle { if (Emulator.getConfig().getBoolean("bundle.bots.enabled")) { try (PreparedStatement statement = connection.prepareStatement("INSERT INTO bots (owner_id, room_id, name, motto, figure, gender, x, y, z, chat_lines, chat_auto, chat_random, chat_delay, dance, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) { - synchronized (this.room.getRoomUnitManager().getCurrentBots()) { + synchronized (this.room.getRoomUnitManager().getRoomBotManager().getCurrentBots()) { statement.setInt(1, userId); statement.setInt(2, roomId); - for (Bot bot : this.room.getRoomUnitManager().getCurrentBots().values()) { + for (Bot bot : this.room.getRoomUnitManager().getRoomBotManager().getCurrentBots().values()) { statement.setString(3, bot.getName()); statement.setString(4, bot.getMotto()); statement.setString(5, bot.getFigure()); diff --git a/src/main/java/com/eu/habbo/habbohotel/commands/list/BotsCommand.java b/src/main/java/com/eu/habbo/habbohotel/commands/list/BotsCommand.java index 494aab57..a747ec9e 100644 --- a/src/main/java/com/eu/habbo/habbohotel/commands/list/BotsCommand.java +++ b/src/main/java/com/eu/habbo/habbohotel/commands/list/BotsCommand.java @@ -15,9 +15,9 @@ public class BotsCommand extends Command { if (gameClient.getHabbo().getRoomUnit().getRoom() == null || !gameClient.getHabbo().getRoomUnit().getRoom().getRoomRightsManager().hasRights(gameClient.getHabbo())) return false; - StringBuilder data = new StringBuilder(getTextsValue("total") + ": " + gameClient.getHabbo().getRoomUnit().getRoom().getRoomUnitManager().getCurrentBots().values().size()); + StringBuilder data = new StringBuilder(getTextsValue("total") + ": " + gameClient.getHabbo().getRoomUnit().getRoom().getRoomUnitManager().getRoomBotManager().getCurrentBots().values().size()); - for (Bot bot : gameClient.getHabbo().getRoomUnit().getRoom().getRoomUnitManager().getCurrentBots().values()) { + for (Bot bot : gameClient.getHabbo().getRoomUnit().getRoom().getRoomUnitManager().getRoomBotManager().getCurrentBots().values()) { data.append("\r"); data.append(""); data.append(Emulator.getTexts().getValue("generic.bot.name")); diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionEffectTile.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionEffectTile.java index a89643ef..64105177 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionEffectTile.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionEffectTile.java @@ -60,7 +60,7 @@ public class InteractionEffectTile extends InteractionPressurePlate { this.giveEffect(room, roomUnit, habbo.getHabboInfo().getGender()); } } else if (roomUnit.getRoomUnitType() == RoomUnitType.BOT) { - Bot bot = room.getRoomUnitManager().getRoomBotById(roomUnit.getVirtualId()); + Bot bot = room.getRoomUnitManager().getRoomBotManager().getRoomBotById(roomUnit.getVirtualId()); if (bot != null) { this.giveEffect(room, roomUnit, bot.getGender()); diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionFurniHaveRoom.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionFurniHaveRoom.java index 498f74ff..89fbe17d 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionFurniHaveRoom.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionFurniHaveRoom.java @@ -32,7 +32,7 @@ public class WiredConditionFurniHaveRoom extends InteractionWiredCondition { } Collection habbos = room.getRoomUnitManager().getCurrentHabbos().values(); - Collection bots = room.getRoomUnitManager().getCurrentBots().values(); + Collection bots = room.getRoomUnitManager().getRoomBotManager().getCurrentBots().values(); Collection pets = room.getRoomUnitManager().getCurrentPets().values(); return this.getWiredSettings().getItems(room).stream().allMatch(item -> { diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionNotFurniHaveRoom.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionNotFurniHaveRoom.java index bd60b14a..be7baf1e 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionNotFurniHaveRoom.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/conditions/WiredConditionNotFurniHaveRoom.java @@ -32,7 +32,7 @@ public class WiredConditionNotFurniHaveRoom extends InteractionWiredCondition { } Collection habbos = room.getRoomUnitManager().getCurrentHabbos().values(); - Collection bots = room.getRoomUnitManager().getCurrentBots().values(); + Collection bots = room.getRoomUnitManager().getRoomBotManager().getCurrentBots().values(); Collection pets = room.getRoomUnitManager().getCurrentPets().values(); return this.getWiredSettings().getItems(room).stream().noneMatch(item -> { diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotClothes.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotClothes.java index cef31e8d..086dfc6c 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotClothes.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotClothes.java @@ -29,7 +29,7 @@ public class WiredEffectBotClothes extends InteractionWiredEffect { String botName = stringParams[0].substring(0, Math.min(stringParams[0].length(), Emulator.getConfig().getInt("hotel.wired.message.max_length", 100))); String botLook = stringParams[1]; - List bots = room.getRoomUnitManager().getBotsByName(botName); + List bots = room.getRoomUnitManager().getRoomBotManager().getBotsByName(botName); if(bots.size() == 0) { return false; diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotFollowRoom.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotFollowRoom.java index f7f29026..31004acc 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotFollowRoom.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotFollowRoom.java @@ -29,7 +29,7 @@ public class WiredEffectBotFollowRoom extends InteractionWiredEffect { @Override public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) { Habbo habbo = room.getRoomUnitManager().getHabboByRoomUnit(roomUnit); - List bots = room.getRoomUnitManager().getBotsByName(this.getWiredSettings().getStringParam()); + List bots = room.getRoomUnitManager().getRoomBotManager().getBotsByName(this.getWiredSettings().getStringParam()); if (habbo != null && bots.size() == 1) { Bot bot = bots.get(0); diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotGiveHandItem.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotGiveHandItem.java index 9fd302a5..67efc8de 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotGiveHandItem.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotGiveHandItem.java @@ -38,7 +38,7 @@ public class WiredEffectBotGiveHandItem extends InteractionWiredEffect { } Habbo habbo = room.getRoomUnitManager().getHabboByRoomUnit(roomAvatar); - List bots = room.getRoomUnitManager().getBotsByName(this.getWiredSettings().getStringParam()); + List bots = room.getRoomUnitManager().getRoomBotManager().getBotsByName(this.getWiredSettings().getStringParam()); int itemId = this.getWiredSettings().getIntegerParams().get(PARAM_ITEM_ID); if (habbo != null && bots.size() == 1) { diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTalk.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTalk.java index 8377b39c..7a256a00 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTalk.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTalk.java @@ -48,7 +48,7 @@ public class WiredEffectBotTalk extends InteractionWiredEffect { .replace(Emulator.getTexts().getValue("wired.variable.user_count", "%user_count%"), room.getRoomUnitManager().getRoomHabbosCount() + ""); } - List bots = room.getRoomUnitManager().getBotsByName(botName); + List bots = room.getRoomUnitManager().getRoomBotManager().getBotsByName(botName); if (bots.size() == 1) { Bot bot = bots.get(0); diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTalkToRoom.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTalkToRoom.java index a74dd952..17e64a70 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTalkToRoom.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTalkToRoom.java @@ -47,7 +47,7 @@ public class WiredEffectBotTalkToRoom extends InteractionWiredEffect { .replace(Emulator.getTexts().getValue("wired.variable.roomname", "%roomname%"), room.getRoomInfo().getName()) .replace(Emulator.getTexts().getValue("wired.variable.user_count", "%user_count%"), room.getRoomUnitManager().getRoomHabbosCount() + ""); - List bots = room.getRoomUnitManager().getBotsByName(botName); + List bots = room.getRoomUnitManager().getRoomBotManager().getBotsByName(botName); if (bots.size() != 1) { return false; diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTeleport.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTeleport.java index 609d9600..22c4ef17 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTeleport.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotTeleport.java @@ -9,7 +9,7 @@ import com.eu.habbo.habbohotel.rooms.RoomTile; import com.eu.habbo.habbohotel.rooms.constants.RoomTileState; import com.eu.habbo.habbohotel.rooms.items.entities.RoomItem; import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnit; -import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomBot; +import com.eu.habbo.habbohotel.rooms.bots.entities.RoomBot; import com.eu.habbo.habbohotel.users.HabboInfo; import com.eu.habbo.habbohotel.wired.WiredEffectType; import com.eu.habbo.habbohotel.wired.WiredHandler; @@ -38,7 +38,7 @@ public class WiredEffectBotTeleport extends InteractionWiredEffect { } String botName = this.getWiredSettings().getStringParam(); - List bots = room.getRoomUnitManager().getBotsByName(botName); + List bots = room.getRoomUnitManager().getRoomBotManager().getBotsByName(botName); if (bots.size() == 0) { return false; diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotWalkToFurni.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotWalkToFurni.java index 28347e23..316ee22f 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotWalkToFurni.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectBotWalkToFurni.java @@ -31,7 +31,7 @@ public class WiredEffectBotWalkToFurni extends InteractionWiredEffect { } String botName = this.getWiredSettings().getStringParam(); - List bots = room.getRoomUnitManager().getBotsByName(botName); + List bots = room.getRoomUnitManager().getRoomBotManager().getBotsByName(botName); if (bots.size() == 0) { return false; diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerBotReachedFurni.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerBotReachedFurni.java index 34617c12..a2d8b7b4 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerBotReachedFurni.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerBotReachedFurni.java @@ -29,7 +29,7 @@ public class WiredTriggerBotReachedFurni extends InteractionWiredTrigger { } if (stuff[0] instanceof RoomItem) { - return this.getWiredSettings().getItems(room).contains(stuff[0]) && room.getRoomUnitManager().getBotsByName(this.getWiredSettings().getStringParam()).stream().anyMatch(bot -> bot.getRoomUnit() == roomUnit); + return this.getWiredSettings().getItems(room).contains(stuff[0]) && room.getRoomUnitManager().getRoomBotManager().getBotsByName(this.getWiredSettings().getStringParam()).stream().anyMatch(bot -> bot.getRoomUnit() == roomUnit); } return false; diff --git a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerBotReachedRoom.java b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerBotReachedRoom.java index 93758c00..99f120e0 100644 --- a/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerBotReachedRoom.java +++ b/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerBotReachedRoom.java @@ -21,7 +21,7 @@ public class WiredTriggerBotReachedRoom extends InteractionWiredTrigger { @Override public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) { - return room.getRoomUnitManager().getBotsByName(this.getWiredSettings().getStringParam()).stream().anyMatch(bot -> bot.getRoomUnit() == roomUnit); + return room.getRoomUnitManager().getRoomBotManager().getBotsByName(this.getWiredSettings().getStringParam()).stream().anyMatch(bot -> bot.getRoomUnit() == roomUnit); } @Override diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java index 3d0ce1dd..4616cd61 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -516,8 +516,8 @@ public class Room implements Comparable, ISerialize, Runnable { } } - if (!this.roomUnitManager.getCurrentBots().isEmpty()) { - Iterator botIterator = this.roomUnitManager.getCurrentBots().values().iterator(); + if (!this.roomUnitManager.getRoomBotManager().getCurrentBots().isEmpty()) { + Iterator botIterator = this.roomUnitManager.getRoomBotManager().getCurrentBots().values().iterator(); while(botIterator.hasNext()) { try { @@ -1196,7 +1196,7 @@ public class Room implements Comparable, ISerialize, Runnable { for (short x = item.getCurrentPosition().getX(); x < item.getCurrentPosition().getX() + item.getBaseItem().getLength(); x++) { for (short y = item.getCurrentPosition().getY(); y < item.getCurrentPosition().getY() + item.getBaseItem().getWidth(); y++) { RoomTile tile = this.layout.getTile(x, y); - bots.addAll(this.roomUnitManager.getBotsAt(tile)); + bots.addAll(this.roomUnitManager.getRoomBotManager().getBotsAt(tile)); } } @@ -1260,11 +1260,11 @@ public class Room implements Comparable, ISerialize, Runnable { } public void habboEntered(Habbo habbo) { - synchronized (this.roomUnitManager.getCurrentBots()) { + synchronized (this.roomUnitManager.getRoomBotManager().getCurrentBots()) { if (habbo.getHabboInfo().getId() != this.roomInfo.getOwnerInfo().getId()) return; - Iterator botIterator = this.roomUnitManager.getCurrentBots().values().iterator(); + Iterator botIterator = this.roomUnitManager.getRoomBotManager().getCurrentBots().values().iterator(); while (botIterator.hasNext()) { try { @@ -1494,8 +1494,8 @@ public class Room implements Comparable, ISerialize, Runnable { } if (chatType == RoomChatType.TALK || chatType == RoomChatType.SHOUT) { - synchronized (this.roomUnitManager.getCurrentBots()) { - Iterator botIterator = this.roomUnitManager.getCurrentBots().values().iterator(); + synchronized (this.roomUnitManager.getRoomBotManager().getCurrentBots()) { + Iterator botIterator = this.roomUnitManager.getRoomBotManager().getCurrentBots().values().iterator(); while (botIterator.hasNext()) { try { diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomLayout.java b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomLayout.java index aa4d29a1..8b9eb13c 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomLayout.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomLayout.java @@ -4,7 +4,7 @@ import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.rooms.constants.RoomTileState; import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnit; import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomAvatar; -import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomBot; +import com.eu.habbo.habbohotel.rooms.bots.entities.RoomBot; import gnu.trove.set.hash.THashSet; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomManager.java index 47cbcd2f..c86e220b 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomManager.java @@ -711,14 +711,14 @@ public class RoomManager { } - habbo.getClient().sendResponse(new RoomUsersComposer(room.getRoomUnitManager().getCurrentBots().values())); + habbo.getClient().sendResponse(new RoomUsersComposer(room.getRoomUnitManager().getRoomBotManager().getCurrentBots().values())); - if (!room.getRoomUnitManager().getCurrentBots().isEmpty()) { - room.getRoomUnitManager().getCurrentBots().values().stream() + if (!room.getRoomUnitManager().getRoomBotManager().getCurrentBots().isEmpty()) { + room.getRoomUnitManager().getRoomBotManager().getCurrentBots().values().stream() .filter(b -> !b.getRoomUnit().getDanceType().equals(DanceType.NONE)) .forEach(b -> habbo.getClient().sendResponse(new DanceMessageComposer(b.getRoomUnit()))); - room.getRoomUnitManager().getCurrentBots().values() + room.getRoomUnitManager().getRoomBotManager().getCurrentBots().values() .forEach(b -> habbo.getClient().sendResponse(new UserUpdateComposer(b.getRoomUnit(), b.getRoomUnit().getCurrentZ()))); } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java index 2c4ae256..ba776da7 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java @@ -2,33 +2,22 @@ package com.eu.habbo.habbohotel.rooms; import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.bots.Bot; -import com.eu.habbo.habbohotel.bots.BotManager; import com.eu.habbo.habbohotel.items.Item; -import com.eu.habbo.habbohotel.permissions.Permission; import com.eu.habbo.habbohotel.pets.Pet; import com.eu.habbo.habbohotel.pets.PetManager; +import com.eu.habbo.habbohotel.rooms.bots.RoomBotManager; import com.eu.habbo.habbohotel.rooms.constants.RoomTileState; import com.eu.habbo.habbohotel.rooms.constants.RoomUnitStatus; import com.eu.habbo.habbohotel.rooms.entities.RoomRotation; -import com.eu.habbo.habbohotel.rooms.items.entities.RoomItem; import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnit; import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnitType; -import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomBot; import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomHabbo; +import com.eu.habbo.habbohotel.rooms.items.entities.RoomItem; import com.eu.habbo.habbohotel.units.Unit; -import com.eu.habbo.habbohotel.users.DanceType; import com.eu.habbo.habbohotel.users.Habbo; -import com.eu.habbo.habbohotel.users.HabboInfo; -import com.eu.habbo.messages.outgoing.generic.alerts.BotErrorComposer; -import com.eu.habbo.messages.outgoing.inventory.BotAddedToInventoryComposer; -import com.eu.habbo.messages.outgoing.inventory.BotRemovedFromInventoryComposer; import com.eu.habbo.messages.outgoing.inventory.PetAddedToInventoryComposer; import com.eu.habbo.messages.outgoing.rooms.pets.RoomPetComposer; -import com.eu.habbo.messages.outgoing.rooms.users.RoomUsersComposer; import com.eu.habbo.messages.outgoing.rooms.users.UserRemoveMessageComposer; -import com.eu.habbo.plugin.Event; -import com.eu.habbo.plugin.events.bots.BotPickUpEvent; -import com.eu.habbo.plugin.events.bots.BotPlacedEvent; import gnu.trove.set.hash.THashSet; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -50,68 +39,27 @@ public class RoomUnitManager { private final Room room; private final ConcurrentHashMap currentRoomUnits; private final ConcurrentHashMap currentHabbos; - private final ConcurrentHashMap currentBots; private final ConcurrentHashMap currentPets; private volatile int roomUnitCounter; public final Object roomUnitLock; + private final RoomBotManager roomBotManager; public RoomUnitManager(Room room) { this.room = room; this.currentRoomUnits = new ConcurrentHashMap<>(); this.currentHabbos = new ConcurrentHashMap<>(); - this.currentBots = new ConcurrentHashMap<>(); this.currentPets = new ConcurrentHashMap<>(); this.roomUnitCounter = 0; this.roomUnitLock = new Object(); + roomBotManager = new RoomBotManager(this); } public synchronized void load(Connection connection) { - this.loadBots(connection); + roomBotManager.loadBots(connection); this.loadPets(connection); } - private synchronized void loadBots(Connection connection) { - this.currentBots.clear(); - try (PreparedStatement statement = connection.prepareStatement("SELECT users.username AS owner_name, bots.* FROM bots INNER JOIN users ON bots.owner_id = users.id WHERE room_id = ?")) { - statement.setInt(1, this.room.getRoomInfo().getId()); - try (ResultSet set = statement.executeQuery()) { - while (set.next()) { - Bot bot = Emulator.getGameEnvironment().getBotManager().loadBot(set); - - if (bot != null) { - bot.getRoomUnit().setRoom(this.room); - - RoomTile spawnTile = this.room.getLayout().getTile((short) set.getInt("x"), (short) set.getInt("y")); - - if(spawnTile == null) { - bot.getRoomUnit().setCanWalk(false); - } else { - if(spawnTile.getState().equals(RoomTileState.INVALID)) { - bot.getRoomUnit().setCanWalk(false); - } - - bot.getRoomUnit().setCurrentPosition(spawnTile); - bot.getRoomUnit().setCurrentZ(set.getDouble("z")); - bot.getRoomUnit().setRotation(RoomRotation.values()[set.getInt("rot")]); - bot.getRoomUnit().setSpawnTile(spawnTile); - bot.getRoomUnit().setSpawnHeight(spawnTile.getState().equals(RoomTileState.INVALID) ? 0 : spawnTile.getStackHeight()); - } - - bot.getRoomUnit().setDanceType(DanceType.values()[set.getInt("dance")]); - - bot.getRoomUnit().giveEffect(set.getInt("effect"), Integer.MAX_VALUE, false); - - this.addRoomUnit(bot); - } - } - } - } catch (SQLException e) { - log.error(CAUGHT_SQL_EXCEPTION, e); - } catch (Exception e) { - log.error("Caught Exception", e); - } - } private synchronized void loadPets(Connection connection) { this.currentPets.clear(); @@ -160,7 +108,7 @@ public class RoomUnitManager { unit.getRoomUnit().getRoom().updateDatabaseUserCount(); } case BOT -> { - this.currentBots.put(((Bot) unit).getId(), (Bot) unit); + roomBotManager.addBot((Bot)unit); } case PET -> { this.currentPets.put(((Pet) unit).getId(), (Pet) unit); @@ -190,7 +138,7 @@ public class RoomUnitManager { } public List getAvatarsAt(RoomTile tile) { - return Stream.concat(this.getHabbosAt(tile).stream(), this.getBotsAt(tile).stream()).map(Unit::getRoomUnit).collect(Collectors.toList()); + return Stream.concat(this.getHabbosAt(tile).stream(), roomBotManager.getBotsAt(tile).stream()).map(Unit::getRoomUnit).collect(Collectors.toList()); } public int getRoomHabbosCount() { @@ -256,148 +204,9 @@ public class RoomUnitManager { } } - public Bot getRoomBotById(int botId) { - return this.currentBots.get(botId); - } - public List getBotsByName(String name) { - synchronized (this.currentBots) { - return currentBots.values().stream().filter(bot -> bot.getName().equalsIgnoreCase(name)).toList(); - } - } - public Bot getBotByRoomUnit(RoomUnit roomUnit) { - return this.currentBots.values().stream().filter(bot -> bot.getRoomUnit() == roomUnit).findFirst().orElse(null); - } - public boolean hasBotsAt(RoomTile tile) { - return this.currentBots.values().stream().anyMatch(bot -> bot.getRoomUnit().getCurrentPosition().equals(tile)); - } - - public Collection getBotsAt(RoomTile tile) { - return this.currentBots.values().stream().filter(bot -> bot.getRoomUnit().getCurrentPosition().equals(tile)).collect(Collectors.toSet()); - } - - public void placeBot(Bot bot, Habbo botOwner, int x, int y) { - synchronized (this.currentBots) { - RoomTile spawnTile = room.getLayout().getTile((short) x, (short) y); - - if(spawnTile == null || (!spawnTile.isWalkable() && !this.room.canSitOrLayAt(spawnTile)) || this.areRoomUnitsAt(spawnTile)) { - botOwner.getClient().sendResponse(new BotErrorComposer(BotErrorComposer.ROOM_ERROR_BOTS_SELECTED_TILE_NOT_FREE)); - return; - } - - if (Emulator.getPluginManager().isRegistered(BotPlacedEvent.class, false)) { - Event event = new BotPlacedEvent(bot, spawnTile, botOwner); - Emulator.getPluginManager().fireEvent(event); - - if (event.isCancelled()) { - return; - } - } - - if(this.currentBots.size() >= Room.MAXIMUM_BOTS && !botOwner.hasPermissionRight(Permission.ACC_UNLIMITED_BOTS)) { - botOwner.getClient().sendResponse(new BotErrorComposer(BotErrorComposer.ROOM_ERROR_MAX_BOTS)); - return; - } - - RoomBot roomBot = bot.getRoomUnit(); - - roomBot.setRoom(this.room); - roomBot.setCurrentPosition(spawnTile); - roomBot.setCurrentZ(spawnTile.getStackHeight()); - roomBot.setRotation(RoomRotation.SOUTH); - roomBot.setSpawnTile(spawnTile); - roomBot.setSpawnHeight(spawnTile.getState().equals(RoomTileState.INVALID) ? 0 : spawnTile.getStackHeight()); - - bot.setSqlUpdateNeeded(true); - Emulator.getThreading().run(bot); - - this.addRoomUnit(bot); - - this.room.sendComposer(new RoomUsersComposer(bot).compose()); - - roomBot.instantUpdate(); - - botOwner.getInventory().getBotsComponent().removeBot(bot); - botOwner.getClient().sendResponse(new BotRemovedFromInventoryComposer(bot)); - bot.onPlace(botOwner, room); - } - } - - public void updateBotsAt(RoomTile tile) { - Collection bots = this.getBotsAt(tile); - - if(bots == null || bots.isEmpty()) { - return; - } - - RoomItem item = this.room.getRoomItemManager().getTopItemAt(tile.getX(), tile.getY()); - - bots.forEach(bot -> { - double z = bot.getRoomUnit().getCurrentPosition().getStackHeight(); - - if (bot.getRoomUnit().hasStatus(RoomUnitStatus.SIT) && ((item == null && !bot.getRoomUnit().isCmdSitEnabled()) || (item != null && !item.getBaseItem().allowSit()))) { - bot.getRoomUnit().removeStatus(RoomUnitStatus.SIT); - } - - if (bot.getRoomUnit().hasStatus(RoomUnitStatus.LAY) && ((item == null && !bot.getRoomUnit().isCmdLayEnabled()) || (item != null && !item.getBaseItem().allowLay()))) { - bot.getRoomUnit().removeStatus(RoomUnitStatus.LAY); - } - - if (item != null && (item.getBaseItem().allowSit() || item.getBaseItem().allowLay())) { - if(item.getBaseItem().allowSit()) { - bot.getRoomUnit().addStatus(RoomUnitStatus.SIT, String.valueOf(Item.getCurrentHeight(item))); - } else if(item.getBaseItem().allowLay()) { - bot.getRoomUnit().addStatus(RoomUnitStatus.LAY, String.valueOf(Item.getCurrentHeight(item))); - } - - bot.getRoomUnit().setCurrentZ(item.getCurrentZ()); - bot.getRoomUnit().setRotation(RoomRotation.fromValue(item.getRotation())); - } else { - bot.getRoomUnit().setCurrentZ(z); - } - }); - } - - public void pickUpBot(Bot bot, Habbo picker) { - HabboInfo botOwnerInfo = picker == null ? bot.getOwnerInfo() : picker.getHabboInfo(); - - BotPickUpEvent pickedUpEvent = new BotPickUpEvent(bot, picker); - Emulator.getPluginManager().fireEvent(pickedUpEvent); - - if (pickedUpEvent.isCancelled()) - return; - - if (picker == null || (bot.getOwnerInfo().getId() == picker.getHabboInfo().getId() || picker.hasPermissionRight(Permission.ACC_ANYROOMOWNER))) { - if (picker != null && !picker.hasPermissionRight(Permission.ACC_UNLIMITED_BOTS) && picker.getInventory().getBotsComponent().getBots().size() >= BotManager.MAXIMUM_BOT_INVENTORY_SIZE) { - picker.alert(Emulator.getTexts().getValue("error.bots.max.inventory").replace("%amount%", String.valueOf(BotManager.MAXIMUM_BOT_INVENTORY_SIZE))); - return; - } - - bot.onPickUp(picker, this.room); - - bot.setFollowingHabboId(0); - - //@DEPRECATED - bot.setOwnerId(botOwnerInfo.getId()); - bot.setOwnerName(botOwnerInfo.getUsername()); - - bot.setOwnerInfo(botOwnerInfo); - - this.removeBot(bot); - - bot.setSqlUpdateNeeded(true); - Emulator.getThreading().run(bot); - - Habbo receiver = picker == null ? Emulator.getGameEnvironment().getHabboManager().getHabbo(botOwnerInfo.getId()) : picker; - - if (receiver != null) { - receiver.getInventory().getBotsComponent().addBot(bot); - receiver.getClient().sendResponse(new BotAddedToInventoryComposer(bot)); - } - } - } public void placePet(Pet pet, Room room, short x, short y, double z) { synchronized (this.currentPets) { @@ -476,7 +285,7 @@ public class RoomUnitManager { synchronized (this.roomUnitLock) { this.currentHabbos.remove(habbo.getHabboInfo().getId()); - this.currentRoomUnits.remove(roomHabbo.getVirtualId()); + removeUnit(roomHabbo.getVirtualId()); } roomHabbo.getRoom().sendComposer(new UserRemoveMessageComposer(roomHabbo).compose()); @@ -510,25 +319,10 @@ public class RoomUnitManager { roomHabbo.clear(); } - public void removeBot(Bot bot) { - if (this.currentBots.containsKey(bot.getId())) { - //TODO gotta do a method to removeUnit and clear tile - if (bot.getRoomUnit().getCurrentPosition() != null) { - bot.getRoomUnit().getCurrentPosition().removeUnit(bot.getRoomUnit()); - } - - this.currentBots.remove(bot.getId()); - this.currentRoomUnits.remove(bot.getRoomUnit().getVirtualId()); - - bot.getRoomUnit().setRoom(null); - - this.room.sendComposer(new UserRemoveMessageComposer(bot.getRoomUnit()).compose()); - } - } public Pet removePet(int petId) { Pet pet = this.currentPets.get(petId); - this.currentRoomUnits.remove(pet.getRoomUnit().getVirtualId()); + removeUnit(pet.getRoomUnit().getVirtualId()); return this.currentPets.remove(petId); } @@ -566,7 +360,7 @@ public class RoomUnitManager { for (Pet pet : removedPets) { this.currentPets.remove(pet.getId()); - this.currentRoomUnits.remove(pet.getRoomUnit().getVirtualId()); + removeUnit(pet.getRoomUnit().getVirtualId()); } } @@ -574,7 +368,7 @@ public class RoomUnitManager { synchronized (this.roomUnitLock) { this.currentRoomUnits.clear(); this.currentHabbos.clear(); - this.currentBots.clear(); + this.roomBotManager.clear(); this.currentPets.clear(); this.roomUnitCounter = 0; } @@ -587,21 +381,7 @@ public class RoomUnitManager { this.currentHabbos.clear(); - Iterator botIterator = this.currentBots.values().iterator(); - - while(botIterator.hasNext()) { - try { - Bot bot = botIterator.next(); - bot.setSqlUpdateNeeded(true); - Emulator.getThreading().run(bot); - } catch (NoSuchElementException e) { - log.error("Caught Exception", e); - break; - } - } - - this.currentBots.clear(); - + roomBotManager.dispose(); Iterator petIterator = this.currentPets.values().iterator(); while(petIterator.hasNext()) { @@ -618,4 +398,8 @@ public class RoomUnitManager { this.currentPets.clear(); this.currentRoomUnits.clear(); } + + public void removeUnit(int virtualId) { + this.currentRoomUnits.remove(virtualId); + } } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/bots/RoomBotManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/bots/RoomBotManager.java new file mode 100644 index 00000000..24c99516 --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/bots/RoomBotManager.java @@ -0,0 +1,288 @@ +package com.eu.habbo.habbohotel.rooms.bots; + +import com.eu.habbo.Emulator; +import com.eu.habbo.habbohotel.bots.Bot; +import com.eu.habbo.habbohotel.bots.BotManager; +import com.eu.habbo.habbohotel.items.Item; +import com.eu.habbo.habbohotel.permissions.Permission; +import com.eu.habbo.habbohotel.rooms.Room; +import com.eu.habbo.habbohotel.rooms.RoomTile; +import com.eu.habbo.habbohotel.rooms.RoomUnitManager; +import com.eu.habbo.habbohotel.rooms.bots.entities.RoomBot; +import com.eu.habbo.habbohotel.rooms.constants.RoomTileState; +import com.eu.habbo.habbohotel.rooms.constants.RoomUnitStatus; +import com.eu.habbo.habbohotel.rooms.entities.RoomRotation; +import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnit; +import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomUnitSubManager; +import com.eu.habbo.habbohotel.rooms.items.entities.RoomItem; +import com.eu.habbo.habbohotel.users.DanceType; +import com.eu.habbo.habbohotel.users.Habbo; +import com.eu.habbo.habbohotel.users.HabboInfo; +import com.eu.habbo.messages.outgoing.generic.alerts.BotErrorComposer; +import com.eu.habbo.messages.outgoing.inventory.BotAddedToInventoryComposer; +import com.eu.habbo.messages.outgoing.inventory.BotRemovedFromInventoryComposer; +import com.eu.habbo.messages.outgoing.rooms.users.RoomUsersComposer; +import com.eu.habbo.messages.outgoing.rooms.users.UserRemoveMessageComposer; +import com.eu.habbo.plugin.Event; +import com.eu.habbo.plugin.events.bots.BotPickUpEvent; +import com.eu.habbo.plugin.events.bots.BotPlacedEvent; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +import static com.eu.habbo.database.DatabaseConstants.CAUGHT_SQL_EXCEPTION; + + +@Slf4j +public class RoomBotManager extends RoomUnitSubManager { + private final RoomUnitManager roomUnitManager; + + @Getter + private final ConcurrentHashMap currentBots; + + public RoomBotManager(RoomUnitManager roomUnitManager) { + super(roomUnitManager); + this.roomUnitManager = roomUnitManager; + currentBots = new ConcurrentHashMap<>(); + } + + public synchronized void loadBots(Connection connection) { + this.currentBots.clear(); + + try (PreparedStatement statement = connection.prepareStatement("SELECT users.username AS owner_name, bots.* FROM bots INNER JOIN users ON bots.owner_id = users.id WHERE room_id = ?")) { + statement.setInt(1, this.room.getRoomInfo().getId()); + try (ResultSet set = statement.executeQuery()) { + while (set.next()) { + Bot bot = Emulator.getGameEnvironment().getBotManager().loadBot(set); + + if (bot != null) { + bot.getRoomUnit().setRoom(this.room); + + RoomTile spawnTile = this.room.getLayout().getTile((short) set.getInt("x"), (short) set.getInt("y")); + + if (spawnTile == null) { + bot.getRoomUnit().setCanWalk(false); + } else { + if (spawnTile.getState().equals(RoomTileState.INVALID)) { + bot.getRoomUnit().setCanWalk(false); + } + + bot.getRoomUnit().setCurrentPosition(spawnTile); + bot.getRoomUnit().setCurrentZ(set.getDouble("z")); + bot.getRoomUnit().setRotation(RoomRotation.values()[set.getInt("rot")]); + bot.getRoomUnit().setSpawnTile(spawnTile); + bot.getRoomUnit().setSpawnHeight(spawnTile.getState().equals(RoomTileState.INVALID) ? 0 : spawnTile.getStackHeight()); + } + + bot.getRoomUnit().setDanceType(DanceType.values()[set.getInt("dance")]); + + bot.getRoomUnit().giveEffect(set.getInt("effect"), Integer.MAX_VALUE, false); + + this.addBot(bot); + } + } + } + } catch (SQLException e) { + log.error(CAUGHT_SQL_EXCEPTION, e); + } catch (Exception e) { + log.error("Caught Exception", e); + } + } + + public Bot getRoomBotById(int botId) { + return this.currentBots.get(botId); + } + + public List getBotsByName(String name) { + synchronized (this.currentBots) { + return currentBots.values().stream().filter(bot -> bot.getName().equalsIgnoreCase(name)).toList(); + } + } + + public Bot getBotByRoomUnit(RoomUnit roomUnit) { + return this.currentBots.values().stream().filter(bot -> bot.getRoomUnit() == roomUnit).findFirst().orElse(null); + } + + public boolean hasBotsAt(RoomTile tile) { + return this.currentBots.values().stream().anyMatch(bot -> bot.getRoomUnit().getCurrentPosition().equals(tile)); + } + + public Collection getBotsAt(RoomTile tile) { + return this.currentBots.values().stream().filter(bot -> bot.getRoomUnit().getCurrentPosition().equals(tile)).collect(Collectors.toSet()); + } + + public void placeBot(Bot bot, Habbo botOwner, int x, int y) { + synchronized (this.currentBots) { + RoomTile spawnTile = room.getLayout().getTile((short) x, (short) y); + + if (spawnTile == null || (!spawnTile.isWalkable() && !this.room.canSitOrLayAt(spawnTile)) || roomUnitManager.areRoomUnitsAt(spawnTile)) { + botOwner.getClient().sendResponse(new BotErrorComposer(BotErrorComposer.ROOM_ERROR_BOTS_SELECTED_TILE_NOT_FREE)); + return; + } + + if (Emulator.getPluginManager().isRegistered(BotPlacedEvent.class, false)) { + Event event = new BotPlacedEvent(bot, spawnTile, botOwner); + Emulator.getPluginManager().fireEvent(event); + + if (event.isCancelled()) { + return; + } + } + + if (this.currentBots.size() >= Room.MAXIMUM_BOTS && !botOwner.hasPermissionRight(Permission.ACC_UNLIMITED_BOTS)) { + botOwner.getClient().sendResponse(new BotErrorComposer(BotErrorComposer.ROOM_ERROR_MAX_BOTS)); + return; + } + + RoomBot roomBot = bot.getRoomUnit(); + + roomBot.setRoom(this.room); + roomBot.setCurrentPosition(spawnTile); + roomBot.setCurrentZ(spawnTile.getStackHeight()); + roomBot.setRotation(RoomRotation.SOUTH); + roomBot.setSpawnTile(spawnTile); + roomBot.setSpawnHeight(spawnTile.getState().equals(RoomTileState.INVALID) ? 0 : spawnTile.getStackHeight()); + + bot.setSqlUpdateNeeded(true); + Emulator.getThreading().run(bot); + + this.addBot(bot); + + this.room.sendComposer(new RoomUsersComposer(bot).compose()); + + roomBot.instantUpdate(); + + botOwner.getInventory().getBotsComponent().removeBot(bot); + botOwner.getClient().sendResponse(new BotRemovedFromInventoryComposer(bot)); + bot.onPlace(botOwner, room); + } + } + + public void updateBotsAt(RoomTile tile) { + Collection bots = this.getBotsAt(tile); + + if (bots == null || bots.isEmpty()) { + return; + } + + RoomItem item = this.room.getRoomItemManager().getTopItemAt(tile.getX(), tile.getY()); + + bots.forEach(bot -> { + double z = bot.getRoomUnit().getCurrentPosition().getStackHeight(); + + if (bot.getRoomUnit().hasStatus(RoomUnitStatus.SIT) && ((item == null && !bot.getRoomUnit().isCmdSitEnabled()) || (item != null && !item.getBaseItem().allowSit()))) { + bot.getRoomUnit().removeStatus(RoomUnitStatus.SIT); + } + + if (bot.getRoomUnit().hasStatus(RoomUnitStatus.LAY) && ((item == null && !bot.getRoomUnit().isCmdLayEnabled()) || (item != null && !item.getBaseItem().allowLay()))) { + bot.getRoomUnit().removeStatus(RoomUnitStatus.LAY); + } + + if (item != null && (item.getBaseItem().allowSit() || item.getBaseItem().allowLay())) { + if (item.getBaseItem().allowSit()) { + bot.getRoomUnit().addStatus(RoomUnitStatus.SIT, String.valueOf(Item.getCurrentHeight(item))); + } else if (item.getBaseItem().allowLay()) { + bot.getRoomUnit().addStatus(RoomUnitStatus.LAY, String.valueOf(Item.getCurrentHeight(item))); + } + + bot.getRoomUnit().setCurrentZ(item.getCurrentZ()); + bot.getRoomUnit().setRotation(RoomRotation.fromValue(item.getRotation())); + } else { + bot.getRoomUnit().setCurrentZ(z); + } + }); + } + + public void pickUpBot(Bot bot, Habbo picker) { + HabboInfo botOwnerInfo = picker == null ? bot.getOwnerInfo() : picker.getHabboInfo(); + + BotPickUpEvent pickedUpEvent = new BotPickUpEvent(bot, picker); + Emulator.getPluginManager().fireEvent(pickedUpEvent); + + if (pickedUpEvent.isCancelled()) + return; + + if (picker == null || (bot.getOwnerInfo().getId() == picker.getHabboInfo().getId() || picker.hasPermissionRight(Permission.ACC_ANYROOMOWNER))) { + if (picker != null && !picker.hasPermissionRight(Permission.ACC_UNLIMITED_BOTS) && picker.getInventory().getBotsComponent().getBots().size() >= BotManager.MAXIMUM_BOT_INVENTORY_SIZE) { + picker.alert(Emulator.getTexts().getValue("error.bots.max.inventory").replace("%amount%", String.valueOf(BotManager.MAXIMUM_BOT_INVENTORY_SIZE))); + return; + } + + bot.onPickUp(picker, this.room); + + bot.setFollowingHabboId(0); + + //@DEPRECATED + bot.setOwnerId(botOwnerInfo.getId()); + bot.setOwnerName(botOwnerInfo.getUsername()); + + bot.setOwnerInfo(botOwnerInfo); + + this.removeBot(bot); + + bot.setSqlUpdateNeeded(true); + Emulator.getThreading().run(bot); + + Habbo receiver = picker == null ? Emulator.getGameEnvironment().getHabboManager().getHabbo(botOwnerInfo.getId()) : picker; + + if (receiver != null) { + receiver.getInventory().getBotsComponent().addBot(bot); + receiver.getClient().sendResponse(new BotAddedToInventoryComposer(bot)); + } + } + } + + + public void addBot(Bot unit) { + currentBots.put(unit.getId(), unit); + } + + public void removeBot(Bot bot) { + if (this.currentBots.containsKey(bot.getId())) { + //TODO gotta do a method to removeUnit and clear tile + if (bot.getRoomUnit().getCurrentPosition() != null) { + bot.getRoomUnit().getCurrentPosition().removeUnit(bot.getRoomUnit()); + } + + this.currentBots.remove(bot.getId()); + roomUnitManager.removeUnit(bot.getRoomUnit().getVirtualId()); + + + bot.getRoomUnit().setRoom(null); + + this.room.sendComposer(new UserRemoveMessageComposer(bot.getRoomUnit()).compose()); + } + } + + public void clear() { + currentBots.clear(); + } + + public void dispose() { + Iterator botIterator = this.currentBots.values().iterator(); + + while (botIterator.hasNext()) { + try { + Bot bot = botIterator.next(); + bot.setSqlUpdateNeeded(true); + Emulator.getThreading().run(bot); + } catch (NoSuchElementException e) { + log.error("Caught Exception", e); + break; + } + } + + this.currentBots.clear(); + + } +} diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomBot.java b/src/main/java/com/eu/habbo/habbohotel/rooms/bots/entities/RoomBot.java similarity index 97% rename from src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomBot.java rename to src/main/java/com/eu/habbo/habbohotel/rooms/bots/entities/RoomBot.java index 59f47dd56..24d30e82 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomBot.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/bots/entities/RoomBot.java @@ -1,4 +1,4 @@ -package com.eu.habbo.habbohotel.rooms.entities.units.types; +package com.eu.habbo.habbohotel.rooms.bots.entities; import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.bots.Bot; @@ -7,6 +7,7 @@ import com.eu.habbo.habbohotel.rooms.constants.RoomChatMessageBubbles; import com.eu.habbo.habbohotel.rooms.RoomTile; import com.eu.habbo.habbohotel.rooms.constants.RoomUserAction; import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnitType; +import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomAvatar; import com.eu.habbo.habbohotel.wired.WiredHandler; import com.eu.habbo.habbohotel.wired.WiredTriggerType; import com.eu.habbo.messages.outgoing.rooms.users.ChatMessageComposer; diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/RoomUnit.java b/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/RoomUnit.java index 65a09baa..cb94d790 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/RoomUnit.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/RoomUnit.java @@ -166,7 +166,7 @@ public abstract class RoomUnit extends RoomEntity { super.setCurrentZ(currentZ); if (this.room != null) { - Bot bot = this.room.getRoomUnitManager().getRoomBotById(getVirtualId()); + Bot bot = this.room.getRoomUnitManager().getRoomBotManager().getRoomBotById(getVirtualId()); if (bot != null) { bot.setSqlUpdateNeeded(true); } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomUnitSubManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomUnitSubManager.java new file mode 100644 index 00000000..ecb63b19 --- /dev/null +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomUnitSubManager.java @@ -0,0 +1,14 @@ +package com.eu.habbo.habbohotel.rooms.entities.units.types; + +import com.eu.habbo.habbohotel.rooms.Room; +import com.eu.habbo.habbohotel.rooms.RoomUnitManager; + +public abstract class RoomUnitSubManager { + + protected RoomUnitManager roomUnitManager; + protected Room room; + public RoomUnitSubManager(RoomUnitManager roomUnitManager){ + this.roomUnitManager = roomUnitManager; + this.room = roomUnitManager.getRoom(); + } +} diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/items/RoomItemManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/items/RoomItemManager.java index 51e868fb..595dd1b8 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/items/RoomItemManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/items/RoomItemManager.java @@ -263,7 +263,7 @@ public class RoomItemManager { for (RoomTile t : occupiedTiles) { this.room.getRoomUnitManager().updateHabbosAt(t); - this.room.getRoomUnitManager().updateBotsAt(t); + this.room.getRoomUnitManager().getRoomBotManager().updateBotsAt(t); } Emulator.getThreading().run(item); @@ -343,7 +343,7 @@ public class RoomItemManager { if (checkForUnits && !magicTile) { if (this.room.getRoomUnitManager().hasHabbosAt(t)) return FurnitureMovementError.TILE_HAS_HABBOS; - if (!this.room.getRoomUnitManager().getBotsAt(t).isEmpty()) + if (!this.room.getRoomUnitManager().getRoomBotManager().getBotsAt(t).isEmpty()) return FurnitureMovementError.TILE_HAS_BOTS; if (this.room.getRoomUnitManager().hasPetsAt(t)) return FurnitureMovementError.TILE_HAS_PETS; @@ -459,7 +459,7 @@ public class RoomItemManager { //Update Habbos at old position for (RoomTile t : occupiedTiles) { this.room.getRoomUnitManager().updateHabbosAt(t); - this.room.getRoomUnitManager().updateBotsAt(t); + this.room.getRoomUnitManager().getRoomBotManager().updateBotsAt(t); } if (Emulator.getConfig().getBoolean("wired.place.under", false)) { @@ -520,7 +520,7 @@ public class RoomItemManager { updatedTiles.forEach(tile -> { this.room.getRoomUnitManager().updateHabbosAt(tile); - this.room.getRoomUnitManager().updateBotsAt(tile); + this.room.getRoomUnitManager().getRoomBotManager().updateBotsAt(tile); }); } else if (roomItem.getBaseItem().getType() == FurnitureType.WALL) { this.room.sendComposer(new ItemRemoveMessageComposer(roomItem).compose()); @@ -606,7 +606,7 @@ public class RoomItemManager { if (!Emulator.getConfig().getBoolean("wired.place.under", false) || (Emulator.getConfig().getBoolean("wired.place.under", false) && !item.isWalkable() && !item.getBaseItem().allowSit() && !item.getBaseItem().allowLay())) { if (checkForUnits && this.room.getRoomUnitManager().hasHabbosAt(occupiedTile)) return FurnitureMovementError.TILE_HAS_HABBOS; - if (checkForUnits && this.room.getRoomUnitManager().hasBotsAt(occupiedTile)) + if (checkForUnits && this.room.getRoomUnitManager().getRoomBotManager().hasBotsAt(occupiedTile)) return FurnitureMovementError.TILE_HAS_BOTS; if (checkForUnits && this.room.getRoomUnitManager().hasPetsAt(occupiedTile)) return FurnitureMovementError.TILE_HAS_PETS; diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/items/entities/RoomItem.java b/src/main/java/com/eu/habbo/habbohotel/rooms/items/entities/RoomItem.java index e472da5a..857c3d24 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/items/entities/RoomItem.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/items/entities/RoomItem.java @@ -367,7 +367,7 @@ public abstract class RoomItem extends RoomEntity implements Runnable, IEventTri } RoomTile tile = room.getLayout().getTile(this.getCurrentPosition().getX(), this.getCurrentPosition().getY()); - for (Bot bot : room.getRoomUnitManager().getBotsAt(tile)) { + for (Bot bot : room.getRoomUnitManager().getRoomBotManager().getBotsAt(tile)) { if (this.getBaseItem().getEffectM() > 0 && bot.getGender().equals(HabboGender.M) && bot.getRoomUnit().getEffectId() == this.getBaseItem().getEffectM()) { bot.getRoomUnit().giveEffect(nextEffectM, -1); } @@ -397,12 +397,12 @@ public abstract class RoomItem extends RoomEntity implements Runnable, IEventTri for (RoomTile tile : room.getLayout().getTilesAt(oldLocation, this.getBaseItem().getWidth(), this.getBaseItem().getLength(), this.getRotation())) { oldHabbos.addAll(room.getRoomUnitManager().getHabbosAt(tile)); - oldBots.addAll(room.getRoomUnitManager().getBotsAt(tile)); + oldBots.addAll(room.getRoomUnitManager().getRoomBotManager().getBotsAt(tile)); } for (RoomTile tile : room.getLayout().getTilesAt(oldLocation, this.getBaseItem().getWidth(), this.getBaseItem().getLength(), this.getRotation())) { newHabbos.addAll(room.getRoomUnitManager().getHabbosAt(tile)); - newBots.addAll(room.getRoomUnitManager().getBotsAt(tile)); + newBots.addAll(room.getRoomUnitManager().getRoomBotManager().getBotsAt(tile)); } oldHabbos.removeAll(newHabbos); diff --git a/src/main/java/com/eu/habbo/habbohotel/users/Habbo.java b/src/main/java/com/eu/habbo/habbohotel/users/Habbo.java index d26dc17f..ff63c4d8 100644 --- a/src/main/java/com/eu/habbo/habbohotel/users/Habbo.java +++ b/src/main/java/com/eu/habbo/habbohotel/users/Habbo.java @@ -347,7 +347,7 @@ public class Habbo extends Avatar implements Runnable { public void deleteBot(Bot bot) { this.removeBot(bot); - bot.getRoomUnit().getRoom().getRoomUnitManager().removeBot(bot); + bot.getRoomUnit().getRoom().getRoomUnitManager().getRoomBotManager().removeBot(bot); Emulator.getGameEnvironment().getBotManager().deleteBot(bot); } diff --git a/src/main/java/com/eu/habbo/messages/incoming/navigator/DeleteRoomEvent.java b/src/main/java/com/eu/habbo/messages/incoming/navigator/DeleteRoomEvent.java index 25cf6167..42b2c00f 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/navigator/DeleteRoomEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/navigator/DeleteRoomEvent.java @@ -50,7 +50,7 @@ public class DeleteRoomEvent extends MessageHandler { room.getRoomItemManager().ejectAllFurni(); room.getRoomItemManager().ejectUserFurni(room.getRoomInfo().getOwnerInfo().getId()); - List bots = new ArrayList<>(room.getRoomUnitManager().getCurrentBots().values()); + List bots = new ArrayList<>(room.getRoomUnitManager().getRoomBotManager().getCurrentBots().values()); for (Bot bot : bots) { Emulator.getGameEnvironment().getBotManager().pickUpBot(bot, null, room); } diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/CommandBotEvent.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/CommandBotEvent.java index 08befe11..c8bcd759 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/CommandBotEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/CommandBotEvent.java @@ -28,7 +28,7 @@ public class CommandBotEvent extends MessageHandler { if (room.getRoomInfo().isRoomOwner(this.client.getHabbo()) || this.client.getHabbo().hasPermissionRight(Permission.ACC_ANYROOMOWNER)) { int botId = this.packet.readInt(); - Bot bot = room.getRoomUnitManager().getRoomBotById(Math.abs(botId)); + Bot bot = room.getRoomUnitManager().getRoomBotManager().getRoomBotById(Math.abs(botId)); if (bot == null) { return; diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/GetBotCommandConfigurationDataEvent.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/GetBotCommandConfigurationDataEvent.java index 7c9f337b..c7d88043 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/GetBotCommandConfigurationDataEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/GetBotCommandConfigurationDataEvent.java @@ -17,7 +17,7 @@ public class GetBotCommandConfigurationDataEvent extends MessageHandler { if (room.getRoomInfo().getOwnerInfo().getId() == this.client.getHabbo().getHabboInfo().getId() || this.client.getHabbo().hasPermissionRight(Permission.ACC_ANYROOMOWNER)) { int botId = this.packet.readInt(); - Bot bot = room.getRoomUnitManager().getRoomBotById(Math.abs(botId)); + Bot bot = room.getRoomUnitManager().getRoomBotManager().getRoomBotById(Math.abs(botId)); if (bot == null) return; diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/PlaceBotEvent.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/PlaceBotEvent.java index 3e81a536..31a3dd8c 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/PlaceBotEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/PlaceBotEvent.java @@ -22,6 +22,6 @@ public class PlaceBotEvent extends MessageHandler { int x = this.packet.readInt(); int y = this.packet.readInt(); - room.getRoomUnitManager().placeBot(bot, this.client.getHabbo(), x, y); + room.getRoomUnitManager().getRoomBotManager().placeBot(bot, this.client.getHabbo(), x, y); } } diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/RemoveBotFromFlatEvent.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/RemoveBotFromFlatEvent.java index d55a372a..971a37b3 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/RemoveBotFromFlatEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/bots/RemoveBotFromFlatEvent.java @@ -17,12 +17,12 @@ public class RemoveBotFromFlatEvent extends MessageHandler { int botId = this.packet.readInt(); - Bot bot = room.getRoomUnitManager().getRoomBotById(Math.abs(botId)); + Bot bot = room.getRoomUnitManager().getRoomBotManager().getRoomBotById(Math.abs(botId)); if(bot == null) { return; } - room.getRoomUnitManager().pickUpBot(bot, this.client.getHabbo()); + room.getRoomUnitManager().getRoomBotManager().pickUpBot(bot, this.client.getHabbo()); } } diff --git a/src/main/java/com/eu/habbo/threading/runnables/RoomUnitTeleport.java b/src/main/java/com/eu/habbo/threading/runnables/RoomUnitTeleport.java index c8533960..51da0ed1 100644 --- a/src/main/java/com/eu/habbo/threading/runnables/RoomUnitTeleport.java +++ b/src/main/java/com/eu/habbo/threading/runnables/RoomUnitTeleport.java @@ -55,7 +55,7 @@ public class RoomUnitTeleport implements Runnable { roomUnit.setWiredTeleporting(false); this.room.getRoomUnitManager().updateHabbosAt(newLocation); - this.room.getRoomUnitManager().updateBotsAt(newLocation); + this.room.getRoomUnitManager().getRoomBotManager().updateBotsAt(newLocation); topItem = room.getRoomItemManager().getTopItemAt(x, y); if (topItem != null && roomUnit.getCurrentPosition().equals(room.getLayout().getTile((short) x, (short) y))) { From a2180c3b89b152cd569d89004add6900402c61be Mon Sep 17 00:00:00 2001 From: Dominic Bridge Date: Fri, 8 Dec 2023 17:25:29 +0000 Subject: [PATCH 2/2] room unit bot manager --- .../com/eu/habbo/habbohotel/rooms/Room.java | 398 +----------------- .../habbohotel/rooms/RoomUnitManager.java | 375 +++++++++++++++++ .../habbohotel/rooms/bots/RoomBotManager.java | 46 +- .../units/types/RoomUnitSubManager.java | 6 + 4 files changed, 439 insertions(+), 386 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java index 4616cd61..86c79146 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -92,7 +92,7 @@ public class Room implements Comparable, ISerialize, Runnable { private final RoomWordFilterManager roomWordFilterManager; @Getter private RoomTraxManager roomTraxManager; - private static final String CAUGHT_EXCEPTION = "Caught exception"; + public static final String CAUGHT_EXCEPTION = "Caught exception"; public static final Comparator SORT_SCORE = Comparator.comparingInt(room -> room.roomInfo.getScore()); public static final Comparator SORT_ID = Comparator.comparingInt(room -> room.roomInfo.getId()); public static final Comparator SORT_USERS_COUNT = Comparator @@ -127,6 +127,7 @@ public class Room implements Comparable, ISerialize, Runnable { public volatile boolean preventUnloading = false; public volatile boolean preventUncaching = false; public final ConcurrentHashMap.KeySetView scheduledComposers = ConcurrentHashMap.newKeySet(); + public ConcurrentHashMap.KeySetView scheduledTasks = ConcurrentHashMap.newKeySet(); @Getter private String wordQuiz = ""; @@ -155,9 +156,8 @@ public class Room implements Comparable, ISerialize, Runnable { private volatile boolean loaded; @Getter private volatile boolean preLoaded; - private int roomIdleCycles; + private final int muteTime = Emulator.getConfig().getInt("hotel.flood.mute.time", 30); - private long rollerCycle = System.currentTimeMillis(); @Getter @Setter private volatile int lastTimerReset = Emulator.getIntUnixTimestamp(); @@ -235,7 +235,7 @@ public class Room implements Comparable, ISerialize, Runnable { this.roomUnitManager.load(connection); this.roomWordFilterManager.load(connection); - this.roomIdleCycles = 0; + this.loaded = true; this.roomCycleTask = Emulator.getThreading().getService().scheduleAtFixedRate(this, 500, 500, TimeUnit.MILLISECONDS); @@ -431,7 +431,7 @@ public class Room implements Comparable, ISerialize, Runnable { private void cycle() { this.cycleOdd = !this.cycleOdd; this.cycleTimestamp = System.currentTimeMillis(); - final boolean[] foundRightHolder = {false}; + boolean foundRightHolder = false; boolean loaded; synchronized (this.loadLock) { @@ -452,380 +452,11 @@ public class Room implements Comparable, ISerialize, Runnable { task.cycle(this); } - if (!this.roomUnitManager.getCurrentHabbos().isEmpty()) { - this.roomIdleCycles = 0; - - THashSet updatedUnit = new THashSet<>(); - ArrayList toKick = new ArrayList<>(); - - final long millis = System.currentTimeMillis(); - - for (Habbo habbo : this.roomUnitManager.getCurrentHabbos().values()) { - if (!foundRightHolder[0]) { - foundRightHolder[0] = habbo.getRoomUnit().getRightsLevel() != RoomRightLevels.NONE; - } - - if (habbo.getRoomUnit().getEffectId() > 0 && millis / 1000 > habbo.getRoomUnit().getEffectEndTimestamp()) { - habbo.getRoomUnit().giveEffect(0, -1); - } - - if (habbo.getRoomUnit().isKicked()) { - habbo.getRoomUnit().setKickCount(habbo.getRoomUnit().getKickCount() + 1); - - if (habbo.getRoomUnit().getKickCount() >= 5) { - this.scheduledTasks.add(() -> Emulator.getGameEnvironment().getRoomManager().leaveRoom(habbo, this)); - continue; - } - } - - if (Emulator.getConfig().getBoolean("hotel.rooms.auto.idle")) { - cycleIdle(this, habbo, toKick); - } - - if (Emulator.getConfig().getBoolean("hotel.rooms.deco_hosting") && this.roomInfo.getOwnerInfo().getId() != habbo.getHabboInfo().getId()) { - //Check if the time already have 1 minute (120 / 2 = 60s) - if (habbo.getRoomUnit().getTimeInRoom() >= 120) { - AchievementManager.progressAchievement(this.roomInfo.getOwnerInfo().getId(), Emulator.getGameEnvironment().getAchievementManager().getAchievement("RoomDecoHosting")); - habbo.getRoomUnit().resetTimeInRoom(); - } else { - habbo.getRoomUnit().increaseTimeInRoom(); - } - } - - if (habbo.getHabboStats().isMutedBubbleTracker() && habbo.getHabboStats().allowTalk()) { - habbo.getHabboStats().setMutedBubbleTracker(false); - this.sendComposer(new IgnoreResultMessageComposer(habbo, IgnoreResultMessageComposer.UNIGNORED).compose()); - } - - // Substract 1 from the chatCounter every odd cycle, which is every (500ms * 2). - if (this.cycleOdd && habbo.getHabboStats().getChatCounter().get() > 0) { - habbo.getHabboStats().getChatCounter().decrementAndGet(); - } - - habbo.getRoomUnit().cycle(); - - if(habbo.getRoomUnit().isStatusUpdateNeeded()) { - habbo.getRoomUnit().setStatusUpdateNeeded(false); - updatedUnit.add(habbo.getRoomUnit()); - } - } - - if (!toKick.isEmpty()) { - for (Habbo habbo : toKick) { - Emulator.getGameEnvironment().getRoomManager().leaveRoom(habbo, this); - } - } - - if (!this.roomUnitManager.getRoomBotManager().getCurrentBots().isEmpty()) { - Iterator botIterator = this.roomUnitManager.getRoomBotManager().getCurrentBots().values().iterator(); - - while(botIterator.hasNext()) { - try { - final Bot bot; - try { - bot = botIterator.next(); - } catch (Exception e) { - break; - } - - if (!this.allowBotsWalk && bot.getRoomUnit().isWalking()) { - bot.getRoomUnit().stopWalking(); - updatedUnit.add(bot.getRoomUnit()); - continue; - } - - bot.getRoomUnit().cycle(); - - if(bot.getRoomUnit().isStatusUpdateNeeded()) { - bot.getRoomUnit().setStatusUpdateNeeded(false); - updatedUnit.add(bot.getRoomUnit()); - } - - - } catch (NoSuchElementException e) { - log.error(CAUGHT_EXCEPTION, e); - break; - } - } - } - - if (!this.roomUnitManager.getCurrentPets().isEmpty() && this.allowBotsWalk) { - Iterator petIterator = this.roomUnitManager.getCurrentPets().values().iterator(); - while(petIterator.hasNext()) { - final Pet pet; - try { - pet = petIterator.next(); - } catch (Exception e) { - break; - } - - pet.getRoomUnit().cycle(); - pet.cycle(); - - if(pet.getRoomUnit().isStatusUpdateNeeded()) { - pet.getRoomUnit().setStatusUpdateNeeded(false); - updatedUnit.add(pet.getRoomUnit()); - } - - if (pet.getRoomUnit().isWalking() && pet.getRoomUnit().getPath().size() == 1 && pet.getRoomUnit().hasStatus(RoomUnitStatus.GESTURE)) { - pet.getRoomUnit().removeStatus(RoomUnitStatus.GESTURE); - updatedUnit.add(pet.getRoomUnit()); - } - } - } - - - if (this.roomInfo.getRollerSpeed() != -1 && this.rollerCycle >= this.roomInfo.getRollerSpeed()) { - this.rollerCycle = 0; - - THashSet messages = new THashSet<>(); - - //Find alternative for this. - //Reason is that tile gets updated after every roller. - List rollerFurniIds = new ArrayList<>(); - List rolledUnitIds = new ArrayList<>(); - - - this.roomSpecialTypes.getRollers().forEachValue(roller -> { - - RoomItem newRoller = null; - - RoomTile rollerTile = this.layout.getTile(roller.getCurrentPosition().getX(), roller.getCurrentPosition().getY()); - - if (rollerTile == null) - return true; - - THashSet itemsOnRoller = new THashSet<>(); - - for (RoomItem item : this.roomItemManager.getItemsAt(rollerTile)) { - if (item.getCurrentZ() >= roller.getCurrentZ() + Item.getCurrentHeight(roller)) { - itemsOnRoller.add(item); - } - } - - itemsOnRoller.remove(roller); - - if (!this.roomUnitManager.areRoomUnitsAt(rollerTile) && itemsOnRoller.isEmpty()) - return true; - - RoomTile tileInFront = Room.this.layout.getTileInFront(Room.this.layout.getTile(roller.getCurrentPosition().getX(), roller.getCurrentPosition().getY()), roller.getRotation()); - - if (tileInFront == null) - return true; - - if (!Room.this.layout.tileExists(tileInFront.getX(), tileInFront.getY())) - return true; - - if (tileInFront.getState() == RoomTileState.INVALID) - return true; - - if (!tileInFront.getAllowStack() && !(tileInFront.isWalkable() || tileInFront.getState() == RoomTileState.SIT || tileInFront.getState() == RoomTileState.LAY)) - return true; - - if (this.roomUnitManager.areRoomUnitsAt(tileInFront)) - return true; - - THashSet itemsNewTile = new THashSet<>(); - itemsNewTile.addAll(this.roomItemManager.getItemsAt(tileInFront)); - itemsNewTile.removeAll(itemsOnRoller); - - itemsOnRoller.removeIf(item -> item.getCurrentPosition().getX() != roller.getCurrentPosition().getX() || item.getCurrentPosition().getY() != roller.getCurrentPosition().getY() || rollerFurniIds.contains(item.getId())); - - RoomItem topItem = this.roomItemManager.getTopItemAt(tileInFront.getX(), tileInFront.getY()); - - boolean allowUsers = true; - boolean allowFurniture = true; - boolean stackContainsRoller = false; - - for (RoomItem item : itemsNewTile) { - if (!(item.getBaseItem().allowWalk() || item.getBaseItem().allowSit()) && !(item instanceof InteractionGate && item.getExtraData().equals("1"))) { - allowUsers = false; - } - if (item instanceof InteractionRoller) { - newRoller = item; - stackContainsRoller = true; - - if ((item.getCurrentZ() != roller.getCurrentZ() || (itemsNewTile.size() > 1 && item != topItem)) && !InteractionRoller.NO_RULES) { - allowUsers = false; - allowFurniture = false; - continue; - } - - break; - } else { - allowFurniture = false; - } - } - - if (allowFurniture) { - allowFurniture = tileInFront.getAllowStack(); - } - - double zOffset = 0; - if (newRoller != null) { - if ((!itemsNewTile.isEmpty() && (itemsNewTile.size() > 1)) && !InteractionRoller.NO_RULES) { - return true; - } - } else { - zOffset = -Item.getCurrentHeight(roller) + tileInFront.getStackHeight() - rollerTile.getZ(); - } - - if (allowUsers) { - Event roomUserRolledEvent = null; - - if (Emulator.getPluginManager().isRegistered(UserRolledEvent.class, true)) { - roomUserRolledEvent = new UserRolledEvent(null, null, null); - } - - ArrayList unitsOnTile = new ArrayList<>(this.roomUnitManager.getRoomUnitsAt(rollerTile)); - - for (RoomUnit roomUnit : this.roomUnitManager.getRoomUnitsAt(rollerTile)) { - if (roomUnit instanceof RoomPet) { - Pet pet = this.roomUnitManager.getPetByRoomUnit(roomUnit); - if (pet instanceof RideablePet rideablePet && rideablePet.getRider() != null) { - unitsOnTile.remove(roomUnit); - } - } - } - - THashSet usersRolledThisTile = new THashSet<>(); - - for (RoomUnit roomUnit : unitsOnTile) { - if (rolledUnitIds.contains(roomUnit.getVirtualId())) continue; - - if (usersRolledThisTile.size() >= Room.ROLLERS_MAXIMUM_ROLL_AVATARS) break; - - if (stackContainsRoller && !allowFurniture && !(topItem != null && topItem.isWalkable())) - continue; - - if (roomUnit.hasStatus(RoomUnitStatus.MOVE)) - continue; - - double newZ = roomUnit.getCurrentZ() + zOffset; - - if (roomUserRolledEvent != null && roomUnit.getRoomUnitType() == RoomUnitType.HABBO) { - roomUserRolledEvent = new UserRolledEvent(this.getRoomUnitManager().getHabboByRoomUnit(roomUnit), roller, tileInFront); - Emulator.getPluginManager().fireEvent(roomUserRolledEvent); - - if (roomUserRolledEvent.isCancelled()) - continue; - } - - // horse riding - boolean isRiding = false; - if (roomUnit.getRoomUnitType() == RoomUnitType.HABBO) { - Habbo rollingHabbo = this.getRoomUnitManager().getHabboByRoomUnit(roomUnit); - if (rollingHabbo != null && rollingHabbo.getHabboInfo() != null) { - RideablePet ridingPet = rollingHabbo.getRoomUnit().getRidingPet(); - if (ridingPet != null) { - RoomUnit ridingUnit = ridingPet.getRoomUnit(); - newZ = ridingUnit.getCurrentZ() + zOffset; - rolledUnitIds.add(ridingUnit.getVirtualId()); - updatedUnit.remove(ridingUnit); - messages.add(new RoomUnitOnRollerComposer(ridingUnit, roller, ridingUnit.getCurrentPosition(), ridingUnit.getCurrentZ(), tileInFront, newZ, this)); - isRiding = true; - } - } - } - - usersRolledThisTile.add(roomUnit.getVirtualId()); - rolledUnitIds.add(roomUnit.getVirtualId()); - updatedUnit.remove(roomUnit); - messages.add(new RoomUnitOnRollerComposer(roomUnit, roller, roomUnit.getCurrentPosition(), roomUnit.getCurrentZ() + (isRiding ? 1 : 0), tileInFront, newZ + (isRiding ? 1 : 0), this)); - - if (itemsOnRoller.isEmpty()) { - RoomItem item = this.getRoomItemManager().getTopItemAt(tileInFront.getX(), tileInFront.getY()); - - if (item != null && itemsNewTile.contains(item) && !itemsOnRoller.contains(item)) { - Emulator.getThreading().run(() -> { - if (roomUnit.getTargetPosition() == rollerTile) { - try { - item.onWalkOn(roomUnit, this, new Object[]{rollerTile, tileInFront}); - } catch (Exception e) { - log.error(CAUGHT_EXCEPTION, e); - } - } - }, this.roomInfo.getRollerSpeed() == 0 ? 250 : InteractionRoller.DELAY); - } - } - } - } - - if (!messages.isEmpty()) { - for (MessageComposer message : messages) { - this.sendComposer(message.compose()); - } - messages.clear(); - } - - if (allowFurniture || !stackContainsRoller || InteractionRoller.NO_RULES) { - Event furnitureRolledEvent = null; - - if (Emulator.getPluginManager().isRegistered(FurnitureRolledEvent.class, true)) { - furnitureRolledEvent = new FurnitureRolledEvent(null, null, null); - } - - if (newRoller == null || topItem == newRoller) { - List sortedItems = new ArrayList<>(itemsOnRoller); - sortedItems.sort((o1, o2) -> { - return Double.compare(o2.getCurrentZ(), o1.getCurrentZ()); - }); - - for (RoomItem item : sortedItems) { - if ((item.getCurrentPosition().getX() == roller.getCurrentPosition().getX() && item.getCurrentPosition().getY() == roller.getCurrentPosition().getY() && zOffset <= 0) && (item != roller)) { - if (furnitureRolledEvent != null) { - furnitureRolledEvent = new FurnitureRolledEvent(item, roller, tileInFront); - Emulator.getPluginManager().fireEvent(furnitureRolledEvent); - - if (furnitureRolledEvent.isCancelled()) - continue; - } - - messages.add(new FloorItemOnRollerComposer(item, roller, tileInFront, zOffset, this)); - rollerFurniIds.add(item.getId()); - } - } - } - } - - - if (!messages.isEmpty()) { - for (MessageComposer message : messages) { - this.sendComposer(message.compose()); - } - messages.clear(); - } - - return true; - }); - - - int currentTime = (int) (this.cycleTimestamp / 1000); - for (RoomItem pyramid : this.roomSpecialTypes.getItemsOfType(InteractionPyramid.class)) { - if (pyramid instanceof InteractionPyramid interactionPyramid && interactionPyramid.getNextChange() < currentTime) { - interactionPyramid.change(this); - } - } - } else { - this.rollerCycle++; - } - - if (!updatedUnit.isEmpty()) { - this.sendComposer(new UserUpdateComposer(updatedUnit).compose()); - } - - this.roomTraxManager.cycle(); - } else { - - if (this.roomIdleCycles < 60) - this.roomIdleCycles++; - else - this.dispose(); - } + foundRightHolder = roomUnitManager.cycle(cycleOdd); } synchronized (this.habboQueue) { - if (!this.habboQueue.isEmpty() && !foundRightHolder[0]) { + if (!this.habboQueue.isEmpty() && !foundRightHolder) { this.habboQueue.forEachEntry((a, b) -> { if (b.isOnline()) { if (b.getHabboInfo().getRoomQueueId() == this.roomInfo.getId()) { @@ -1042,7 +673,7 @@ public class Room implements Comparable, ISerialize, Runnable { public void setRollerSpeed(int rollerSpeed) { this.roomInfo.setRollerSpeed(rollerSpeed); - this.rollerCycle = 0; + roomUnitManager.setRollerCycle(0); this.needsUpdate = true; } @@ -1652,7 +1283,7 @@ public class Room implements Comparable, ISerialize, Runnable { } public boolean canSitOrLayAt(RoomTile tile) { - if(tile == null) { + if (tile == null) { return false; } @@ -1667,7 +1298,7 @@ public class Room implements Comparable, ISerialize, Runnable { public boolean canSitAt(int x, int y) { RoomTile tile = this.layout.getTile((short) x, (short) y); - if(tile == null) { + if (tile == null) { return false; } @@ -1753,7 +1384,9 @@ public class Room implements Comparable, ISerialize, Runnable { } for (Habbo habbo : this.roomUnitManager.getCurrentHabbos().values()) { - if (habbo == null) { return ; } + if (habbo == null) { + return; + } if (!habbo.getHabboStats().isIgnoreBots()) habbo.getClient().sendResponse(message); } @@ -1779,7 +1412,7 @@ public class Room implements Comparable, ISerialize, Runnable { } public RoomRightLevels getGuildRightLevel(Habbo habbo) { - if(!this.roomInfo.hasGuild()) { + if (!this.roomInfo.hasGuild()) { return RoomRightLevels.NONE; } @@ -1867,7 +1500,8 @@ public class Room implements Comparable, ISerialize, Runnable { THashSet items = new THashSet<>(); for (RoomItem item : this.roomItemManager.getCurrentItems().values()) { - if (!items.contains(item.getBaseItem()) && item.getOwnerInfo().getId() == userId) items.add(item.getBaseItem()); + if (!items.contains(item.getBaseItem()) && item.getOwnerInfo().getId() == userId) + items.add(item.getBaseItem()); } return items.size(); diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java index ba776da7..87f7fe95 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java @@ -1,25 +1,42 @@ package com.eu.habbo.habbohotel.rooms; import com.eu.habbo.Emulator; +import com.eu.habbo.habbohotel.achievements.AchievementManager; import com.eu.habbo.habbohotel.bots.Bot; import com.eu.habbo.habbohotel.items.Item; +import com.eu.habbo.habbohotel.items.interactions.InteractionGate; +import com.eu.habbo.habbohotel.items.interactions.InteractionPyramid; +import com.eu.habbo.habbohotel.items.interactions.InteractionRoller; import com.eu.habbo.habbohotel.pets.Pet; import com.eu.habbo.habbohotel.pets.PetManager; +import com.eu.habbo.habbohotel.pets.RideablePet; import com.eu.habbo.habbohotel.rooms.bots.RoomBotManager; +import com.eu.habbo.habbohotel.rooms.constants.RoomRightLevels; import com.eu.habbo.habbohotel.rooms.constants.RoomTileState; import com.eu.habbo.habbohotel.rooms.constants.RoomUnitStatus; import com.eu.habbo.habbohotel.rooms.entities.RoomRotation; import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnit; import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnitType; import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomHabbo; +import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomPet; import com.eu.habbo.habbohotel.rooms.items.entities.RoomItem; import com.eu.habbo.habbohotel.units.Unit; 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.inventory.PetAddedToInventoryComposer; +import com.eu.habbo.messages.outgoing.rooms.items.FloorItemOnRollerComposer; import com.eu.habbo.messages.outgoing.rooms.pets.RoomPetComposer; +import com.eu.habbo.messages.outgoing.rooms.users.IgnoreResultMessageComposer; +import com.eu.habbo.messages.outgoing.rooms.users.RoomUnitOnRollerComposer; import com.eu.habbo.messages.outgoing.rooms.users.UserRemoveMessageComposer; +import com.eu.habbo.messages.outgoing.rooms.users.UserUpdateComposer; +import com.eu.habbo.plugin.Event; +import com.eu.habbo.plugin.events.furniture.FurnitureRolledEvent; +import com.eu.habbo.plugin.events.users.UserRolledEvent; import gnu.trove.set.hash.THashSet; import lombok.Getter; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import java.sql.Connection; @@ -32,6 +49,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import static com.eu.habbo.database.DatabaseConstants.CAUGHT_SQL_EXCEPTION; +import static com.eu.habbo.habbohotel.rooms.Room.CAUGHT_EXCEPTION; +import static com.eu.habbo.habbohotel.rooms.utils.cycle.CycleFunctions.cycleIdle; @Slf4j @Getter @@ -44,6 +63,13 @@ public class RoomUnitManager { public final Object roomUnitLock; private final RoomBotManager roomBotManager; + + private int roomIdleCycles; + + @Setter + private long rollerCycle = System.currentTimeMillis(); + + public RoomUnitManager(Room room) { this.room = room; this.currentRoomUnits = new ConcurrentHashMap<>(); @@ -55,6 +81,7 @@ public class RoomUnitManager { } public synchronized void load(Connection connection) { + this.roomIdleCycles = 0; roomBotManager.loadBots(connection); this.loadPets(connection); } @@ -402,4 +429,352 @@ public class RoomUnitManager { public void removeUnit(int virtualId) { this.currentRoomUnits.remove(virtualId); } + + public boolean cycle(boolean cycleOdd){ + boolean foundRightHolder = false; + if (!getCurrentHabbos().isEmpty()) { + this.roomIdleCycles = 0; + + THashSet updatedUnit = new THashSet<>(); + ArrayList toKick = new ArrayList<>(); + + final long millis = System.currentTimeMillis(); + + for (Habbo habbo : getCurrentHabbos().values()) { + if (!foundRightHolder) { + foundRightHolder = habbo.getRoomUnit().getRightsLevel() != RoomRightLevels.NONE; + } + + if (habbo.getRoomUnit().getEffectId() > 0 && millis / 1000 > habbo.getRoomUnit().getEffectEndTimestamp()) { + habbo.getRoomUnit().giveEffect(0, -1); + } + + if (habbo.getRoomUnit().isKicked()) { + habbo.getRoomUnit().setKickCount(habbo.getRoomUnit().getKickCount() + 1); + + if (habbo.getRoomUnit().getKickCount() >= 5) { + room.scheduledTasks.add(() -> Emulator.getGameEnvironment().getRoomManager().leaveRoom(habbo, room)); + continue; + } + } + + if (Emulator.getConfig().getBoolean("hotel.rooms.auto.idle")) { + cycleIdle(room, habbo, toKick); + } + + if (Emulator.getConfig().getBoolean("hotel.rooms.deco_hosting") && room.getRoomInfo().getOwnerInfo().getId() != habbo.getHabboInfo().getId()) { + //Check if the time already have 1 minute (120 / 2 = 60s) + if (habbo.getRoomUnit().getTimeInRoom() >= 120) { + AchievementManager.progressAchievement(room.getRoomInfo().getOwnerInfo().getId(), Emulator.getGameEnvironment().getAchievementManager().getAchievement("RoomDecoHosting")); + habbo.getRoomUnit().resetTimeInRoom(); + } else { + habbo.getRoomUnit().increaseTimeInRoom(); + } + } + + if (habbo.getHabboStats().isMutedBubbleTracker() && habbo.getHabboStats().allowTalk()) { + habbo.getHabboStats().setMutedBubbleTracker(false); + room.sendComposer(new IgnoreResultMessageComposer(habbo, IgnoreResultMessageComposer.UNIGNORED).compose()); + } + + // Substract 1 from the chatCounter every odd cycle, which is every (500ms * 2). + if (cycleOdd && habbo.getHabboStats().getChatCounter().get() > 0) { + habbo.getHabboStats().getChatCounter().decrementAndGet(); + } + + habbo.getRoomUnit().cycle(); + + if(habbo.getRoomUnit().isStatusUpdateNeeded()) { + habbo.getRoomUnit().setStatusUpdateNeeded(false); + updatedUnit.add(habbo.getRoomUnit()); + } + } + + if (!toKick.isEmpty()) { + for (Habbo habbo : toKick) { + Emulator.getGameEnvironment().getRoomManager().leaveRoom(habbo, room); + } + } + + updatedUnit.addAll(roomBotManager.cycle()); + if (!getCurrentPets().isEmpty() && room.isAllowBotsWalk()) { + Iterator petIterator = getCurrentPets().values().iterator(); + while(petIterator.hasNext()) { + final Pet pet; + try { + pet = petIterator.next(); + } catch (Exception e) { + break; + } + + pet.getRoomUnit().cycle(); + pet.cycle(); + + if(pet.getRoomUnit().isStatusUpdateNeeded()) { + pet.getRoomUnit().setStatusUpdateNeeded(false); + updatedUnit.add(pet.getRoomUnit()); + } + + if (pet.getRoomUnit().isWalking() && pet.getRoomUnit().getPath().size() == 1 && pet.getRoomUnit().hasStatus(RoomUnitStatus.GESTURE)) { + pet.getRoomUnit().removeStatus(RoomUnitStatus.GESTURE); + updatedUnit.add(pet.getRoomUnit()); + } + } + } + + + if (room.getRoomInfo().getRollerSpeed() != -1 && this.rollerCycle >= room.getRoomInfo().getRollerSpeed()) { + this.rollerCycle = 0; + + THashSet messages = new THashSet<>(); + + //Find alternative for this. + //Reason is that tile gets updated after every roller. + List rollerFurniIds = new ArrayList<>(); + List rolledUnitIds = new ArrayList<>(); + + + room.getRoomSpecialTypes().getRollers().forEachValue(roller -> { + + RoomItem newRoller = null; + + RoomTile rollerTile = room.getLayout().getTile(roller.getCurrentPosition().getX(), roller.getCurrentPosition().getY()); + + if (rollerTile == null) + return true; + + THashSet itemsOnRoller = new THashSet<>(); + + for (RoomItem item : room.getRoomItemManager().getItemsAt(rollerTile)) { + if (item.getCurrentZ() >= roller.getCurrentZ() + Item.getCurrentHeight(roller)) { + itemsOnRoller.add(item); + } + } + + itemsOnRoller.remove(roller); + + if (!areRoomUnitsAt(rollerTile) && itemsOnRoller.isEmpty()) + return true; + + RoomTile tileInFront = room.getLayout().getTileInFront(room.getLayout().getTile(roller.getCurrentPosition().getX(), roller.getCurrentPosition().getY()), roller.getRotation()); + + if (tileInFront == null) + return true; + + if (!room.getLayout().tileExists(tileInFront.getX(), tileInFront.getY())) + return true; + + if (tileInFront.getState() == RoomTileState.INVALID) + return true; + + if (!tileInFront.getAllowStack() && !(tileInFront.isWalkable() || tileInFront.getState() == RoomTileState.SIT || tileInFront.getState() == RoomTileState.LAY)) + return true; + + if (areRoomUnitsAt(tileInFront)) + return true; + + THashSet itemsNewTile = new THashSet<>(); + itemsNewTile.addAll(room.getRoomItemManager().getItemsAt(tileInFront)); + itemsNewTile.removeAll(itemsOnRoller); + + itemsOnRoller.removeIf(item -> item.getCurrentPosition().getX() != roller.getCurrentPosition().getX() || item.getCurrentPosition().getY() != roller.getCurrentPosition().getY() || rollerFurniIds.contains(item.getId())); + + RoomItem topItem = room.getRoomItemManager().getTopItemAt(tileInFront.getX(), tileInFront.getY()); + + boolean allowUsers = true; + boolean allowFurniture = true; + boolean stackContainsRoller = false; + + for (RoomItem item : itemsNewTile) { + if (!(item.getBaseItem().allowWalk() || item.getBaseItem().allowSit()) && !(item instanceof InteractionGate && item.getExtraData().equals("1"))) { + allowUsers = false; + } + if (item instanceof InteractionRoller) { + newRoller = item; + stackContainsRoller = true; + + if ((item.getCurrentZ() != roller.getCurrentZ() || (itemsNewTile.size() > 1 && item != topItem)) && !InteractionRoller.NO_RULES) { + allowUsers = false; + allowFurniture = false; + continue; + } + + break; + } else { + allowFurniture = false; + } + } + + if (allowFurniture) { + allowFurniture = tileInFront.getAllowStack(); + } + + double zOffset = 0; + if (newRoller != null) { + if ((!itemsNewTile.isEmpty() && (itemsNewTile.size() > 1)) && !InteractionRoller.NO_RULES) { + return true; + } + } else { + zOffset = -Item.getCurrentHeight(roller) + tileInFront.getStackHeight() - rollerTile.getZ(); + } + + if (allowUsers) { + Event roomUserRolledEvent = null; + + if (Emulator.getPluginManager().isRegistered(UserRolledEvent.class, true)) { + roomUserRolledEvent = new UserRolledEvent(null, null, null); + } + + ArrayList unitsOnTile = new ArrayList<>(getRoomUnitsAt(rollerTile)); + + for (RoomUnit roomUnit : getRoomUnitsAt(rollerTile)) { + if (roomUnit instanceof RoomPet) { + Pet pet = getPetByRoomUnit(roomUnit); + if (pet instanceof RideablePet rideablePet && rideablePet.getRider() != null) { + unitsOnTile.remove(roomUnit); + } + } + } + + THashSet usersRolledThisTile = new THashSet<>(); + + for (RoomUnit roomUnit : unitsOnTile) { + if (rolledUnitIds.contains(roomUnit.getVirtualId())) continue; + + if (usersRolledThisTile.size() >= Room.ROLLERS_MAXIMUM_ROLL_AVATARS) break; + + if (stackContainsRoller && !allowFurniture && !(topItem != null && topItem.isWalkable())) + continue; + + if (roomUnit.hasStatus(RoomUnitStatus.MOVE)) + continue; + + double newZ = roomUnit.getCurrentZ() + zOffset; + + if (roomUserRolledEvent != null && roomUnit.getRoomUnitType() == RoomUnitType.HABBO) { + roomUserRolledEvent = new UserRolledEvent(getHabboByRoomUnit(roomUnit), roller, tileInFront); + Emulator.getPluginManager().fireEvent(roomUserRolledEvent); + + if (roomUserRolledEvent.isCancelled()) + continue; + } + + // horse riding + boolean isRiding = false; + if (roomUnit.getRoomUnitType() == RoomUnitType.HABBO) { + Habbo rollingHabbo = getHabboByRoomUnit(roomUnit); + if (rollingHabbo != null && rollingHabbo.getHabboInfo() != null) { + RideablePet ridingPet = rollingHabbo.getRoomUnit().getRidingPet(); + if (ridingPet != null) { + RoomUnit ridingUnit = ridingPet.getRoomUnit(); + newZ = ridingUnit.getCurrentZ() + zOffset; + rolledUnitIds.add(ridingUnit.getVirtualId()); + updatedUnit.remove(ridingUnit); + messages.add(new RoomUnitOnRollerComposer(ridingUnit, roller, ridingUnit.getCurrentPosition(), ridingUnit.getCurrentZ(), tileInFront, newZ, room)); + isRiding = true; + } + } + } + + usersRolledThisTile.add(roomUnit.getVirtualId()); + rolledUnitIds.add(roomUnit.getVirtualId()); + updatedUnit.remove(roomUnit); + messages.add(new RoomUnitOnRollerComposer(roomUnit, roller, roomUnit.getCurrentPosition(), roomUnit.getCurrentZ() + (isRiding ? 1 : 0), tileInFront, newZ + (isRiding ? 1 : 0), room)); + + if (itemsOnRoller.isEmpty()) { + RoomItem item = room.getRoomItemManager().getTopItemAt(tileInFront.getX(), tileInFront.getY()); + + if (item != null && itemsNewTile.contains(item) && !itemsOnRoller.contains(item)) { + Emulator.getThreading().run(() -> { + if (roomUnit.getTargetPosition() == rollerTile) { + try { + item.onWalkOn(roomUnit, room, new Object[]{rollerTile, tileInFront}); + } catch (Exception e) { + log.error(CAUGHT_EXCEPTION, e); + } + } + }, room.getRoomInfo().getRollerSpeed() == 0 ? 250 : InteractionRoller.DELAY); + } + } + } + } + + if (!messages.isEmpty()) { + for (MessageComposer message : messages) { + room.sendComposer(message.compose()); + } + messages.clear(); + } + + if (allowFurniture || !stackContainsRoller || InteractionRoller.NO_RULES) { + Event furnitureRolledEvent = null; + + if (Emulator.getPluginManager().isRegistered(FurnitureRolledEvent.class, true)) { + furnitureRolledEvent = new FurnitureRolledEvent(null, null, null); + } + + if (newRoller == null || topItem == newRoller) { + List sortedItems = new ArrayList<>(itemsOnRoller); + sortedItems.sort((o1, o2) -> { + return Double.compare(o2.getCurrentZ(), o1.getCurrentZ()); + }); + + for (RoomItem item : sortedItems) { + if ((item.getCurrentPosition().getX() == roller.getCurrentPosition().getX() && item.getCurrentPosition().getY() == roller.getCurrentPosition().getY() && zOffset <= 0) && (item != roller)) { + if (furnitureRolledEvent != null) { + furnitureRolledEvent = new FurnitureRolledEvent(item, roller, tileInFront); + Emulator.getPluginManager().fireEvent(furnitureRolledEvent); + + if (furnitureRolledEvent.isCancelled()) + continue; + } + + messages.add(new FloorItemOnRollerComposer(item, roller, tileInFront, zOffset, room)); + rollerFurniIds.add(item.getId()); + } + } + } + } + + + if (!messages.isEmpty()) { + for (MessageComposer message : messages) { + this.sendComposer(message.compose()); + } + messages.clear(); + } + + return true; + }); + + + int currentTime = (int) (room.getCycleTimestamp() / 1000); + for (RoomItem pyramid : room.getRoomSpecialTypes().getItemsOfType(InteractionPyramid.class)) { + if (pyramid instanceof InteractionPyramid interactionPyramid && interactionPyramid.getNextChange() < currentTime) { + interactionPyramid.change(room); + } + } + } else { + this.rollerCycle++; + } + + if (!updatedUnit.isEmpty()) { + this.sendComposer(new UserUpdateComposer(updatedUnit).compose()); + } + + room.getRoomTraxManager().cycle(); + } else { + + if (this.roomIdleCycles < 60) + this.roomIdleCycles++; + else + this.dispose(); + } + + return foundRightHolder; + } + + private void sendComposer(ServerMessage serverMessage) { + room.sendComposer(serverMessage); + } } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/bots/RoomBotManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/bots/RoomBotManager.java index 24c99516..63060c8b 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/bots/RoomBotManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/bots/RoomBotManager.java @@ -33,14 +33,12 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.NoSuchElementException; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import static com.eu.habbo.database.DatabaseConstants.CAUGHT_SQL_EXCEPTION; +import static com.eu.habbo.habbohotel.rooms.Room.CAUGHT_EXCEPTION; @Slf4j @@ -285,4 +283,44 @@ public class RoomBotManager extends RoomUnitSubManager { this.currentBots.clear(); } + + @Override + public List cycle() { + List updatedBots = new ArrayList<>(); + if (!getCurrentBots().isEmpty()) { + Iterator botIterator = getCurrentBots().values().iterator(); + + while(botIterator.hasNext()) { + try { + final Bot bot; + try { + bot = botIterator.next(); + } catch (Exception e) { + break; + } + + if (!room.isAllowBotsWalk() && bot.getRoomUnit().isWalking()) { + bot.getRoomUnit().stopWalking(); + updatedBots.add(bot.getRoomUnit()); + continue; + } + + bot.getRoomUnit().cycle(); + + if(bot.getRoomUnit().isStatusUpdateNeeded()) { + bot.getRoomUnit().setStatusUpdateNeeded(false); + updatedBots.add(bot.getRoomUnit()); + } + + + } catch (NoSuchElementException e) { + log.error(CAUGHT_EXCEPTION, e); + break; + } + } + } + + + return updatedBots; + } } diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomUnitSubManager.java b/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomUnitSubManager.java index ecb63b19..d9d01aeb 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomUnitSubManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomUnitSubManager.java @@ -1,7 +1,11 @@ package com.eu.habbo.habbohotel.rooms.entities.units.types; +import com.eu.habbo.habbohotel.bots.Bot; import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.habbohotel.rooms.RoomUnitManager; +import com.eu.habbo.habbohotel.rooms.entities.units.RoomUnit; + +import java.util.List; public abstract class RoomUnitSubManager { @@ -11,4 +15,6 @@ public abstract class RoomUnitSubManager { this.roomUnitManager = roomUnitManager; this.room = roomUnitManager.getRoom(); } + + public abstract List cycle(); }