2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.users ;
import com.eu.habbo.Emulator ;
import com.eu.habbo.habbohotel.modtool.ModToolBan ;
2018-09-12 18:45:00 +02:00
import com.eu.habbo.habbohotel.permissions.Permission ;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.permissions.Rank ;
import com.eu.habbo.messages.ServerMessage ;
import com.eu.habbo.messages.outgoing.catalog.* ;
import com.eu.habbo.messages.outgoing.catalog.marketplace.MarketplaceConfigComposer ;
import com.eu.habbo.messages.outgoing.generic.alerts.GenericAlertComposer ;
import com.eu.habbo.messages.outgoing.modtool.ModToolComposer ;
import com.eu.habbo.messages.outgoing.users.UserPerksComposer ;
import com.eu.habbo.messages.outgoing.users.UserPermissionsComposer ;
2018-11-17 14:28:00 +01:00
import com.eu.habbo.plugin.events.users.UserRankChangedEvent ;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.plugin.events.users.UserRegisteredEvent ;
2020-05-03 01:46:07 +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 ;
import java.util.AbstractMap ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
import java.util.concurrent.ConcurrentHashMap ;
2019-05-26 20:14:53 +02:00
public class HabboManager {
2020-05-03 01:46:07 +02:00
private static final Logger LOGGER = LoggerFactory . getLogger ( HabboManager . class ) ;
2018-07-06 15:30:00 +02:00
//Configuration. Loaded from database & updated accordingly.
public static String WELCOME_MESSAGE = " " ;
public static boolean NAMECHANGE_ENABLED = false ;
private final ConcurrentHashMap < Integer , Habbo > onlineHabbos ;
2019-05-26 20:14:53 +02:00
public HabboManager ( ) {
2018-07-06 15:30:00 +02:00
long millis = System . currentTimeMillis ( ) ;
2018-09-28 21:25:00 +02:00
this . onlineHabbos = new ConcurrentHashMap < > ( ) ;
2018-07-06 15:30:00 +02:00
2020-05-03 01:46:07 +02:00
LOGGER . info ( " Habbo Manager -> Loaded! ( " + ( System . currentTimeMillis ( ) - millis ) + " MS) " ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public static HabboInfo getOfflineHabboInfo ( int id ) {
HabboInfo info = null ;
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " SELECT * FROM users WHERE id = ? LIMIT 1 " ) ) {
statement . setInt ( 1 , id ) ;
try ( ResultSet set = statement . executeQuery ( ) ) {
if ( set . next ( ) ) {
info = new HabboInfo ( set ) ;
}
}
} catch ( SQLException e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2019-05-26 20:14:53 +02:00
}
return info ;
}
public static HabboInfo getOfflineHabboInfo ( String username ) {
HabboInfo info = null ;
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " SELECT * FROM users WHERE username = ? LIMIT 1 " ) ) {
statement . setString ( 1 , username ) ;
try ( ResultSet set = statement . executeQuery ( ) ) {
if ( set . next ( ) ) {
info = new HabboInfo ( set ) ;
}
}
} catch ( SQLException e ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught SQL exception " , e ) ;
2019-05-26 20:14:53 +02:00
}
return info ;
}
public void addHabbo ( Habbo habbo ) {
2018-07-06 15:30:00 +02:00
this . onlineHabbos . put ( habbo . getHabboInfo ( ) . getId ( ) , habbo ) ;
}
2019-05-26 20:14:53 +02:00
public void removeHabbo ( Habbo habbo ) {
2018-07-06 15:30:00 +02:00
this . onlineHabbos . remove ( habbo . getHabboInfo ( ) . getId ( ) ) ;
}
2019-05-26 20:14:53 +02:00
public Habbo getHabbo ( int id ) {
2018-07-06 15:30:00 +02:00
return this . onlineHabbos . get ( id ) ;
}
2019-05-26 20:14:53 +02:00
public Habbo getHabbo ( String username ) {
synchronized ( this . onlineHabbos ) {
for ( Map . Entry < Integer , Habbo > map : this . onlineHabbos . entrySet ( ) ) {
2018-07-06 15:30:00 +02:00
if ( map . getValue ( ) . getHabboInfo ( ) . getUsername ( ) . equalsIgnoreCase ( username ) )
return map . getValue ( ) ;
}
}
return null ;
}
2019-05-26 20:14:53 +02:00
public Habbo loadHabbo ( String sso ) {
2019-03-18 02:22:00 +01:00
Habbo habbo ;
2018-07-06 15:30:00 +02:00
int userId = 0 ;
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ;
PreparedStatement statement = connection . prepareStatement ( " SELECT id FROM users WHERE auth_ticket = ? LIMIT 1 " ) ) {
2018-07-06 15:30:00 +02:00
statement . setString ( 1 , sso ) ;
2019-05-26 20:14:53 +02:00
try ( ResultSet s = statement . executeQuery ( ) ) {
if ( s . next ( ) ) {
2018-07-06 15:30:00 +02:00
userId = s . getInt ( " id " ) ;
}
}
statement . close ( ) ;
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-03-18 02:22:00 +01:00
habbo = this . cloneCheck ( userId ) ;
2019-05-26 20:14:53 +02:00
if ( habbo ! = null ) {
2019-03-18 02:22:00 +01:00
habbo . alert ( Emulator . getTexts ( ) . getValue ( " loggedin.elsewhere " ) ) ;
2018-12-22 11:39:00 +01:00
Emulator . getGameServer ( ) . getGameClientManager ( ) . disposeClient ( habbo . getClient ( ) ) ;
2018-07-06 15:30:00 +02:00
habbo = null ;
}
ModToolBan ban = Emulator . getGameEnvironment ( ) . getModToolManager ( ) . checkForBan ( userId ) ;
2019-05-26 20:14:53 +02:00
if ( ban ! = null ) {
2018-07-06 15:30:00 +02:00
return null ;
}
2019-03-18 02:22:00 +01:00
2019-05-26 20:14:53 +02:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ;
PreparedStatement statement = connection . prepareStatement ( " SELECT * FROM users WHERE auth_ticket = ? LIMIT 1 " ) ) {
2018-07-06 15:30:00 +02:00
statement . setString ( 1 , sso ) ;
2019-05-26 20:14:53 +02:00
try ( ResultSet set = statement . executeQuery ( ) ) {
if ( set . next ( ) ) {
2019-03-18 02:22:00 +01:00
habbo = new Habbo ( set ) ;
2019-05-26 20:14:53 +02:00
if ( habbo . getHabboInfo ( ) . firstVisit ) {
2019-03-18 02:22:00 +01:00
Emulator . getPluginManager ( ) . fireEvent ( new UserRegisteredEvent ( habbo ) ) ;
}
2019-05-26 20:14:53 +02:00
if ( ! Emulator . debugging ) {
try ( PreparedStatement stmt = connection . prepareStatement ( " UPDATE users SET auth_ticket = ? WHERE id = ? LIMIT 1 " ) ) {
2019-03-18 02:22:00 +01:00
stmt . setString ( 1 , " " ) ;
2019-05-21 20:09:54 +02:00
stmt . setInt ( 2 , habbo . getHabboInfo ( ) . getId ( ) ) ;
2019-03-18 02:22:00 +01:00
stmt . execute ( ) ;
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 ) ;
2019-03-18 02:22:00 +01:00
}
}
}
}
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 ) ;
2019-05-26 20:14:53 +02:00
} catch ( Exception ex ) {
2020-05-03 01:46:07 +02:00
LOGGER . error ( " Caught exception " , ex ) ;
2018-07-06 15:30:00 +02:00
}
return habbo ;
}
2019-05-11 00:45:07 +02:00
public HabboInfo getHabboInfo ( int id ) {
2019-05-26 20:14:53 +02:00
if ( this . getHabbo ( id ) = = null ) {
2019-05-11 00:45:07 +02:00
return getOfflineHabboInfo ( id ) ;
}
return this . getHabbo ( id ) . getHabboInfo ( ) ;
}
2019-05-26 20:14:53 +02:00
public int getOnlineCount ( ) {
2018-07-06 15:30:00 +02:00
return this . onlineHabbos . size ( ) ;
}
2019-05-26 20:14:53 +02:00
public Habbo cloneCheck ( int id ) {
2019-03-18 02:22:00 +01:00
return Emulator . getGameServer ( ) . getGameClientManager ( ) . getHabbo ( id ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public void sendPacketToHabbosWithPermission ( ServerMessage message , String perm ) {
synchronized ( this . onlineHabbos ) {
for ( Habbo habbo : this . onlineHabbos . values ( ) ) {
if ( habbo . hasPermission ( perm ) ) {
2018-07-06 15:30:00 +02:00
habbo . getClient ( ) . sendResponse ( message ) ;
}
}
}
}
2019-05-26 20:14:53 +02:00
public ConcurrentHashMap < Integer , Habbo > getOnlineHabbos ( ) {
2018-07-06 15:30:00 +02:00
return this . onlineHabbos ;
}
2019-05-26 20:14:53 +02:00
public synchronized void dispose ( ) {
2019-04-22 01:42:00 +02:00
//
2020-05-03 01:46:07 +02:00
LOGGER . info ( " Habbo Manager -> Disposed! " ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public ArrayList < HabboInfo > getCloneAccounts ( Habbo habbo , int limit ) {
2018-09-28 21:25:00 +02:00
ArrayList < HabboInfo > habboInfo = new ArrayList < > ( ) ;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " SELECT * FROM users WHERE ip_register = ? OR ip_current = ? AND id != ? ORDER BY id DESC LIMIT ? " ) ) {
2018-07-06 15:30:00 +02:00
statement . setString ( 1 , habbo . getHabboInfo ( ) . getIpRegister ( ) ) ;
statement . setString ( 2 , habbo . getClient ( ) . getChannel ( ) . remoteAddress ( ) . toString ( ) ) ;
statement . setInt ( 3 , habbo . getHabboInfo ( ) . getId ( ) ) ;
statement . setInt ( 4 , limit ) ;
2019-05-26 20:14:53 +02:00
try ( ResultSet set = statement . executeQuery ( ) ) {
while ( set . next ( ) ) {
2018-07-06 15:30:00 +02:00
habboInfo . add ( new HabboInfo ( set ) ) ;
}
}
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
}
return habboInfo ;
}
2019-05-26 20:14:53 +02:00
public List < Map . Entry < Integer , String > > getNameChanges ( int userId , int limit ) {
2018-07-06 15:30:00 +02:00
List < Map . Entry < Integer , String > > nameChanges = new ArrayList < > ( ) ;
2019-05-26 20:14:53 +02:00
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " SELECT timestamp, new_name FROM namechange_log WHERE user_id = ? ORDER by timestamp DESC LIMIT ? " ) ) {
2018-07-06 15:30:00 +02:00
statement . setInt ( 1 , userId ) ;
statement . setInt ( 2 , limit ) ;
2019-05-26 20:14:53 +02:00
try ( ResultSet set = statement . executeQuery ( ) ) {
while ( set . next ( ) ) {
2018-09-28 21:25:00 +02:00
nameChanges . add ( new AbstractMap . SimpleEntry < > ( set . getInt ( " timestamp " ) , set . getString ( " new_name " ) ) ) ;
2018-07-06 15:30:00 +02:00
}
}
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
}
return nameChanges ;
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void setRank ( int userId , int rankId ) throws Exception {
2018-07-06 15:30:00 +02:00
Habbo habbo = this . getHabbo ( userId ) ;
2019-05-26 20:14:53 +02:00
if ( ! Emulator . getGameEnvironment ( ) . getPermissionsManager ( ) . rankExists ( rankId ) ) {
2018-07-06 15:30:00 +02:00
throw new Exception ( " Rank ID ( " + rankId + " ) does not exist " ) ;
}
2019-04-22 01:42:00 +02:00
Rank newRank = Emulator . getGameEnvironment ( ) . getPermissionsManager ( ) . getRank ( rankId ) ;
2019-05-26 20:14:53 +02:00
if ( habbo ! = null & & habbo . getHabboStats ( ) ! = null ) {
2019-04-22 01:42:00 +02:00
Rank oldRank = habbo . getHabboInfo ( ) . getRank ( ) ;
2019-05-26 20:14:53 +02:00
if ( ! oldRank . getBadge ( ) . isEmpty ( ) ) {
2019-04-22 01:42:00 +02:00
habbo . deleteBadge ( habbo . getInventory ( ) . getBadgesComponent ( ) . getBadge ( oldRank . getBadge ( ) ) ) ;
2020-01-30 05:03:00 +01:00
}
if ( oldRank . getRoomEffect ( ) > 0 ) {
habbo . getInventory ( ) . getEffectsComponent ( ) . effects . remove ( oldRank . getRoomEffect ( ) ) ;
2019-04-22 01:42:00 +02:00
}
habbo . getHabboInfo ( ) . setRank ( newRank ) ;
2019-05-26 20:14:53 +02:00
if ( ! newRank . getBadge ( ) . isEmpty ( ) ) {
2019-04-22 01:42:00 +02:00
habbo . addBadge ( newRank . getBadge ( ) ) ;
}
2020-01-30 05:03:00 +01:00
if ( newRank . getRoomEffect ( ) > 0 ) {
habbo . getInventory ( ) . getEffectsComponent ( ) . createRankEffect ( habbo . getHabboInfo ( ) . getRank ( ) . getRoomEffect ( ) ) ;
}
2018-07-06 15:30:00 +02:00
habbo . getClient ( ) . sendResponse ( new UserPermissionsComposer ( habbo ) ) ;
habbo . getClient ( ) . sendResponse ( new UserPerksComposer ( habbo ) ) ;
2019-05-26 20:14:53 +02:00
if ( habbo . hasPermission ( Permission . ACC_SUPPORTTOOL ) ) {
2018-07-06 15:30:00 +02:00
habbo . getClient ( ) . sendResponse ( new ModToolComposer ( habbo ) ) ;
}
habbo . getHabboInfo ( ) . run ( ) ;
habbo . getClient ( ) . sendResponse ( new CatalogUpdatedComposer ( ) ) ;
habbo . getClient ( ) . sendResponse ( new CatalogModeComposer ( 0 ) ) ;
habbo . getClient ( ) . sendResponse ( new DiscountComposer ( ) ) ;
habbo . getClient ( ) . sendResponse ( new MarketplaceConfigComposer ( ) ) ;
habbo . getClient ( ) . sendResponse ( new GiftConfigurationComposer ( ) ) ;
habbo . getClient ( ) . sendResponse ( new RecyclerLogicComposer ( ) ) ;
2019-04-22 01:42:00 +02:00
habbo . alert ( Emulator . getTexts ( ) . getValue ( " commands.generic.cmd_give_rank.new_rank " ) . replace ( " id " , newRank . getName ( ) ) ) ;
2019-05-26 20:14:53 +02:00
} else {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE users SET `rank` = ? WHERE id = ? LIMIT 1 " ) ) {
2018-07-06 15:30:00 +02:00
statement . setInt ( 1 , rankId ) ;
statement . setInt ( 2 , userId ) ;
statement . execute ( ) ;
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
}
}
2018-11-17 14:28:00 +01:00
Emulator . getPluginManager ( ) . fireEvent ( new UserRankChangedEvent ( habbo ) ) ;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public void giveCredits ( int userId , int credits ) {
2018-07-06 15:30:00 +02:00
Habbo habbo = this . getHabbo ( userId ) ;
2019-05-26 20:14:53 +02:00
if ( habbo ! = null ) {
2018-07-06 15:30:00 +02:00
habbo . giveCredits ( credits ) ;
2019-05-26 20:14:53 +02:00
} else {
try ( Connection connection = Emulator . getDatabase ( ) . getDataSource ( ) . getConnection ( ) ; PreparedStatement statement = connection . prepareStatement ( " UPDATE users SET credits = credits + ? WHERE id = ? LIMIT 1 " ) ) {
2018-07-06 15:30:00 +02:00
statement . setInt ( 1 , credits ) ;
statement . setInt ( 2 , userId ) ;
statement . execute ( ) ;
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
public void staffAlert ( String message ) {
2018-07-06 15:30:00 +02:00
message = Emulator . getTexts ( ) . getValue ( " commands.generic.cmd_staffalert.title " ) + " \ r \ n " + message ;
ServerMessage msg = new GenericAlertComposer ( message ) . compose ( ) ;
Emulator . getGameEnvironment ( ) . getHabboManager ( ) . sendPacketToHabbosWithPermission ( msg , " cmd_staffalert " ) ;
}
}