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 ;
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 ;
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 ;
2020-11-02 02:32:27 +01:00
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 ;
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 ) ;
2019-05-24 12:51:41 +02:00
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 ) ;
2019-05-24 12:51:41 +02:00
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 ) {
2018-07-06 15:30:00 +02:00
this . setExtradata ( this . getExtradata ( ) . equals ( " 1 " ) ? " 0 " : " 1 " ) ;
room . sendComposer ( new ItemStateComposer ( this ) . compose ( ) ) ;
}
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 ;
}
2018-07-06 15:30:00 +02:00
}