Search in sources :

Example 1 with HomeDelayTask

use of com.alessiodp.parties.common.tasks.HomeDelayTask in project Parties by AlessioDP.

the class CommandHome method onCommand.

@Override
public void onCommand(@NotNull CommandData commandData) {
    User sender = commandData.getSender();
    PartyPlayerImpl partyPlayer = ((PartiesCommandData) commandData).getPartyPlayer();
    PartyImpl party = getPlugin().getPartyManager().getPartyOfPlayer(partyPlayer);
    // Command handling
    PartyHomeImpl partyHome = null;
    if (commandData.getArgs().length == 1) {
        if (party == null) {
            sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_NOTINPARTY);
            return;
        }
        if (party.getHomes().size() == 0) {
            sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
            return;
        }
    }
    if (ConfigParties.ADDITIONAL_HOME_MAX_HOMES > 1) {
        // Multiple home
        if (commandData.getArgs().length == 1) {
            if (party.getHomes().size() > 1) {
                sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_MUST_SELECT_HOME, party);
                printValidHomes(sender, partyPlayer, party);
                return;
            }
            Optional<PartyHome> opt = party.getHomes().stream().findFirst();
            if (opt.isPresent())
                partyHome = (PartyHomeImpl) opt.get();
            else {
                sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
                printValidHomes(sender, partyPlayer, party);
                return;
            }
        } else if (commandData.getArgs().length == 2) {
            // Not admin - Not in party
            if (!commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS) && party == null) {
                sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_NOTINPARTY);
                return;
            }
            // Get the partyHome of the current party
            if (party != null) {
                Optional<PartyHome> opt = party.getHomes().stream().filter((ph) -> ph.getName() != null && ph.getName().equalsIgnoreCase(commandData.getArgs()[1])).findAny();
                if (opt.isPresent())
                    partyHome = (PartyHomeImpl) opt.get();
            }
            // If no home but home.others permission
            if (partyHome == null && commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS)) {
                party = getPlugin().getPartyManager().getParty(commandData.getArgs()[1]);
                if (party != null) {
                    if (party.getHomes().size() > 1) {
                        sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_MUST_SELECT_HOME, party);
                        printValidHomes(sender, partyPlayer, party);
                        return;
                    }
                    Optional<PartyHome> opt = party.getHomes().stream().findFirst();
                    if (opt.isPresent())
                        partyHome = (PartyHomeImpl) opt.get();
                }
            }
            // Not in party
            if (party == null) {
                sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_NOTINPARTY);
                return;
            }
            // No party home found
            if (partyHome == null) {
                sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_INVALID_HOME, party);
                printValidHomes(sender, partyPlayer, party);
                return;
            }
        } else if (commandData.getArgs().length == 3 && commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS)) {
            party = getPlugin().getPartyManager().getParty(commandData.getArgs()[1]);
            if (party != null) {
                Optional<PartyHome> opt = party.getHomes().stream().filter((ph) -> ph.getName() != null && ph.getName().equalsIgnoreCase(commandData.getArgs()[1])).findAny();
                if (opt.isPresent())
                    partyHome = (PartyHomeImpl) opt.get();
                else {
                    sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
                    printValidHomes(sender, partyPlayer, party);
                    return;
                }
            } else {
                sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_PARTYNOTFOUND.replace("%party%", commandData.getArgs()[1]));
                return;
            }
        } else {
            sendMessage(sender, partyPlayer, Messages.PARTIES_SYNTAX_WRONG_MESSAGE.replace("%syntax%", getSyntaxForUser(sender)));
            return;
        }
    } else {
        // Single home
        if (commandData.getArgs().length == 1) {
            Optional<PartyHome> opt = party.getHomes().stream().findFirst();
            if (opt.isPresent())
                partyHome = (PartyHomeImpl) opt.get();
            else {
                sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
                return;
            }
        } else if (commandData.getArgs().length == 2 && commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS)) {
            party = getPlugin().getPartyManager().getParty(commandData.getArgs()[1]);
            if (party != null) {
                Optional<PartyHome> opt = party.getHomes().stream().findFirst();
                if (opt.isPresent())
                    partyHome = (PartyHomeImpl) opt.get();
                else {
                    sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_NOHOME, party);
                    return;
                }
            } else {
                sendMessage(sender, partyPlayer, Messages.PARTIES_COMMON_PARTYNOTFOUND.replace("%party%", commandData.getArgs()[1]));
                return;
            }
        } else {
            sendMessage(sender, partyPlayer, Messages.PARTIES_SYNTAX_WRONG_MESSAGE.replace("%syntax%", getSyntaxForUser(sender)));
            return;
        }
    }
    if (!commandData.havePermission(PartiesPermission.ADMIN_HOME_OTHERS) && !getPlugin().getRankManager().checkPlayerRankAlerter(partyPlayer, RankPermission.HOME))
        return;
    if (partyPlayer.getPendingHomeDelay() != null) {
        sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_TELEPORTWAITING, party);
        return;
    }
    boolean mustStartCooldown = false;
    if (ConfigParties.ADDITIONAL_HOME_COOLDOWN_HOME > 0 && !commandData.havePermission(PartiesPermission.ADMIN_COOLDOWN_HOME_BYPASS)) {
        mustStartCooldown = true;
        long remainingCooldown = getPlugin().getCooldownManager().canAction(CooldownManager.Action.HOME, sender.getUUID(), ConfigParties.ADDITIONAL_HOME_COOLDOWN_HOME);
        if (remainingCooldown > 0) {
            sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_COOLDOWN.replace("%seconds%", String.valueOf(remainingCooldown)));
            return;
        }
    }
    if (getPlugin().getEconomyManager().payCommand(EconomyManager.PaidCommand.HOME, partyPlayer, commandData.getCommandLabel(), commandData.getArgs()))
        return;
    if (mustStartCooldown)
        getPlugin().getCooldownManager().startAction(CooldownManager.Action.HOME, sender.getUUID(), ConfigParties.ADDITIONAL_HOME_COOLDOWN_HOME);
    // Command starts
    int delay = ConfigParties.ADDITIONAL_HOME_DELAY;
    String homeDelayPermission = sender.getDynamicPermission(PartiesPermission.USER_HOME + ".");
    if (homeDelayPermission != null) {
        try {
            delay = Integer.parseInt(homeDelayPermission);
        } catch (Exception ignored) {
        }
    }
    if (delay > 0) {
        HomeDelayTask homeDelayTask = teleportPlayerWithDelay(partyPlayer, partyHome, delay);
        CancellableTask task = plugin.getScheduler().scheduleAsyncRepeating(homeDelayTask, 0, 300, TimeUnit.MILLISECONDS);
        partyPlayer.setPendingHomeDelay(task);
        sendMessage(sender, partyPlayer, Messages.ADDCMD_HOME_TELEPORTIN.replace("%seconds%", Integer.toString(delay)));
    } else {
        teleportPlayer(sender, partyPlayer, partyHome);
    }
    plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_CMD_HOME, partyPlayer.getName(), party.getName() != null ? party.getName() : "_", CommonUtils.getNoEmptyOr(partyHome.getName(), "default")), true);
}
Also used : PartiesPermission(com.alessiodp.parties.common.utils.PartiesPermission) EconomyManager(com.alessiodp.parties.common.utils.EconomyManager) PartyPlayerImpl(com.alessiodp.parties.common.players.objects.PartyPlayerImpl) ConfigParties(com.alessiodp.parties.common.configuration.data.ConfigParties) CommonCommands(com.alessiodp.parties.common.commands.list.CommonCommands) PartyHomeImpl(com.alessiodp.parties.common.parties.objects.PartyHomeImpl) PartiesCommandData(com.alessiodp.parties.common.commands.utils.PartiesCommandData) ConfigMain(com.alessiodp.parties.common.configuration.data.ConfigMain) User(com.alessiodp.core.common.user.User) RankPermission(com.alessiodp.parties.common.utils.RankPermission) ADPPlugin(com.alessiodp.core.common.ADPPlugin) PartiesSubCommand(com.alessiodp.parties.common.commands.utils.PartiesSubCommand) CommonUtils(com.alessiodp.core.common.utils.CommonUtils) CooldownManager(com.alessiodp.parties.common.parties.CooldownManager) CommandData(com.alessiodp.core.common.commands.utils.CommandData) Messages(com.alessiodp.parties.common.configuration.data.Messages) HomeDelayTask(com.alessiodp.parties.common.tasks.HomeDelayTask) TimeUnit(java.util.concurrent.TimeUnit) PartyHome(com.alessiodp.parties.api.interfaces.PartyHome) PartyImpl(com.alessiodp.parties.common.parties.objects.PartyImpl) ADPMainCommand(com.alessiodp.core.common.commands.utils.ADPMainCommand) PartiesConstants(com.alessiodp.parties.common.configuration.PartiesConstants) Optional(java.util.Optional) NotNull(org.jetbrains.annotations.NotNull) CancellableTask(com.alessiodp.core.common.scheduling.CancellableTask) User(com.alessiodp.core.common.user.User) PartyPlayerImpl(com.alessiodp.parties.common.players.objects.PartyPlayerImpl) Optional(java.util.Optional) PartiesCommandData(com.alessiodp.parties.common.commands.utils.PartiesCommandData) CancellableTask(com.alessiodp.core.common.scheduling.CancellableTask) PartyHomeImpl(com.alessiodp.parties.common.parties.objects.PartyHomeImpl) PartyHome(com.alessiodp.parties.api.interfaces.PartyHome) PartyImpl(com.alessiodp.parties.common.parties.objects.PartyImpl) HomeDelayTask(com.alessiodp.parties.common.tasks.HomeDelayTask)

