Bot now only serves once when the distance is more than reach distance

This commit is contained in:
Remco 2021-01-29 19:08:41 +01:00
parent 9c0cb00628
commit 5851162a63
2 changed files with 42 additions and 23 deletions

View File

@ -58,58 +58,78 @@ public class ButlerBot extends Bot {
@Override @Override
public void onUserSay(final RoomChatMessage message) { public void onUserSay(final RoomChatMessage message) {
if (this.getRoomUnit().hasStatus(RoomUnitStatus.MOVE)) if (this.getRoomUnit().hasStatus(RoomUnitStatus.MOVE) || this.getRoom() == null) {
return; 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) { if (message.getUnfilteredMessage() != null) {
for (Map.Entry<THashSet<String>, Integer> set : serveItems.entrySet()) { for (Map.Entry<THashSet<String>, Integer> set : serveItems.entrySet()) {
for (String s : set.getKey()) { for (String keyword : set.getKey()) {
if (message.getUnfilteredMessage().toLowerCase().contains(s)) {
// 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()); BotServerItemEvent serveEvent = new BotServerItemEvent(this, message.getHabbo(), set.getValue());
if (Emulator.getPluginManager().fireEvent(serveEvent).isCancelled()) { if (Emulator.getPluginManager().fireEvent(serveEvent).isCancelled()) {
return; return;
} }
// Start give handitem process
if (this.getRoomUnit().canWalk()) { if (this.getRoomUnit().canWalk()) {
final String key = s; final String key = keyword;
final Bot b = this; final Bot bot = this;
b.lookAt(serveEvent.habbo);
final List<Runnable> 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<Runnable> 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(serveEvent.habbo.getRoomUnit(), serveEvent.habbo.getHabboInfo().getCurrentRoom(), serveEvent.itemId));
tasks.add(new RoomUnitGiveHanditem(this.getRoomUnit(), serveEvent.habbo.getHabboInfo().getCurrentRoom(), 0)); tasks.add(new RoomUnitGiveHanditem(this.getRoomUnit(), serveEvent.habbo.getHabboInfo().getCurrentRoom(), 0));
tasks.add(() -> { tasks.add(() -> {
if(this.getRoom() != null) { if(this.getRoom() != null) {
String msg = Emulator.getTexts().getValue("bots.butler.given").replace("%key%", key).replace("%username%", serveEvent.habbo.getHabboInfo().getUsername()); String botMessage = Emulator.getTexts()
if (!WiredHandler.handle(WiredTriggerType.SAY_SOMETHING, this.getRoomUnit(), this.getRoom(), new Object[]{msg})) { .getValue("bots.butler.given")
b.talk(msg); .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<Runnable> failedReached = new ArrayList(); List<Runnable> failedReached = new ArrayList<>();
failedReached.add(() -> { failedReached.add(() -> {
if (b.getRoomUnit().getCurrentLocation().distance(serveEvent.habbo.getRoomUnit().getCurrentLocation()) <= Emulator.getConfig().getInt("hotel.bot.butler.servedistance", 8)) { if (distanceBetweenBotAndHabbo <= Emulator.getConfig().getInt("hotel.bot.butler.servedistance", 8)) {
for (Runnable t : tasks) { System.out.println("Failed reached!");
t.run(); 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)); 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))); 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 { } else {
Emulator.getThreading().run(failedReached.get(0), 1000); Emulator.getThreading().run(failedReached.get(0), 1000);
} }
} else { } else {
this.getRoom().giveHandItem(serveEvent.habbo, serveEvent.itemId);
if(this.getRoom() != null) { 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})) { if (!WiredHandler.handle(WiredTriggerType.SAY_SOMETHING, this.getRoomUnit(), this.getRoom(), new Object[]{msg})) {
this.talk(msg); this.talk(msg);
} }
@ -120,5 +140,6 @@ public class ButlerBot extends Bot {
} }
} }
} }
}
} }
} }

View File

@ -42,17 +42,15 @@ public class RoomUnitWalkToRoomUnit implements Runnable {
if (this.goalTile == null) { if (this.goalTile == null) {
this.findNewLocation(); this.findNewLocation();
Emulator.getThreading().run(this, 500); Emulator.getThreading().run(this, 500);
}
if (this.goalTile == null)
return; return;
}
if (this.walker.getGoal().equals(this.goalTile)) { // check that the action hasn't been cancelled by changing the goal 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) { if (this.walker.getCurrentLocation().distance(this.goalTile) <= this.minDistance) {
for (Runnable r : this.targetReached) { for (Runnable r : this.targetReached) {
Emulator.getThreading().run(r); 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 { } else {
Emulator.getThreading().run(this, 500); Emulator.getThreading().run(this, 500);