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;
|
|
|
|
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredEffect;
|
2018-09-12 18:45:00 +02:00
|
|
|
import com.eu.habbo.habbohotel.rooms.*;
|
2018-07-06 15:30:00 +02:00
|
|
|
import com.eu.habbo.habbohotel.users.HabboItem;
|
2020-10-15 03:47:34 +02:00
|
|
|
import com.eu.habbo.habbohotel.wired.*;
|
2018-07-06 15:30:00 +02:00
|
|
|
import com.eu.habbo.messages.ClientMessage;
|
|
|
|
import com.eu.habbo.messages.ServerMessage;
|
2020-10-12 11:18:52 +02:00
|
|
|
import com.eu.habbo.messages.incoming.wired.WiredSaveException;
|
2018-09-12 18:45:00 +02:00
|
|
|
import com.eu.habbo.messages.outgoing.rooms.items.FloorItemOnRollerComposer;
|
2018-07-06 15:30:00 +02:00
|
|
|
import gnu.trove.map.hash.THashMap;
|
2018-09-12 18:45:00 +02:00
|
|
|
import gnu.trove.set.hash.THashSet;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public class WiredEffectChangeFurniDirection extends InteractionWiredEffect {
|
2018-09-12 18:45:00 +02:00
|
|
|
public static final int ACTION_WAIT = 0;
|
2019-05-26 20:14:53 +02:00
|
|
|
public static final int ACTION_TURN_RIGHT_45 = 1;
|
|
|
|
public static final int ACTION_TURN_RIGHT_90 = 2;
|
|
|
|
public static final int ACTION_TURN_LEFT_45 = 3;
|
|
|
|
public static final int ACTION_TURN_LEFT_90 = 4;
|
|
|
|
public static final int ACTION_TURN_BACK = 5;
|
|
|
|
public static final int ACTION_TURN_RANDOM = 6;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2018-09-12 18:45:00 +02:00
|
|
|
public static final WiredEffectType type = WiredEffectType.MOVE_DIRECTION;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
private final THashMap<HabboItem, WiredChangeDirectionSetting> items = new THashMap<>(0);
|
2018-09-12 18:45:00 +02:00
|
|
|
private RoomUserRotation startRotation = RoomUserRotation.NORTH;
|
2020-10-15 03:47:34 +02:00
|
|
|
private int blockedAction = 0;
|
2019-05-26 20:14:53 +02:00
|
|
|
|
|
|
|
public WiredEffectChangeFurniDirection(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 WiredEffectChangeFurniDirection(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) {
|
2018-09-28 21:25:00 +02:00
|
|
|
THashSet<HabboItem> items = new THashSet<>();
|
2018-09-12 18:45:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : this.items.keySet()) {
|
2018-09-12 18:45:00 +02:00
|
|
|
if (Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null)
|
|
|
|
items.add(item);
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
for (HabboItem item : items) {
|
2018-09-12 18:45:00 +02:00
|
|
|
this.items.remove(item);
|
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
if (this.items.isEmpty()) return false;
|
2020-10-12 11:18:52 +02:00
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
for (Map.Entry<HabboItem, WiredChangeDirectionSetting> entry : this.items.entrySet()) {
|
|
|
|
HabboItem item = entry.getKey();
|
|
|
|
RoomTile targetTile = room.getLayout().getTileInFront(room.getLayout().getTile(item.getX(), item.getY()), entry.getValue().direction.getValue());
|
2018-09-12 18:45:00 +02:00
|
|
|
|
|
|
|
int count = 1;
|
2020-10-15 03:47:34 +02:00
|
|
|
while ((targetTile == null || targetTile.state == RoomTileState.INVALID || room.furnitureFitsAt(targetTile, item, item.getRotation(), false) != FurnitureMovementError.NONE) && count < 8) {
|
|
|
|
entry.getValue().direction = this.nextRotation(entry.getValue().direction);
|
|
|
|
|
|
|
|
RoomTile tile = room.getLayout().getTileInFront(room.getLayout().getTile(item.getX(), item.getY()), entry.getValue().direction.getValue());
|
|
|
|
if (tile != null && tile.state != RoomTileState.INVALID) {
|
|
|
|
targetTile = tile;
|
|
|
|
}
|
|
|
|
|
2018-09-12 18:45:00 +02:00
|
|
|
count++;
|
|
|
|
}
|
2020-10-12 11:18:52 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
for (Map.Entry<HabboItem, WiredChangeDirectionSetting> entry : this.items.entrySet()) {
|
|
|
|
HabboItem item = entry.getKey();
|
|
|
|
int newDirection = entry.getValue().direction.getValue();
|
|
|
|
|
|
|
|
RoomTile targetTile = room.getLayout().getTileInFront(room.getLayout().getTile(item.getX(), item.getY()), newDirection);
|
2018-09-12 18:45:00 +02:00
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
if(item.getRotation() != entry.getValue().rotation) {
|
|
|
|
if(room.furnitureFitsAt(targetTile, item, entry.getValue().rotation, false) != FurnitureMovementError.NONE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
room.moveFurniTo(entry.getKey(), targetTile, entry.getValue().rotation, null, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetTile != null && targetTile.state != RoomTileState.INVALID && room.furnitureFitsAt(targetTile, item, item.getRotation(), false) == FurnitureMovementError.NONE) {
|
2020-10-08 19:34:14 +02:00
|
|
|
boolean hasRoomUnits = false;
|
2020-10-15 03:47:34 +02:00
|
|
|
THashSet<RoomTile> newOccupiedTiles = room.getLayout().getTilesAt(targetTile, item.getBaseItem().getWidth(), item.getBaseItem().getLength(), item.getRotation());
|
|
|
|
for(RoomTile tile : newOccupiedTiles) {
|
|
|
|
for (RoomUnit _roomUnit : room.getRoomUnits(tile)) {
|
|
|
|
hasRoomUnits = true;
|
|
|
|
if(_roomUnit.getCurrentLocation() == targetTile) {
|
|
|
|
Emulator.getThreading().run(() -> WiredHandler.handle(WiredTriggerType.COLLISION, _roomUnit, room, new Object[]{entry.getKey()}));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 18:45:00 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
if (!hasRoomUnits) {
|
|
|
|
RoomTile oldLocation = room.getLayout().getTile(entry.getKey().getX(), entry.getKey().getY());
|
|
|
|
double oldZ = entry.getKey().getZ();
|
|
|
|
if(room.moveFurniTo(entry.getKey(), targetTile, item.getRotation(), null, false) == FurnitureMovementError.NONE) {
|
|
|
|
room.sendComposer(new FloorItemOnRollerComposer(entry.getKey(), null, oldLocation, oldZ, targetTile, entry.getKey().getZ(), 0, room).compose());
|
|
|
|
}
|
2018-09-12 18:45:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public String getWiredData() {
|
2020-10-15 03:47:34 +02:00
|
|
|
StringBuilder data = new StringBuilder(this.getDelay() + "\t" + this.startRotation.getValue() + "\t" + this.blockedAction + "\t" + this.items.size());
|
2018-09-12 18:45:00 +02:00
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
for (Map.Entry<HabboItem, WiredChangeDirectionSetting> entry : this.items.entrySet()) {
|
|
|
|
data.append("\t").append(entry.getKey().getId()).append(":").append(entry.getValue().direction.getValue()).append(":").append(entry.getValue().rotation);
|
2018-09-12 18:45: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 {
|
2018-09-12 18:45:00 +02:00
|
|
|
String[] data = set.getString("wired_data").split("\t");
|
|
|
|
|
2020-10-12 11:18:52 +02:00
|
|
|
if (data.length >= 4) {
|
2020-10-08 19:34:14 +02:00
|
|
|
this.setDelay(Integer.parseInt(data[0]));
|
2020-10-12 11:18:52 +02:00
|
|
|
this.startRotation = RoomUserRotation.fromValue(Integer.parseInt(data[1]));
|
2020-10-15 03:47:34 +02:00
|
|
|
this.blockedAction = Integer.parseInt(data[2]);
|
2018-09-12 18:45:00 +02:00
|
|
|
|
2020-10-12 11:18:52 +02:00
|
|
|
int itemCount = Integer.parseInt(data[3]);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (itemCount > 0) {
|
2020-10-12 11:18:52 +02:00
|
|
|
for (int i = 4; i < data.length; i++) {
|
2018-09-12 18:45:00 +02:00
|
|
|
String[] subData = data[i].split(":");
|
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
if (subData.length >= 2) {
|
2020-10-08 19:34:14 +02:00
|
|
|
HabboItem item = room.getHabboItem(Integer.parseInt(subData[0]));
|
2018-09-12 18:45:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (item != null) {
|
2020-10-15 03:47:34 +02:00
|
|
|
int rotation = item.getRotation();
|
|
|
|
|
|
|
|
if(subData.length > 2) {
|
|
|
|
rotation = Integer.parseInt(subData[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.items.put(item, new WiredChangeDirectionSetting(item.getId(), rotation, RoomUserRotation.fromValue(Integer.parseInt(subData[1]))));
|
2018-09-12 18:45:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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.setDelay(0);
|
|
|
|
this.items.clear();
|
2020-10-15 03:47:34 +02:00
|
|
|
this.blockedAction = 0;
|
2018-09-12 18:45:00 +02:00
|
|
|
this.startRotation = RoomUserRotation.NORTH;
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
2018-07-06 15:30:00 +02:00
|
|
|
message.appendBoolean(false);
|
|
|
|
message.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);
|
|
|
|
message.appendInt(this.items.size());
|
2020-10-15 03:47:34 +02:00
|
|
|
for (Map.Entry<HabboItem, WiredChangeDirectionSetting> item : this.items.entrySet()) {
|
2018-07-06 15:30:00 +02:00
|
|
|
message.appendInt(item.getKey().getId());
|
|
|
|
}
|
|
|
|
message.appendInt(this.getBaseItem().getSpriteId());
|
|
|
|
message.appendInt(this.getId());
|
|
|
|
message.appendString("");
|
2018-09-12 18:45:00 +02:00
|
|
|
message.appendInt(2);
|
|
|
|
message.appendInt(this.startRotation.getValue());
|
2020-10-15 03:47:34 +02:00
|
|
|
message.appendInt(this.blockedAction);
|
2018-07-06 15:30:00 +02:00
|
|
|
message.appendInt(0);
|
|
|
|
message.appendInt(this.getType().code);
|
|
|
|
message.appendInt(this.getDelay());
|
|
|
|
message.appendInt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-12 11:18:52 +02:00
|
|
|
public boolean saveData(ClientMessage packet, GameClient gameClient) throws WiredSaveException {
|
2018-09-12 18:45:00 +02:00
|
|
|
packet.readInt();
|
2020-10-15 03:47:34 +02:00
|
|
|
int startDirectionInt = packet.readInt();
|
2020-10-12 11:18:52 +02:00
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
if(startDirectionInt < 0 || startDirectionInt > 7 || (startDirectionInt % 2) != 0) {
|
|
|
|
throw new WiredSaveException("Start direction is invalid");
|
2020-10-12 11:18:52 +02:00
|
|
|
}
|
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
RoomUserRotation startDirection = RoomUserRotation.fromValue(startDirectionInt);
|
2020-10-12 11:18:52 +02:00
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
int blockedActionInt = packet.readInt();
|
2020-10-12 11:18:52 +02:00
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
if(blockedActionInt < 0 || blockedActionInt > 6) {
|
2020-10-12 11:18:52 +02:00
|
|
|
throw new WiredSaveException("Blocked action is invalid");
|
|
|
|
}
|
|
|
|
|
2018-09-12 18:45:00 +02:00
|
|
|
packet.readString();
|
|
|
|
|
2020-10-12 11:18:52 +02:00
|
|
|
int itemsCount = packet.readInt();
|
2020-09-15 19:38:31 +02:00
|
|
|
|
2020-10-12 11:18:52 +02:00
|
|
|
if(itemsCount > Emulator.getConfig().getInt("hotel.wired.furni.selection.count")) {
|
|
|
|
throw new WiredSaveException("Too many furni selected");
|
|
|
|
}
|
2020-09-15 19:38:31 +02:00
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
THashMap<HabboItem, WiredChangeDirectionSetting> newItems = new THashMap<>();
|
2018-09-12 18:45:00 +02:00
|
|
|
|
2020-10-12 11:18:52 +02:00
|
|
|
for (int i = 0; i < itemsCount; i++) {
|
|
|
|
int itemId = packet.readInt();
|
|
|
|
HabboItem it = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(itemId);
|
|
|
|
|
|
|
|
if(it == null)
|
|
|
|
throw new WiredSaveException(String.format("Item %s not found", itemId));
|
|
|
|
|
2020-10-15 03:47:34 +02:00
|
|
|
newItems.put(it, new WiredChangeDirectionSetting(it.getId(), it.getRotation(), startDirection));
|
2018-09-12 18:45:00 +02:00
|
|
|
}
|
2020-10-12 11:18:52 +02:00
|
|
|
|
|
|
|
int delay = packet.readInt();
|
|
|
|
|
|
|
|
if(delay > Emulator.getConfig().getInt("hotel.wired.max_delay", 20))
|
|
|
|
throw new WiredSaveException("Delay too long");
|
|
|
|
|
|
|
|
this.items.clear();
|
|
|
|
this.items.putAll(newItems);
|
2020-10-15 03:47:34 +02:00
|
|
|
this.startRotation = startDirection;
|
|
|
|
this.blockedAction = blockedActionInt;
|
2020-10-12 11:18:52 +02:00
|
|
|
this.setDelay(delay);
|
|
|
|
|
2018-09-12 18:45:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
private RoomUserRotation nextRotation(RoomUserRotation currentRotation) {
|
2020-10-15 03:47:34 +02:00
|
|
|
switch (this.blockedAction) {
|
2018-09-12 18:45:00 +02:00
|
|
|
case ACTION_TURN_BACK:
|
|
|
|
return RoomUserRotation.fromValue(currentRotation.getValue() + 4);
|
|
|
|
case ACTION_TURN_LEFT_45:
|
|
|
|
return RoomUserRotation.counterClockwise(currentRotation);
|
|
|
|
case ACTION_TURN_LEFT_90:
|
|
|
|
return RoomUserRotation.counterClockwise(RoomUserRotation.counterClockwise(currentRotation));
|
|
|
|
case ACTION_TURN_RIGHT_45:
|
|
|
|
return RoomUserRotation.clockwise(currentRotation);
|
|
|
|
case ACTION_TURN_RIGHT_90:
|
|
|
|
return RoomUserRotation.clockwise(RoomUserRotation.clockwise(currentRotation));
|
|
|
|
case ACTION_TURN_RANDOM:
|
|
|
|
return RoomUserRotation.fromValue(Emulator.getRandom().nextInt(8));
|
|
|
|
case ACTION_WAIT:
|
|
|
|
default:
|
|
|
|
return currentRotation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
protected long requiredCooldown() {
|
2018-09-12 18:45:00 +02:00
|
|
|
return 495;
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
}
|