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

223 lines
7.5 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;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredTrigger;
2022-11-16 16:56:06 +01:00
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomTile;
import com.eu.habbo.habbohotel.rooms.RoomUnit;
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.incoming.wired.WiredSaveException;
2018-07-06 15:30:00 +02:00
import gnu.trove.procedure.TObjectProcedure;
import gnu.trove.set.hash.THashSet;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public class WiredEffectTriggerStacks extends InteractionWiredEffect {
2018-07-06 15:30:00 +02:00
public static final WiredEffectType type = WiredEffectType.CALL_STACKS;
private THashSet<HabboItem> items;
2019-05-26 20:14:53 +02:00
public WiredEffectTriggerStacks(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.items = new THashSet<>();
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public WiredEffectTriggerStacks(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.items = new THashSet<>();
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-09-28 21:25:00 +02:00
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)
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);
}
message.appendBoolean(false);
message.appendInt(WiredHandler.MAXIMUM_FURNI_SELECTION);
message.appendInt(this.items.size());
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items) {
2018-07-06 15:30:00 +02:00
message.appendInt(item.getId());
}
message.appendInt(this.getBaseItem().getSpriteId());
message.appendInt(this.getId());
message.appendString("");
message.appendInt(0);
message.appendInt(0);
message.appendInt(this.getType().code);
message.appendInt(this.getDelay());
2019-05-26 20:14:53 +02:00
if (this.requiresTriggeringUser()) {
2018-07-06 15:30:00 +02:00
List<Integer> invalidTriggers = new ArrayList<>();
2019-05-26 20:14:53 +02:00
room.getRoomSpecialTypes().getTriggers(this.getX(), this.getY()).forEach(new TObjectProcedure<InteractionWiredTrigger>() {
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(InteractionWiredTrigger object) {
if (!object.isTriggeredByRoomUnit()) {
2018-07-06 15:30:00 +02:00
invalidTriggers.add(object.getBaseItem().getSpriteId());
}
return true;
}
});
message.appendInt(invalidTriggers.size());
2019-05-26 20:14:53 +02:00
for (Integer i : invalidTriggers) {
2018-07-06 15:30:00 +02:00
message.appendInt(i);
}
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
message.appendInt(0);
}
}
@Override
2022-11-16 16:56:06 +01:00
public boolean saveData(WiredSettings settings, GameClient gameClient) throws WiredSaveException {
int itemsCount = settings.getFurniIds().length;
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");
}
List<HabboItem> newItems = new ArrayList<>();
for (int i = 0; i < itemsCount; i++) {
2022-11-16 16:56:06 +01:00
int itemId = settings.getFurniIds()[i];
HabboItem it = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(itemId);
2018-07-06 15:30:00 +02:00
if(it == null)
throw new WiredSaveException(String.format("Item %s not found", itemId));
newItems.add(it);
2018-07-06 15:30:00 +02:00
}
2022-11-16 16:56:06 +01:00
int delay = settings.getDelay();
if(delay > Emulator.getConfig().getInt("hotel.wired.max_delay", 20))
throw new WiredSaveException("Delay too long");
this.items.clear();
this.items.addAll(newItems);
this.setDelay(delay);
2018-07-06 15:30:00 +02:00
return true;
}
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
if (stuff == null || (stuff.length >= 1 && stuff[stuff.length - 1] instanceof WiredEffectTriggerStacks)) {
2018-07-06 15:30:00 +02:00
return false;
}
2018-09-28 21:25:00 +02:00
THashSet<RoomTile> usedTiles = new THashSet<>();
2018-07-06 15:30:00 +02:00
boolean found;
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.items) {
2018-07-06 15:30:00 +02:00
//if(item instanceof InteractionWiredTrigger)
{
found = false;
2019-05-26 20:14:53 +02:00
for (RoomTile tile : usedTiles) {
if (tile.x == item.getX() && tile.y == item.getY()) {
2018-07-06 15:30:00 +02:00
found = true;
break;
}
}
2019-05-26 20:14:53 +02:00
if (!found) {
2018-07-06 15:30:00 +02:00
usedTiles.add(room.getLayout().getTile(item.getX(), item.getY()));
}
}
}
2020-04-23 17:06:17 +02:00
Object[] newStuff = new Object[stuff.length + 1];
System.arraycopy(stuff, 0, newStuff, 0, stuff.length);
newStuff[newStuff.length - 1] = this;
WiredHandler.executeEffectsAtTiles(usedTiles, roomUnit, room, stuff);
2018-07-06 15:30:00 +02:00
2020-04-23 17:06:17 +02:00
return true;
}
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
return WiredHandler.getGsonBuilder().create().toJson(new JsonData(
this.getDelay(),
this.items.stream().map(HabboItem::getId).collect(Collectors.toList())
));
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-28 21:25:00 +02:00
this.items = new THashSet<>();
String wiredData = set.getString("wired_data");
if (wiredData.startsWith("{")) {
JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, JsonData.class);
this.setDelay(data.delay);
for (Integer id: data.itemIds) {
HabboItem item = room.getHabboItem(id);
if (item != null) {
this.items.add(item);
}
}
} else {
String[] wiredDataOld = wiredData.split("\t");
2018-07-06 15:30:00 +02:00
if (wiredDataOld.length >= 1) {
this.setDelay(Integer.parseInt(wiredDataOld[0]));
}
if (wiredDataOld.length == 2) {
if (wiredDataOld[1].contains(";")) {
for (String s : wiredDataOld[1].split(";")) {
HabboItem item = room.getHabboItem(Integer.parseInt(s));
2018-07-06 15:30:00 +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.items.clear();
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
protected long requiredCooldown() {
2019-03-18 02:22:00 +01:00
return 250;
2018-07-06 15:30:00 +02:00
}
static class JsonData {
int delay;
List<Integer> itemIds;
public JsonData(int delay, List<Integer> itemIds) {
this.delay = delay;
this.itemIds = itemIds;
}
}
2018-07-06 15:30:00 +02:00
}