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

100 lines
2.9 KiB
Java

package com.eu.habbo.habbohotel.items.interactions;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.items.Item;
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.messages.ServerMessage;
import com.eu.habbo.messages.outgoing.rooms.items.ItemStateComposer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class InteractionWired extends InteractionDefault {
private static final Logger LOGGER = LoggerFactory.getLogger(InteractionWired.class);
private long cooldown;
InteractionWired(ResultSet set, Item baseItem) throws SQLException {
super(set, baseItem);
this.setExtradata("0");
}
InteractionWired(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
super(id, userId, item, extradata, limitedStack, limitedSells);
this.setExtradata("0");
}
public abstract boolean execute(RoomUnit roomUnit, Room room, Object[] stuff);
public abstract String getWiredData();
public abstract void serializeWiredData(ServerMessage message, Room room);
public abstract void loadWiredData(ResultSet set, Room room) throws SQLException;
@Override
public void run() {
if (this.needsUpdate()) {
String wiredData = this.getWiredData();
if (wiredData == null) {
wiredData = "";
}
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE items SET wired_data = ? WHERE id = ?")) {
if (this.getRoomId() != 0) {
statement.setString(1, wiredData);
} else {
statement.setString(1, "");
}
statement.setInt(2, this.getId());
statement.execute();
} catch (SQLException e) {
LOGGER.error("Caught SQL exception", e);
}
}
super.run();
}
@Override
public void onPickUp(Room room) {
this.onPickUp();
}
public abstract void onPickUp();
public void activateBox(Room room) {
this.setExtradata(this.getExtradata().equals("1") ? "0" : "1");
room.sendComposer(new ItemStateComposer(this).compose());
}
protected long requiredCooldown() {
return 50L;
}
public boolean canExecute(long newMillis) {
return newMillis - this.cooldown >= this.requiredCooldown();
}
public void setCooldown(long newMillis) {
this.cooldown = newMillis;
}
@Override
public boolean allowWiredResetState() {
return false;
}
@Override
public boolean isUsable() {
return true;
}
}