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

326 lines
14 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.core.CommandLog;
import com.eu.habbo.habbohotel.gameclients.GameClient;
2018-09-12 18:45:00 +02:00
import com.eu.habbo.habbohotel.permissions.Permission;
import com.eu.habbo.habbohotel.permissions.PermissionSetting;
import com.eu.habbo.habbohotel.pets.Pet;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.pets.PetCommand;
import com.eu.habbo.habbohotel.pets.PetVocalsType;
import com.eu.habbo.habbohotel.pets.RideablePet;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.rooms.Room;
2020-11-24 14:40:42 +01:00
import com.eu.habbo.habbohotel.rooms.RoomRightLevels;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.messages.outgoing.rooms.users.RoomUserTypingComposer;
import com.eu.habbo.plugin.events.users.UserCommandEvent;
import com.eu.habbo.plugin.events.users.UserExecuteCommandEvent;
import gnu.trove.iterator.TIntObjectIterator;
import gnu.trove.map.hash.THashMap;
2020-05-03 01:46:07 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public class CommandHandler {
2020-05-03 01:46:07 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(CommandHandler.class);
2018-09-28 21:25:00 +02:00
private final static THashMap<String, Command> commands = new THashMap<>(5);
2019-05-26 20:14:53 +02:00
private static final Comparator<Command> ALPHABETICAL_ORDER = new Comparator<Command>() {
public int compare(Command c1, Command c2) {
int res = String.CASE_INSENSITIVE_ORDER.compare(c1.permission, c2.permission);
return (res != 0) ? res : c1.permission.compareTo(c2.permission);
}
};
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public CommandHandler() {
2018-07-06 15:30:00 +02:00
long millis = System.currentTimeMillis();
2019-03-18 02:22:00 +01:00
this.reloadCommands();
2020-05-03 01:46:07 +02:00
LOGGER.info("Command Handler -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public static void addCommand(Command command) {
if (command == null)
return;
commands.put(command.getClass().getName(), command);
}
public static void addCommand(Class<? extends Command> command) {
try {
//command.getConstructor().setAccessible(true);
addCommand(command.newInstance());
2020-05-03 01:46:07 +02:00
LOGGER.debug("Added command: {}", command.getName());
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2020-05-03 01:46:07 +02:00
LOGGER.error("Caught exception", e);
2019-05-26 20:14:53 +02:00
}
}
public static boolean handleCommand(GameClient gameClient, String commandLine) {
if (gameClient != null) {
if (commandLine.startsWith(":")) {
commandLine = commandLine.replaceFirst(":", "");
String[] parts = commandLine.split(" ");
if (parts.length >= 1) {
for (Command command : commands.values()) {
for (String s : command.keys) {
if (s.toLowerCase().equals(parts[0].toLowerCase())) {
boolean succes = false;
2020-11-24 14:40:42 +01:00
if (command.permission == null || gameClient.getHabbo().hasPermission(command.permission, gameClient.getHabbo().getHabboInfo().getCurrentRoom() != null && (gameClient.getHabbo().getHabboInfo().getCurrentRoom().hasRights(gameClient.getHabbo())) || gameClient.getHabbo().hasPermission(Permission.ACC_PLACEFURNI) || (gameClient.getHabbo().getHabboInfo().getCurrentRoom() != null && gameClient.getHabbo().getHabboInfo().getCurrentRoom().getGuildId() > 0 && gameClient.getHabbo().getHabboInfo().getCurrentRoom().getGuildRightLevel(gameClient.getHabbo()).isEqualOrGreaterThan(RoomRightLevels.GUILD_RIGHTS)))) {
2019-05-26 20:14:53 +02:00
try {
2020-11-10 22:13:06 +01:00
UserExecuteCommandEvent userExecuteCommandEvent = new UserExecuteCommandEvent(gameClient.getHabbo(), command, parts);
Emulator.getPluginManager().fireEvent(userExecuteCommandEvent);
if(userExecuteCommandEvent.isCancelled()) {
return userExecuteCommandEvent.isSuccess();
}
2019-05-26 20:14:53 +02:00
if (gameClient.getHabbo().getHabboInfo().getCurrentRoom() != null)
gameClient.getHabbo().getHabboInfo().getCurrentRoom().sendComposer(new RoomUserTypingComposer(gameClient.getHabbo().getRoomUnit(), false).compose());
UserCommandEvent event = new UserCommandEvent(gameClient.getHabbo(), parts, command.handle(gameClient, parts));
Emulator.getPluginManager().fireEvent(event);
succes = event.succes;
} catch (Exception e) {
2020-05-03 01:46:07 +02:00
LOGGER.error("Caught exception", e);
2019-05-26 20:14:53 +02:00
}
if (gameClient.getHabbo().getHabboInfo().getRank().isLogCommands()) {
2020-05-03 01:46:07 +02:00
Emulator.getDatabaseLogger().store(new CommandLog(gameClient.getHabbo().getHabboInfo().getId(), command, commandLine, succes));
2019-05-26 20:14:53 +02:00
}
}
return succes;
}
}
}
}
} else {
String[] args = commandLine.split(" ");
if (args.length <= 1)
return false;
if (gameClient.getHabbo().getHabboInfo().getCurrentRoom() != null) {
Room room = gameClient.getHabbo().getHabboInfo().getCurrentRoom();
if (room.getCurrentPets().isEmpty())
return false;
TIntObjectIterator<Pet> petIterator = room.getCurrentPets().iterator();
for (int j = room.getCurrentPets().size(); j-- > 0; ) {
try {
petIterator.advance();
} catch (NoSuchElementException e) {
break;
}
Pet pet = petIterator.value();
if (pet != null) {
if (pet.getName().equalsIgnoreCase(args[0])) {
StringBuilder s = new StringBuilder();
for (int i = 1; i < args.length; i++) {
s.append(args[i]).append(" ");
}
s = new StringBuilder(s.substring(0, s.length() - 1));
for (PetCommand command : pet.getPetData().getPetCommands()) {
if (command.key.equalsIgnoreCase(s.toString())) {
if (pet instanceof RideablePet && ((RideablePet) pet).getRider() != null) {
if (((RideablePet) pet).getRider().getHabboInfo().getId() == gameClient.getHabbo().getHabboInfo().getId()) {
((RideablePet) pet).getRider().getHabboInfo().dismountPet();
}
break;
}
if (command.level <= pet.getLevel())
pet.handleCommand(command, gameClient.getHabbo(), args);
else
pet.say(pet.getPetData().randomVocal(PetVocalsType.UNKNOWN_COMMAND));
break;
}
}
}
}
}
}
}
}
return false;
}
public static Command getCommand(String key) {
for (Command command : commands.values()) {
for (String k : command.keys) {
if (key.equalsIgnoreCase(k)) {
return command;
}
}
}
return null;
}
public void reloadCommands() {
2018-07-06 15:30:00 +02:00
addCommand(new AboutCommand());
addCommand(new AlertCommand());
addCommand(new AllowTradingCommand());
addCommand(new ArcturusCommand());
addCommand(new BadgeCommand());
addCommand(new BanCommand());
addCommand(new BlockAlertCommand());
addCommand(new BotsCommand());
2018-09-12 18:45:00 +02:00
addCommand(new CalendarCommand());
2018-07-06 15:30:00 +02:00
addCommand(new ChangeNameCommand());
addCommand(new ChatTypeCommand());
addCommand(new CommandsCommand());
addCommand(new ConnectCameraCommand());
addCommand(new ControlCommand());
addCommand(new CoordsCommand());
addCommand(new CreditsCommand());
addCommand(new DiagonalCommand());
addCommand(new DisconnectCommand());
addCommand(new EjectAllCommand());
addCommand(new EmptyInventoryCommand());
addCommand(new EmptyBotsInventoryCommand());
addCommand(new EmptyPetsInventoryCommand());
addCommand(new EnableCommand());
addCommand(new EventCommand());
addCommand(new FacelessCommand());
addCommand(new FastwalkCommand());
2018-10-07 00:28:00 +02:00
addCommand(new FilterWordCommand());
2018-07-06 15:30:00 +02:00
addCommand(new FreezeBotsCommand());
addCommand(new FreezeCommand());
addCommand(new GiftCommand());
addCommand(new GiveRankCommand());
addCommand(new HabnamCommand());
addCommand(new HandItemCommand());
addCommand(new HappyHourCommand());
2018-09-28 21:25:00 +02:00
addCommand(new HideWiredCommand());
2018-07-06 15:30:00 +02:00
addCommand(new HotelAlertCommand());
addCommand(new HotelAlertLinkCommand());
2018-09-28 21:25:00 +02:00
addCommand(new InvisibleCommand());
2018-07-06 15:30:00 +02:00
addCommand(new IPBanCommand());
addCommand(new LayCommand());
addCommand(new MachineBanCommand());
addCommand(new MassBadgeCommand());
2021-01-06 20:39:30 +01:00
addCommand(new RoomBadgeCommand());
2018-07-06 15:30:00 +02:00
addCommand(new MassCreditsCommand());
addCommand(new MassGiftCommand());
addCommand(new MassPixelsCommand());
addCommand(new MassPointsCommand());
addCommand(new MimicCommand());
addCommand(new MoonwalkCommand());
addCommand(new MultiCommand());
addCommand(new MuteBotsCommand());
addCommand(new MuteCommand());
addCommand(new MutePetsCommand());
addCommand(new PetInfoCommand());
addCommand(new PickallCommand());
addCommand(new PixelCommand());
addCommand(new PluginsCommand());
addCommand(new PointsCommand());
2018-10-07 00:28:00 +02:00
addCommand(new PromoteTargetOfferCommand());
2018-07-06 15:30:00 +02:00
addCommand(new PullCommand());
addCommand(new PushCommand());
addCommand(new RedeemCommand());
addCommand(new ReloadRoomCommand());
addCommand(new RoomAlertCommand());
addCommand(new RoomBundleCommand());
addCommand(new RoomCreditsCommand());
addCommand(new RoomDanceCommand());
addCommand(new RoomEffectCommand());
addCommand(new RoomItemCommand());
addCommand(new RoomKickCommand());
addCommand(new RoomMuteCommand());
addCommand(new RoomPixelsCommand());
addCommand(new RoomPointsCommand());
addCommand(new SayAllCommand());
addCommand(new SayCommand());
addCommand(new SetMaxCommand());
addCommand(new SetPollCommand());
addCommand(new SetSpeedCommand());
addCommand(new ShoutAllCommand());
addCommand(new ShoutCommand());
addCommand(new ShutdownCommand());
addCommand(new SitCommand());
2020-03-13 18:00:48 +01:00
addCommand(new StandCommand());
2018-07-06 15:30:00 +02:00
addCommand(new SitDownCommand());
addCommand(new StaffAlertCommand());
addCommand(new StaffOnlineCommand());
addCommand(new StalkCommand());
addCommand(new SummonCommand());
addCommand(new SummonRankCommand());
addCommand(new SuperbanCommand());
addCommand(new SuperPullCommand());
addCommand(new TakeBadgeCommand());
addCommand(new TeleportCommand());
addCommand(new TransformCommand());
addCommand(new TrashCommand());
addCommand(new UnbanCommand());
addCommand(new UnloadRoomCommand());
addCommand(new UnmuteCommand());
2019-04-22 01:42:00 +02:00
addCommand(new UpdateAchievements());
2018-07-06 15:30:00 +02:00
addCommand(new UpdateBotsCommand());
2022-03-21 19:51:52 +01:00
addCommand(new UpdateCalendarCommand());
2018-07-06 15:30:00 +02:00
addCommand(new UpdateCatalogCommand());
addCommand(new UpdateConfigCommand());
addCommand(new UpdateGuildPartsCommand());
addCommand(new UpdateHotelViewCommand());
addCommand(new UpdateItemsCommand());
addCommand(new UpdateNavigatorCommand());
addCommand(new UpdatePermissionsCommand());
addCommand(new UpdatePetDataCommand());
addCommand(new UpdatePluginsCommand());
addCommand(new UpdatePollsCommand());
addCommand(new UpdateTextsCommand());
addCommand(new UpdateWordFilterCommand());
addCommand(new UserInfoCommand());
addCommand(new WordQuizCommand());
addCommand(new UpdateYoutubePlaylistsCommand());
addCommand(new AddYoutubePlaylistCommand());
addCommand(new SoftKickCommand());
addCommand(new SubscriptionCommand());
2018-07-06 15:30:00 +02:00
addCommand(new TestCommand());
}
2019-05-26 20:14:53 +02:00
public List<Command> getCommandsForRank(int rankId) {
2018-09-28 21:25:00 +02:00
List<Command> allowedCommands = new ArrayList<>();
2019-05-26 20:14:53 +02:00
if (Emulator.getGameEnvironment().getPermissionsManager().rankExists(rankId)) {
2018-09-12 18:45:00 +02:00
THashMap<String, Permission> permissions = Emulator.getGameEnvironment().getPermissionsManager().getRank(rankId).getPermissions();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (Command command : commands.values()) {
2018-07-06 15:30:00 +02:00
if (allowedCommands.contains(command))
continue;
2019-05-26 20:14:53 +02:00
if (permissions.contains(command.permission) && permissions.get(command.permission).setting != PermissionSetting.DISALLOWED) {
2018-07-06 15:30:00 +02:00
allowedCommands.add(command);
}
}
}
2019-03-18 02:22:00 +01:00
allowedCommands.sort(CommandHandler.ALPHABETICAL_ORDER);
2018-07-06 15:30:00 +02:00
return allowedCommands;
}
2019-05-26 20:14:53 +02:00
public void dispose() {
2018-07-06 15:30:00 +02:00
commands.clear();
2020-05-03 01:46:07 +02:00
LOGGER.info("Command Handler -> Disposed!");
2018-07-06 15:30:00 +02:00
}
2020-11-24 14:40:42 +01:00
}