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

253 lines
9.3 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.InteractionMultiHeight;
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;
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.habbohotel.wired.WiredMatchFurniSetting;
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.incoming.wired.WiredSaveException;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.messages.outgoing.rooms.items.FloorItemOnRollerComposer;
import gnu.trove.set.hash.THashSet;
2020-05-04 22:24:09 +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.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public class WiredEffectMatchFurni extends InteractionWiredEffect {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(WiredEffectMatchFurni.class);
2018-07-06 15:30:00 +02:00
private static final WiredEffectType type = WiredEffectType.MATCH_SSHOT;
2019-05-26 20:14:53 +02:00
public boolean checkForWiredResetPermission = true;
2018-07-06 15:30:00 +02:00
private THashSet<WiredMatchFurniSetting> settings;
private boolean state = false;
private boolean direction = false;
private boolean position = false;
2019-05-26 20:14:53 +02:00
public WiredEffectMatchFurni(ResultSet set, Item baseItem) throws SQLException {
2018-07-06 15:30:00 +02:00
super(set, baseItem);
2018-09-28 21:25:00 +02:00
this.settings = new THashSet<>(0);
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public WiredEffectMatchFurni(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);
2018-09-28 21:25:00 +02:00
this.settings = new THashSet<>(0);
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) {
2018-11-17 14:28:00 +01:00
if(this.settings.isEmpty())
return true;
2019-05-26 20:14:53 +02:00
for (WiredMatchFurniSetting setting : this.settings) {
2018-07-06 15:30:00 +02:00
HabboItem item = room.getHabboItem(setting.itemId);
2019-05-26 20:14:53 +02:00
if (item != null) {
if (this.state && (this.checkForWiredResetPermission && item.allowWiredResetState())) {
if (!setting.state.equals(" ")) {
2018-07-06 15:30:00 +02:00
item.setExtradata(setting.state);
room.updateItemState(item);
2018-07-06 15:30:00 +02:00
}
}
RoomTile oldLocation = room.getLayout().getTile(item.getX(), item.getY());
double oldZ = item.getZ();
2018-12-22 11:39:00 +01:00
if(this.direction && !this.position) {
if(item.getRotation() != setting.rotation && room.furnitureFitsAt(oldLocation, item, setting.rotation, false) == FurnitureMovementError.NONE) {
room.moveFurniTo(item, oldLocation, setting.rotation, null, true);
}
}
else if(this.position) {
boolean slideAnimation = !this.direction || item.getRotation() == setting.rotation;
RoomTile newLocation = room.getLayout().getTile((short) setting.x, (short) setting.y);
if(newLocation != null && newLocation.state != RoomTileState.INVALID && room.furnitureFitsAt(newLocation, item, this.direction ? setting.rotation : item.getRotation(), true) == FurnitureMovementError.NONE) {
if(room.moveFurniTo(item, newLocation, this.direction ? setting.rotation : item.getRotation(), null, !slideAnimation) == FurnitureMovementError.NONE) {
if(slideAnimation) {
room.sendComposer(new FloorItemOnRollerComposer(item, null, oldLocation, oldZ, newLocation, item.getZ(), 0, room).compose());
2018-12-22 11:39:00 +01:00
}
2018-10-07 00:28:00 +02:00
}
2018-09-12 18:45:00 +02:00
}
2018-07-06 15:30:00 +02:00
}
}
}
return true;
}
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
2018-07-06 15:30:00 +02:00
this.refresh();
2019-03-18 02:22:00 +01:00
StringBuilder data = new StringBuilder(this.settings.size() + ":");
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (this.settings.isEmpty()) {
2019-03-18 02:22:00 +01:00
data.append(";");
2019-05-26 20:14:53 +02:00
} else {
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 (WiredMatchFurniSetting item : this.settings) {
2019-03-18 02:22:00 +01:00
HabboItem i;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (room != null) {
2018-07-06 15:30:00 +02:00
i = room.getHabboItem(item.itemId);
2019-05-26 20:14:53 +02:00
if (i != null) {
2019-03-18 02:22:00 +01:00
data.append(item.toString(this.checkForWiredResetPermission && i.allowWiredResetState())).append(";");
2018-07-06 15:30:00 +02:00
}
}
}
}
2019-03-18 02:22:00 +01:00
data.append(":").append(this.state ? 1 : 0).append(":").append(this.direction ? 1 : 0).append(":").append(this.position ? 1 : 0).append(":").append(this.getDelay());
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 {
2018-07-06 15:30:00 +02:00
String[] data = set.getString("wired_data").split(":");
int itemCount = Integer.parseInt(data[0]);
2018-07-06 15:30:00 +02:00
String[] items = data[1].split(Pattern.quote(";"));
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (int i = 0; i < items.length; i++) {
try {
2018-07-06 15:30:00 +02:00
String[] stuff = items[i].split(Pattern.quote("-"));
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (stuff.length >= 5) {
this.settings.add(new WiredMatchFurniSetting(Integer.parseInt(stuff[0]), stuff[1], Integer.parseInt(stuff[2]), Integer.parseInt(stuff[3]), Integer.parseInt(stuff[4])));
2018-12-22 11:39:00 +01:00
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught exception", e);
2018-07-06 15:30:00 +02:00
}
}
this.state = data[2].equals("1");
this.direction = data[3].equals("1");
this.position = data[4].equals("1");
this.setDelay(Integer.parseInt(data[5]));
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.settings.clear();
this.state = false;
this.direction = false;
this.position = false;
this.setDelay(0);
}
@Override
2019-05-26 20:14:53 +02:00
public WiredEffectType getType() {
2018-07-06 15:30:00 +02:00
return type;
}
@Override
2019-05-26 20:14:53 +02:00
public void serializeWiredData(ServerMessage message, Room room) {
2018-07-06 15:30:00 +02:00
this.refresh();
message.appendBoolean(false);
message.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);
message.appendInt(this.settings.size());
2019-05-26 20:14:53 +02:00
for (WiredMatchFurniSetting item : this.settings)
2018-07-06 15:30:00 +02:00
message.appendInt(item.itemId);
message.appendInt(this.getBaseItem().getSpriteId());
message.appendInt(this.getId());
message.appendString("");
message.appendInt(3);
2019-05-26 20:14:53 +02:00
message.appendInt(this.state ? 1 : 0);
message.appendInt(this.direction ? 1 : 0);
message.appendInt(this.position ? 1 : 0);
2018-07-06 15:30:00 +02:00
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 {
2018-07-06 15:30:00 +02:00
packet.readInt();
boolean setState = packet.readInt() == 1;
boolean setDirection = packet.readInt() == 1;
boolean setPosition = packet.readInt() == 1;
2018-07-06 15:30:00 +02:00
packet.readString();
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
2019-05-26 20:14:53 +02:00
if (room == null)
throw new WiredSaveException("Trying to save wired in unloaded room");
2018-07-06 15:30:00 +02:00
int itemsCount = packet.readInt();
2020-09-15 19:38:31 +02:00
if(itemsCount > Emulator.getConfig().getInt("hotel.wired.furni.selection.count")) {
throw new WiredSaveException("Too many furni selected");
}
2018-07-06 15:30:00 +02:00
List<WiredMatchFurniSetting> newSettings = new ArrayList<>();
for (int i = 0; i < itemsCount; i++) {
2018-07-06 15:30:00 +02:00
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));
2018-07-06 15:30:00 +02:00
newSettings.add(new WiredMatchFurniSetting(it.getId(), this.checkForWiredResetPermission && it.allowWiredResetState() ? it.getExtradata() : " ", it.getRotation(), it.getX(), it.getY()));
2018-07-06 15:30:00 +02:00
}
int delay = packet.readInt();
if(delay > Emulator.getConfig().getInt("hotel.wired.max_delay", 20))
throw new WiredSaveException("Delay too long");
this.state = setState;
this.direction = setDirection;
this.position = setPosition;
this.settings.clear();
this.settings.addAll(newSettings);
2020-10-12 14:16:16 +02:00
this.setDelay(delay);
2018-07-06 15:30:00 +02:00
return true;
}
2019-05-26 20:14:53 +02:00
private void refresh() {
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 && room.isLoaded()) {
2018-09-28 21:25:00 +02:00
THashSet<WiredMatchFurniSetting> remove = new THashSet<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (WiredMatchFurniSetting setting : this.settings) {
2018-07-06 15:30:00 +02:00
HabboItem item = room.getHabboItem(setting.itemId);
2019-05-26 20:14:53 +02:00
if (item == null) {
2018-07-06 15:30:00 +02:00
remove.add(setting);
}
}
2019-05-26 20:14:53 +02:00
for (WiredMatchFurniSetting setting : remove) {
2018-07-06 15:30:00 +02:00
this.settings.remove(setting);
}
}
}
}