Search in sources :

Example 6 with TopVoter

use of com.bencodez.votingplugin.topvoter.TopVoter in project VotingPlugin by Ben12345rocks.

the class VoteTopVoter method onChest.

@Override
public void onChest(Player player) {
    try {
        if (top == null) {
            top = TopVoter.getDefault();
        }
        Set<Entry<TopVoterPlayer, Integer>> users = null;
        String topVoter = top.getName();
        @SuppressWarnings("unchecked") LinkedHashMap<TopVoterPlayer, Integer> topVotes = (LinkedHashMap<TopVoterPlayer, Integer>) plugin.getTopVoter(top).clone();
        users = topVotes.entrySet();
        BInventory inv = new BInventory(plugin.getGui().getChestVoteTopName());
        inv.addPlaceholder("topvoter", topVoter);
        if (!plugin.getConfigFile().isAlwaysCloseInventory()) {
            inv.dontClose();
        }
        int pos = 1;
        for (Entry<TopVoterPlayer, Integer> entry : users) {
            ItemBuilder playerItem;
            if (plugin.getGui().isChestVoteTopUseSkull()) {
                playerItem = new ItemBuilder(entry.getKey().getPlayerHead());
            } else {
                playerItem = new ItemBuilder(Material.valueOf(plugin.getGui().getChestVoteTopPlayerItemMaterial()));
            }
            playerItem.setLore(new ArrayList<String>());
            inv.addButton(new BInventoryButton(playerItem.setName(plugin.getGui().getChestVoteTopItemName()).addLoreLine(plugin.getGui().getChestVoteTopItemLore()).addPlaceholder("position", "" + pos).addPlaceholder("player", entry.getKey().getPlayerName()).addPlaceholder("votes", "" + entry.getValue())) {

                @Override
                public void onClick(ClickEvent clickEvent) {
                    if (plugin.getGui().getChestVoteTopOpenMainGUIOnClick()) {
                        TopVoterPlayer user = (TopVoterPlayer) getData("User");
                        new VoteGUI(plugin, player, user.getUser()).open(GUIMethod.valueOf(plugin.getGui().getGuiMethodGUI().toUpperCase()));
                    } else {
                        getInv().forceClose(clickEvent.getPlayer());
                    }
                }
            }.addData("player", entry.getKey().getPlayerName()).addData("User", entry.getKey()));
            pos++;
        }
        final TopVoter cur = top;
        inv.getPageButtons().add(new BInventoryButton(new ItemBuilder(plugin.getGui().getChestVoteTopSwitchItem()).addPlaceholder("Top", topVoter)) {

            @Override
            public void onClick(ClickEvent clickEvent) {
                if (!clickEvent.getClick().equals(ClickType.RIGHT)) {
                    new VoteTopVoter(plugin, player, user, cur.next(), 0).open(GUIMethod.CHEST);
                } else {
                    new VoteTopVoter(plugin, player, user, cur.prev(), 0).open(GUIMethod.CHEST);
                }
            }
        });
        if (plugin.getGui().getChestVoteTopBackButton()) {
            inv.getPageButtons().add(plugin.getCommandLoader().getBackButton(user).setSlot(1));
        }
        inv.setPages(true);
        inv.setMaxInvSize(plugin.getGui().getChestVoteTopSize());
        inv.openInventory(player);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : BInventoryButton(com.bencodez.advancedcore.api.inventory.BInventoryButton) ClickEvent(com.bencodez.advancedcore.api.inventory.BInventory.ClickEvent) TopVoterPlayer(com.bencodez.votingplugin.topvoter.TopVoterPlayer) BInventory(com.bencodez.advancedcore.api.inventory.BInventory) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) ItemBuilder(com.bencodez.advancedcore.api.item.ItemBuilder) TopVoter(com.bencodez.votingplugin.topvoter.TopVoter)

Example 7 with TopVoter

use of com.bencodez.votingplugin.topvoter.TopVoter in project VotingPlugin by Ben12345rocks.

the class SpecialRewards method checkCumualativeVotes.

public boolean checkCumualativeVotes(VotingPluginUser user, BungeeMessageData bungeeMessageData, boolean forceBungee) {
    boolean gotCumulativeAny = false;
    Set<String> votes = plugin.getSpecialRewardsConfig().getCumulativeVotes();
    for (String vote : votes) {
        if (StringParser.getInstance().isInt(vote)) {
            int votesRequired = Integer.parseInt(vote);
            if (votesRequired != 0) {
                if (plugin.getSpecialRewardsConfig().getCumulativeRewardEnabled(votesRequired) && RewardHandler.getInstance().hasRewards(plugin.getSpecialRewardsConfig().getData(), plugin.getSpecialRewardsConfig().getCumulativeRewardsPath(votesRequired))) {
                    boolean gotCumulative = false;
                    int total = 0;
                    boolean useBungeeTotalNum = bungeeMessageData != null;
                    String totalToUse = plugin.getSpecialRewardsConfig().getCumulativeVotesTotal(votesRequired);
                    TopVoter top = TopVoter.getTopVoter(totalToUse);
                    if (!useBungeeTotalNum) {
                        total = user.getTotal(top);
                    } else {
                        switch(top) {
                            case AllTime:
                                total = bungeeMessageData.getAllTimeTotal();
                                break;
                            case Daily:
                                total = bungeeMessageData.getDailyTotal();
                                break;
                            case Monthly:
                                total = bungeeMessageData.getMonthTotal();
                                break;
                            case Weekly:
                                total = bungeeMessageData.getWeeklyTotal();
                                break;
                            default:
                                break;
                        }
                    }
                    if (total != 0 && total >= votesRequired) {
                        if ((total % votesRequired) == 0) {
                            gotCumulative = true;
                        }
                    }
                    if (gotCumulative) {
                        if (!plugin.getSpecialRewardsConfig().isOnlyOneCumulative() || !gotCumulativeAny) {
                            gotCumulativeAny = true;
                            plugin.debug(user.getPlayerName() + " got cumulative " + votesRequired + ", total: " + top.toString() + ", current total: " + total);
                            giveCumulativeVoteReward(user, user.isOnline(), votesRequired, forceBungee);
                        } else {
                            plugin.debug("Already got one cumulative");
                        }
                    } else {
                        plugin.devDebug(user.getPlayerName() + " not able to get cumulative " + votesRequired + ", total required: " + top.toString() + ", current player total: " + total);
                    }
                }
            }
        } else {
            plugin.debug("Invalid cumulative number: " + vote);
        }
    }
    return gotCumulativeAny;
}
Also used : TopVoter(com.bencodez.votingplugin.topvoter.TopVoter)

Example 8 with TopVoter

use of com.bencodez.votingplugin.topvoter.TopVoter in project VotingPlugin by Ben12345rocks.

the class PlaceHolders method load.

public void load() {
    placeholders.clear();
    nonPlayerPlaceholders.clear();
    // older placeholders, might be removed in the future
    placeholders.add(new PlaceHolder<VotingPluginUser>("total") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getTotal(TopVoter.Monthly));
        }
    }.withDescription("Month total").updateDataKey("MonthTotal"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("alltimetotal") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getTotal(TopVoter.AllTime));
        }
    }.withDescription("Alltime total").updateDataKey("AllTimeTotal"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("lastmonthtotal") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getLastMonthTotal());
        }
    }.withDescription("Last month total").updateDataKey("LastMonthTotal"));
    // end of older placeholders
    placeholders.add(new PlaceHolder<VotingPluginUser>("DisableBroadcast") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return "" + user.getDisableBroadcast();
        }
    }.withDescription("Returns true/false if user has broadcast disabled").updateDataKey("DisableBroadcast"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("MilestoneCount") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return "" + user.getMilestoneCount();
        }
    }.withDescription("User milestonecount").updateDataKey("MilestoneCount"));
    for (final String identifier : plugin.getGui().getChestShopIdentifiers()) {
        if (plugin.getGui().getChestShopIdentifierLimit(identifier) > 0) {
            placeholders.add(new PlaceHolder<VotingPluginUser>("VoteShopLimit_" + identifier) {

                @Override
                public String placeholderRequest(VotingPluginUser user, String ident) {
                    return "" + user.getVoteShopIdentifierLimit(identifier);
                }
            }.withDescription("User voteshop limit for " + identifier).updateDataKey("VoteShopLimit" + identifier));
        }
    }
    for (final TopVoter top : TopVoter.values()) {
        placeholders.add(new PlaceHolder<VotingPluginUser>("Total_" + top.toString()) {

            @Override
            public String placeholderRequest(VotingPluginUser user, String identifier) {
                return Integer.toString(user.getTotal(top));
            }
        }.withDescription("User total for " + top.getName()).updateDataKey(top.getColumnName()));
    }
    placeholders.add(new PlaceHolder<VotingPluginUser>("BestDailyTotal") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getHighestDailyTotal());
        }
    }.withDescription("Best daily total").updateDataKey("HighestDailyTotal"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("BestWeeklyTotal") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getHighestWeeklyTotal());
        }
    }.withDescription("Best weekly total").updateDataKey("HighestWeeklyTotal"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("BestMonthlyTotal") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getHighestMonthlyTotal());
        }
    }.withDescription("Best monthly total").updateDataKey("HighestMonthlyTotal"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("DailyVoteStreak") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getDayVoteStreak());
        }
    }.withDescription("Current daily votestreak").updateDataKey("DayVoteStreak"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("WeeklyVoteStreak") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getWeekVoteStreak());
        }
    }.withDescription("Current weekly votestreak").updateDataKey("WeekVoteStreak"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("MonthVoteStreak") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getMonthVoteStreak());
        }
    }.withDescription("Current month votestreak").updateDataKey("MonthVoteStreak"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("BestDailyVoteStreak") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getBestDayVoteStreak());
        }
    }.withDescription("Best daily votestreak").updateDataKey("BestDayVoteStreak"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("BestWeeklyVoteStreak") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getBestWeekVoteStreak());
        }
    }.withDescription("Best weekly votestreak").updateDataKey("BestWeekVoteStreak"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("BestMonthVoteStreak") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getBestMonthVoteStreak());
        }
    }.withDescription("Best month votestreak").updateDataKey("BestMonthVoteStreak"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("Points") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Integer.toString(user.getPoints());
        }
    }.withDescription("User points").updateDataKey("Points"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("Points_Format") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
            return numberFormat.format(user.getPoints());
        }
    }.withDescription("User points").updateDataKey("Points"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("CanVote") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return Boolean.toString(user.canVoteAll());
        }
    }.withDescription("Return true/false if player can vote on all sites").updateDataKey("LastVotes"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("CanVoteSites") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return "" + user.getSitesNotVotedOn();
        }
    }.withDescription("Return number of votesites available").updateDataKey("LastVotes"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("Next_AnySite") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            if (user.canVoteAny()) {
                return plugin.getConfigFile().getFormatCommandsVoteNextInfoCanVote();
            }
            long smallest = -1;
            HashMap<Long, VoteSite> times = new HashMap<Long, VoteSite>();
            for (VoteSite site : plugin.getVoteSites()) {
                long t = user.voteNextDurationTime(site);
                if (smallest == -1) {
                    smallest = t;
                }
                if (t < smallest) {
                    smallest = t;
                }
                times.put(t, site);
            }
            for (Entry<Long, VoteSite> entry : times.entrySet()) {
                if (entry.getKey().longValue() == smallest) {
                    return user.voteCommandNextInfo(entry.getValue());
                }
            }
            return "Error";
        }
    }.withDescription("How long until user can vote on anysite").updateDataKey("LastVotes"));
    for (final VoteSite voteSite : plugin.getVoteSites()) {
        placeholders.add(new PlaceHolder<VotingPluginUser>("Next_" + voteSite.getKey()) {

            @Override
            public String placeholderRequest(VotingPluginUser user, String identifier) {
                return user.voteCommandNextInfo(voteSite);
            }
        }.withDescription("How long until user can vote on " + voteSite.getKey()).updateDataKey("LastVotes"));
        placeholders.add(new PlaceHolder<VotingPluginUser>("Last_" + voteSite.getKey()) {

            @Override
            public String placeholderRequest(VotingPluginUser user, String identifier) {
                return user.voteCommandLastDuration(voteSite);
            }
        }.withDescription("How long ago user voted on " + voteSite.getKey()).updateDataKey("LastVotes"));
        placeholders.add(new PlaceHolder<VotingPluginUser>("CanVote_" + voteSite.getKey()) {

            @Override
            public String placeholderRequest(VotingPluginUser user, String identifier) {
                return "" + user.canVoteSite(voteSite);
            }
        }.withDescription("Whether or not player can vote on " + voteSite.getKey()).updateDataKey("LastVotes"));
    }
    placeholders.add(new PlaceHolder<VotingPluginUser>("Top_All_Position") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            int num = 1;
            UUID toMatch = UUID.fromString(user.getUUID());
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.AllTime).entrySet()) {
                if (entry.getKey().getUuid().equals(toMatch)) {
                    return "" + num;
                }
                num++;
            }
            return "";
        }
    }.withDescription("Get user top voter position"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("Top_AllVotes_") {

        @Override
        public String placeholderRequest(String identifier) {
            int num = 1;
            int number = Integer.parseInt(identifier.split("_")[2]);
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.AllTime).entrySet()) {
                if (num == number) {
                    return "" + entry.getValue().intValue();
                }
                num++;
            }
            return "";
        }
    }.useStartsWith().withDescription("Get user votes at position in top voter"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("Top_All_") {

        @Override
        public String placeholderRequest(String identifier) {
            if (!StringParser.getInstance().isInt(identifier.split("_")[2])) {
                return null;
            }
            int num = 1;
            int number = Integer.parseInt(identifier.split("_")[2]);
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.AllTime).entrySet()) {
                if (num == number) {
                    return entry.getKey().getPlayerName();
                }
                num++;
            }
            return "";
        }
    }.useStartsWith().withDescription("Get user at postion in top voter"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("Top_Month_Position") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            int num = 1;
            UUID toMatch = UUID.fromString(user.getUUID());
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Monthly).entrySet()) {
                if (entry.getKey().getUuid().equals(toMatch)) {
                    return "" + num;
                }
                num++;
            }
            return "";
        }
    }.withDescription("Get user top voter position"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("Top_Month_") {

        @Override
        public String placeholderRequest(String identifier) {
            if (!StringParser.getInstance().isInt(identifier.split("_")[2])) {
                return null;
            }
            int num = 1;
            int number = Integer.parseInt(identifier.split("_")[2]);
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Monthly).entrySet()) {
                if (num == number) {
                    return entry.getKey().getPlayerName();
                }
                num++;
            }
            return "";
        }
    }.useStartsWith().withDescription("Get user at position in top voter"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("Top_MonthVotes_") {

        @Override
        public String placeholderRequest(String identifier) {
            int num = 1;
            int number = Integer.parseInt(identifier.split("_")[2]);
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Monthly).entrySet()) {
                if (num == number) {
                    return "" + entry.getValue().intValue();
                }
                num++;
            }
            return "";
        }
    }.useStartsWith().withDescription("Get user votes at position in top voter"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("Top_Week_Position") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            int num = 1;
            UUID toMatch = UUID.fromString(user.getUUID());
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Weekly).entrySet()) {
                if (entry.getKey().getUuid().equals(toMatch)) {
                    return "" + num;
                }
                num++;
            }
            return "";
        }
    }.withDescription("Get user top voter position"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("Top_Week_") {

        @Override
        public String placeholderRequest(String identifier) {
            if (!StringParser.getInstance().isInt(identifier.split("_")[2])) {
                return null;
            }
            int num = 1;
            int number = Integer.parseInt(identifier.split("_")[2]);
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Weekly).entrySet()) {
                if (num == number) {
                    return entry.getKey().getPlayerName();
                }
                num++;
            }
            return "";
        }
    }.useStartsWith().withDescription("Get user at postion in top voter"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("Top_WeekVotes_") {

        @Override
        public String placeholderRequest(String identifier) {
            int num = 1;
            int number = Integer.parseInt(identifier.split("_")[2]);
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Weekly).entrySet()) {
                if (num == number) {
                    return "" + entry.getValue().intValue();
                }
                num++;
            }
            return "";
        }
    }.useStartsWith().withDescription("Get user votes at position in top voter"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("Top_Daily_Position") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            int num = 1;
            UUID toMatch = UUID.fromString(user.getUUID());
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Daily).entrySet()) {
                if (entry.getKey().getUuid().equals(toMatch)) {
                    return "" + num;
                }
                num++;
            }
            return "";
        }
    }.withDescription("Get user top voter position"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("Top_Daily_") {

        @Override
        public String placeholderRequest(String identifier) {
            if (!StringParser.getInstance().isInt(identifier.split("_")[2])) {
                return null;
            }
            int num = 1;
            int number = Integer.parseInt(identifier.split("_")[2]);
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Daily).entrySet()) {
                if (num == number) {
                    return entry.getKey().getPlayerName();
                }
                num++;
            }
            return "";
        }
    }.useStartsWith().withDescription("Get user at postion in top voter"));
    placeholders.add(new PlaceHolder<VotingPluginUser>("VotePartyContributedVotes") {

        @Override
        public String placeholderRequest(VotingPluginUser user, String identifier) {
            return "" + user.getVotePartyVotes();
        }
    }.useStartsWith().withDescription("See vote party placeholders contributed").updateDataKey("VotePartyVotes"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("Top_DailyVotes_") {

        @Override
        public String placeholderRequest(String identifier) {
            int num = 1;
            int number = Integer.parseInt(identifier.split("_")[2]);
            for (Entry<TopVoterPlayer, Integer> entry : plugin.getTopVoter(TopVoter.Daily).entrySet()) {
                if (num == number) {
                    return "" + entry.getValue().intValue();
                }
                num++;
            }
            return "";
        }
    }.useStartsWith().withDescription("Get user votes at position in top voter"));
    // non players
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("VotePartyVotesCurrent") {

        @Override
        public String placeholderRequest(String identifier) {
            return Integer.toString(plugin.getVoteParty().getTotalVotes());
        }
    }.withDescription("Current amount of voteparty votes"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("VotePartyVotesNeeded") {

        @Override
        public String placeholderRequest(String identifier) {
            return Integer.toString(plugin.getVoteParty().getNeededVotes());
        }
    }.withDescription("Voteparty votes needed"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("VotePartyVotesRequired") {

        @Override
        public String placeholderRequest(String identifier) {
            return Integer.toString(plugin.getVoteParty().getVotesRequired());
        }
    }.withDescription("Amount of votes needed for voteparty"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("BungeeVotePartyVotesCurrent") {

        @Override
        public String placeholderRequest(String identifier) {
            if (plugin.getBungeeHandler() != null) {
                return Integer.toString(plugin.getBungeeHandler().getBungeeVotePartyCurrent());
            }
            return "" + 0;
        }
    }.withDescription("Current amount of bungee voteparty votes"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("BungeeVotePartyVotesNeeded") {

        @Override
        public String placeholderRequest(String identifier) {
            if (plugin.getBungeeHandler() != null) {
                return Integer.toString(plugin.getBungeeHandler().getBungeeVotePartyRequired() - plugin.getBungeeHandler().getBungeeVotePartyCurrent());
            }
            return "" + 0;
        }
    }.withDescription("Voteparty bungee votes needed"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("BungeeVotePartyVotesRequired") {

        @Override
        public String placeholderRequest(String identifier) {
            if (plugin.getBungeeHandler() != null) {
                return Integer.toString(plugin.getBungeeHandler().getBungeeVotePartyRequired());
            }
            return "" + 0;
        }
    }.withDescription("Amount of votes needed for bungee  voteparty"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("GlobalMonthTotal") {

        @Override
        public String placeholderRequest(String identifier) {
            int total = 0;
            for (int num : VotingPluginMain.plugin.getTopVoter(TopVoter.Monthly).values()) {
                total += num;
            }
            return Integer.toString(total);
        }
    }.withDescription("Global month total"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("GlobalAllTimeTotal") {

        @Override
        public String placeholderRequest(String identifier) {
            int total = 0;
            for (int num : VotingPluginMain.plugin.getTopVoter(TopVoter.AllTime).values()) {
                total += num;
            }
            return Integer.toString(total);
        }
    }.withDescription("Global alltime total"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("GlobalWeeklyTotal") {

        @Override
        public String placeholderRequest(String identifier) {
            int total = 0;
            for (int num : VotingPluginMain.plugin.getTopVoter(TopVoter.Weekly).values()) {
                total += num;
            }
            return Integer.toString(total);
        }
    }.withDescription("Global weekly total"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("GlobalDailyTotal") {

        @Override
        public String placeholderRequest(String identifier) {
            int total = 0;
            for (int num : VotingPluginMain.plugin.getTopVoter(TopVoter.Daily).values()) {
                total += num;
            }
            return Integer.toString(total);
        }
    }.withDescription("Global daily total"));
    nonPlayerPlaceholders.add(new NonPlayerPlaceHolder<VotingPluginUser>("TimeUntilDayReset") {

        @Override
        public String placeholderRequest(String identifier) {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime offsetoclocktoday = plugin.getTimeChecker().getTime().withHour(0).withMinute(0);
            LocalDateTime offsetoclocktomorrow = plugin.getTimeChecker().getTime().plusDays(1).withHour(0).withMinute(0);
            String timeMsg = "%hours% Hours %minutes% Minutes";
            Duration dur = null;
            if (!now.isBefore(offsetoclocktoday)) {
                dur = Duration.between(now, offsetoclocktomorrow);
            } else {
                dur = Duration.between(now, offsetoclocktoday);
            }
            int diffHours = (int) (dur.getSeconds() / (60 * 60));
            long diffMinutes = dur.getSeconds() / 60 - diffHours * 60;
            if (diffHours < 0) {
                diffHours = diffHours * -1;
            }
            if (diffHours >= 24) {
                diffHours = diffHours - 24;
            }
            if (diffMinutes < 0) {
                diffMinutes = diffMinutes * -1;
            }
            timeMsg = StringParser.getInstance().replaceIgnoreCase(timeMsg, "%hours%", Integer.toString(diffHours));
            timeMsg = StringParser.getInstance().replaceIgnoreCase(timeMsg, "%minutes%", Long.toString(diffMinutes));
            return timeMsg;
        }
    }.withDescription("Time until plugin time day changes"));
    Set<String> placeholdersSet = new HashSet<String>();
    placeholdersSet.addAll(plugin.getConfigFile().getCachedPlaceholders());
    if (plugin.getConfigFile().isAutoCachePlaceholders()) {
        placeholdersSet.addAll(plugin.getServerData().getAutoCachedPlaceholder());
    }
    for (String toCache : placeholdersSet) {
        if (toCache.startsWith("VotingPlugin")) {
            toCache = toCache.substring("VotingPlugin_".length());
        }
        for (NonPlayerPlaceHolder<VotingPluginUser> placeholder : nonPlayerPlaceholders) {
            if (placeholder.isUseStartsWith()) {
                if (toCache.startsWith(placeholder.getIdentifier())) {
                    placeholder.setUseCache(true, toCache);
                    cachedPlaceholders.add(toCache);
                    plugin.extraDebug("Caching placeholder " + toCache);
                }
            } else {
                if (placeholder.getIdentifier().equalsIgnoreCase(toCache)) {
                    placeholder.setUseCache(true, toCache);
                    cachedPlaceholders.add(toCache);
                    plugin.extraDebug("Caching placeholder " + toCache);
                }
            }
        }
        for (PlaceHolder<VotingPluginUser> placeholder : placeholders) {
            if (placeholder.isUseStartsWith()) {
                if (toCache.startsWith(placeholder.getIdentifier())) {
                    placeholder.setUseCache(true, toCache);
                    cachedPlaceholders.add(toCache);
                    plugin.extraDebug("Caching placeholder " + toCache);
                }
            } else {
                if (placeholder.getIdentifier().equalsIgnoreCase(toCache)) {
                    placeholder.setUseCache(true, toCache);
                    cachedPlaceholders.add(toCache);
                    plugin.extraDebug("Caching placeholder " + toCache);
                }
            }
        }
    }
    plugin.getUserManager().getUserDataChange().add(new UserDataChanged() {

        @Override
        public void onChange(AdvancedCoreUser user, String... keys) {
            for (PlaceHolder<VotingPluginUser> placeholder : placeholders) {
                if (placeholder.isUsesCache()) {
                    for (String key : keys) {
                        if (placeholder.getUpdateDataKey().equalsIgnoreCase(key)) {
                            VotingPluginUser vpUser = UserManager.getInstance().getVotingPluginUser(user);
                            for (String ident : placeholder.getCache().keySet()) {
                                placeholder.getCache().get(ident).put(vpUser.getJavaUUID(), placeholder.placeholderRequest(vpUser, ident));
                                plugin.devDebug("Updated cache for " + vpUser.getUUID() + " on " + key);
                            }
                        }
                    }
                }
            }
        }
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) PlaceHolder(com.bencodez.advancedcore.api.placeholder.PlaceHolder) NonPlayerPlaceHolder(com.bencodez.advancedcore.api.placeholder.NonPlayerPlaceHolder) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UserDataChanged(com.bencodez.advancedcore.api.user.UserDataChanged) VoteSite(com.bencodez.votingplugin.objects.VoteSite) Entry(java.util.Map.Entry) TopVoter(com.bencodez.votingplugin.topvoter.TopVoter) UUID(java.util.UUID) HashSet(java.util.HashSet) TopVoterPlayer(com.bencodez.votingplugin.topvoter.TopVoterPlayer) Duration(java.time.Duration) AdvancedCoreUser(com.bencodez.advancedcore.api.user.AdvancedCoreUser) NonPlayerPlaceHolder(com.bencodez.advancedcore.api.placeholder.NonPlayerPlaceHolder) VotingPluginUser(com.bencodez.votingplugin.user.VotingPluginUser) NumberFormat(java.text.NumberFormat)

Example 9 with TopVoter

use of com.bencodez.votingplugin.topvoter.TopVoter in project VotingPlugin by Ben12345rocks.

the class VotingPluginMain method update.

public void update() {
    if (update || configFile.isAlwaysUpdate()) {
        if (!updateStarted && plugin != null) {
            if (!configFile.isUpdateWithPlayersOnlineOnly() || Bukkit.getOnlinePlayers().size() != 0) {
                updateStarted = true;
                update = false;
                synchronized (plugin) {
                    if (plugin != null && plugin.isEnabled()) {
                        getUserManager().getDataManager().clearCacheBasic();
                        plugin.debug("Starting background task, current cached users: " + plugin.getUserManager().getDataManager().getUserDataCache().keySet().size());
                        placeholders.onUpdate();
                        int dataLoadLimit = getConfigFile().getPlayerDataLoadLimit();
                        try {
                            boolean extraBackgroundUpdate = configFile.isExtraBackgroundUpdate();
                            long startTime = System.currentTimeMillis();
                            LinkedHashMap<TopVoterPlayer, HashMap<VoteSite, LocalDateTime>> voteToday = new LinkedHashMap<TopVoterPlayer, HashMap<VoteSite, LocalDateTime>>();
                            LinkedHashMap<TopVoter, LinkedHashMap<TopVoterPlayer, Integer>> tempTopVoter = new LinkedHashMap<TopVoter, LinkedHashMap<TopVoterPlayer, Integer>>();
                            ArrayList<TopVoter> topVotersToCheck = new ArrayList<TopVoter>();
                            for (TopVoter top : TopVoter.values()) {
                                if (plugin.getConfigFile().getLoadTopVoter(top)) {
                                    topVotersToCheck.add(top);
                                    tempTopVoter.put(top, new LinkedHashMap<TopVoterPlayer, Integer>());
                                }
                            }
                            boolean topVoterIgnorePermissionUse = plugin.getConfigFile().getTopVoterIgnorePermission();
                            ArrayList<String> blackList = plugin.getConfigFile().getBlackList();
                            ArrayList<String> uuids = UserManager.getInstance().getAllUUIDs();
                            int currentDay = LocalDateTime.now().getDayOfMonth();
                            int currentDataLoad = 0;
                            for (String uuid : uuids) {
                                if (uuid != null && !uuid.isEmpty()) {
                                    VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(UUID.fromString(uuid), false);
                                    user.dontCache();
                                    user.tempCache();
                                    user.getUserData().updateCacheWithTemp();
                                    if (!user.isBanned() && !blackList.contains(user.getPlayerName())) {
                                        if (!topVoterIgnorePermissionUse || !user.isTopVoterIgnore()) {
                                            for (TopVoter top : topVotersToCheck) {
                                                int total = user.getTotal(top);
                                                if (total > 0) {
                                                    tempTopVoter.get(top).put(user.getTopVoterPlayer(), total);
                                                }
                                            }
                                        }
                                        HashMap<VoteSite, LocalDateTime> times = new HashMap<VoteSite, LocalDateTime>();
                                        for (Entry<VoteSite, Long> entry : user.getLastVotes().entrySet()) {
                                            if (entry.getKey().isEnabled() && !entry.getKey().isHidden()) {
                                                long time = entry.getValue();
                                                if ((currentDay == MiscUtils.getInstance().getDayFromMili(time)) && (LocalDateTime.now().getMonthValue() == MiscUtils.getInstance().getMonthFromMili(time)) && (LocalDateTime.now().getYear() == MiscUtils.getInstance().getYearFromMili(time))) {
                                                    times.put(entry.getKey(), LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
                                                }
                                            }
                                        }
                                        if (times.keySet().size() > 0) {
                                            voteToday.put(user.getTopVoterPlayer(), times);
                                        }
                                    }
                                    if (!extraBackgroundUpdate) {
                                        if (user.isOnline()) {
                                            user.offVote();
                                        }
                                    }
                                    plugin.getPlaceholders().onUpdate(user);
                                    user.clearTempCache();
                                    user = null;
                                    if (dataLoadLimit > 0) {
                                        currentDataLoad++;
                                        if (currentDataLoad >= dataLoadLimit) {
                                            currentDataLoad -= dataLoadLimit;
                                            Thread.sleep(1000);
                                        }
                                    }
                                }
                            }
                            update = false;
                            long time1 = ((System.currentTimeMillis() - startTime) / 1000);
                            plugin.debug("Finished loading player data in " + time1 + " seconds, " + uuids.size() + " users, " + plugin.getStorageType().toString() + ", data load limit: " + dataLoadLimit);
                            time1 = System.currentTimeMillis();
                            topVoterHandler.updateTopVoters(tempTopVoter);
                            setVoteToday(voteToday);
                            serverData.updateValues();
                            getSigns().updateSigns();
                            tempTopVoter = null;
                            time1 = ((System.currentTimeMillis() - time1) / 1000);
                            long totalTime = ((System.currentTimeMillis() - startTime) / 1000);
                            plugin.debug("Background task finished. Final processing took " + time1 + " seconds. Total time: " + totalTime + " seconds");
                            plugin.extraDebug("Current cached users: " + plugin.getUserManager().getDataManager().getUserDataCache().keySet().size());
                            checkFirstTimeLoaded();
                        } catch (Exception ex) {
                            if (plugin != null) {
                                plugin.getLogger().info("Looks like something went wrong");
                            }
                            ex.printStackTrace();
                        }
                    }
                }
                updateStarted = false;
            }
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) TopVoterPlayer(com.bencodez.votingplugin.topvoter.TopVoterPlayer) LinkedHashMap(java.util.LinkedHashMap) VoteSite(com.bencodez.votingplugin.objects.VoteSite) TopVoter(com.bencodez.votingplugin.topvoter.TopVoter) VotingPluginUser(com.bencodez.votingplugin.user.VotingPluginUser)

Example 10 with TopVoter

use of com.bencodez.votingplugin.topvoter.TopVoter in project VotingPlugin by Ben12345rocks.

the class VoteTotal method onChest.

@Override
public void onChest(Player player) {
    BInventory inv = new BInventory(plugin.getGui().getChestVoteTotalName());
    inv.addPlaceholder("player", user.getPlayerName());
    for (final TopVoter top : TopVoter.values()) {
        inv.addButton(new UpdatingBInventoryButton(new ItemBuilder(plugin.getGui().getChestVoteTotalItem(top)).addPlaceholder("Total", "" + user.getTotal(top)).addPlaceholder("topvoter", top.getName()), 1000, 1000) {

            @Override
            public void onClick(ClickEvent clickEvent) {
            }

            @Override
            public ItemBuilder onUpdate(Player arg0) {
                return new ItemBuilder(plugin.getGui().getChestVoteTotalItem(top)).addPlaceholder("Total", "" + user.getTotal(top)).addPlaceholder("topvoter", top.getName()).addPlaceholder("player", user.getPlayerName());
            }
        });
    }
    if (plugin.getGui().getChestVoteTotalBackButton()) {
        inv.addButton(plugin.getCommandLoader().getBackButton(user));
    }
    inv.openInventory(player);
}
Also used : UpdatingBInventoryButton(com.bencodez.advancedcore.api.inventory.UpdatingBInventoryButton) ItemBuilder(com.bencodez.advancedcore.api.item.ItemBuilder) Player(org.bukkit.entity.Player) TopVoter(com.bencodez.votingplugin.topvoter.TopVoter) ClickEvent(com.bencodez.advancedcore.api.inventory.BInventory.ClickEvent) BInventory(com.bencodez.advancedcore.api.inventory.BInventory)

Aggregations

TopVoter (com.bencodez.votingplugin.topvoter.TopVoter)10 VoteSite (com.bencodez.votingplugin.objects.VoteSite)5 TopVoterPlayer (com.bencodez.votingplugin.topvoter.TopVoterPlayer)5 VotingPluginUser (com.bencodez.votingplugin.user.VotingPluginUser)5 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 Player (org.bukkit.entity.Player)4 ItemBuilder (com.bencodez.advancedcore.api.item.ItemBuilder)3 VoteTopVoter (com.bencodez.votingplugin.commands.gui.player.VoteTopVoter)3 VoteURLVoteSite (com.bencodez.votingplugin.commands.gui.player.VoteURLVoteSite)3 LinkedHashMap (java.util.LinkedHashMap)3 CommandHandler (com.bencodez.advancedcore.api.command.CommandHandler)2 BInventory (com.bencodez.advancedcore.api.inventory.BInventory)2 ClickEvent (com.bencodez.advancedcore.api.inventory.BInventory.ClickEvent)2 RewardOptions (com.bencodez.advancedcore.api.rewards.RewardOptions)2 AdminGUI (com.bencodez.votingplugin.commands.gui.AdminGUI)2 AdminVoteHelp (com.bencodez.votingplugin.commands.gui.admin.AdminVoteHelp)2 AdminVotePlaceholdersPlayer (com.bencodez.votingplugin.commands.gui.admin.AdminVotePlaceholdersPlayer)2 AdminVoteVotePlayer (com.bencodez.votingplugin.commands.gui.admin.AdminVoteVotePlayer)2 AdminVoteVoteShop (com.bencodez.votingplugin.commands.gui.admin.voteshop.AdminVoteVoteShop)2