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

180 lines
6.1 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.InteractionCrackable;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredEffect;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredTrigger;
import com.eu.habbo.habbohotel.items.interactions.games.InteractionGameTimer;
import com.eu.habbo.habbohotel.items.interactions.games.freeze.InteractionFreezeBlock;
import com.eu.habbo.habbohotel.rooms.Room;
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 gnu.trove.procedure.TObjectProcedure;
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;
2019-05-26 20:14:53 +02:00
public class WiredEffectToggleRandom extends InteractionWiredEffect {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(WiredEffectToggleRandom.class);
2018-07-06 15:30:00 +02:00
public static final WiredEffectType type = WiredEffectType.TOGGLE_RANDOM;
2019-05-26 20:14:53 +02:00
private final THashSet<HabboItem> items = new THashSet<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public WiredEffectToggleRandom(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 WiredEffectToggleRandom(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 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
2019-05-26 20:14:53 +02:00
public boolean saveData(ClientMessage packet, GameClient gameClient) {
packet.readInt();
packet.readString();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
this.items.clear();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
int count = packet.readInt();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (int i = 0; i < count; i++) {
HabboItem item = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId()).getHabboItem(packet.readInt());
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (item instanceof InteractionFreezeBlock || item instanceof InteractionGameTimer || item instanceof InteractionCrackable)
continue;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
this.items.add(item);
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
this.setDelay(packet.readInt());
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) {
2018-07-06 15:30:00 +02:00
THashSet<HabboItem> items = this.items;
2019-05-26 20:14:53 +02:00
for (HabboItem item : items) {
if (item.getRoomId() == 0) {
2018-07-06 15:30:00 +02:00
this.items.remove(item);
continue;
}
2019-05-26 20:14:53 +02:00
try {
2018-07-06 15:30:00 +02:00
item.setExtradata(Emulator.getRandom().nextInt(item.getBaseItem().getStateCount() + 1) + "");
room.updateItem(item);
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
}
}
return true;
}
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
2019-03-18 02:22:00 +01:00
StringBuilder wiredData = new StringBuilder(this.getDelay() + "\t");
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (!this.items.isEmpty()) {
for (HabboItem item : this.items) {
2019-03-18 02:22:00 +01:00
wiredData.append(item.getId()).append(";");
2018-07-06 15:30:00 +02:00
}
}
2019-03-18 02:22:00 +01:00
return wiredData.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
this.items.clear();
String[] wiredData = set.getString("wired_data").split("\t");
2019-05-26 20:14:53 +02:00
if (wiredData.length >= 1) {
2018-07-06 15:30:00 +02:00
this.setDelay(Integer.valueOf(wiredData[0]));
}
2019-05-26 20:14:53 +02:00
if (wiredData.length == 2) {
if (wiredData[1].contains(";")) {
for (String s : wiredData[1].split(";")) {
2018-07-06 15:30:00 +02:00
HabboItem item = room.getHabboItem(Integer.valueOf(s));
if (item instanceof InteractionFreezeBlock || item instanceof InteractionGameTimer || item instanceof InteractionCrackable)
continue;
if (item != null)
this.items.add(item);
}
}
}
}
@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;
}
}