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; import com.eu.habbo.habbohotel.rooms.*; import com.eu.habbo.habbohotel.users.HabboItem; import com.eu.habbo.habbohotel.wired.WiredEffectType; import com.eu.habbo.habbohotel.wired.WiredHandler; import com.eu.habbo.habbohotel.wired.WiredTriggerType; import com.eu.habbo.messages.ClientMessage; import com.eu.habbo.messages.ServerMessage; import com.eu.habbo.messages.incoming.wired.WiredSaveException; import com.eu.habbo.messages.outgoing.rooms.items.FloorItemOnRollerComposer; import gnu.trove.map.hash.THashMap; import gnu.trove.set.hash.THashSet; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; public class WiredEffectChangeFurniDirection extends InteractionWiredEffect { public static final int ACTION_WAIT = 0; 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; public static final WiredEffectType type = WiredEffectType.MOVE_DIRECTION; private final THashMap items = new THashMap<>(0); private RoomUserRotation startRotation = RoomUserRotation.NORTH; private int rotateAction = 0; public WiredEffectChangeFurniDirection(ResultSet set, Item baseItem) throws SQLException { super(set, baseItem); } public WiredEffectChangeFurniDirection(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) { super(id, userId, item, extradata, limitedStack, limitedSells); } @Override public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) { THashSet items = new THashSet<>(); for (HabboItem item : this.items.keySet()) { if (Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null) items.add(item); } for (HabboItem item : items) { this.items.remove(item); } if (this.items.isEmpty()) return false; for (Map.Entry entry : this.items.entrySet()) { RoomTile targetTile = room.getLayout().getTileInFront(room.getLayout().getTile(entry.getKey().getX(), entry.getKey().getY()), entry.getValue().getValue()); int count = 1; while ((targetTile == null || !targetTile.getAllowStack() || targetTile.state == RoomTileState.INVALID) && count < 8) { entry.setValue(this.nextRotation(entry.getValue())); targetTile = room.getLayout().getTileInFront(room.getLayout().getTile(entry.getKey().getX(), entry.getKey().getY()), entry.getValue().getValue()); count++; } } for (Map.Entry entry : this.items.entrySet()) { RoomTile targetTile = room.getLayout().getTileInFront(room.getLayout().getTile(entry.getKey().getX(), entry.getKey().getY()), entry.getValue().getValue()); if (targetTile != null && targetTile.state != RoomTileState.INVALID) { boolean hasRoomUnits = false; for (RoomUnit _roomUnit : room.getRoomUnitsAt(targetTile)) { hasRoomUnits = true; Emulator.getThreading().run(() -> WiredHandler.handle(WiredTriggerType.COLLISION, _roomUnit, room, new Object[]{entry.getKey()})); } if (!hasRoomUnits && targetTile.getAllowStack() && targetTile.state != RoomTileState.INVALID) { THashSet refreshTiles = room.getLayout().getTilesAt(room.getLayout().getTile(entry.getKey().getX(), entry.getKey().getY()), entry.getKey().getBaseItem().getWidth(), entry.getKey().getBaseItem().getLength(), entry.getKey().getRotation()); room.sendComposer(new FloorItemOnRollerComposer(entry.getKey(), null, targetTile, targetTile.getStackHeight() - entry.getKey().getZ(), room).compose()); room.getLayout().getTilesAt(room.getLayout().getTile(entry.getKey().getX(), entry.getKey().getY()), entry.getKey().getBaseItem().getWidth(), entry.getKey().getBaseItem().getLength(), entry.getKey().getRotation()); room.updateTiles(refreshTiles); } } } return false; } @Override public String getWiredData() { StringBuilder data = new StringBuilder(this.getDelay() + "\t" + this.startRotation.getValue() + "\t" + this.rotateAction + "\t" + this.items.size()); for (Map.Entry entry : this.items.entrySet()) { data.append("\t").append(entry.getKey().getId()).append(":").append(entry.getValue().getValue()); } return data.toString(); } @Override public void loadWiredData(ResultSet set, Room room) throws SQLException { String[] data = set.getString("wired_data").split("\t"); if (data.length >= 4) { this.setDelay(Integer.parseInt(data[0])); this.startRotation = RoomUserRotation.fromValue(Integer.parseInt(data[1])); this.rotateAction = Integer.parseInt(data[2]); int itemCount = Integer.parseInt(data[3]); if (itemCount > 0) { for (int i = 4; i < data.length; i++) { String[] subData = data[i].split(":"); if (subData.length == 2) { HabboItem item = room.getHabboItem(Integer.parseInt(subData[0])); if (item != null) { this.items.put(item, RoomUserRotation.fromValue(Integer.parseInt(subData[1]))); } } } } } } @Override public void onPickUp() { this.setDelay(0); this.items.clear(); this.rotateAction = 0; this.startRotation = RoomUserRotation.NORTH; } @Override public WiredEffectType getType() { return type; } @Override public void serializeWiredData(ServerMessage message, Room room) { message.appendBoolean(false); message.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION); message.appendInt(this.items.size()); for (Map.Entry item : this.items.entrySet()) { message.appendInt(item.getKey().getId()); } message.appendInt(this.getBaseItem().getSpriteId()); message.appendInt(this.getId()); message.appendString(""); message.appendInt(2); message.appendInt(this.startRotation.getValue()); message.appendInt(this.rotateAction); message.appendInt(0); message.appendInt(this.getType().code); message.appendInt(this.getDelay()); message.appendInt(0); } @Override public boolean saveData(ClientMessage packet, GameClient gameClient) throws WiredSaveException { packet.readInt(); int startRotationInt = packet.readInt(); if(startRotationInt < 0 || startRotationInt > 7 || (startRotationInt % 2) != 0) { throw new WiredSaveException("Direction is invalid"); } RoomUserRotation startRotation = RoomUserRotation.fromValue(startRotationInt); int rotateAction = packet.readInt(); if(rotateAction < 0 || rotateAction > 6) { throw new WiredSaveException("Blocked action is invalid"); } packet.readString(); int itemsCount = packet.readInt(); if(itemsCount > Emulator.getConfig().getInt("hotel.wired.furni.selection.count")) { throw new WiredSaveException("Too many furni selected"); } THashMap newItems = new THashMap<>(); 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)); newItems.put(it, startRotation); } 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); this.startRotation = startRotation; this.rotateAction = rotateAction; this.setDelay(delay); return true; } private RoomUserRotation nextRotation(RoomUserRotation currentRotation) { switch (this.rotateAction) { 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 protected long requiredCooldown() { return 495; } }