Search in sources :

Example 1 with IPartyPreRenameEvent

use of com.alessiodp.parties.api.events.common.party.IPartyPreRenameEvent in project Parties by AlessioDP.

the class CommandRename method onCommand.

@Override
public void onCommand(CommandData commandData) {
    User sender = commandData.getSender();
    PartyPlayerImpl partyPlayer = ((PartiesCommandData) commandData).getPartyPlayer();
    // Command handling
    PartyImpl party = null;
    String partyName;
    if (commandData.getArgs().length == 2) {
        // 2 arguments - own party
        if (partyPlayer.isInParty())
            party = getPlugin().getPartyManager().getParty(partyPlayer.getPartyId());
        if (party == null) {
            sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_NOTINPARTY);
            return;
        }
        if (!getPlugin().getRankManager().checkPlayerRankAlerter(partyPlayer, RankPermission.ADMIN_RENAME))
            return;
        partyName = commandData.getArgs()[1];
    } else {
        // 3 arguments - another party
        if (!sender.isPlayer() || commandData.havePermission(PartiesPermission.ADMIN_RENAME_OTHERS)) {
            party = getPlugin().getPartyManager().getParty(commandData.getArgs()[1]);
        }
        if (party == null) {
            sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_PARTYNOTFOUND.replace("%party%", commandData.getArgs()[1]));
            return;
        }
        partyName = commandData.getArgs()[2];
    }
    if (partyName.length() > ConfigParties.GENERAL_NAME_MAXLENGTH) {
        sendMessage(sender, partyPlayer, Messages.MAINCMD_CREATE_NAMETOOLONG);
        return;
    }
    if (partyName.length() < ConfigParties.GENERAL_NAME_MINLENGTH) {
        sendMessage(sender, partyPlayer, Messages.MAINCMD_CREATE_NAMETOOSHORT);
        return;
    }
    if (!CensorUtils.checkAllowedCharacters(ConfigParties.GENERAL_NAME_ALLOWEDCHARS, partyName, PartiesConstants.DEBUG_CMD_CREATE_REGEXERROR_ALLOWEDCHARS)) {
        sendMessage(sender, partyPlayer, Messages.MAINCMD_CREATE_INVALIDNAME);
        return;
    }
    if (CensorUtils.checkCensor(ConfigParties.GENERAL_NAME_CENSORREGEX, partyName, PartiesConstants.DEBUG_CMD_CREATE_REGEXERROR_CENSORED)) {
        sendMessage(sender, partyPlayer, Messages.MAINCMD_CREATE_CENSORED);
        return;
    }
    if (getPlugin().getPartyManager().existsParty(partyName)) {
        sendMessage(sender, partyPlayer, Messages.MAINCMD_CREATE_NAMEEXISTS.replace("%party%", partyName));
        return;
    }
    boolean mustStartCooldown = false;
    if (ConfigParties.GENERAL_NAME_RENAME_COOLDOWN > 0 && !commandData.havePermission(PartiesPermission.ADMIN_COOLDOWN_RENAME_BYPASS)) {
        mustStartCooldown = true;
        long remainingCooldown = getPlugin().getCooldownManager().canAction(CooldownManager.Action.RENAME, party.getId(), ConfigParties.GENERAL_NAME_RENAME_COOLDOWN);
        if (remainingCooldown > 0) {
            sendMessage(sender, partyPlayer, Messages.MAINCMD_RENAME_COOLDOWN.replace("%seconds%", String.valueOf(remainingCooldown)));
            return;
        }
    }
    if (partyPlayer != null && getPlugin().getEconomyManager().payCommand(EconomyManager.PaidCommand.RENAME, partyPlayer, commandData.getCommandLabel(), commandData.getArgs()))
        return;
    if (mustStartCooldown) {
        getPlugin().getCooldownManager().startAction(CooldownManager.Action.RENAME, party.getId(), ConfigParties.GENERAL_NAME_RENAME_COOLDOWN);
    }
    // Command starts
    String oldPartyName = party.getName();
    // Calling API event
    IPartyPreRenameEvent partiesPreRenameEvent = getPlugin().getEventManager().preparePartyPreRenameEvent(party, party.getName(), partyName, partyPlayer, commandData.getArgs().length > 2);
    getPlugin().getEventManager().callEvent(partiesPreRenameEvent);
    partyName = partiesPreRenameEvent.getNewPartyName();
    if (!partiesPreRenameEvent.isCancelled()) {
        party.rename(partyName, partyPlayer, commandData.getArgs().length > 2);
        sendMessage(sender, partyPlayer, Messages.MAINCMD_RENAME_RENAMED.replace("%old%", CommonUtils.getOr(oldPartyName, "null")), party);
        party.broadcastMessage(Messages.MAINCMD_RENAME_BROADCAST.replace("%old%", CommonUtils.getOr(oldPartyName, "null")), partyPlayer);
        plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_CMD_RENAME, sender.getName(), oldPartyName, party.getName()), true);
    } else {
        plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_API_RENAMEEVENT_DENY, party.getName(), oldPartyName, sender.getUUID().toString()), true);
    }
}
Also used : IPartyPreRenameEvent(com.alessiodp.parties.api.events.common.party.IPartyPreRenameEvent) User(com.alessiodp.core.common.user.User) PartyPlayerImpl(com.alessiodp.parties.common.players.objects.PartyPlayerImpl) PartiesCommandData(com.alessiodp.parties.common.commands.utils.PartiesCommandData) PartyImpl(com.alessiodp.parties.common.parties.objects.PartyImpl)

Aggregations

User (com.alessiodp.core.common.user.User)1 IPartyPreRenameEvent (com.alessiodp.parties.api.events.common.party.IPartyPreRenameEvent)1 PartiesCommandData (com.alessiodp.parties.common.commands.utils.PartiesCommandData)1 PartyImpl (com.alessiodp.parties.common.parties.objects.PartyImpl)1 PartyPlayerImpl (com.alessiodp.parties.common.players.objects.PartyPlayerImpl)1