Aggregations

ADPPlugin (com.alessiodp.core.common.ADPPlugin)1 ADPMainCommand (com.alessiodp.core.common.commands.utils.ADPMainCommand)1 CommandData (com.alessiodp.core.common.commands.utils.CommandData)1 CancellableTask (com.alessiodp.core.common.scheduling.CancellableTask)1 User (com.alessiodp.core.common.user.User)1 CommonUtils (com.alessiodp.core.common.utils.CommonUtils)1 PartyHome (com.alessiodp.parties.api.interfaces.PartyHome)1 CommonCommands (com.alessiodp.parties.common.commands.list.CommonCommands)1 PartiesCommandData (com.alessiodp.parties.common.commands.utils.PartiesCommandData)1 PartiesSubCommand (com.alessiodp.parties.common.commands.utils.PartiesSubCommand)1 PartiesConstants (com.alessiodp.parties.common.configuration.PartiesConstants)1 ConfigMain (com.alessiodp.parties.common.configuration.data.ConfigMain)1 ConfigParties (com.alessiodp.parties.common.configuration.data.ConfigParties)1 Messages (com.alessiodp.parties.common.configuration.data.Messages)1 CooldownManager (com.alessiodp.parties.common.parties.CooldownManager)1 PartyHomeImpl (com.alessiodp.parties.common.parties.objects.PartyHomeImpl)1 PartyImpl (com.alessiodp.parties.common.parties.objects.PartyImpl)1 PartyPlayerImpl (com.alessiodp.parties.common.players.objects.PartyPlayerImpl)1 HomeDelayTask (com.alessiodp.parties.common.tasks.HomeDelayTask)1 EconomyManager (com.alessiodp.parties.common.utils.EconomyManager)1