488 lines
16 KiB
Java
Raw Normal View History

2018-07-06 13:30:00 +00:00
package com.eu.habbo.habbohotel.bots;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.rooms.*;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboGender;
import com.eu.habbo.habbohotel.wired.WiredHandler;
import com.eu.habbo.habbohotel.wired.WiredTriggerType;
2019-05-27 16:05:56 +03:00
import com.eu.habbo.messages.outgoing.rooms.users.*;
2018-07-06 13:30:00 +00:00
import com.eu.habbo.plugin.events.bots.BotChatEvent;
import com.eu.habbo.plugin.events.bots.BotShoutEvent;
import com.eu.habbo.plugin.events.bots.BotTalkEvent;
import com.eu.habbo.plugin.events.bots.BotWhisperEvent;
import com.eu.habbo.threading.runnables.BotFollowHabbo;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-06 13:30:00 +00:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
2019-05-26 21:14:53 +03:00
public class Bot implements Runnable {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class);
2018-09-28 19:25:00 +00:00
public static final String NO_CHAT_SET = "${bot.skill.chatter.configuration.text.placeholder}";
2020-02-05 19:17:51 +02:00
public static String[] PLACEMENT_MESSAGES = "Yo!;Hello I'm a real party animal!;Hello!".split(";");
2019-05-26 21:14:53 +03:00
private final ArrayList<String> chatLines;
2019-03-18 01:22:00 +00:00
private transient int id;
2018-07-06 13:30:00 +00:00
private String name;
private String motto;
private String figure;
private HabboGender gender;
private int ownerId;
private String ownerName;
private Room room;
private RoomUnit roomUnit;
private boolean chatAuto;
private boolean chatRandom;
private short chatDelay;
private int chatTimeOut;
2019-03-18 01:22:00 +00:00
private int chatTimestamp;
2018-07-06 13:30:00 +00:00
private short lastChatIndex;
private int bubble;
2018-07-06 13:30:00 +00:00
2018-07-08 21:32:00 +00:00
2018-07-06 13:30:00 +00:00
private String type;
2018-07-08 21:32:00 +00:00
2018-07-06 13:30:00 +00:00
private int effect;
2019-03-18 01:22:00 +00:00
private transient boolean canWalk = true;
2018-10-06 22:28:00 +00:00
2018-07-08 21:32:00 +00:00
2018-07-06 13:30:00 +00:00
private boolean needsUpdate;
2018-07-08 21:32:00 +00:00
2019-03-18 01:22:00 +00:00
private transient int followingHabboId;
2018-07-06 13:30:00 +00:00
2019-05-26 21:14:53 +03:00
public Bot(int id, String name, String motto, String figure, HabboGender gender, int ownerId, String ownerName) {
2018-07-06 13:30:00 +00:00
this.id = id;
this.name = name;
this.motto = motto;
this.figure = figure;
this.gender = gender;
this.ownerId = ownerId;
this.ownerName = ownerName;
this.chatAuto = false;
this.chatRandom = false;
this.chatDelay = 1000;
2018-09-28 19:25:00 +00:00
this.chatLines = new ArrayList<>();
2018-07-06 13:30:00 +00:00
this.type = "generic_bot";
this.room = null;
this.bubble = RoomChatMessageBubbles.BOT_RENTABLE.getType();
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public Bot(ResultSet set) throws SQLException {
this.id = set.getInt("id");
this.name = set.getString("name");
this.motto = set.getString("motto");
this.figure = set.getString("figure");
this.gender = HabboGender.valueOf(set.getString("gender"));
this.ownerId = set.getInt("user_id");
this.ownerName = set.getString("owner_name");
this.chatAuto = set.getString("chat_auto").equals("1");
this.chatRandom = set.getString("chat_random").equals("1");
this.chatDelay = set.getShort("chat_delay");
this.chatLines = new ArrayList<>(Arrays.asList(set.getString("chat_lines").split("\r")));
this.type = set.getString("type");
this.effect = set.getInt("effect");
this.canWalk = set.getString("freeroam").equals("1");
this.room = null;
this.roomUnit = null;
this.chatTimeOut = Emulator.getIntUnixTimestamp() + this.chatDelay;
this.needsUpdate = false;
this.bubble = set.getInt("bubble_id");
2019-05-26 21:14:53 +03:00
}
public Bot(Bot bot) {
this.name = bot.getName();
this.motto = bot.getMotto();
this.figure = bot.getFigure();
this.gender = bot.getGender();
this.ownerId = bot.getOwnerId();
this.ownerName = bot.getOwnerName();
this.chatAuto = true;
this.chatRandom = false;
this.chatDelay = 10;
this.chatTimeOut = Emulator.getIntUnixTimestamp() + this.chatDelay;
this.chatLines = new ArrayList<>(Arrays.asList("Default Message :D"));
this.type = bot.getType();
this.effect = bot.getEffect();
this.bubble = bot.getBubbleId();
2018-07-06 13:30:00 +00:00
this.needsUpdate = false;
}
2019-05-26 21:14:53 +03:00
public static void initialise() {
2018-07-08 21:32:00 +00:00
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public static void dispose() {
}
2018-07-08 21:32:00 +00:00
2019-05-26 21:14:53 +03:00
public void needsUpdate(boolean needsUpdate) {
this.needsUpdate = needsUpdate;
}
public boolean needsUpdate() {
2018-07-06 13:30:00 +00:00
return this.needsUpdate;
}
@Override
2019-05-26 21:14:53 +03:00
public void run() {
if (this.needsUpdate) {
2020-10-15 00:27:53 +02:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE bots SET name = ?, motto = ?, figure = ?, gender = ?, user_id = ?, room_id = ?, x = ?, y = ?, z = ?, rot = ?, dance = ?, freeroam = ?, chat_lines = ?, chat_auto = ?, chat_random = ?, chat_delay = ?, effect = ?, bubble_id = ? WHERE id = ?")) {
2018-07-06 13:30:00 +00:00
statement.setString(1, this.name);
statement.setString(2, this.motto);
statement.setString(3, this.figure);
statement.setString(4, this.gender.toString());
statement.setInt(5, this.ownerId);
statement.setInt(6, this.room == null ? 0 : this.room.getId());
statement.setInt(7, this.roomUnit == null ? 0 : this.roomUnit.getX());
statement.setInt(8, this.roomUnit == null ? 0 : this.roomUnit.getY());
statement.setDouble(9, this.roomUnit == null ? 0 : this.roomUnit.getZ());
statement.setInt(10, this.roomUnit == null ? 0 : this.roomUnit.getBodyRotation().getValue());
statement.setInt(11, this.roomUnit == null ? 0 : this.roomUnit.getDanceType().getType());
2018-10-06 22:28:00 +00:00
statement.setString(12, this.canWalk ? "1" : "0");
2019-03-18 01:22:00 +00:00
StringBuilder text = new StringBuilder();
2019-05-26 21:14:53 +03:00
for (String s : this.chatLines) {
2019-03-18 01:22:00 +00:00
text.append(s).append("\r");
2018-07-06 13:30:00 +00:00
}
2019-03-18 01:22:00 +00:00
statement.setString(13, text.toString());
2018-07-06 13:30:00 +00:00
statement.setString(14, this.chatAuto ? "1" : "0");
statement.setString(15, this.chatRandom ? "1" : "0");
statement.setInt(16, this.chatDelay);
2019-04-21 23:42:00 +00:00
statement.setInt(17, this.effect);
statement.setInt(18, this.bubble);
statement.setInt(19, this.id);
2018-07-06 13:30:00 +00:00
statement.execute();
this.needsUpdate = false;
2019-05-26 21:14:53 +03:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 13:30:00 +00:00
}
}
}
2019-05-26 21:14:53 +03:00
public void cycle(boolean allowBotsWalk) {
if (this.roomUnit != null) {
if (allowBotsWalk && this.canWalk) {
if (!this.roomUnit.isWalking()) {
if (this.roomUnit.getWalkTimeOut() < Emulator.getIntUnixTimestamp() && this.followingHabboId == 0) {
2018-07-06 13:30:00 +00:00
this.roomUnit.setGoalLocation(this.room.getRandomWalkableTile());
int timeOut = Emulator.getRandom().nextInt(20) * 2;
this.roomUnit.setWalkTimeOut((timeOut < 10 ? 5 : timeOut) + Emulator.getIntUnixTimestamp());
}
}/* else {
2019-05-26 21:14:53 +03:00
for (RoomTile t : this.room.getLayout().getTilesAround(this.room.getLayout().getTile(this.getRoomUnit().getX(), this.getRoomUnit().getY()))) {
2019-03-18 01:22:00 +00:00
WiredHandler.handle(WiredTriggerType.BOT_REACHED_STF, this.roomUnit, this.room, this.room.getItemsAt(t).toArray());
2018-07-06 13:30:00 +00:00
}
}*/
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
if (!this.chatLines.isEmpty() && this.chatTimeOut <= Emulator.getIntUnixTimestamp() && this.chatAuto) {
if (this.room != null) {
this.lastChatIndex = (this.chatRandom ? (short) Emulator.getRandom().nextInt(this.chatLines.size()) : (this.lastChatIndex == (this.chatLines.size() - 1) ? 0 : this.lastChatIndex++));
2018-09-28 19:25:00 +00:00
2019-05-26 21:14:53 +03:00
if (this.lastChatIndex >= this.chatLines.size()) {
2018-09-28 19:25:00 +00:00
this.lastChatIndex = 0;
}
String message = this.chatLines.get(this.lastChatIndex)
2018-07-06 13:30:00 +00:00
.replace("%owner%", this.room.getOwnerName())
.replace("%item_count%", this.room.itemCount() + "")
.replace("%name%", this.name)
.replace("%roomname%", this.room.getName())
.replace("%user_count%", this.room.getUserCount() + "");
if(!WiredHandler.handle(WiredTriggerType.SAY_SOMETHING, this.getRoomUnit(), room, new Object[]{ message })) {
this.talk(message);
}
2018-09-28 19:25:00 +00:00
2018-07-06 13:30:00 +00:00
this.chatTimeOut = Emulator.getIntUnixTimestamp() + this.chatDelay;
}
}
}
}
2019-05-26 21:14:53 +03:00
public void talk(String message) {
if (this.room != null) {
2018-07-06 13:30:00 +00:00
BotChatEvent event = new BotTalkEvent(this, message);
2019-05-26 21:14:53 +03:00
if (Emulator.getPluginManager().fireEvent(event).isCancelled())
2018-07-06 13:30:00 +00:00
return;
2019-03-18 01:22:00 +00:00
this.chatTimestamp = Emulator.getIntUnixTimestamp();
this.room.botChat(new RoomUserTalkComposer(new RoomChatMessage(event.message, this.roomUnit, RoomChatMessageBubbles.getBubble(this.getBubbleId()))).compose());
2019-05-27 16:05:56 +03:00
if (message.equals("o/") || message.equals("_o/")) {
this.room.sendComposer(new RoomUserActionComposer(this.roomUnit, RoomUserAction.WAVE).compose());
}
2018-07-06 13:30:00 +00:00
}
}
2019-05-26 21:14:53 +03:00
public void shout(String message) {
if (this.room != null) {
2018-07-06 13:30:00 +00:00
BotChatEvent event = new BotShoutEvent(this, message);
2019-05-26 21:14:53 +03:00
if (Emulator.getPluginManager().fireEvent(event).isCancelled())
2018-07-06 13:30:00 +00:00
return;
2019-03-18 01:22:00 +00:00
this.chatTimestamp = Emulator.getIntUnixTimestamp();
this.room.botChat(new RoomUserShoutComposer(new RoomChatMessage(event.message, this.roomUnit, RoomChatMessageBubbles.getBubble(this.getBubbleId()))).compose());
2019-05-27 16:05:56 +03:00
if (message.equals("o/") || message.equals("_o/")) {
this.room.sendComposer(new RoomUserActionComposer(this.roomUnit, RoomUserAction.WAVE).compose());
}
2018-07-06 13:30:00 +00:00
}
}
2019-05-26 21:14:53 +03:00
public void whisper(String message, Habbo habbo) {
if (this.room != null && habbo != null) {
2018-07-06 13:30:00 +00:00
BotWhisperEvent event = new BotWhisperEvent(this, message, habbo);
2019-05-26 21:14:53 +03:00
if (Emulator.getPluginManager().fireEvent(event).isCancelled())
2018-07-06 13:30:00 +00:00
return;
2019-03-18 01:22:00 +00:00
this.chatTimestamp = Emulator.getIntUnixTimestamp();
event.target.getClient().sendResponse(new RoomUserWhisperComposer(new RoomChatMessage(event.message, this.roomUnit, RoomChatMessageBubbles.getBubble(this.getBubbleId()))));
2018-07-06 13:30:00 +00:00
}
}
2019-05-26 21:14:53 +03:00
public void onPlace(Habbo habbo, Room room) {
if (this.roomUnit != null) {
2019-04-21 23:42:00 +00:00
room.giveEffect(this.roomUnit, this.effect, -1);
}
2020-02-05 19:17:51 +02:00
if(PLACEMENT_MESSAGES.length > 0) {
String message = PLACEMENT_MESSAGES[Emulator.getRandom().nextInt(PLACEMENT_MESSAGES.length)];
if (!WiredHandler.handle(WiredTriggerType.SAY_SOMETHING, this.getRoomUnit(), room, new Object[]{message})) {
this.talk(message);
}
}
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public void onPickUp(Habbo habbo, Room room) {
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public void onUserSay(final RoomChatMessage message) {
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public int getId() {
2018-07-06 13:30:00 +00:00
return this.id;
}
2019-05-26 21:14:53 +03:00
public void setId(int id) {
2018-07-06 13:30:00 +00:00
this.id = id;
}
2019-05-26 21:14:53 +03:00
public String getName() {
2018-07-06 13:30:00 +00:00
return this.name;
}
public int getBubbleId() {
return bubble;
}
2019-05-26 21:14:53 +03:00
public void setName(String name) {
this.name = name;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
//if(this.room != null)
2019-05-26 21:14:53 +03:00
//this.room.sendComposer(new ChangeNameUpdatedComposer(this.getRoomUnit(), this.getName()).compose());
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public String getMotto() {
2018-07-06 13:30:00 +00:00
return this.motto;
}
2019-05-26 21:14:53 +03:00
public void setMotto(String motto) {
this.motto = motto;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
}
2019-05-26 21:14:53 +03:00
public String getFigure() {
2018-07-06 13:30:00 +00:00
return this.figure;
}
2019-05-26 21:14:53 +03:00
public void setFigure(String figure) {
this.figure = figure;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
2019-05-26 21:14:53 +03:00
if (this.room != null)
2018-07-06 13:30:00 +00:00
this.room.sendComposer(new RoomUsersComposer(this).compose());
}
2019-05-26 21:14:53 +03:00
public HabboGender getGender() {
2018-07-06 13:30:00 +00:00
return this.gender;
}
2019-05-26 21:14:53 +03:00
public void setGender(HabboGender gender) {
this.gender = gender;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
2019-05-26 21:14:53 +03:00
if (this.room != null)
2018-07-06 13:30:00 +00:00
this.room.sendComposer(new RoomUsersComposer(this).compose());
}
2019-05-26 21:14:53 +03:00
public int getOwnerId() {
2018-07-06 13:30:00 +00:00
return this.ownerId;
}
2019-05-26 21:14:53 +03:00
public void setOwnerId(int ownerId) {
this.ownerId = ownerId;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
2019-05-26 21:14:53 +03:00
if (this.room != null)
2018-07-06 13:30:00 +00:00
this.room.sendComposer(new RoomUsersComposer(this).compose());
}
2019-05-26 21:14:53 +03:00
public String getOwnerName() {
2018-07-06 13:30:00 +00:00
return this.ownerName;
}
2019-05-26 21:14:53 +03:00
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
2019-05-26 21:14:53 +03:00
if (this.room != null)
2018-07-06 13:30:00 +00:00
this.room.sendComposer(new RoomUsersComposer(this).compose());
}
2019-05-26 21:14:53 +03:00
public Room getRoom() {
2018-07-06 13:30:00 +00:00
return this.room;
}
2019-05-26 21:14:53 +03:00
public void setRoom(Room room) {
2018-07-06 13:30:00 +00:00
this.room = room;
}
2019-05-26 21:14:53 +03:00
public RoomUnit getRoomUnit() {
2018-07-06 13:30:00 +00:00
return this.roomUnit;
}
2019-05-26 21:14:53 +03:00
public void setRoomUnit(RoomUnit roomUnit) {
2018-07-06 13:30:00 +00:00
this.roomUnit = roomUnit;
}
2019-05-26 21:14:53 +03:00
public boolean isChatAuto() {
2018-07-06 13:30:00 +00:00
return this.chatAuto;
}
2019-05-26 21:14:53 +03:00
public void setChatAuto(boolean chatAuto) {
this.chatAuto = chatAuto;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
}
2019-05-26 21:14:53 +03:00
public boolean isChatRandom() {
2018-07-06 13:30:00 +00:00
return this.chatRandom;
}
2019-05-26 21:14:53 +03:00
public void setChatRandom(boolean chatRandom) {
this.chatRandom = chatRandom;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
}
2019-05-26 21:14:53 +03:00
public boolean hasChat() {
return !this.chatLines.isEmpty();
}
2018-07-08 21:32:00 +00:00
2019-05-26 21:14:53 +03:00
public int getChatDelay() {
2018-07-06 13:30:00 +00:00
return this.chatDelay;
}
2019-05-26 21:14:53 +03:00
public void setChatDelay(short chatDelay) {
this.chatDelay = (short) Math.min(Math.max(chatDelay, BotManager.MINIMUM_CHAT_SPEED), BotManager.MAXIMUM_CHAT_SPEED);
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
2018-09-28 19:25:00 +00:00
this.chatTimeOut = Emulator.getIntUnixTimestamp() + this.chatDelay;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public int getChatTimestamp() {
2019-03-18 01:22:00 +00:00
return this.chatTimestamp;
}
2019-05-26 21:14:53 +03:00
public void clearChat() {
synchronized (this.chatLines) {
2018-07-06 13:30:00 +00:00
this.chatLines.clear();
this.needsUpdate = true;
}
}
2019-05-26 21:14:53 +03:00
public String getType() {
2018-07-06 13:30:00 +00:00
return this.type;
}
2019-05-26 21:14:53 +03:00
public int getEffect() {
2018-07-06 13:30:00 +00:00
return this.effect;
}
2019-05-26 21:14:53 +03:00
public void setEffect(int effect, int duration) {
this.effect = effect;
2018-07-06 13:30:00 +00:00
this.needsUpdate = true;
2019-05-26 21:14:53 +03:00
if (this.roomUnit != null) {
if (this.room != null) {
2019-03-18 01:22:00 +00:00
this.room.giveEffect(this.roomUnit, this.effect, duration);
2018-07-06 13:30:00 +00:00
}
}
}
2019-05-26 21:14:53 +03:00
public void addChatLines(ArrayList<String> chatLines) {
synchronized (this.chatLines) {
2018-07-06 13:30:00 +00:00
this.chatLines.addAll(chatLines);
this.needsUpdate = true;
}
}
2019-05-26 21:14:53 +03:00
public void addChatLine(String chatLine) {
synchronized (this.chatLines) {
2018-07-06 13:30:00 +00:00
this.chatLines.add(chatLine);
this.needsUpdate = true;
}
}
2019-05-26 21:14:53 +03:00
public ArrayList<String> getChatLines() {
2018-07-06 13:30:00 +00:00
return this.chatLines;
}
2019-05-26 21:14:53 +03:00
public int getFollowingHabboId() {
2018-07-06 13:30:00 +00:00
return this.followingHabboId;
}
2019-05-26 21:14:53 +03:00
public void startFollowingHabbo(Habbo habbo) {
2018-07-06 13:30:00 +00:00
this.followingHabboId = habbo.getHabboInfo().getId();
Emulator.getThreading().run(new BotFollowHabbo(this, habbo, habbo.getHabboInfo().getCurrentRoom()));
}
2019-05-26 21:14:53 +03:00
public void stopFollowingHabbo() {
2018-07-06 13:30:00 +00:00
this.followingHabboId = 0;
}
2019-05-26 21:14:53 +03:00
public boolean canWalk() {
2018-10-06 22:28:00 +00:00
return this.canWalk;
}
2019-05-26 21:14:53 +03:00
public void setCanWalk(boolean canWalk) {
2018-10-06 22:28:00 +00:00
this.canWalk = canWalk;
}
public void lookAt(Habbo habbo) {
this.lookAt(habbo.getRoomUnit().getCurrentLocation());
}
public void lookAt(RoomUnit roomUnit) {
this.lookAt(roomUnit.getCurrentLocation());
}
public void lookAt(RoomTile tile) {
this.roomUnit.lookAtPoint(tile);
this.roomUnit.statusUpdate(true);
}
2018-07-06 13:30:00 +00:00
}