2018-07-06 13:30:00 +00:00
package com.eu.habbo.habbohotel.users ;
import com.eu.habbo.Emulator ;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-07-06 13:30:00 +00:00
import java.sql.* ;
2019-05-26 21:14:53 +03:00
public class HabboBadge implements Runnable {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory . getLogger ( HabboBadge . class ) ;
2018-07-06 13:30:00 +00:00
private int id ;
private String code ;
private int slot ;
private Habbo habbo ;
private boolean needsUpdate ;
private boolean needsInsert ;
2019-05-26 21:14:53 +03:00
public HabboBadge ( ResultSet set , Habbo habbo ) throws SQLException {
2018-07-06 13:30:00 +00:00
this . id = set . getInt ( " id " ) ;
this . code = set . getString ( " badge_code " ) ;
this . slot = set . getInt ( " slot_id " ) ;
this . habbo = habbo ;
this . needsUpdate = false ;
this . needsInsert = false ;
}
2019-05-26 21:14:53 +03:00
public HabboBadge ( int id , String code , int slot , Habbo habbo ) {
2018-07-06 13:30:00 +00:00
this . id = id ;
this . code = code ;
this . slot = slot ;
this . habbo = habbo ;
this . needsUpdate = false ;
this . needsInsert = true ;
}
2019-05-26 21:14:53 +03:00
public int getId ( ) {
2019-03-18 01:22:00 +00:00
return this . id ;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public String getCode ( ) {
2019-03-18 01:22:00 +00:00
return this . code ;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public void setCode ( String code ) {
2018-07-06 13:30:00 +00:00
this . code = code ;
}
2019-05-26 21:14:53 +03:00
public int getSlot ( ) {
return this . slot ;
2018-07-06 13:30:00 +00:00
}
2019-05-26 21:14:53 +03:00
public void setSlot ( int slot ) {
this . slot = slot ;
2018-07-06 13:30:00 +00:00
}
@Override
2019-05-26 21:14:53 +03:00
public void run ( ) {
try {
if ( this . needsInsert ) {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " INSERT INTO users_badges (user_id, slot_id, badge_code) VALUES (?, ?, ?) " , Statement . RETURN_GENERATED_KEYS ) ) {
2018-07-06 13:30:00 +00:00
statement . setInt ( 1 , this . habbo . getHabboInfo ( ) . getId ( ) ) ;
statement . setInt ( 2 , this . slot ) ;
statement . setString ( 3 , this . code ) ;
statement . execute ( ) ;
2019-05-26 21:14:53 +03:00
try ( ResultSet set = statement . getGeneratedKeys ( ) ) {
if ( set . next ( ) ) {
2018-07-06 13:30:00 +00:00
this . id = set . getInt ( 1 ) ;
}
}
}
this . needsInsert = false ;
2019-05-26 21:14:53 +03:00
} else if ( this . needsUpdate ) {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE users_badges SET slot_id = ?, badge_code = ? WHERE id = ? AND user_id = ? " ) ) {
2018-07-06 13:30:00 +00:00
statement . setInt ( 1 , this . slot ) ;
statement . setString ( 2 , this . code ) ;
statement . setInt ( 3 , this . id ) ;
statement . setInt ( 4 , this . habbo . getHabboInfo ( ) . getId ( ) ) ;
statement . execute ( ) ;
}
this . needsUpdate = false ;
}
2019-05-26 21:14:53 +03:00
} catch ( SQLException e ) {
2020-05-04 22:24:09 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2018-07-06 13:30:00 +00:00
}
}
2019-05-26 21:14:53 +03:00
public void needsUpdate ( boolean needsUpdate ) {
2018-07-06 13:30:00 +00:00
this . needsUpdate = needsUpdate ;
}
2019-05-26 21:14:53 +03:00
public void needsInsert ( boolean needsInsert ) {
2018-07-06 13:30:00 +00:00
this . needsInsert = needsInsert ;
}
}