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.gameclients.GameClient ;
import com.eu.habbo.habbohotel.items.Item ;
import com.eu.habbo.habbohotel.rooms.Room ;
import com.eu.habbo.habbohotel.rooms.RoomLayout ;
import com.eu.habbo.habbohotel.rooms.RoomUnit ;
import com.eu.habbo.habbohotel.users.Habbo ;
import com.eu.habbo.habbohotel.users.HabboItem ;
import com.eu.habbo.messages.ServerMessage ;
import com.eu.habbo.messages.outgoing.rooms.items.rentablespaces.RentableSpaceInfoComposer ;
import com.eu.habbo.threading.runnables.ClearRentedSpace ;
import gnu.trove.set.hash.THashSet ;
import java.awt.* ;
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
2019-05-26 20:14:53 +02:00
public class InteractionRentableSpace extends HabboItem {
2018-07-06 15:30:00 +02:00
private int renterId ;
private String renterName ;
private int endTimestamp ;
2019-05-26 20:14:53 +02:00
public InteractionRentableSpace ( ResultSet set , Item baseItem ) throws SQLException {
2018-07-06 15:30:00 +02:00
super ( set , baseItem ) ;
String [ ] data = set . getString ( " extra_data " ) . split ( " : " ) ;
this . renterName = " Unknown " ;
2019-05-26 20:14:53 +02:00
if ( data . length = = 2 ) {
2018-07-06 15:30:00 +02:00
this . renterId = Integer . valueOf ( data [ 0 ] ) ;
this . endTimestamp = Integer . valueOf ( data [ 1 ] ) ;
2019-05-26 20:14:53 +02:00
if ( this . renterId > 0 ) {
if ( this . isRented ( ) ) {
2018-07-06 15:30:00 +02:00
Habbo habbo = Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getHabbo ( this . renterId ) ;
2019-05-26 20:14:53 +02:00
if ( habbo ! = null ) {
2018-07-06 15:30:00 +02:00
this . renterName = habbo . getHabboInfo ( ) . getUsername ( ) ;
2019-05-26 20:14:53 +02:00
} else {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " SELECT username FROM users WHERE id = ? LIMIT 1 " ) ) {
2018-07-06 15:30:00 +02:00
statement . setInt ( 1 , this . renterId ) ;
2019-05-26 20:14:53 +02:00
try ( ResultSet row = statement . executeQuery ( ) ) {
if ( row . next ( ) ) {
2018-07-06 15:30:00 +02:00
this . renterName = row . getString ( " username " ) ;
}
}
2019-05-26 20:14:53 +02:00
} catch ( SQLException e ) {
2020-05-03 01:46:07 +02:00
logger . error ( " Caught SQL exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
} else {
if ( this . getRoomId ( ) > 0 ) {
2018-07-06 15:30:00 +02:00
Emulator . getThreading ( ) . run ( new ClearRentedSpace ( this , Emulator . getGameEnvironment ( ) . getRoomManager ( ) . getRoom ( this . getRoomId ( ) ) ) ) ;
this . renterId = 0 ;
}
}
}
}
}
2019-05-26 20:14:53 +02:00
public InteractionRentableSpace ( 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 . renterName = " " ;
}
@Override
2019-05-26 20:14:53 +02:00
public boolean canWalkOn ( RoomUnit roomUnit , Room room , Object [ ] objects ) {
if ( this . getExtradata ( ) . isEmpty ( ) )
2018-07-06 15:30:00 +02:00
return false ;
Habbo habbo = room . getHabbo ( roomUnit ) ;
2019-05-26 20:14:53 +02:00
if ( habbo = = null )
2018-07-06 15:30:00 +02:00
return true ;
2019-05-26 20:14:53 +02:00
if ( habbo . getHabboInfo ( ) . getId ( ) = = room . getId ( ) )
2018-07-06 15:30:00 +02:00
return true ;
2019-05-26 20:14:53 +02:00
if ( this . endTimestamp > Emulator . getIntUnixTimestamp ( ) ) {
2019-03-18 02:22:00 +01:00
return this . renterId > 0 & & this . renterId = = habbo . getHabboInfo ( ) . getId ( ) ;
2018-07-06 15:30:00 +02:00
}
return false ;
}
@Override
2019-05-26 20:14:53 +02:00
public void onClick ( GameClient client , Room room , Object [ ] objects ) throws Exception {
2018-07-06 15:30:00 +02:00
this . sendRentWidget ( client . getHabbo ( ) ) ;
}
@Override
2019-05-26 20:14:53 +02:00
public boolean isWalkable ( ) {
2018-07-06 15:30:00 +02:00
return true ;
}
@Override
2019-05-26 20:14:53 +02:00
public void onWalk ( RoomUnit roomUnit , Room room , Object [ ] objects ) throws Exception {
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void serializeExtradata ( ServerMessage serverMessage ) {
if ( this . getExtradata ( ) . isEmpty ( ) )
2018-07-06 15:30:00 +02:00
this . setExtradata ( " 0:0 " ) ;
serverMessage . appendInt ( 1 + ( this . isLimited ( ) ? 256 : 0 ) ) ;
2019-05-26 20:14:53 +02:00
if ( this . isRented ( ) ) {
2018-07-06 15:30:00 +02:00
serverMessage . appendInt ( 1 ) ;
serverMessage . appendString ( " renterId " ) ;
serverMessage . appendString ( this . renterId + " " ) ;
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
serverMessage . appendInt ( 0 ) ;
}
super . serializeExtradata ( serverMessage ) ;
}
2019-05-26 20:14:53 +02:00
public void rent ( Habbo habbo ) {
if ( this . isRented ( ) )
2018-07-06 15:30:00 +02:00
return ;
2019-05-26 20:14:53 +02:00
if ( habbo . getHabboStats ( ) . isRentingSpace ( ) )
2018-07-06 15:30:00 +02:00
return ;
2019-05-26 20:14:53 +02:00
if ( habbo . getHabboInfo ( ) . getCredits ( ) < this . rentCost ( ) )
2018-07-06 15:30:00 +02:00
return ;
2019-05-26 20:14:53 +02:00
if ( habbo . getHabboStats ( ) . getClubExpireTimestamp ( ) < Emulator . getIntUnixTimestamp ( ) )
2018-07-06 15:30:00 +02:00
return ;
this . setRenterId ( habbo . getHabboInfo ( ) . getId ( ) ) ;
this . setRenterName ( habbo . getHabboInfo ( ) . getUsername ( ) ) ;
this . setEndTimestamp ( Emulator . getIntUnixTimestamp ( ) + ( 7 * 86400 ) ) ;
habbo . getHabboStats ( ) . setRentedItemId ( this . getId ( ) ) ;
habbo . getHabboStats ( ) . setRentedTimeEnd ( this . endTimestamp ) ;
2018-09-28 21:25:00 +02:00
this . needsUpdate ( true ) ;
this . run ( ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public void endRent ( ) {
2018-07-06 15:30:00 +02:00
this . setEndTimestamp ( 0 ) ;
Room room = Emulator . getGameEnvironment ( ) . getRoomManager ( ) . getRoom ( this . getRoomId ( ) ) ;
2019-05-26 20:14:53 +02:00
if ( room = = null )
2018-07-06 15:30:00 +02:00
return ;
Rectangle rect = RoomLayout . getRectangle ( this . getX ( ) , this . getY ( ) , this . getBaseItem ( ) . getWidth ( ) , this . getBaseItem ( ) . getLength ( ) , this . getRotation ( ) ) ;
2018-09-28 21:25:00 +02:00
THashSet < HabboItem > items = new THashSet < > ( ) ;
2019-05-26 20:14:53 +02:00
for ( int i = rect . x ; i < rect . x + rect . getWidth ( ) ; i + + ) {
for ( int j = rect . y ; j < rect . y + rect . getHeight ( ) ; j + + ) {
2018-07-06 15:30:00 +02:00
items . addAll ( room . getItemsAt ( i , j , this . getZ ( ) ) ) ;
}
}
2019-05-26 20:14:53 +02:00
for ( HabboItem item : items ) {
if ( item . getUserId ( ) = = this . renterId ) {
2018-07-06 15:30:00 +02:00
room . pickUpItem ( item , null ) ;
}
}
Habbo habbo = Emulator . getGameEnvironment ( ) . getHabboManager ( ) . getHabbo ( this . renterId ) ;
2019-05-26 20:14:53 +02:00
if ( habbo ! = null ) {
2018-07-06 15:30:00 +02:00
habbo . getHabboStats ( ) . setRentedItemId ( 0 ) ;
habbo . getHabboStats ( ) . setRentedTimeEnd ( 0 ) ;
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
int zero = 0 ;
2019-05-26 20:14:53 +02:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE users_settings SET rent_space_id = ?, rent_space_endtime = ? WHERE user_id = ? LIMIT 1 " ) ) {
2018-07-06 15:30:00 +02:00
statement . setInt ( 1 , zero ) ;
statement . setInt ( 2 , zero ) ;
2019-05-26 20:14:53 +02:00
} catch ( SQLException e ) {
2020-05-03 01:46:07 +02:00
logger . error ( " Caught SQL exception " , e ) ;
2018-07-06 15:30:00 +02:00
}
}
//room.ejectUserFurni(this.renterId);
this . setRenterId ( 0 ) ;
this . setRenterName ( " " ) ;
2018-09-28 21:25:00 +02:00
this . needsUpdate ( true ) ;
this . run ( ) ;
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public String getExtradata ( ) {
2018-07-06 15:30:00 +02:00
return this . renterId + " : " + this . endTimestamp ;
}
2019-05-26 20:14:53 +02:00
public int getRenterId ( ) {
2018-07-06 15:30:00 +02:00
return this . renterId ;
}
2019-05-26 20:14:53 +02:00
public void setRenterId ( int renterId ) {
2018-07-06 15:30:00 +02:00
this . renterId = renterId ;
}
2019-05-26 20:14:53 +02:00
public String getRenterName ( ) {
2018-07-06 15:30:00 +02:00
return this . renterName ;
}
2019-05-26 20:14:53 +02:00
public void setRenterName ( String renterName ) {
2018-07-06 15:30:00 +02:00
this . renterName = renterName ;
}
2019-05-26 20:14:53 +02:00
public int getEndTimestamp ( ) {
2018-07-06 15:30:00 +02:00
return this . endTimestamp ;
}
2019-05-26 20:14:53 +02:00
public void setEndTimestamp ( int endTimestamp ) {
2018-07-06 15:30:00 +02:00
this . endTimestamp = endTimestamp ;
}
2019-05-26 20:14:53 +02:00
public boolean isRented ( ) {
2018-07-06 15:30:00 +02:00
return this . endTimestamp > Emulator . getIntUnixTimestamp ( ) ;
}
2019-05-26 20:14:53 +02:00
public int rentCost ( ) {
2018-07-06 15:30:00 +02:00
String [ ] data = this . getBaseItem ( ) . getName ( ) . replace ( " hblooza_spacerent " , " " ) . split ( " x " ) ;
2019-05-26 20:14:53 +02:00
if ( data . length = = 2 ) {
2018-07-06 15:30:00 +02:00
int x = Integer . valueOf ( data [ 0 ] ) ;
int y = Integer . valueOf ( data [ 1 ] ) ;
return 10 * ( x * y ) ;
}
return 1337 ;
}
2019-05-26 20:14:53 +02:00
public int getRentErrorCode ( Habbo habbo ) {
if ( this . isRented ( ) & & this . renterId ! = habbo . getHabboInfo ( ) . getId ( ) ) {
2018-07-06 15:30:00 +02:00
return RentableSpaceInfoComposer . SPACE_ALREADY_RENTED ;
}
2019-05-26 20:14:53 +02:00
if ( habbo . getHabboStats ( ) . isRentingSpace ( ) & & habbo . getHabboStats ( ) . getRentedItemId ( ) ! = this . getId ( ) ) {
2018-07-06 15:30:00 +02:00
return RentableSpaceInfoComposer . CAN_RENT_ONLY_ONE_SPACE ;
}
2019-05-26 20:14:53 +02:00
if ( habbo . getHabboStats ( ) . getClubExpireTimestamp ( ) < Emulator . getIntUnixTimestamp ( ) ) {
2018-07-06 15:30:00 +02:00
return RentableSpaceInfoComposer . CANT_RENT_NO_HABBO_CLUB ;
}
2019-05-26 20:14:53 +02:00
if ( this . rentCost ( ) > habbo . getHabboInfo ( ) . getCredits ( ) ) {
2018-07-06 15:30:00 +02:00
return RentableSpaceInfoComposer . NOT_ENOUGH_CREDITS ;
}
return 0 ;
}
2019-05-26 20:14:53 +02:00
public void sendRentWidget ( Habbo habbo ) {
2018-07-06 15:30:00 +02:00
habbo . getClient ( ) . sendResponse ( new RentableSpaceInfoComposer ( habbo , this , this . getRentErrorCode ( habbo ) ) ) ;
}
}