Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/commands/BadgeCommand.java

87 lines
4.0 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.commands;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.rooms.RoomChatMessageBubbles;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboInfo;
import com.eu.habbo.habbohotel.users.HabboManager;
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;
2019-05-26 20:14:53 +02:00
public class BadgeCommand extends Command {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(BadgeCommand.class);
2019-05-26 20:14:53 +02:00
public BadgeCommand() {
2018-07-06 15:30:00 +02:00
super("cmd_badge", Emulator.getTexts().getValue("commands.keys.cmd_badge").split(";"));
}
@Override
2019-05-26 20:14:53 +02:00
public boolean handle(GameClient gameClient, String[] params) throws Exception {
if (params.length == 1) {
2018-07-06 15:30:00 +02:00
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_badge.forgot_username"), RoomChatMessageBubbles.ALERT);
return true;
}
2019-05-26 20:14:53 +02:00
if (params.length == 2) {
2018-07-06 15:30:00 +02:00
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_badge.forgot_badge").replace("%user%", params[1]), RoomChatMessageBubbles.ALERT);
return true;
}
2019-05-26 20:14:53 +02:00
if (params.length == 3) {
2018-07-06 15:30:00 +02:00
Habbo habbo = Emulator.getGameEnvironment().getHabboManager().getHabbo(params[1]);
2019-05-26 20:14:53 +02:00
if (habbo != null) {
if (habbo.addBadge(params[2])) {
2019-04-22 01:42:00 +02:00
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.succes.cmd_badge.given").replace("%user%", params[1]).replace("%badge%", params[2]), RoomChatMessageBubbles.ALERT);
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_badge.already_owned").replace("%user%", params[1]).replace("%badge%", params[2]), RoomChatMessageBubbles.ALERT);
}
return true;
2019-05-26 20:14:53 +02:00
} else {
HabboInfo habboInfo = HabboManager.getOfflineHabboInfo(params[1]);
if (habboInfo == null) {
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_badge.unknown_user"), RoomChatMessageBubbles.ALERT);
return true;
}
2019-05-26 20:14:53 +02:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) {
2019-03-18 02:22:00 +01:00
boolean found;
2018-07-06 15:30:00 +02:00
try (PreparedStatement statement = connection.prepareStatement("SELECT `badge_code` FROM `users_badges` WHERE `user_id` = ? AND `badge_code` = ? LIMIT 1")) {
statement.setInt(1, habboInfo.getId());
2018-07-06 15:30:00 +02:00
statement.setString(2, params[2]);
2019-05-26 20:14:53 +02:00
try (ResultSet set = statement.executeQuery()) {
2018-07-06 15:30:00 +02:00
found = set.next();
}
}
2019-05-26 20:14:53 +02:00
if (found) {
2018-07-06 15:30:00 +02:00
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.error.cmd_badge.already_owns").replace("%user%", params[1]).replace("%badge%", params[2]), RoomChatMessageBubbles.ALERT);
return true;
2019-05-26 20:14:53 +02:00
} else {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO users_badges (`id`, `user_id`, `slot_id`, `badge_code`) VALUES (null, ?, 0, ?)")) {
statement.setInt(1, habboInfo.getId());
2018-07-06 15:30:00 +02:00
statement.setString(2, params[2]);
statement.execute();
}
gameClient.getHabbo().whisper(Emulator.getTexts().getValue("commands.succes.cmd_badge.given").replace("%user%", params[1]).replace("%badge%", params[2]), RoomChatMessageBubbles.ALERT);
return true;
}
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
}
}
}
return true;
}
}