Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/InteractionWired.java

163 lines
4.8 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items.interactions;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.Item;
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.RoomUnit;
2022-11-16 16:56:06 +01:00
import com.eu.habbo.messages.ClientMessage;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.outgoing.rooms.items.ItemStateComposer;
import gnu.trove.map.hash.TLongLongHashMap;
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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class InteractionWired extends InteractionDefault {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(InteractionWired.class);
2018-07-08 23:32:00 +02:00
private long cooldown;
private TLongLongHashMap userExecutionCache = new TLongLongHashMap(3);
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
InteractionWired(ResultSet set, Item baseItem) throws SQLException {
2018-07-06 15:30:00 +02:00
super(set, baseItem);
this.setExtradata("0");
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
InteractionWired(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);
this.setExtradata("0");
2018-07-06 15:30:00 +02:00
}
public abstract boolean execute(RoomUnit roomUnit, Room room, Object[] stuff);
2019-03-18 02:22:00 +01:00
public abstract String getWiredData();
2018-07-06 15:30:00 +02:00
public abstract void serializeWiredData(ServerMessage message, Room room);
public abstract void loadWiredData(ResultSet set, Room room) throws SQLException;
@Override
2019-05-26 20:14:53 +02:00
public void run() {
if (this.needsUpdate()) {
2018-07-06 15:30:00 +02:00
String wiredData = this.getWiredData();
2019-05-26 20:14:53 +02:00
if (wiredData == null) {
2018-07-06 15:30:00 +02:00
wiredData = "";
}
2019-05-26 20:14:53 +02:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE items SET wired_data = ? WHERE id = ?")) {
if (this.getRoomId() != 0) {
2018-07-06 15:30:00 +02:00
statement.setString(1, wiredData);
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
statement.setString(1, "");
}
statement.setInt(2, this.getId());
statement.execute();
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
}
super.run();
}
@Override
2019-05-26 20:14:53 +02:00
public void onPickUp(Room room) {
2019-03-18 02:22:00 +01:00
this.onPickUp();
2018-07-06 15:30:00 +02:00
}
public abstract void onPickUp();
2019-05-26 20:14:53 +02:00
public void activateBox(Room room) {
this.activateBox(room, (RoomUnit)null, 0L);
}
public void activateBox(Room room, RoomUnit roomUnit, long millis) {
2018-07-06 15:30:00 +02:00
this.setExtradata(this.getExtradata().equals("1") ? "0" : "1");
room.sendComposer(new ItemStateComposer(this).compose());
if (roomUnit != null) {
this.addUserExecutionCache(roomUnit.getId(), millis);
}
2018-07-06 15:30:00 +02:00
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
protected long requiredCooldown() {
2019-05-16 11:15:33 +02:00
return 50L;
2018-07-08 23:32:00 +02:00
}
2019-05-26 20:14:53 +02:00
public boolean canExecute(long newMillis) {
2019-03-18 02:22:00 +01:00
return newMillis - this.cooldown >= this.requiredCooldown();
2018-07-08 23:32:00 +02:00
}
2018-10-07 00:28:00 +02:00
2019-05-26 20:14:53 +02:00
public void setCooldown(long newMillis) {
2018-12-22 11:39:00 +01:00
this.cooldown = newMillis;
}
2019-03-18 02:22:00 +01:00
2018-11-17 14:28:00 +01:00
@Override
2019-05-26 20:14:53 +02:00
public boolean allowWiredResetState() {
2018-11-17 14:28:00 +01:00
return false;
}
2018-10-07 00:28:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public boolean isUsable() {
2018-10-07 00:28:00 +02:00
return true;
}
public boolean userCanExecute(int roomUnitId, long timestamp) {
if (roomUnitId == -1) {
return true;
} else {
if (this.userExecutionCache.containsKey((long)roomUnitId)) {
long lastTimestamp = this.userExecutionCache.get((long)roomUnitId);
if (timestamp - lastTimestamp < 100L) {
return false;
}
}
return true;
}
}
public void clearUserExecutionCache() {
this.userExecutionCache.clear();
}
public void addUserExecutionCache(int roomUnitId, long timestamp) {
this.userExecutionCache.put((long)roomUnitId, timestamp);
}
2022-11-16 16:56:06 +01:00
public static WiredSettings readSettings(ClientMessage packet, boolean isEffect)
{
int intParamCount = packet.readInt();
int[] intParams = new int[intParamCount];
for(int i = 0; i < intParamCount; i++)
{
intParams[i] = packet.readInt();
}
String stringParam = packet.readString();
int itemCount = packet.readInt();
int[] itemIds = new int[itemCount];
for(int i = 0; i < itemCount; i++)
{
itemIds[i] = packet.readInt();
}
WiredSettings settings = new WiredSettings(intParams, stringParam, itemIds, -1);
if(isEffect)
{
settings.setDelay(packet.readInt());
}
settings.setStuffTypeSelectionCode(packet.readInt());
return settings;
}
2018-07-06 15:30:00 +02:00
}