Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/triggers/WiredTriggerRepeaterLong.java

151 lines
4.9 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items.interactions.wired.triggers;
import com.eu.habbo.Emulator;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.items.ICycleable;
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;
import com.eu.habbo.habbohotel.items.interactions.wired.WiredTriggerReset;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomUnit;
import com.eu.habbo.habbohotel.wired.WiredHandler;
import com.eu.habbo.habbohotel.wired.WiredTriggerType;
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.messages.ServerMessage;
import gnu.trove.procedure.TObjectProcedure;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class WiredTriggerRepeaterLong extends InteractionWiredTrigger implements ICycleable, WiredTriggerReset {
public static final int DEFAULT_DELAY = 10 * 5000;
2019-05-26 20:14:53 +02:00
private static final WiredTriggerType type = WiredTriggerType.PERIODICALLY_LONG;
2018-07-06 15:30:00 +02:00
private int repeatTime = DEFAULT_DELAY;
private int counter = 0;
2019-05-26 20:14:53 +02:00
public WiredTriggerRepeaterLong(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 WiredTriggerRepeaterLong(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-07-06 15:30:00 +02:00
return true;
}
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
return WiredHandler.getGsonBuilder().create().toJson(new JsonData(
this.repeatTime
));
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 {
String wiredData = set.getString("wired_data");
if (wiredData.startsWith("{")) {
JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, JsonData.class);
this.repeatTime = data.repeatTime;
} else {
if (wiredData.length() >= 1) {
this.repeatTime = (Integer.valueOf(wiredData));
}
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
if (this.repeatTime < 5000) {
2018-07-06 15:30:00 +02:00
this.repeatTime = 20 * 5000;
}
}
@Override
2019-05-26 20:14:53 +02:00
public void onPickUp() {
2018-07-06 15:30:00 +02:00
this.repeatTime = DEFAULT_DELAY;
}
@Override
2019-05-26 20:14:53 +02:00
public WiredTriggerType 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
message.appendBoolean(false);
message.appendInt(5);
message.appendInt(0);
message.appendInt(this.getBaseItem().getSpriteId());
message.appendInt(this.getId());
message.appendString("");
message.appendInt(1);
message.appendInt(this.repeatTime / 5000);
message.appendInt(0);
message.appendInt(this.getType().code);
2019-05-26 20:14:53 +02:00
if (!this.isTriggeredByRoomUnit()) {
2018-07-06 15:30:00 +02:00
List<Integer> invalidTriggers = new ArrayList<>();
2019-05-26 20:14:53 +02:00
room.getRoomSpecialTypes().getEffects(this.getX(), this.getY()).forEach(new TObjectProcedure<InteractionWiredEffect>() {
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(InteractionWiredEffect object) {
if (object.requiresTriggeringUser()) {
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) {
if(settings.getIntParams().length < 1) return false;
this.repeatTime = settings.getIntParams()[0] * 5000;
2018-07-06 15:30:00 +02:00
this.counter = 0;
return true;
}
@Override
2019-05-26 20:14:53 +02:00
public void cycle(Room room) {
2018-07-06 15:30:00 +02:00
this.counter += 500;
2019-05-26 20:14:53 +02:00
if (this.counter >= this.repeatTime) {
2018-07-06 15:30:00 +02:00
this.counter = 0;
2019-05-26 20:14:53 +02:00
if (this.getRoomId() != 0) {
if (room.isLoaded()) {
2018-07-06 15:30:00 +02:00
WiredHandler.handle(this, null, room, new Object[]{this});
}
}
}
}
@Override
public void resetTimer() {
this.counter = 0;
if (this.getRoomId() != 0) {
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
if (room != null && room.isLoaded()) {
WiredHandler.handle(this, null, room, new Object[]{this});
}
}
}
static class JsonData {
int repeatTime;
public JsonData(int repeatTime) {
this.repeatTime = repeatTime;
}
}
2018-07-06 15:30:00 +02:00
}