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

306 lines
12 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;
2020-10-31 03:11:33 +01:00
import com.eu.habbo.habbohotel.items.ICycleable;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredEffect;
2018-11-17 14:28:00 +01:00
import com.eu.habbo.habbohotel.rooms.*;
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;
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.outgoing.rooms.items.FloorItemOnRollerComposer;
import gnu.trove.set.hash.THashSet;
2020-09-15 19:38:31 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-06 15:30:00 +02:00
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
2018-07-06 15:30:00 +02:00
2020-10-31 03:11:33 +01:00
public class WiredEffectMoveRotateFurni extends InteractionWiredEffect implements ICycleable {
2020-09-15 19:38:31 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(WiredEffectMoveRotateFurni.class);
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;
2020-10-31 03:11:33 +01:00
private THashSet<HabboItem> itemCooldowns;
2018-07-06 15:30:00 +02:00
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);
2020-10-31 03:11:33 +01:00
this.itemCooldowns = new THashSet<>();
2018-07-06 15:30:00 +02:00
}
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);
2020-10-31 03:11:33 +01:00
this.itemCooldowns = new THashSet<>();
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
// remove items that are no longer in the room
2020-09-15 19:38:31 +02:00
this.items.removeIf(item -> Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(item.getId()) == null);
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items) {
2020-10-31 03:11:33 +01:00
if(this.itemCooldowns.contains(item))
continue;
int newRotation = this.rotation > 0 ? this.getNewRotation(item) : item.getRotation();
RoomTile newLocation = room.getLayout().getTile(item.getX(), item.getY());
RoomTile oldLocation = room.getLayout().getTile(item.getX(), item.getY());
double oldZ = item.getZ();
2019-04-22 01:42:00 +02:00
if(this.direction > 0) {
RoomUserRotation moveDirection = this.getMovementDirection();
newLocation = room.getLayout().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)))
);
}
boolean slideAnimation = item.getRotation() == newRotation;
2019-03-18 02:22:00 +01:00
FurnitureMovementError furniMoveTest = room.furnitureFitsAt(newLocation, item, newRotation, true);
if(newLocation != null && newLocation.state != RoomTileState.INVALID && (newLocation != oldLocation || newRotation != item.getRotation()) && (furniMoveTest == FurnitureMovementError.NONE || ((furniMoveTest == FurnitureMovementError.TILE_HAS_BOTS || furniMoveTest == FurnitureMovementError.TILE_HAS_HABBOS || furniMoveTest == FurnitureMovementError.TILE_HAS_PETS) && newLocation == oldLocation))) {
if(room.furnitureFitsAt(newLocation, item, newRotation, false) == FurnitureMovementError.NONE && room.moveFurniTo(item, newLocation, newRotation, null, !slideAnimation) == FurnitureMovementError.NONE) {
2020-10-31 03:11:33 +01:00
this.itemCooldowns.add(item);
if(slideAnimation) {
room.sendComposer(new FloorItemOnRollerComposer(item, null, oldLocation, oldZ, newLocation, item.getZ(), 0, room).compose());
2019-03-18 02:22:00 +01:00
}
}
}
2019-05-26 20:14:53 +02:00
}
2018-12-22 11:39:00 +01:00
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 {
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) {
2020-10-08 19:34:14 +02:00
System.out.println(e);
2019-05-26 20:14:53 +02:00
}
for (String s : data[3].split("\r")) {
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) {
THashSet<HabboItem> items = new THashSet<>();
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();
2020-09-15 19:38:31 +02:00
if (count > Emulator.getConfig().getInt("hotel.wired.furni.selection.count", 5)) return false;
2018-07-06 15:30:00 +02:00
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
/**
* Returns a new rotation for an item based on the wired options
2020-09-15 19:38:31 +02:00
*
2020-10-08 19:34:14 +02:00
* @param item HabboItem
* @return new rotation
*/
private int getNewRotation(HabboItem item) {
int rotationToAdd = 0;
if(item.getMaximumRotations() == 2) {
return item.getRotation() == 0 ? 4 : 0;
}
else if(item.getMaximumRotations() == 1) {
return item.getRotation();
}
else if(item.getMaximumRotations() > 4) {
if (this.rotation == 1) {
return item.getRotation() == item.getMaximumRotations() - 1 ? 0 : item.getRotation() + 1;
} else if (this.rotation == 2) {
return item.getRotation() > 0 ? item.getRotation() - 1 : item.getMaximumRotations() - 1;
} else if (this.rotation == 3) { //Random rotation
THashSet<Integer> possibleRotations = new THashSet<>();
for (int i = 0; i < item.getMaximumRotations(); i++)
{
possibleRotations.add(i);
}
possibleRotations.remove(item.getRotation());
if(possibleRotations.size() > 0) {
int index = Emulator.getRandom().nextInt(possibleRotations.size());
Iterator<Integer> iter = possibleRotations.iterator();
for (int i = 0; i < index; i++) {
iter.next();
}
return iter.next();
}
}
}
else {
if (this.rotation == 1) {
return (item.getRotation() + 2) % 8;
} else if (this.rotation == 2) {
int rot = (item.getRotation() - 2) % 8;
if(rot < 0) {
rot += 8;
}
return rot;
} else if (this.rotation == 3) { //Random rotation
THashSet<Integer> possibleRotations = new THashSet<>();
for (int i = 0; i < item.getMaximumRotations(); i++)
{
possibleRotations.add(i * 2);
}
possibleRotations.remove(item.getRotation());
if(possibleRotations.size() > 0) {
int index = Emulator.getRandom().nextInt(possibleRotations.size());
Iterator<Integer> iter = possibleRotations.iterator();
for (int i = 0; i < index; i++) {
iter.next();
}
return iter.next();
}
}
}
return item.getRotation();
}
/**
* Returns the direction of movement based on the wired settings
2020-09-15 19:38:31 +02:00
*
* @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) {
2020-10-08 19:34:14 +02:00
if (Emulator.getRandom().nextInt(2) != 1) {
movemementDirection = RoomUserRotation.SOUTH;
}
} else if (this.direction == 4) {
movemementDirection = RoomUserRotation.SOUTH;
} else if (this.direction == 5) {
movemementDirection = RoomUserRotation.EAST;
} else if (this.direction == 7) {
movemementDirection = RoomUserRotation.WEST;
}
return movemementDirection;
2018-09-12 18:45:00 +02:00
}
2020-10-31 03:11:33 +01:00
@Override
public void cycle(Room room) {
this.itemCooldowns.clear();
}
}