Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectChangeFurniDirection.java

214 lines
8.6 KiB
Java
Raw Normal View History

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.*;
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;
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
2018-09-28 21:25:00 +02:00
private final THashMap<HabboItem, RoomUserRotation> items = new THashMap<>(0);
2018-09-12 18:45:00 +02:00
private RoomUserRotation startRotation = RoomUserRotation.NORTH;
private int rotateAction = 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;
2019-05-26 20:14:53 +02:00
for (Map.Entry<HabboItem, RoomUserRotation> entry : this.items.entrySet()) {
2018-09-12 18:45:00 +02:00
RoomUserRotation currentRotation = entry.getValue();
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 || targetTile.state == RoomTileState.BLOCKED) && count < 8) {
2019-03-18 02:22:00 +01:00
entry.setValue(this.nextRotation(entry.getValue()));
2018-09-12 18:45:00 +02:00
targetTile = room.getLayout().getTileInFront(room.getLayout().getTile(entry.getKey().getX(), entry.getKey().getY()), entry.getValue().getValue());
count++;
}
if (targetTile != null && targetTile.state != RoomTileState.INVALID) {
2020-10-08 19:34:14 +02:00
boolean hasRoomUnits = false;
for (RoomUnit _roomUnit : room.getHabbosAndBotsAt(targetTile)) {
hasRoomUnits = true;
Emulator.getThreading().run(() -> WiredHandler.handle(WiredTriggerType.COLLISION, _roomUnit, room, new Object[]{entry.getKey()}));
2018-09-12 18:45:00 +02:00
}
2020-10-08 19:34:14 +02:00
if (!hasRoomUnits) {
2018-09-12 18:45:00 +02:00
THashSet<RoomTile> 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);
}
}
}
2018-07-06 15:30:00 +02:00
return false;
}
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
StringBuilder data = new StringBuilder(this.getDelay() + this.startRotation.getValue() + "\t" + this.rotateAction + "\t" + this.items.size());
2018-09-12 18:45:00 +02:00
2019-05-26 20:14:53 +02:00
for (Map.Entry<HabboItem, RoomUserRotation> entry : this.items.entrySet()) {
2019-03-18 02:22:00 +01:00
data.append("\t").append(entry.getKey().getId()).append(":").append(entry.getValue().getValue());
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");
if (data.length >= 1) {
2020-10-08 19:34:14 +02:00
this.setDelay(Integer.parseInt(data[0]));
}
2019-05-26 20:14:53 +02:00
if (data.length >= 3) {
2020-10-08 19:34:14 +02:00
this.startRotation = RoomUserRotation.fromValue(Integer.parseInt(data[0]));
this.rotateAction = Integer.parseInt(data[1]);
2018-09-12 18:45:00 +02:00
2020-10-08 19:34:14 +02:00
int itemCount = Integer.parseInt(data[2]);
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (itemCount > 0) {
for (int i = 3; i < data.length; i++) {
2018-09-12 18:45:00 +02:00
String[] subData = data[i].split(":");
2019-05-26 20:14:53 +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-08 19:34:14 +02:00
this.items.put(item, 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();
2018-09-12 18:45:00 +02:00
this.rotateAction = 0;
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());
2019-05-26 20:14:53 +02:00
for (Map.Entry<HabboItem, RoomUserRotation> 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());
message.appendInt(this.rotateAction);
2018-07-06 15:30:00 +02:00
message.appendInt(0);
message.appendInt(this.getType().code);
message.appendInt(this.getDelay());
message.appendInt(0);
}
@Override
2019-05-26 20:14:53 +02:00
public boolean saveData(ClientMessage packet, GameClient gameClient) {
2018-09-12 18:45:00 +02:00
packet.readInt();
this.startRotation = RoomUserRotation.fromValue(packet.readInt());
this.rotateAction = packet.readInt();
packet.readString();
2020-09-15 19:38:31 +02:00
int count = packet.readInt();
if (count > Emulator.getConfig().getInt("hotel.wired.furni.selection.count")) return false;
this.items.clear();
for (int i = 0; i < count; i++) {
2018-09-12 18:45:00 +02:00
HabboItem item = gameClient.getHabbo().getHabboInfo().getCurrentRoom().getHabboItem(packet.readInt());
2019-05-26 20:14:53 +02:00
if (item != null) {
2018-09-12 18:45:00 +02:00
this.items.put(item, this.startRotation);
}
}
this.setDelay(packet.readInt());
2018-09-12 18:45:00 +02:00
return true;
}
2019-05-26 20:14:53 +02:00
private RoomUserRotation nextRotation(RoomUserRotation currentRotation) {
switch (this.rotateAction) {
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
}
}