Skip to content

Commit c28bddd

Browse files
committed
Add previous month top voters GUI, uses Month totals with date
1 parent 1afd825 commit c28bddd

File tree

4 files changed

+225
-2
lines changed

4 files changed

+225
-2
lines changed

VotingPlugin/src/com/bencodez/votingplugin/VotingPluginMain.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.security.CodeSource;
88
import java.time.Instant;
99
import java.time.LocalDateTime;
10+
import java.time.YearMonth;
1011
import java.time.ZoneId;
1112
import java.time.format.DateTimeFormatter;
1213
import java.util.ArrayList;
@@ -151,6 +152,10 @@ public class VotingPluginMain extends AdvancedCorePlugin {
151152
@Getter
152153
private LinkedHashMap<TopVoterPlayer, Integer> lastMonthTopVoter;
153154

155+
@Getter
156+
@Setter
157+
private LinkedHashMap<YearMonth, LinkedHashMap<TopVoterPlayer, Integer>> previousMonthsTopVoters;
158+
154159
@Getter
155160
private MVdWPlaceholders mvdwPlaceholders;
156161

@@ -1063,7 +1068,7 @@ public void onPostLoad() {
10631068

10641069
loadVersionFile();
10651070
getOptions().setServer(bungeeSettings.getServer());
1066-
1071+
10671072
// only purges if enabled in config
10681073
getVotingPluginUserManager().purgeOldPlayersNoData();
10691074

@@ -1111,12 +1116,15 @@ public void run() {
11111116

11121117
topVoterHandler = new TopVoterHandler(this);
11131118
lastMonthTopVoter = new LinkedHashMap<TopVoterPlayer, Integer>();
1119+
previousMonthsTopVoters = new LinkedHashMap<YearMonth, LinkedHashMap<TopVoterPlayer, Integer>>();
11141120
plugin.getBukkitScheduler().runTaskAsynchronously(plugin, new Runnable() {
11151121

11161122
@Override
11171123
public void run() {
11181124
topVoterHandler.loadLastMonth();
11191125
debug("Loaded last month top voters");
1126+
1127+
topVoterHandler.loadPreviousMonthTopVoters();
11201128
}
11211129
});
11221130
topVoter = new LinkedHashMap<TopVoter, LinkedHashMap<TopVoterPlayer, Integer>>();
@@ -1514,7 +1522,7 @@ private void reloadPlugin(boolean userStorage) {
15141522
}
15151523
}
15161524
checkYMLError();
1517-
1525+
15181526
if (broadcastHandler != null) {
15191527
broadcastHandler.schedule(getConfigFile().getFormatAlternateBroadcastDelay());
15201528
}

VotingPlugin/src/com/bencodez/votingplugin/commands/CommandLoader.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import com.bencodez.votingplugin.commands.gui.player.VoteToday;
6767
import com.bencodez.votingplugin.commands.gui.player.VoteTopVoter;
6868
import com.bencodez.votingplugin.commands.gui.player.VoteTopVoterLastMonth;
69+
import com.bencodez.votingplugin.commands.gui.player.VoteTopVoterPreviousMonths;
6970
import com.bencodez.votingplugin.commands.gui.player.VoteTotal;
7071
import com.bencodez.votingplugin.commands.gui.player.VoteURL;
7172
import com.bencodez.votingplugin.commands.gui.player.VoteURLVoteSite;
@@ -2476,6 +2477,18 @@ public void execute(CommandSender sender, String[] args) {
24762477
});
24772478
}
24782479

