Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/users/HabboManager.java

409 lines
13 KiB
Java
Raw Normal View History

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;
2019-04-22 01:42:00 +02:00
import com.eu.habbo.habbohotel.users.inventory.BadgesComponent;
2018-07-06 15:30:00 +02:00
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;
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;
public class HabboManager
{
//Configuration. Loaded from database & updated accordingly.
public static String WELCOME_MESSAGE = "";
public static boolean NAMECHANGE_ENABLED = false;
private final ConcurrentHashMap<Integer, Habbo> onlineHabbos;
public HabboManager()
{
long millis = System.currentTimeMillis();
2018-09-28 21:25:00 +02:00
this.onlineHabbos = new ConcurrentHashMap<>();
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logStart("Habbo Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
}
public void addHabbo(Habbo habbo)
{
this.onlineHabbos.put(habbo.getHabboInfo().getId(), habbo);
}
public void removeHabbo(Habbo habbo)
{
this.onlineHabbos.remove(habbo.getHabboInfo().getId());
}
public Habbo getHabbo(int id)
{
return this.onlineHabbos.get(id);
}
public Habbo getHabbo(String username)
{
synchronized (this.onlineHabbos)
{
for (Map.Entry<Integer, Habbo> map : this.onlineHabbos.entrySet())
{
if (map.getValue().getHabboInfo().getUsername().equalsIgnoreCase(username))
return map.getValue();
}
}
return null;
}
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
2018-07-06 15:30:00 +02:00
try(Connection connection = Emulator.getDatabase().getDataSource().getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT id FROM users WHERE auth_ticket = ? LIMIT 1"))
{
statement.setString(1, sso);
try (ResultSet s = statement.executeQuery())
{
if (s.next())
{
userId = s.getInt("id");
}
}
statement.close();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
2019-03-18 02:22:00 +01:00
habbo = this.cloneCheck(userId);
2018-07-06 15:30:00 +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);
if (ban != null)
{
return null;
}
2019-03-18 02:22:00 +01:00
2018-07-06 15:30:00 +02:00
try(Connection connection = Emulator.getDatabase().getDataSource().getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE auth_ticket LIKE ? LIMIT 1"))
{
statement.setString(1, sso);
2019-03-18 02:22:00 +01:00
try (ResultSet set = statement.executeQuery())
{
if (set.next())
{
habbo = new Habbo(set);
2019-05-11 00:45:07 +02:00
if (habbo.getHabboInfo().firstVisit)
2019-03-18 02:22:00 +01:00
{
Emulator.getPluginManager().fireEvent(new UserRegisteredEvent(habbo));
}
if (!Emulator.debugging)
{
try (PreparedStatement stmt = connection.prepareStatement("UPDATE users SET auth_ticket = ? WHERE auth_ticket LIKE ? AND id = ? LIMIT 1"))
{
stmt.setString(1, "");
stmt.setString(2, sso);
stmt.setInt(3, habbo.getHabboInfo().getId());
stmt.execute();
} catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
}
}
}
2018-07-06 15:30:00 +02:00
}
catch(SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
catch (Exception ex)
{
Emulator.getLogging().logErrorLine(ex);
}
return habbo;
}
2019-05-11 00:45:07 +02:00
public HabboInfo getHabboInfo(int id) {
if(this.getHabbo(id) == null) {
return getOfflineHabboInfo(id);
}
return this.getHabbo(id).getHabboInfo();
}
2018-07-06 15:30:00 +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)
{
Emulator.getLogging().logSQLException(e);
}
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)
{
Emulator.getLogging().logSQLException(e);
}
return info;
}
public int getOnlineCount()
{
return this.onlineHabbos.size();
}
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
}
public void sendPacketToHabbosWithPermission(ServerMessage message, String perm)
{
synchronized (this.onlineHabbos)
{
for(Habbo habbo : this.onlineHabbos.values())
{
if(habbo.hasPermission(perm))
{
habbo.getClient().sendResponse(message);
}
}
}
}
public ConcurrentHashMap<Integer, Habbo> getOnlineHabbos()
{
return this.onlineHabbos;
}
public synchronized void dispose()
{
2019-04-22 01:42:00 +02:00
//
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logShutdownLine("Habbo Manager -> Disposed!");
}
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
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 ?"))
{
statement.setString(1, habbo.getHabboInfo().getIpRegister());
statement.setString(2, habbo.getClient().getChannel().remoteAddress().toString());
statement.setInt(3, habbo.getHabboInfo().getId());
statement.setInt(4, limit);
try (ResultSet set = statement.executeQuery())
{
while (set.next())
{
habboInfo.add(new HabboInfo(set));
}
}
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
return habboInfo;
}
public List<Map.Entry<Integer, String>> getNameChanges(int userId, int limit)
{
List<Map.Entry<Integer, String>> nameChanges = new ArrayList<>();
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 ?"))
{
statement.setInt(1, userId);
statement.setInt(2, limit);
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
}
}
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
return nameChanges;
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public void setRank(int userId, int rankId) throws Exception
{
Habbo habbo = this.getHabbo(userId);
if (!Emulator.getGameEnvironment().getPermissionsManager().rankExists(rankId))
{
throw new Exception("Rank ID (" + rankId + ") does not exist");
}
2019-04-22 01:42:00 +02:00
Rank newRank = Emulator.getGameEnvironment().getPermissionsManager().getRank(rankId);
2018-09-28 21:25:00 +02:00
if(habbo != null && habbo.getHabboStats() != null)
2018-07-06 15:30:00 +02:00
{
2019-04-22 01:42:00 +02:00
Rank oldRank = habbo.getHabboInfo().getRank();
if (!oldRank.getBadge().isEmpty())
{
habbo.deleteBadge(habbo.getInventory().getBadgesComponent().getBadge(oldRank.getBadge()));
2019-05-12 06:04:46 +02:00
//BadgesComponent.deleteBadge(userId, oldRank.getBadge()); // unnecessary as Habbo.deleteBadge does this
2019-04-22 01:42:00 +02:00
}
habbo.getHabboInfo().setRank(newRank);
if (!newRank.getBadge().isEmpty())
{
habbo.addBadge(newRank.getBadge());
}
2018-07-06 15:30:00 +02:00
habbo.getClient().sendResponse(new UserPermissionsComposer(habbo));
habbo.getClient().sendResponse(new UserPerksComposer(habbo));
2018-09-12 18:45:00 +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()));
2018-07-06 15:30:00 +02:00
}
else
{
2019-04-22 01:42:00 +02:00
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();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
}
2018-11-17 14:28:00 +01:00
Emulator.getPluginManager().fireEvent(new UserRankChangedEvent(habbo));
2018-07-06 15:30:00 +02:00
}
public void giveCredits(int userId, int credits)
{
Habbo habbo = this.getHabbo(userId);
if (habbo != null)
{
habbo.giveCredits(credits);
}
else
{
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement= connection.prepareStatement("UPDATE users SET credits = credits + ? WHERE id = ? LIMIT 1"))
{
statement.setInt(1, credits);
statement.setInt(2, userId);
statement.execute();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
}
}
public void staffAlert(String message)
{
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");
}
}