Search in sources :

Example 1 with Pair

use of com.alessiodp.core.common.utils.Pair in project Parties by AlessioDP.

the class BukkitExpManager method shareExperience.

/**
 * Share the experience if sharing system enabled
 *
 * @return Returns false if the player that should get the experience is only one
 */
private boolean shareExperience(double experience, PartyPlayerImpl killer, PartyImpl party, Entity killedMob, ExpConvert type) {
    boolean ret = false;
    if (BukkitConfigMain.ADDITIONAL_EXP_DROP_SHARING_ENABLE) {
        List<Pair<PartyPlayerImpl, Integer>> playersToShare = new ArrayList<>();
        Location mobKilledLocation = killedMob.getLocation();
        // Get nearest player
        for (PartyPlayer player : party.getOnlineMembers(true)) {
            try {
                // Note, this will add all near players + killer
                double distance = Bukkit.getPlayer(player.getPlayerUUID()).getLocation().distanceSquared(mobKilledLocation);
                if (distance <= rangeSquared || player.getPlayerUUID().equals(killer.getPlayerUUID())) {
                    playersToShare.add(new Pair<>((PartyPlayerImpl) player, (int) distance));
                }
            }// Ignoring different world
             catch (Exception ignored) {
            }
        }
        if (playersToShare.size() > BukkitConfigMain.ADDITIONAL_EXP_DROP_SHARING_IFMORETHAN) {
            // Calculate experience for each player
            String formulaKiller = BukkitConfigMain.ADDITIONAL_EXP_DROP_SHARING_DIVIDEFORMULA_KILLER.replace("%exp%", Double.toString(experience)).replace("%number_players%", Integer.toString(playersToShare.size())).replace("%level%", Integer.toString(party.getLevel()));
            String formulaOthers = BukkitConfigMain.ADDITIONAL_EXP_DROP_SHARING_DIVIDEFORMULA_OTHERS.replace("%exp%", Double.toString(experience)).replace("%number_players%", Integer.toString(playersToShare.size())).replace("%level%", Integer.toString(party.getLevel()));
            try {
                for (Pair<PartyPlayerImpl, Integer> pShare : playersToShare) {
                    Player bukkitPlayer = Bukkit.getPlayer(pShare.getKey().getPlayerUUID());
                    User user = plugin.getPlayer(pShare.getKey().getPlayerUUID());
                    if (bukkitPlayer != null && user != null) {
                        double effectiveExperience;
                        if (pShare.getKey().getPlayerUUID().equals(killer.getPlayerUUID())) {
                            effectiveExperience = Double.parseDouble(FormulaUtils.calculate(formulaKiller.replace("%distance%", Integer.toString(pShare.getValue()))));
                        } else {
                            effectiveExperience = Double.parseDouble(FormulaUtils.calculate(formulaOthers.replace("%distance%", Integer.toString(pShare.getValue()))));
                        }
                        if (BukkitConfigMain.ADDITIONAL_EXP_DROP_SHARING_ROUND_EXP_DROP)
                            effectiveExperience = (int) effectiveExperience;
                        String message = "";
                        switch(type) {
                            case NORMAL:
                                bukkitPlayer.giveExp((int) effectiveExperience);
                                message = pShare.getKey().getPlayerUUID().equals(killer.getPlayerUUID()) ? BukkitMessages.ADDCMD_EXP_NORMAL_GAINED_KILLER : BukkitMessages.ADDCMD_EXP_NORMAL_GAINED_OTHERS;
                                break;
                            case LEVELPOINTS:
                                LevelPointsHandler.giveExp(bukkitPlayer, effectiveExperience);
                                message = pShare.getKey().getPlayerUUID().equals(killer.getPlayerUUID()) ? BukkitMessages.ADDCMD_EXP_LEVELPOINTS_GAINED_KILLER : BukkitMessages.ADDCMD_EXP_LEVELPOINTS_GAINED_OTHERS;
                                break;
                            case MMOCORE:
                                MMOCoreHandler.giveExp(pShare.getKey().getPlayerUUID(), bukkitPlayer.getLocation(), effectiveExperience);
                                message = pShare.getKey().getPlayerUUID().equals(killer.getPlayerUUID()) ? BukkitMessages.ADDCMD_EXP_MMOCORE_GAINED_KILLER : BukkitMessages.ADDCMD_EXP_MMOCORE_GAINED_OTHERS;
                                break;
                            case SKILLAPI:
                                SkillAPIHandler.giveExp(pShare.getKey().getPlayerUUID(), effectiveExperience);
                                message = pShare.getKey().getPlayerUUID().equals(killer.getPlayerUUID()) ? BukkitMessages.ADDCMD_EXP_SKILLAPI_GAINED_KILLER : BukkitMessages.ADDCMD_EXP_SKILLAPI_GAINED_OTHERS;
                                break;
                            default:
                                // Nothing to do
                                break;
                        }
                        user.sendMessage(plugin.getMessageUtils().convertPlaceholders(message.replace("%exp%", CommonUtils.formatDouble(effectiveExperience)).replace("%total_exp%", CommonUtils.formatDouble(experience)), killer, party), true);
                        plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_EXP_SENT, effectiveExperience, type.name(), pShare.getKey().getName(), pShare.getKey().getPlayerUUID()), true);
                    }
                }
                ret = true;
            } catch (Exception ex) {
                plugin.getLoggerManager().printError(String.format(PartiesConstants.DEBUG_EXP_EXPRESSIONERROR, formulaKiller, formulaOthers, ex.getMessage()));
                ex.printStackTrace();
            }
        }
    }
    return ret;
}
Also used : Player(org.bukkit.entity.Player) PartyPlayer(com.alessiodp.parties.api.interfaces.PartyPlayer) User(com.alessiodp.core.common.user.User) PartyPlayer(com.alessiodp.parties.api.interfaces.PartyPlayer) PartyPlayerImpl(com.alessiodp.parties.common.players.objects.PartyPlayerImpl) ArrayList(java.util.ArrayList) Pair(com.alessiodp.core.common.utils.Pair) Location(org.bukkit.Location)