2480+
plugin.getVoteCommand()
2481+
.add(new CommandHandler(plugin, new String[] { "PreviousMonthsVoters" },
2482+
"VotingPlugin.Commands.Vote.PreviousMonthsVoters",
2483+
"Open list of Top Voters from all known previous months") {
2484+
2485+
@Override
2486+
public void execute(CommandSender sender, String[] args) {
2487+
new VoteTopVoterPreviousMonths(plugin, sender,
2488+
plugin.getVotingPluginUserManager().getVotingPluginUser((Player) sender), 0).open();
2489+
}
2490+
});
2491+
24792492
plugin.getVoteCommand().add(new CommandHandler(plugin, new String[] { "Top" },
24802493
"VotingPlugin.Commands.Vote.Top|" + playerPerm, "Open list of Top Voters") {
24812494

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.bencodez.votingplugin.commands.gui.player;
2+
3+
import java.time.YearMonth;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Map.Entry;
8+
import java.util.Set;
9+
import java.util.stream.Collectors;
10+
11+
import org.bukkit.Material;
12+
import org.bukkit.command.CommandSender;
13+
import org.bukkit.entity.Player;
14+
import org.bukkit.event.inventory.ClickType;
15+
16+
import com.bencodez.advancedcore.api.gui.GUIHandler;
17+
import com.bencodez.advancedcore.api.gui.GUIMethod;
18+
import com.bencodez.advancedcore.api.inventory.BInventory;
19+
import com.bencodez.advancedcore.api.inventory.BInventory.ClickEvent;
20+
import com.bencodez.advancedcore.api.inventory.BInventoryButton;
21+
import com.bencodez.advancedcore.api.item.ItemBuilder;
22+
import com.bencodez.votingplugin.VotingPluginMain;
23+
import com.bencodez.votingplugin.topvoter.TopVoterPlayer;
24+
import com.bencodez.votingplugin.user.VotingPluginUser;
25+
26+
import net.md_5.bungee.api.ChatColor;
27+
28+
public class VoteTopVoterPreviousMonths extends GUIHandler {
29+
30+
private VotingPluginMain plugin;
31+
private VotingPluginUser user;
32+
private int index = -1;
33+
34+
public VoteTopVoterPreviousMonths(VotingPluginMain plugin, CommandSender player, VotingPluginUser user, int index) {
35+
super(plugin, player);
36+
this.plugin = plugin;
37+
this.user = user;
38+
this.index = index;
39+
}
40+
41+
@Override
42+
public ArrayList<String> getChat(CommandSender arg0) {
43+
return null;
44+
}
45+
46+
@Override
47+
public void onBook(Player player) {
48+
// TODO
49+
}
50+
51+
@Override
52+
public void onChat(CommandSender sender) {
53+
}
54+
55+
public void next(int index) {
56+
57+
}
58+
59+
@Override
60+
public void onChest(Player player) {
61+
Set<Entry<TopVoterPlayer, Integer>> users = null;
62+
63+
users = plugin.getLastMonthTopVoter().entrySet();
64+
65+
Set<YearMonth> months = plugin.getPreviousMonthsTopVoters().keySet();
66+
67+
List<YearMonth> yearMonthList = months.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());
68+
YearMonth yearMonth = null;
69+
70+
if (index == -1) {
71+
if (!yearMonthList.isEmpty()) {
72+
yearMonth = yearMonthList.get(0);
73+
}
74+
player.sendMessage(ChatColor.RED + "Can't open previous months, no data");
75+
return;
76+
} else {
77+
if (yearMonthList.size() > index) {
78+
yearMonth = yearMonthList.get(index);
79+
} else {
80+
player.sendMessage(ChatColor.RED + "Can't open previous months, no data for index");
81+
return;
82+
}
83+
}
84+
85+
BInventory inv = new BInventory(plugin.getGui().getChestVoteTopName());
86+
inv.addPlaceholder("topvoter", yearMonth.toString());
87+
if (!plugin.getConfigFile().isAlwaysCloseInventory()) {
88+
inv.dontClose();
89+
}
90+
91+
int pos = 1;
92+
for (Entry<TopVoterPlayer, Integer> entry : users) {
93+
ItemBuilder playerItem;
94+
95+
if (plugin.getGui().isChestVoteTopUseSkull()) {
96+
playerItem = new ItemBuilder(entry.getKey().getPlayerHead());
97+
} else {
98+
playerItem = new ItemBuilder(Material.valueOf(plugin.getGui().getChestVoteTopPlayerItemMaterial()));
99+
}
100+
101+
playerItem.setLore(new ArrayList<String>());
102+
103+
inv.addButton(new BInventoryButton(playerItem.setName(plugin.getGui().getChestVoteTopItemName())
104+
.addLoreLine(plugin.getGui().getChestVoteTopItemLore()).addPlaceholder("position", "" + pos)
105+
.addPlaceholder("player", entry.getKey().getPlayerName())
106+
.addPlaceholder("votes", "" + entry.getValue())) {
107+
108+
@Override
109+
public void onClick(ClickEvent clickEvent) {
110+
TopVoterPlayer user = (TopVoterPlayer) getData("User");
111+
new VoteGUI(plugin, player, user.getUser())
112+
.open(GUIMethod.valueOf(plugin.getGui().getGuiMethodGUI().toUpperCase()));
113+
}
114+
}.addData("player", entry.getKey().getPlayerName()).addData("User", entry.getKey()));
115+
pos++;
116+
}
117+
118+
inv.getPageButtons().add(new BInventoryButton(new ItemBuilder(plugin.getGui().getChestVoteTopSwitchItem())
119+
.addPlaceholder("Top", yearMonth.toString())) {
120+
121+
@Override
122+
public void onClick(ClickEvent clickEvent) {
123+
if (!clickEvent.getClick().equals(ClickType.RIGHT)) {
124+
new VoteTopVoterPreviousMonths(plugin, player, user, index++).open(GUIMethod.CHEST);
125+
} else {
126+
new VoteTopVoterPreviousMonths(plugin, player, user, index--).open(GUIMethod.CHEST);
127+
}
128+
}
129+
});
130+
131+
if (plugin.getGui().isChestVoteTopBackButton()) {
132+
inv.getPageButtons().add(plugin.getCommandLoader().getBackButton(user).setSlot(1));
133+
}
134+
135+
inv.setPages(true);
136+
inv.setMaxInvSize(plugin.getGui().getChestVoteTopSize());
137+
inv.openInventory(player);
138+
}
139+
140+
@Override
141+
public void open() {
142+
open(GUIMethod.CHEST);
143+
}
144+
145+
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.File;
44
import java.time.LocalDateTime;
5+
import java.time.Month;
6+
import java.time.YearMonth;
57
import java.util.ArrayList;
68
import java.util.Collections;
79
import java.util.Comparator;
@@ -85,6 +87,61 @@ private HashMap<Integer, String> handlePlaces(Set<String> places) {
8587
return place;
8688
}
8789

90+
public void loadPreviousMonthTopVoters() {
91+
if (plugin.getConfigFile().isStoreMonthTotalsWithDate()) {
92+
HashMap<UUID, ArrayList<Column>> cols = plugin.getUserManager().getAllKeys();
93+
LocalDateTime now = plugin.getTimeChecker().getTime();
94+
for (String column : plugin.getUserManager().getAllColumns()) {
95+
if (column.startsWith("MonthTotal-")) {
96+
String[] data = column.split("-");
97+
if (data.length > 2) {
98+
String year = data[2];
99+
String month = data[1];
100+
if (MessageAPI.isInt(year)) {
101+
YearMonth yearMonth = YearMonth.of(Integer.parseInt(year), Month.valueOf(month));
102+
if (yearMonth.atDay(now.getDayOfMonth()).atTime(23, 59).isBefore(now)) {
103+
plugin.debug("Loading previous month top voters of " + yearMonth.toString());
104+
plugin.getPreviousMonthsTopVoters().put(yearMonth,
105+
getTopVotersOfMonth(yearMonth, cols));
106+
}
107+
}
108+
}
109+
}
110+
}
111+
cols.clear();
112+
cols = null;
113+
}
114+
}
115+
116+
public LinkedHashMap<TopVoterPlayer, Integer> getTopVotersOfMonth(YearMonth month,
117+
HashMap<UUID, ArrayList<Column>> cols) {
118+
119+
LinkedHashMap<TopVoterPlayer, Integer> totals = new LinkedHashMap<TopVoterPlayer, Integer>();
120+
LocalDateTime atTime = month.atDay(15).atTime(0, 0);
121+
for (Entry<UUID, ArrayList<Column>> playerData : cols.entrySet()) {
122+
123+
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();
138+
}
139+
}
140+
}
141+
142+
return sortByValues(totals, false);
143+
}
144+
88145
public void loadLastMonth() {
89146
if (plugin.getGui().isLastMonthGUI()) {
90147
plugin.getLastMonthTopVoter().clear();

0 commit comments

Comments
 (0)