From 71062dc2fc7f6374ae8a1dfedd93ddee10598a41 Mon Sep 17 00:00:00 2001 From: Stankman Date: Wed, 2 Aug 2023 20:43:44 -0500 Subject: [PATCH] Bot Placement --- .../habbohotel/rooms/RoomUnitManager.java | 67 +++++++++++++++++-- .../rooms/entities/units/RoomUnit.java | 12 ++-- .../rooms/entities/units/types/RoomPet.java | 4 +- .../incoming/rooms/bots/PlaceBotEvent.java | 3 +- .../incoming/rooms/users/MoveAvatarEvent.java | 2 - 5 files changed, 71 insertions(+), 17 deletions(-) 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 9ddb2747..ab5b85f1 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/RoomUnitManager.java @@ -2,6 +2,7 @@ package com.eu.habbo.habbohotel.rooms; import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.bots.Bot; +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.entities.RoomRotation; @@ -14,9 +15,14 @@ import com.eu.habbo.habbohotel.rooms.entities.units.types.RoomPet; 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.messages.outgoing.generic.alerts.BotErrorComposer; +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.BotPlacedEvent; import gnu.trove.set.hash.THashSet; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -218,19 +224,66 @@ public class RoomUnitManager { return this.currentBots.values().stream().filter(bot -> bot.getRoomUnit().getCurrentPosition().equals(tile)).collect(Collectors.toSet()); } + public void placeBot(Bot bot, Habbo habbo, int x, int y) { + synchronized (this.currentBots) { + RoomTile spawnTile = room.getLayout().getTile((short) x, (short) y); + + if(spawnTile == null) { + spawnTile = room.getLayout().getDoorTile(); + } + + if (Emulator.getPluginManager().isRegistered(BotPlacedEvent.class, false)) { + Event event = new BotPlacedEvent(bot, spawnTile, habbo); + Emulator.getPluginManager().fireEvent(event); + + if (event.isCancelled()) { + return; + } + } + + if(this.currentBots.size() >= Room.MAXIMUM_BOTS && !habbo.hasPermissionRight(Permission.ACC_UNLIMITED_BOTS)) { + habbo.getClient().sendResponse(new BotErrorComposer(BotErrorComposer.ROOM_ERROR_MAX_BOTS)); + return; + } + + if((!spawnTile.isWalkable() && !this.room.canSitOrLayAt(spawnTile)) || this.areRoomUnitsAt(spawnTile)) { + habbo.getClient().sendResponse(new BotErrorComposer(BotErrorComposer.ROOM_ERROR_BOTS_SELECTED_TILE_NOT_FREE)); + return; + } + + RoomBot roomBot = bot.getRoomUnit(); + + roomBot.setRoom(this.room); + roomBot.setCurrentPosition(spawnTile); + roomBot.setCurrentZ(spawnTile.getZ()); + roomBot.setRotation(RoomRotation.SOUTH); + roomBot.setCanWalk(this.room.isAllowBotsWalk()); + + this.addRoomUnit(bot); + + this.room.sendComposer(new RoomUsersComposer(bot).compose()); + + roomBot.instantUpdate(); + + habbo.getInventory().getBotsComponent().removeBot(bot); + habbo.getClient().sendResponse(new BotRemovedFromInventoryComposer(bot)); + bot.onPlace(habbo, room); + } + } + public void placePet(Pet pet, Room room, short x, short y, double z) { synchronized (this.currentPets) { - RoomTile tile = room.getLayout().getTile(x, y); + RoomTile spawnTile = room.getLayout().getTile(x, y); - if (tile == null) { - tile = room.getLayout().getDoorTile(); + if (spawnTile == null) { + spawnTile = room.getLayout().getDoorTile(); } pet.setRoomUnit(new RoomPet()); pet.getRoomUnit().setUnit(pet); pet.setRoom(room); - pet.getRoomUnit().walkTo(tile); - pet.getRoomUnit().setLocation(tile) + pet.getRoomUnit().walkTo(spawnTile); + pet.getRoomUnit().setLocation(spawnTile) .setRoomUnitType(RoomUnitType.PET) .setCanWalk(true) .setCurrentZ(z); @@ -343,8 +396,8 @@ public class RoomUnitManager { bot.getRoomUnit().setInRoom(false); bot.setRoom(null); - bot.getRoomUnit().getRoom().sendComposer(new UserRemoveMessageComposer(bot.getRoomUnit()).compose()); - bot.setRoomUnit(null); + + this.room.sendComposer(new UserRemoveMessageComposer(bot.getRoomUnit()).compose()); return true; } } 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 6ecdb699..efdefff8 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 @@ -201,8 +201,8 @@ public abstract class RoomUnit extends RoomEntity { } if(this.nextPosition != null) { - this.currentPosition = this.nextPosition; - this.currentZ = this.nextZ; + this.setCurrentPosition(this.nextPosition); + this.setCurrentZ(this.nextZ); } this.targetPosition = goalLocation; @@ -212,7 +212,7 @@ public abstract class RoomUnit extends RoomEntity { public RoomUnit setLocation(RoomTile location) { if (location != null) { - this.currentPosition = location; + this.setCurrentPosition(location); this.targetPosition = location; } return this; @@ -492,8 +492,8 @@ public abstract class RoomUnit extends RoomEntity { this.statuses.entrySet().removeIf(entry -> entry.getKey().isRemoveWhenWalking()); if(this.getNextPosition() != null) { - this.currentPosition = this.getNextPosition(); - this.currentZ = this.getNextZ(); + this.setCurrentPosition(this.getNextPosition()); + this.setCurrentZ(this.getNextZ()); } if(!this.path.isEmpty()) { @@ -563,12 +563,14 @@ public abstract class RoomUnit extends RoomEntity { */ private boolean isValidTile(RoomTile tile) { boolean canOverrideTile = this.canOverrideTile(tile); + if (canOverrideTile) { return true; } double heightDifference = tile.getStackHeight() - this.currentZ; + //TODO Why bots are not being detected? boolean areRoomUnitsAtTile = this.room.getRoomUnitManager().areRoomUnitsAt(tile); boolean isAboveMaximumStepHeight = (!RoomLayout.ALLOW_FALLING && heightDifference < -RoomLayout.MAXIMUM_STEP_HEIGHT); boolean isOpenTileAboveMaxHeight = (tile.getState() == RoomTileState.OPEN && heightDifference > RoomLayout.MAXIMUM_STEP_HEIGHT); diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomPet.java b/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomPet.java index 45cee9b3..2b59408f 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomPet.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/entities/units/types/RoomPet.java @@ -18,7 +18,9 @@ public class RoomPet extends RoomUnit { } @Override - public void cycle() {} + public void cycle() { + super.cycle(); + } public boolean handleRider(Pet pet, Room room) { Habbo rider = null; 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 1325ee9e..3e81a536 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 @@ -1,6 +1,5 @@ package com.eu.habbo.messages.incoming.rooms.bots; -import com.eu.habbo.Emulator; import com.eu.habbo.habbohotel.bots.Bot; import com.eu.habbo.habbohotel.rooms.Room; import com.eu.habbo.messages.incoming.MessageHandler; @@ -23,6 +22,6 @@ public class PlaceBotEvent extends MessageHandler { int x = this.packet.readInt(); int y = this.packet.readInt(); - Emulator.getGameEnvironment().getBotManager().placeBot(bot, this.client.getHabbo(), room, room.getLayout().getTile((short) x, (short) y)); + room.getRoomUnitManager().placeBot(bot, this.client.getHabbo(), x, y); } } diff --git a/src/main/java/com/eu/habbo/messages/incoming/rooms/users/MoveAvatarEvent.java b/src/main/java/com/eu/habbo/messages/incoming/rooms/users/MoveAvatarEvent.java index b5ca2447..499fafa5 100644 --- a/src/main/java/com/eu/habbo/messages/incoming/rooms/users/MoveAvatarEvent.java +++ b/src/main/java/com/eu/habbo/messages/incoming/rooms/users/MoveAvatarEvent.java @@ -19,8 +19,6 @@ public class MoveAvatarEvent extends MessageHandler { int x = this.packet.readInt(); int y = this.packet.readInt(); - log.info("CLICKED ON TILE [x]: {} [y]: {}", x, y); - Habbo habbo = this.client.getHabbo(); if(habbo == null) {