Example 2 with Pair

use of com.alessiodp.core.common.utils.Pair in project Parties by AlessioDP.

the class CommandList method onCommand.

@Override
public void onCommand(CommandData commandData) {
    User sender = commandData.getSender();
    PartyPlayerImpl player = ((PartiesCommandData) commandData).getPartyPlayer();
    // Command handling
    int selectedPage = 1;
    PartiesDatabaseManager.ListOrder orderBy = null;
    if (commandData.getArgs().length > 1) {
        if (commandData.getArgs().length == 2) {
            try {
                selectedPage = Integer.parseInt(commandData.getArgs()[1]);
            } catch (NumberFormatException ignored) {
                orderBy = PartiesDatabaseManager.ListOrder.parse(commandData.getArgs()[1]);
                if (orderBy == null) {
                    sendMessage(sender, player, Messages.PARTIES_SYNTAX_WRONG_MESSAGE.replace("%syntax%", getSyntaxForUser(sender)));
                    return;
                }
            }
        } else if (commandData.getArgs().length == 3 && ConfigParties.ADDITIONAL_LIST_CHANGE_ORDER) {
            orderBy = PartiesDatabaseManager.ListOrder.parse(commandData.getArgs()[1]);
            if (orderBy == null) {
                sendMessage(sender, player, Messages.ADDCMD_LIST_INVALID_ORDER);
                return;
            }
            try {
                selectedPage = Integer.parseInt(commandData.getArgs()[2]);
            } catch (NumberFormatException ignored) {
                sendMessage(sender, player, Messages.PARTIES_SYNTAX_WRONG_MESSAGE.replace("%syntax%", getSyntaxForUser(sender)));
                return;
            }
        } else {
            sendMessage(sender, player, Messages.PARTIES_SYNTAX_WRONG_MESSAGE.replace("%syntax%", getSyntaxForUser(sender)));
            return;
        }
    }
    if (orderBy != null) {
        if (!commandData.havePermission(permission) && ((orderBy == PartiesDatabaseManager.ListOrder.NAME && !commandData.havePermission(PartiesPermission.USER_LIST_NAME)) || (orderBy == PartiesDatabaseManager.ListOrder.ONLINE_MEMBERS && !commandData.havePermission(PartiesPermission.USER_LIST_ONLINE_MEMBERS)) || (orderBy == PartiesDatabaseManager.ListOrder.MEMBERS && !commandData.havePermission(PartiesPermission.USER_LIST_MEMBERS)) || (orderBy == PartiesDatabaseManager.ListOrder.KILLS && !commandData.havePermission(PartiesPermission.USER_LIST_KILLS)) || (orderBy == PartiesDatabaseManager.ListOrder.EXPERIENCE && !commandData.havePermission(PartiesPermission.USER_LIST_EXPERIENCE)))) {
            sendMessage(sender, player, Messages.ADDCMD_LIST_INVALID_ORDER);
            return;
        }
    } else {
        if (commandData.havePermission(permission))
            orderBy = PartiesDatabaseManager.ListOrder.getType(ConfigParties.ADDITIONAL_LIST_ORDERBY);
        else if (commandData.havePermission(PartiesPermission.USER_LIST_NAME))
            orderBy = PartiesDatabaseManager.ListOrder.NAME;
        else if (commandData.havePermission(PartiesPermission.USER_LIST_ONLINE_MEMBERS))
            orderBy = PartiesDatabaseManager.ListOrder.ONLINE_MEMBERS;
        else if (commandData.havePermission(PartiesPermission.USER_LIST_MEMBERS))
            orderBy = PartiesDatabaseManager.ListOrder.MEMBERS;
        else if (commandData.havePermission(PartiesPermission.USER_LIST_KILLS))
            orderBy = PartiesDatabaseManager.ListOrder.KILLS;
        else if (commandData.havePermission(PartiesPermission.USER_LIST_EXPERIENCE))
            orderBy = PartiesDatabaseManager.ListOrder.EXPERIENCE;
        if (orderBy == null)
            orderBy = PartiesDatabaseManager.ListOrder.ONLINE_MEMBERS;
    }
    // Command starts
    int numberPlayers = Math.min(orderBy == PartiesDatabaseManager.ListOrder.ONLINE_MEMBERS ? getPlugin().getPartyManager().getCacheParties().size() : getPlugin().getDatabaseManager().getListPartiesNumber(), ConfigParties.ADDITIONAL_LIST_LIMITPARTIES);
    int limit = Math.max(1, ConfigParties.ADDITIONAL_LIST_PERPAGE);
    int maxPages;
    if (numberPlayers == 0)
        maxPages = 1;
    else if (numberPlayers % ConfigParties.ADDITIONAL_LIST_PERPAGE == 0)
        maxPages = numberPlayers / ConfigParties.ADDITIONAL_LIST_PERPAGE;
    else
        maxPages = (numberPlayers / ConfigParties.ADDITIONAL_LIST_PERPAGE) + 1;
    if (selectedPage > maxPages)
        selectedPage = maxPages;
    int offset = selectedPage > 1 ? limit * (selectedPage - 1) : 0;
    LinkedHashSet<Pair<Integer, PartyImpl>> parties = new LinkedHashSet<>();
    if (orderBy == PartiesDatabaseManager.ListOrder.ONLINE_MEMBERS) {
        Set<PartyImpl> onlineParties = new TreeSet<>(Comparator.comparing(p -> ((PartyImpl) p).getOnlineMembers(false).size()).reversed().thenComparing(p -> ((PartyImpl) p).getName()).thenComparing(p -> ((PartyImpl) p).getId()));
        getPlugin().getPartyManager().getCacheParties().values().forEach((party) -> {
            if (party.getName() != null && !ConfigParties.ADDITIONAL_LIST_HIDDENPARTIES.contains(party.getName()) && !ConfigParties.ADDITIONAL_LIST_HIDDENPARTIES.contains(party.getId().toString())) {
                onlineParties.add(party);
            }
        });
        // Limit and offset
        Iterator<PartyImpl> iterator = onlineParties.iterator();
        int n = 0;
        for (int c = 0; iterator.hasNext() && n < limit; c++) {
            PartyImpl p = iterator.next();
            if (c >= offset) {
                parties.add(new Pair<>(c + 1, p));
                n++;
            }
        }
    } else {
        int index = 1;
        for (PartyImpl party : getPlugin().getDatabaseManager().getListParties(orderBy, limit, offset)) {
            parties.add(new Pair<>(index, party));
            index++;
        }
    }
    sendMessage(sender, player, Messages.ADDCMD_LIST_HEADER.replace("%total%", Integer.toString(numberPlayers)).replace("%page%", Integer.toString(selectedPage)).replace("%maxpages%", Integer.toString(maxPages)));
    if (parties.size() > 0) {
        parties.forEach((pair) -> {
            pair.getValue().refresh();
            sendMessage(sender, player, Messages.ADDCMD_LIST_FORMATPARTY.replace("%index%", pair.getKey().toString()), pair.getValue());
        });
    } else {
        sendMessage(sender, player, Messages.ADDCMD_LIST_NOONE);
    }
    sendMessage(sender, player, Messages.ADDCMD_LIST_FOOTER.replace("%total%", Integer.toString(numberPlayers)).replace("%page%", Integer.toString(selectedPage)).replace("%maxpages%", Integer.toString(maxPages)));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PartiesPermission(com.alessiodp.parties.common.utils.PartiesPermission) CommandData(com.alessiodp.core.common.commands.utils.CommandData) Messages(com.alessiodp.parties.common.configuration.data.Messages) Iterator(java.util.Iterator) Pair(com.alessiodp.core.common.utils.Pair) Set(java.util.Set) PartyPlayerImpl(com.alessiodp.parties.common.players.objects.PartyPlayerImpl) ConfigParties(com.alessiodp.parties.common.configuration.data.ConfigParties) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) CommonCommands(com.alessiodp.parties.common.commands.list.CommonCommands) PartiesCommandData(com.alessiodp.parties.common.commands.utils.PartiesCommandData) List(java.util.List) ConfigMain(com.alessiodp.parties.common.configuration.data.ConfigMain) PartyImpl(com.alessiodp.parties.common.parties.objects.PartyImpl) ADPMainCommand(com.alessiodp.core.common.commands.utils.ADPMainCommand) User(com.alessiodp.core.common.user.User) PartiesDatabaseManager(com.alessiodp.parties.common.storage.PartiesDatabaseManager) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) ADPPlugin(com.alessiodp.core.common.ADPPlugin) LinkedHashSet(java.util.LinkedHashSet) PartiesSubCommand(com.alessiodp.parties.common.commands.utils.PartiesSubCommand) User(com.alessiodp.core.common.user.User) PartyPlayerImpl(com.alessiodp.parties.common.players.objects.PartyPlayerImpl) PartiesCommandData(com.alessiodp.parties.common.commands.utils.PartiesCommandData) PartiesDatabaseManager(com.alessiodp.parties.common.storage.PartiesDatabaseManager) TreeSet(java.util.TreeSet) Pair(com.alessiodp.core.common.utils.Pair) PartyImpl(com.alessiodp.parties.common.parties.objects.PartyImpl)

Aggregations

User (com.alessiodp.core.common.user.User)2 Pair (com.alessiodp.core.common.utils.Pair)2 PartyPlayerImpl (com.alessiodp.parties.common.players.objects.PartyPlayerImpl)2 ArrayList (java.util.ArrayList)2 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 PartyPlayer (com.alessiodp.parties.api.interfaces.PartyPlayer)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 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 PartyImpl (com.alessiodp.parties.common.parties.objects.PartyImpl)1 PartiesDatabaseManager (com.alessiodp.parties.common.storage.PartiesDatabaseManager)1 PartiesPermission (com.alessiodp.parties.common.utils.PartiesPermission)1 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1