Search in sources :

Example 16 with VotingPluginUser

use of com.bencodez.votingplugin.user.VotingPluginUser 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 17 with VotingPluginUser

use of com.bencodez.votingplugin.user.VotingPluginUser in project VotingPlugin by Ben12345rocks.

the class PlaceHolders method onUpdate.

public void onUpdate() {
    ArrayList<String> uuids = UserManager.getInstance().getAllUUIDs();
    while (!placeholdersToSetCacheOn.isEmpty()) {
        String toCache = placeholdersToSetCacheOn.poll();
        for (NonPlayerPlaceHolder<VotingPluginUser> placeholder : nonPlayerPlaceholders) {
            if (placeholder.matches(toCache)) {
                placeholder.setUseCache(true, toCache);
                cachedPlaceholders.add(toCache);
                plugin.getServerData().addAutoCachedPlaceholder(toCache);
                plugin.debug("Auto caching placeholder: " + toCache);
            }
        }
        for (PlaceHolder<VotingPluginUser> placeholder : placeholders) {
            if (placeholder.matches(toCache)) {
                placeholder.setUseCache(true, toCache);
                cachedPlaceholders.add(toCache);
                plugin.getServerData().addAutoCachedPlaceholder(toCache);
                plugin.debug("Auto caching placeholder: " + toCache);
            }
        }
    }
    for (NonPlayerPlaceHolder<VotingPluginUser> placeholder : nonPlayerPlaceholders) {
        if (placeholder.isUsesCache()) {
            for (String ident : placeholder.getCache().keySet()) {
                if (ident != null) {
                    String str = placeholder.placeholderRequest(ident);
                    if (str != null) {
                        placeholder.getCache().put(ident, str);
                    }
                } else {
                    plugin.debug("ident null: " + placeholder.getIdentifier());
                }
            }
        }
    }
    for (String uuid : uuids) {
        VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(UUID.fromString(uuid));
        user.dontCache();
        for (PlaceHolder<VotingPluginUser> placeholder : placeholders) {
            if (placeholder.isUsesCache()) {
                if (placeholder.getIdentifier().startsWith("Top_")) {
                    for (String ident : placeholder.getCache().keySet()) {
                        if (ident != null) {
                            placeholder.getCache().get(ident).put(user.getJavaUUID(), placeholder.placeholderRequest(user, ident));
                        } else {
                            plugin.debug("ident null: " + placeholder.getIdentifier());
                        }
                    }
                }
            }
        }
    }
}
Also used : VotingPluginUser(com.bencodez.votingplugin.user.VotingPluginUser)

Example 18 with VotingPluginUser

use of com.bencodez.votingplugin.user.VotingPluginUser in project VotingPlugin by Ben12345rocks.

the class PlayerJoinEvent method login.

private void login(Player player) {
    VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(player);
    if (player.isOp() && plugin.isYmlError()) {
        user.sendMessage("&cVotingPlugin: Detected yml error, please check console for details");
    }
    boolean data = user.getData().hasData();
    // run remind
    user.loginMessage();
    if (data) {
        // give offline vote (if they voted offline)
        user.offVote();
    }
    user.loginRewards();
    if (plugin.getBungeeSettings().isUseBungeecoord() && plugin.getBungeeHandler().getMethod().equals(BungeeMethod.PLUGINMESSAGING)) {
        plugin.getPluginMessaging().sendPluginMessage("Login", user.getPlayerName(), user.getUUID());
    }
}
Also used : VotingPluginUser(com.bencodez.votingplugin.user.VotingPluginUser)

Example 19 with VotingPluginUser

use of com.bencodez.votingplugin.user.VotingPluginUser in project VotingPlugin by Ben12345rocks.

the class PlayerJoinEvent method onPlayerQuit.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
    if (plugin.getConfigFile().isDisableAdvancedTab()) {
        return;
    }
    final java.util.UUID uuid = event.getPlayer().getUniqueId();
    plugin.getLoginTimer().schedule(new TimerTask() {

        @Override
        public void run() {
            VotingPluginMain.plugin.getAdvancedTab().remove(uuid);
            VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(uuid);
            user.dontCache();
            user.logoutRewards();
        }
    }, 0);
}
Also used : TimerTask(java.util.TimerTask) VotingPluginUser(com.bencodez.votingplugin.user.VotingPluginUser) EventHandler(org.bukkit.event.EventHandler)

Example 20 with VotingPluginUser

use of com.bencodez.votingplugin.user.VotingPluginUser 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)

Aggregations

VotingPluginUser (com.bencodez.votingplugin.user.VotingPluginUser)27 Player (org.bukkit.entity.Player)11 VoteSite (com.bencodez.votingplugin.objects.VoteSite)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)6 BInventory (com.bencodez.advancedcore.api.inventory.BInventory)5 ClickEvent (com.bencodez.advancedcore.api.inventory.BInventory.ClickEvent)5 ItemBuilder (com.bencodez.advancedcore.api.item.ItemBuilder)5 RewardOptions (com.bencodez.advancedcore.api.rewards.RewardOptions)5 TopVoter (com.bencodez.votingplugin.topvoter.TopVoter)5 TopVoterPlayer (com.bencodez.votingplugin.topvoter.TopVoterPlayer)5 EventHandler (org.bukkit.event.EventHandler)5 BInventoryButton (com.bencodez.advancedcore.api.inventory.BInventoryButton)4 AdminVotePlaceholdersPlayer (com.bencodez.votingplugin.commands.gui.admin.AdminVotePlaceholdersPlayer)3 AdminVoteVotePlayer (com.bencodez.votingplugin.commands.gui.admin.AdminVoteVotePlayer)3 VoteURLVoteSite (com.bencodez.votingplugin.commands.gui.player.VoteURLVoteSite)3 BookWrapper (com.bencodez.advancedcore.api.bookgui.BookWrapper)2 Layout (com.bencodez.advancedcore.api.bookgui.Layout)2 CommandHandler (com.bencodez.advancedcore.api.command.CommandHandler)2