2018-07-06 15:30:00 +02:00
|
|
|
package com.eu.habbo.habbohotel.items.interactions.wired.effects;
|
|
|
|
|
|
|
|
import com.eu.habbo.Emulator;
|
|
|
|
import com.eu.habbo.habbohotel.gameclients.GameClient;
|
|
|
|
import com.eu.habbo.habbohotel.items.Item;
|
2018-12-22 11:39:00 +01:00
|
|
|
import com.eu.habbo.habbohotel.items.interactions.InteractionRoller;
|
2018-07-06 15:30:00 +02:00
|
|
|
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredEffect;
|
2018-11-17 14:28:00 +01:00
|
|
|
import com.eu.habbo.habbohotel.rooms.*;
|
2018-09-12 18:45:00 +02:00
|
|
|
import com.eu.habbo.habbohotel.users.Habbo;
|
2018-07-06 15:30:00 +02:00
|
|
|
import com.eu.habbo.habbohotel.users.HabboItem;
|
|
|
|
import com.eu.habbo.habbohotel.wired.WiredEffectType;
|
|
|
|
import com.eu.habbo.habbohotel.wired.WiredHandler;
|
2018-09-12 18:45:00 +02:00
|
|
|
import com.eu.habbo.habbohotel.wired.WiredTriggerType;
|
2018-07-06 15:30:00 +02:00
|
|
|
import com.eu.habbo.messages.ClientMessage;
|
|
|
|
import com.eu.habbo.messages.ServerMessage;
|
|
|
|
import com.eu.habbo.messages.outgoing.rooms.items.FloorItemOnRollerComposer;
|
|
|
|
import com.eu.habbo.messages.outgoing.rooms.items.FloorItemUpdateComposer;
|
|
|
|
import gnu.trove.set.hash.THashSet;
|
2019-03-18 02:22:00 +01:00
|
|
|
import org.apache.commons.math3.util.Pair;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
2019-03-18 02:22:00 +01:00
|
|
|
import java.util.ArrayList;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public class WiredEffectMoveRotateFurni extends InteractionWiredEffect {
|
2018-07-06 15:30:00 +02:00
|
|
|
public static final WiredEffectType type = WiredEffectType.MOVE_ROTATE;
|
2019-05-26 20:14:53 +02:00
|
|
|
private final THashSet<HabboItem> items = new THashSet<>(WiredHandler.MAXIMUM_FURNI_SELECTION / 2);
|
2018-07-06 15:30:00 +02:00
|
|
|
private int direction;
|
|
|
|
private int rotation;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public WiredEffectMoveRotateFurni(ResultSet set, Item baseItem) throws SQLException {
|
2018-07-06 15:30:00 +02:00
|
|
|
super(set, baseItem);
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public WiredEffectMoveRotateFurni(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
|
2018-07-06 15:30:00 +02:00
|
|
|
super(id, userId, item, extradata, limitedStack, limitedSells);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
|
2020-02-05 21:52:39 +01:00
|
|
|
// remove items that are no longer in the room
|
|
|
|
this.items.removeIf( item -> Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null);
|
2020-02-05 19:06:30 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
THashSet<RoomTile> tilesToUpdate = new THashSet<>(Math.min(this.items.size(), 10));
|
2020-02-05 19:06:30 +01:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : this.items) {
|
2019-04-22 01:42:00 +02:00
|
|
|
//Handle rotation
|
2019-05-26 20:14:53 +02:00
|
|
|
if (this.rotation > 0) {
|
|
|
|
tilesToUpdate.addAll(room.getLayout().getTilesAt(room.getLayout().getTile(item.getX(), item.getY()), item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation()));
|
2020-02-05 21:52:39 +01:00
|
|
|
int newRotation = this.getNewRotation(item);
|
|
|
|
|
|
|
|
//Verify if rotation result in a valid position
|
|
|
|
FurnitureMovementError rotateError = room.furnitureFitsAt(room.getLayout().getTile(item.getX(), item.getY()), item, newRotation);
|
|
|
|
if (item.getRotation() != newRotation && (rotateError.equals(FurnitureMovementError.TILE_HAS_HABBOS) || rotateError.equals(FurnitureMovementError.TILE_HAS_PETS) ||
|
|
|
|
rotateError.equals(FurnitureMovementError.TILE_HAS_BOTS) || rotateError.equals(FurnitureMovementError.NONE)))
|
|
|
|
{
|
|
|
|
item.setRotation(newRotation);
|
|
|
|
if(this.direction == 0) {
|
|
|
|
tilesToUpdate.addAll(room.getLayout().getTilesAt(room.getLayout().getTile(item.getX(), item.getY()), item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation()));
|
|
|
|
room.sendComposer(new FloorItemUpdateComposer(item).compose());
|
|
|
|
for (RoomTile t : tilesToUpdate) {
|
|
|
|
room.updateHabbosAt(t.x, t.y);
|
|
|
|
room.updateBotsAt(t.x, t.y);
|
|
|
|
}
|
2020-01-22 16:22:05 +01:00
|
|
|
}
|
2019-04-22 01:42:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
//handle movement
|
2019-05-26 20:14:53 +02:00
|
|
|
if (this.direction > 0) {
|
2020-02-05 21:52:39 +01:00
|
|
|
RoomUserRotation moveDirection = this.getMovementDirection();
|
2020-02-05 19:06:30 +01:00
|
|
|
boolean validMove = false;
|
2020-02-05 21:52:39 +01:00
|
|
|
RoomLayout layout = room.getLayout();
|
|
|
|
if (layout == null) return false;
|
|
|
|
|
|
|
|
RoomTile newTile = layout.getTile(
|
|
|
|
(short) (item.getX() + ((moveDirection == RoomUserRotation.WEST || moveDirection == RoomUserRotation.NORTH_WEST || moveDirection == RoomUserRotation.SOUTH_WEST) ? -1 : (((moveDirection == RoomUserRotation.EAST || moveDirection == RoomUserRotation.SOUTH_EAST || moveDirection == RoomUserRotation.NORTH_EAST) ? 1 : 0)))),
|
|
|
|
(short) (item.getY() + ((moveDirection == RoomUserRotation.NORTH || moveDirection == RoomUserRotation.NORTH_EAST || moveDirection == RoomUserRotation.NORTH_WEST) ? 1 : ((moveDirection == RoomUserRotation.SOUTH || moveDirection == RoomUserRotation.SOUTH_EAST || moveDirection == RoomUserRotation.SOUTH_WEST) ? -1 : 0)))
|
|
|
|
);
|
|
|
|
|
|
|
|
if (newTile != null) {
|
|
|
|
boolean hasHabbos = false;
|
|
|
|
for (Habbo habbo : room.getHabbosAt(newTile)) {
|
|
|
|
hasHabbos = true;
|
|
|
|
WiredHandler.handle(WiredTriggerType.COLLISION, habbo.getRoomUnit(), room, new Object[]{item});
|
2020-02-05 19:06:30 +01:00
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
if (!hasHabbos && room.getStackHeight(newTile.x, newTile.y, true, item) != Short.MAX_VALUE) {
|
|
|
|
java.awt.Rectangle rectangle = new Rectangle(newTile.x,
|
|
|
|
newTile.y,
|
|
|
|
item.getBaseItem().getWidth(),
|
|
|
|
item.getBaseItem().getLength());
|
|
|
|
|
|
|
|
double offset = -Short.MAX_VALUE;
|
|
|
|
validMove = true;
|
|
|
|
for (short x = (short) rectangle.x; x < rectangle.x + rectangle.getWidth(); x++) {
|
|
|
|
if (!validMove) {
|
|
|
|
break;
|
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
for (short y = (short) rectangle.y; y < rectangle.y + rectangle.getHeight(); y++) {
|
|
|
|
RoomTile tile = layout.getTile(x, y);
|
|
|
|
if (tile == null || tile.state == RoomTileState.INVALID || !tile.getAllowStack()) {
|
|
|
|
validMove = false;
|
2020-02-05 18:39:16 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
THashSet<HabboItem> itemsAtNewTile = room.getItemsAt(tile);
|
|
|
|
if (item instanceof InteractionRoller && !itemsAtNewTile.isEmpty()) {
|
|
|
|
validMove = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
ArrayList<Pair<RoomTile, THashSet<HabboItem>>> tileItems = new ArrayList<>(rectangle.width * rectangle.height);
|
|
|
|
tileItems.add(Pair.create(tile, itemsAtNewTile));
|
|
|
|
if (!item.canStackAt(room, tileItems)) {
|
|
|
|
validMove = false;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-21 21:22:53 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
HabboItem i = room.getTopItemAt(x, y, item);
|
|
|
|
if (i != null && !i.getBaseItem().allowStack()) {
|
|
|
|
validMove = false;
|
|
|
|
break;
|
|
|
|
}
|
2020-02-05 19:06:30 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
offset = Math.max(room.getStackHeight(newTile.x, newTile.y, false, item) - item.getZ(), offset);
|
2020-01-21 21:22:53 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
tilesToUpdate.add(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (item.getZ() + offset > 40) {
|
|
|
|
offset = 40 - item.getZ();
|
|
|
|
}
|
2020-02-05 19:06:30 +01:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
if (validMove) {
|
|
|
|
if(this.rotation > 0) {
|
|
|
|
item.setX(newTile.x);
|
|
|
|
item.setY(newTile.y);
|
2020-02-05 22:23:27 +01:00
|
|
|
item.setZ(item.getZ() + offset);
|
2020-02-05 21:52:39 +01:00
|
|
|
room.sendComposer(new FloorItemUpdateComposer(item).compose());
|
|
|
|
for (RoomTile t : tilesToUpdate) {
|
|
|
|
room.updateHabbosAt(t.x, t.y);
|
|
|
|
room.updateBotsAt(t.x, t.y);
|
2019-03-18 02:22:00 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-05 21:52:39 +01:00
|
|
|
else {
|
2019-03-18 02:22:00 +01:00
|
|
|
room.sendComposer(new FloorItemOnRollerComposer(item, null, newTile, offset, room).compose());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
}
|
2018-12-22 11:39:00 +01:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (!tilesToUpdate.isEmpty()) {
|
2018-12-22 11:39:00 +01:00
|
|
|
room.updateTiles(tilesToUpdate);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public String getWiredData() {
|
2019-03-18 02:22:00 +01:00
|
|
|
THashSet<HabboItem> items = new THashSet<>(this.items.size() / 2);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : this.items) {
|
|
|
|
if (item.getRoomId() != this.getRoomId() || (room != null && room.getHabboItem(item.getId()) == null))
|
2018-07-06 15:30:00 +02:00
|
|
|
items.add(item);
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : items) {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.items.remove(item);
|
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
StringBuilder data = new StringBuilder(this.direction + "\t" +
|
2018-07-06 15:30:00 +02:00
|
|
|
this.rotation + "\t" +
|
2019-03-18 02:22:00 +01:00
|
|
|
this.getDelay() + "\t");
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : this.items) {
|
2019-03-18 02:22:00 +01:00
|
|
|
data.append(item.getId()).append("\r");
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
return data.toString();
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public void loadWiredData(ResultSet set, Room room) throws SQLException {
|
|
|
|
this.items.clear();
|
|
|
|
|
|
|
|
String[] data = set.getString("wired_data").split("\t");
|
|
|
|
|
|
|
|
if (data.length == 4) {
|
|
|
|
try {
|
2020-02-05 21:52:39 +01:00
|
|
|
this.direction = Integer.parseInt(data[0]);
|
|
|
|
this.rotation = Integer.parseInt(data[1]);
|
|
|
|
this.setDelay(Integer.parseInt(data[2]));
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String s : data[3].split("\r")) {
|
2020-02-05 21:52:39 +01:00
|
|
|
HabboItem item = room.getHabboItem(Integer.parseInt(s));
|
2019-05-26 20:14:53 +02:00
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
this.items.add(item);
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public void onPickUp() {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.direction = 0;
|
|
|
|
this.rotation = 0;
|
|
|
|
this.items.clear();
|
|
|
|
this.setDelay(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public WiredEffectType getType() {
|
2018-09-28 21:25:00 +02:00
|
|
|
return type;
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public void serializeWiredData(ServerMessage message, Room room) {
|
2019-03-18 02:22:00 +01:00
|
|
|
THashSet<HabboItem> items = new THashSet<>(this.items.size() / 2);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : this.items) {
|
|
|
|
if (item.getRoomId() != this.getRoomId() || Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null)
|
|
|
|
items.add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (HabboItem item : items) {
|
|
|
|
this.items.remove(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
message.appendBoolean(false);
|
|
|
|
message.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);
|
|
|
|
message.appendInt(this.items.size());
|
|
|
|
for (HabboItem item : this.items)
|
|
|
|
message.appendInt(item.getId());
|
|
|
|
message.appendInt(this.getBaseItem().getSpriteId());
|
|
|
|
message.appendInt(this.getId());
|
|
|
|
message.appendString("");
|
|
|
|
message.appendInt(2);
|
|
|
|
message.appendInt(this.direction);
|
|
|
|
message.appendInt(this.rotation);
|
|
|
|
message.appendInt(0);
|
|
|
|
message.appendInt(this.getType().code);
|
|
|
|
message.appendInt(this.getDelay());
|
|
|
|
message.appendInt(0);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public boolean saveData(ClientMessage packet, GameClient gameClient) {
|
2018-07-06 15:30:00 +02:00
|
|
|
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (room == null)
|
2018-07-06 15:30:00 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
packet.readInt();
|
|
|
|
|
|
|
|
this.direction = packet.readInt();
|
|
|
|
this.rotation = packet.readInt();
|
|
|
|
|
|
|
|
packet.readString();
|
|
|
|
|
|
|
|
int count = packet.readInt();
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
this.items.clear();
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
this.items.add(room.getHabboItem(packet.readInt()));
|
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
this.setDelay(packet.readInt());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-12 18:45:00 +02:00
|
|
|
|
2020-02-05 21:52:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new rotation for an item based on the wired options
|
|
|
|
* @param item
|
|
|
|
* @return new rotation
|
|
|
|
*/
|
|
|
|
private int getNewRotation(HabboItem item) {
|
|
|
|
int rotationToAdd = 0;
|
|
|
|
if (this.rotation == 1) {
|
|
|
|
rotationToAdd = 2;
|
|
|
|
} else if (this.rotation == 2) {
|
|
|
|
rotationToAdd = 6;
|
|
|
|
}
|
|
|
|
//Random rotation
|
|
|
|
else if (this.rotation == 3) {
|
|
|
|
if (Emulator.getRandom().nextInt(2) == 1) {
|
|
|
|
rotationToAdd = 2;
|
|
|
|
} else {
|
|
|
|
rotationToAdd = 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((item.getRotation() + rotationToAdd) % 8) % (item.getBaseItem().getWidth() > 1 || item.getBaseItem().getLength() > 1 ? 4 : 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the direction of movement based on the wired settings
|
|
|
|
* @return direction
|
|
|
|
*/
|
|
|
|
private RoomUserRotation getMovementDirection() {
|
|
|
|
RoomUserRotation movemementDirection = RoomUserRotation.NORTH;
|
|
|
|
if (this.direction == 1) {
|
|
|
|
movemementDirection = RoomUserRotation.values()[Emulator.getRandom().nextInt(RoomUserRotation.values().length / 2) * 2];
|
|
|
|
} else if (this.direction == 2) {
|
|
|
|
if (Emulator.getRandom().nextInt(2) == 1) {
|
|
|
|
movemementDirection = RoomUserRotation.EAST;
|
|
|
|
} else {
|
|
|
|
movemementDirection = RoomUserRotation.WEST;
|
|
|
|
}
|
|
|
|
} else if (this.direction == 3) {
|
|
|
|
if (Emulator.getRandom().nextInt(2) == 1) {
|
|
|
|
movemementDirection = RoomUserRotation.NORTH;
|
|
|
|
} else {
|
|
|
|
movemementDirection = RoomUserRotation.SOUTH;
|
|
|
|
}
|
|
|
|
} else if (this.direction == 4) {
|
|
|
|
movemementDirection = RoomUserRotation.SOUTH;
|
|
|
|
} else if (this.direction == 5) {
|
|
|
|
movemementDirection = RoomUserRotation.EAST;
|
|
|
|
} else if (this.direction == 6) {
|
|
|
|
movemementDirection = RoomUserRotation.NORTH;
|
|
|
|
} else if (this.direction == 7) {
|
|
|
|
movemementDirection = RoomUserRotation.WEST;
|
|
|
|
}
|
|
|
|
return movemementDirection;
|
2018-09-12 18:45:00 +02:00
|
|
|
}
|
2020-02-05 21:52:39 +01:00
|
|
|
}
|