From 5851162a63933b8df65edb706bebf273f8637515 Mon Sep 17 00:00:00 2001 From: Remco Date: Fri, 29 Jan 2021 19:08:41 +0100 Subject: [PATCH] Bot now only serves once when the distance is more than reach distance --- .../eu/habbo/habbohotel/bots/ButlerBot.java | 59 +++++++++++++------ .../runnables/RoomUnitWalkToRoomUnit.java | 6 +- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/bots/ButlerBot.java b/src/main/java/com/eu/habbo/habbohotel/bots/ButlerBot.java index 89a66272..ab81bb10 100644 --- a/src/main/java/com/eu/habbo/habbohotel/bots/ButlerBot.java +++ b/src/main/java/com/eu/habbo/habbohotel/bots/ButlerBot.java @@ -58,58 +58,78 @@ public class ButlerBot extends Bot { @Override public void onUserSay(final RoomChatMessage message) { - if (this.getRoomUnit().hasStatus(RoomUnitStatus.MOVE)) + if (this.getRoomUnit().hasStatus(RoomUnitStatus.MOVE) || this.getRoom() == null) { return; + } + + double distanceBetweenBotAndHabbo = this.getRoomUnit().getCurrentLocation().distance(message.getHabbo().getRoomUnit().getCurrentLocation()); + + if (distanceBetweenBotAndHabbo <= Emulator.getConfig().getInt("hotel.bot.butler.commanddistance")) { - if (this.getRoomUnit().getCurrentLocation().distance(message.getHabbo().getRoomUnit().getCurrentLocation()) <= Emulator.getConfig().getInt("hotel.bot.butler.commanddistance")) if (message.getUnfilteredMessage() != null) { for (Map.Entry, Integer> set : serveItems.entrySet()) { - for (String s : set.getKey()) { - if (message.getUnfilteredMessage().toLowerCase().contains(s)) { + for (String keyword : set.getKey()) { + + // Check if the string contains a certain keyword using a regex. + // If keyword = tea, teapot wouldn't trigger it. + if (message.getUnfilteredMessage().toLowerCase().matches("\\b" + keyword + "\\b")) { + + // Enable plugins to cancel this event BotServerItemEvent serveEvent = new BotServerItemEvent(this, message.getHabbo(), set.getValue()); if (Emulator.getPluginManager().fireEvent(serveEvent).isCancelled()) { return; } + // Start give handitem process if (this.getRoomUnit().canWalk()) { - final String key = s; - final Bot b = this; - b.lookAt(serveEvent.habbo); + final String key = keyword; + final Bot bot = this; - final List tasks = new ArrayList(); + // Step 1: Look at Habbo + bot.lookAt(serveEvent.habbo); + + // Step 2: Prepare tasks for when the Bot (carrying the handitem) reaches the Habbo + final List tasks = new ArrayList<>(); + tasks.add(() -> System.out.println("Serving drinks!")); tasks.add(new RoomUnitGiveHanditem(serveEvent.habbo.getRoomUnit(), serveEvent.habbo.getHabboInfo().getCurrentRoom(), serveEvent.itemId)); tasks.add(new RoomUnitGiveHanditem(this.getRoomUnit(), serveEvent.habbo.getHabboInfo().getCurrentRoom(), 0)); tasks.add(() -> { if(this.getRoom() != null) { - String msg = Emulator.getTexts().getValue("bots.butler.given").replace("%key%", key).replace("%username%", serveEvent.habbo.getHabboInfo().getUsername()); - if (!WiredHandler.handle(WiredTriggerType.SAY_SOMETHING, this.getRoomUnit(), this.getRoom(), new Object[]{msg})) { - b.talk(msg); + String botMessage = Emulator.getTexts() + .getValue("bots.butler.given") + .replace("%key%", key) + .replace("%username%", serveEvent.habbo.getHabboInfo().getUsername()); + + if (!WiredHandler.handle(WiredTriggerType.SAY_SOMETHING, this.getRoomUnit(), this.getRoom(), new Object[]{ botMessage })) { + bot.talk(botMessage); } } }); - List failedReached = new ArrayList(); + List failedReached = new ArrayList<>(); failedReached.add(() -> { - if (b.getRoomUnit().getCurrentLocation().distance(serveEvent.habbo.getRoomUnit().getCurrentLocation()) <= Emulator.getConfig().getInt("hotel.bot.butler.servedistance", 8)) { - for (Runnable t : tasks) { - t.run(); + if (distanceBetweenBotAndHabbo <= Emulator.getConfig().getInt("hotel.bot.butler.servedistance", 8)) { + System.out.println("Failed reached!"); + for (Runnable task : tasks) { + task.run(); } } }); + // Give bot the handitem that it's going to give the Habbo Emulator.getThreading().run(new RoomUnitGiveHanditem(this.getRoomUnit(), serveEvent.habbo.getHabboInfo().getCurrentRoom(), serveEvent.itemId)); - if (b.getRoomUnit().getCurrentLocation().distance(serveEvent.habbo.getRoomUnit().getCurrentLocation()) > Emulator.getConfig().getInt("hotel.bot.butler.reachdistance", 3)) { + if (distanceBetweenBotAndHabbo > Emulator.getConfig().getInt("hotel.bot.butler.reachdistance", 3)) { Emulator.getThreading().run(new RoomUnitWalkToRoomUnit(this.getRoomUnit(), serveEvent.habbo.getRoomUnit(), serveEvent.habbo.getHabboInfo().getCurrentRoom(), tasks, failedReached, Emulator.getConfig().getInt("hotel.bot.butler.reachdistance", 3))); } else { Emulator.getThreading().run(failedReached.get(0), 1000); } } else { - this.getRoom().giveHandItem(serveEvent.habbo, serveEvent.itemId); - if(this.getRoom() != null) { - String msg = Emulator.getTexts().getValue("bots.butler.given").replace("%key%", s).replace("%username%", serveEvent.habbo.getHabboInfo().getUsername()); + this.getRoom().giveHandItem(serveEvent.habbo, serveEvent.itemId); + + String msg = Emulator.getTexts().getValue("bots.butler.given").replace("%key%", keyword).replace("%username%", serveEvent.habbo.getHabboInfo().getUsername()); if (!WiredHandler.handle(WiredTriggerType.SAY_SOMETHING, this.getRoomUnit(), this.getRoom(), new Object[]{msg})) { this.talk(msg); } @@ -120,5 +140,6 @@ public class ButlerBot extends Bot { } } } + } } } diff --git a/src/main/java/com/eu/habbo/threading/runnables/RoomUnitWalkToRoomUnit.java b/src/main/java/com/eu/habbo/threading/runnables/RoomUnitWalkToRoomUnit.java index 296490e7..3ff6ae37 100644 --- a/src/main/java/com/eu/habbo/threading/runnables/RoomUnitWalkToRoomUnit.java +++ b/src/main/java/com/eu/habbo/threading/runnables/RoomUnitWalkToRoomUnit.java @@ -42,17 +42,15 @@ public class RoomUnitWalkToRoomUnit implements Runnable { if (this.goalTile == null) { this.findNewLocation(); Emulator.getThreading().run(this, 500); - } - - if (this.goalTile == null) return; + } if (this.walker.getGoal().equals(this.goalTile)) { // check that the action hasn't been cancelled by changing the goal if (this.walker.getCurrentLocation().distance(this.goalTile) <= this.minDistance) { for (Runnable r : this.targetReached) { Emulator.getThreading().run(r); - WiredHandler.handle(WiredTriggerType.BOT_REACHED_AVTR, this.target, this.room, new Object[]{this.walker}); + WiredHandler.handle(WiredTriggerType.BOT_REACHED_AVTR, this.target, this.room, new Object[]{ this.walker }); } } else { Emulator.getThreading().run(this, 500);