Skip to content

Commit 2aa84c9

Browse files
committed
Adjustments to how previous months top voters are loaded
1 parent c8cace3 commit 2aa84c9

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

VotingPlugin/src/com/bencodez/votingplugin/commands/gui/player/VoteTopVoterPreviousMonths.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,13 @@ public void next(int index) {
5858

5959
@Override
6060
public void onChest(Player player) {
61-
Set<Entry<TopVoterPlayer, Integer>> users = null;
62-
63-
users = plugin.getLastMonthTopVoter().entrySet();
6461

6562
Set<YearMonth> months = plugin.getPreviousMonthsTopVoters().keySet();
6663

6764
List<YearMonth> yearMonthList = months.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());
6865
YearMonth yearMonth = null;
6966

70-
if (index == -1) {
67+
if (index < 0) {
7168
if (!yearMonthList.isEmpty()) {
7269
yearMonth = yearMonthList.get(0);
7370
}
@@ -77,11 +74,19 @@ public void onChest(Player player) {
7774
if (yearMonthList.size() > index) {
7875
yearMonth = yearMonthList.get(index);
7976
} else {
80-
player.sendMessage(ChatColor.RED + "Can't open previous months, no data for index");
77+
player.sendMessage(ChatColor.RED + "Can't open previous months, no data for requested index");
8178
return;
8279
}
8380
}
8481

82+
Set<Entry<TopVoterPlayer, Integer>> users = null;
83+
if (plugin.getPreviousMonthsTopVoters().containsKey(yearMonth)) {
84+
users = plugin.getPreviousMonthsTopVoters().get(yearMonth).entrySet();
85+
} else {
86+
player.sendMessage(ChatColor.RED + "Can't open previous months, no data");
87+
return;
88+
}
89+
8590
BInventory inv = new BInventory(plugin.getGui().getChestVoteTopName());
8691
inv.addPlaceholder("topvoter", yearMonth.toString());
8792
if (!plugin.getConfigFile().isAlwaysCloseInventory()) {
@@ -121,9 +126,9 @@ public void onClick(ClickEvent clickEvent) {
121126
@Override
122127
public void onClick(ClickEvent clickEvent) {
123128
if (!clickEvent.getClick().equals(ClickType.RIGHT)) {
124-
new VoteTopVoterPreviousMonths(plugin, player, user, index++).open(GUIMethod.CHEST);
129+
new VoteTopVoterPreviousMonths(plugin, player, user, index + 1).open(GUIMethod.CHEST);
125130
} else {
126-
new VoteTopVoterPreviousMonths(plugin, player, user, index--).open(GUIMethod.CHEST);
131+
new VoteTopVoterPreviousMonths(plugin, player, user, index - 1).open(GUIMethod.CHEST);
127132
}
128133
}
129134
});

VotingPlugin/src/com/bencodez/votingplugin/topvoter/TopVoterHandler.java

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ private HashMap<Integer, String> handlePlaces(Set<String> places) {
8989

9090
public void loadPreviousMonthTopVoters() {
9191
if (plugin.getConfigFile().isStoreMonthTotalsWithDate()) {
92-
HashMap<UUID, ArrayList<Column>> cols = plugin.getUserManager().getAllKeys();
92+
9393
LocalDateTime now = plugin.getTimeChecker().getTime();
94+
9495
for (String column : plugin.getUserManager().getAllColumns()) {
9596
if (column.startsWith("MonthTotal-")) {
9697
String[] data = column.split("-");
@@ -102,14 +103,55 @@ public void loadPreviousMonthTopVoters() {
102103
if (yearMonth.atDay(now.getDayOfMonth()).atTime(23, 59).isBefore(now)) {
103104
plugin.debug("Loading previous month top voters of " + yearMonth.toString());
104105
plugin.getPreviousMonthsTopVoters().put(yearMonth,
105-
getTopVotersOfMonth(yearMonth, cols));
106+
new LinkedHashMap<TopVoterPlayer, Integer>());
106107
}
107108
}
108109
}
109110
}
110111
}
111-
cols.clear();
112-
cols = null;
112+
113+
if (plugin.getPreviousMonthsTopVoters().size() > 0) {
114+
HashMap<UUID, ArrayList<Column>> cols = plugin.getUserManager().getAllKeys();
115+
for (Entry<UUID, ArrayList<Column>> playerData : cols.entrySet()) {
116+
117+
String uuid = playerData.getKey().toString();
118+
if (uuid != null && !uuid.isEmpty()) {
119+
VotingPluginUser user = plugin.getVotingPluginUserManager()
120+
.getVotingPluginUser(UUID.fromString(uuid), false);
121+
user.dontCache();
122+
user.updateTempCacheWithColumns(playerData.getValue());
123+
124+
for (YearMonth month : plugin.getPreviousMonthsTopVoters().keySet()) {
125+
LocalDateTime atTime = month.atDay(15).atTime(0, 0);
126+
int total = user.getTotal(TopVoter.Monthly, atTime);
127+
if (total > 0) {
128+
plugin.getPreviousMonthsTopVoters().get(month).put(user.getTopVoterPlayer(), total);
129+
}
130+
}
131+
132+
cols.put(playerData.getKey(), null);
133+
user.clearTempCache();
134+
}
135+
}
136+
137+
cols.clear();
138+
cols = null;
139+
}
140+
141+
for (YearMonth month : plugin.getPreviousMonthsTopVoters().keySet()) {
142+
plugin.getPreviousMonthsTopVoters().put(month,
143+
sortByValues(plugin.getPreviousMonthsTopVoters().get(month), false));
144+
}
145+
146+
plugin.extraDebug("Previous Months: " + plugin.getPreviousMonthsTopVoters().keySet().toString());
147+
148+
for (Entry<YearMonth, LinkedHashMap<TopVoterPlayer, Integer>> entry : plugin.getPreviousMonthsTopVoters()
149+
.entrySet()) {
150+
for (Entry<TopVoterPlayer, Integer> entry2 : entry.getValue().entrySet()) {
151+
plugin.extraDebug(entry.getKey().toString() + ": " + entry2.getKey().getUser().getPlayerName() + ":"
152+
+ entry2.getValue().intValue());
153+
}
154+
}
113155
}
114156
}
115157

@@ -121,21 +163,18 @@ public LinkedHashMap<TopVoterPlayer, Integer> getTopVotersOfMonth(YearMonth mont
121163
for (Entry<UUID, ArrayList<Column>> playerData : cols.entrySet()) {
122164

123165
String uuid = playerData.getKey().toString();
124-
if (plugin != null && plugin.isEnabled()) {
125-
if (uuid != null && !uuid.isEmpty()) {
126-
VotingPluginUser user = plugin.getVotingPluginUserManager()
127-
.getVotingPluginUser(UUID.fromString(uuid), false);
128-
user.dontCache();
129-
user.updateTempCacheWithColumns(playerData.getValue());
130-
cols.put(playerData.getKey(), null);
131-
int total = 0;
132-
total = user.getTotal(TopVoter.Monthly, atTime);
133-
134-
if (total > 0) {
135-
totals.put(user.getTopVoterPlayer(), total);
136-
}
137-
user.clearTempCache();
166+
if (uuid != null && !uuid.isEmpty()) {
167+
VotingPluginUser user = plugin.getVotingPluginUserManager().getVotingPluginUser(UUID.fromString(uuid),
168+
false);
169+
user.dontCache();
170+
user.updateTempCacheWithColumns(playerData.getValue());
171+
int total = 0;
172+
total = user.getTotal(TopVoter.Monthly, atTime);
173+
174+
if (total > 0) {
175+
totals.put(user.getTopVoterPlayer(), total);
138176
}
177+
user.clearTempCache();
139178
}
140179
}
141180

0 commit comments

Comments
 (0)