Skip to content

Commit f90b87f

Browse files
committed
Add new options for purging, new purge with no data check
1 parent 5b19d33 commit f90b87f

File tree

5 files changed

+111
-7
lines changed

5 files changed

+111
-7
lines changed

VotingPlugin/Resources/Config.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,20 @@ CloseGUIOnShiftClick: false
747747
# Purge settings
748748
###########################################
749749
# Remove old player files
750-
# Happens on startup or /av purge
750+
# Recommend leaving this disabled
751+
# When disabled it also disables the commands to manually purge
751752
PurgeOldData: false
752753

754+
# Enable auto purging data on startup
755+
# Do not enable unless you know what you are doing
756+
# Purges players with vote totals
757+
# Requires PurgeOldData to be enabled
758+
PurgeDataOnStartup: false
759+
760+
# Same as above, but checks players have no vote total before purging
761+
# Requires PurgeOldData to be enabled
762+
PurgeNoDataOnStartup: false
763+
753764
# Minimum number of days offline in order to purge
754765
PurgeMin: 90
755766

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,9 @@ public void onPostLoad() {
10631063

10641064
loadVersionFile();
10651065
getOptions().setServer(bungeeSettings.getServer());
1066+
1067+
// only purges if enabled in config
1068+
getVotingPluginUserManager().purgeOldPlayersNoData();
10661069

10671070
voteTester = new VoteTester(plugin);
10681071

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,17 @@ public void execute(CommandSender sender, String[] args) {
17391739
}
17401740
});
17411741

1742+
plugin.getAdminVoteCommand().add(new CommandHandler(plugin, new String[] { "PurgeNoData" },
1743+
"VotingPlugin.PurgeNoData",
1744+
"Purge players from database with no data and haven't been online for a number of days (Set in Config.yml)") {
1745+
1746+
@Override
1747+
public void execute(CommandSender sender, String[] args) {
1748+
plugin.getVotingPluginUserManager().purgeOldPlayersNowNoData();
1749+
sendMessage(sender, "&cPurged players with no data");
1750+
}
1751+
});
1752+
17421753
plugin.getAdminVoteCommand()
17431754
.add(new CommandHandler(plugin, new String[] { "RemoveOfflineUUIDs" },
17441755
"VotingPlugin.Commands.AdminVote.RemoveOfflineUUIDs|" + adminPerm,

VotingPlugin/src/com/bencodez/votingplugin/config/Config.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,9 @@ public boolean getLoadTopVoter(TopVoter top) {
663663
@Getter
664664
private int pointsOnVote = 1;
665665

666-
@ConfigDataInt(path = "PurgeMin")
666+
@ConfigDataBoolean(path = "PurgeNoDataOnStartup")
667667
@Getter
668-
private int purgeMin = 90;
669-
670-
@ConfigDataBoolean(path = "PurgeOldData")
671-
@Getter
672-
private boolean purgeOldData = false;
668+
private boolean purgeNoDataOnStartup = false;
673669

674670
@ConfigDataString(path = "RequestAPI.DefaultMethod")
675671
@Getter

VotingPlugin/src/com/bencodez/votingplugin/user/UserManager.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package com.bencodez.votingplugin.user;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.Map.Entry;
46
import java.util.UUID;
57

68
import org.bukkit.OfflinePlayer;
79
import org.bukkit.entity.Player;
810

11+
import com.bencodez.advancedcore.api.user.AdvancedCoreUser;
12+
import com.bencodez.advancedcore.api.user.UserStartup;
913
import com.bencodez.advancedcore.api.user.usercache.UserDataManager;
1014
import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKeyBoolean;
1115
import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKeyInt;
1216
import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKeyString;
17+
import com.bencodez.advancedcore.api.user.userstorage.Column;
1318
import com.bencodez.votingplugin.VotingPluginMain;
19+
import com.bencodez.votingplugin.topvoter.TopVoter;
1420

1521
public class UserManager {
1622
private VotingPluginMain plugin;
@@ -124,4 +130,81 @@ public VotingPluginUser getVotingPluginUser(UUID uuid) {
124130
public VotingPluginUser getVotingPluginUser(UUID uuid, boolean loadName) {
125131
return new VotingPluginUser(plugin, uuid, loadName);
126132
}
133+
134+
public void purgeOldPlayersNoData() {
135+
if (plugin.getOptions().isPurgeOldData() && plugin.getConfigFile().isPurgeNoDataOnStartup()) {
136+
plugin.addUserStartup(new UserStartup() {
137+
138+
@Override
139+
public void onFinish() {
140+
plugin.debug("Finished no data purging");
141+
}
142+
143+
@Override
144+
public void onStart() {
145+
146+
}
147+
148+
@Override
149+
public void onStartUp(AdvancedCoreUser acUser) {
150+
VotingPluginUser user = getVotingPluginUser(acUser);
151+
user.dontCache();
152+
int daysOld = plugin.getOptions().getPurgeMinimumDays();
153+
int days = user.getNumberOfDaysSinceLogin();
154+
if (days == -1) {
155+
// fix ones with no last online
156+
user.setLastOnline(System.currentTimeMillis());
157+
} else if (days > daysOld) {
158+
if (user.getTotal(TopVoter.AllTime) == 0 && user.getMilestoneCount() == 0
159+
&& user.getTotal(TopVoter.Monthly) == 0 && user.getTotal(TopVoter.Weekly) == 0
160+
&& user.getTotal(TopVoter.Daily) == 0) {
161+
plugin.debug("Removing " + user.getUUID() + " because of no data purge");
162+
user.remove();
163+
}
164+
}
165+
166+
}
167+
});
168+
169+
}
170+
171+
plugin.getUserManager().getDataManager().clearCache();
172+
}
173+
174+
public void purgeOldPlayersNowNoData() {
175+
HashMap<UUID, ArrayList<Column>> cols = plugin.getUserManager().getAllKeys();
176+
for (Entry<UUID, ArrayList<Column>> playerData : cols.entrySet()) {
177+
String uuid = playerData.getKey().toString();
178+
if (plugin.isEnabled()) {
179+
if (uuid != null) {
180+
VotingPluginUser user = getVotingPluginUser(UUID.fromString(uuid), false);
181+
if (user != null) {
182+
user.dontCache();
183+
user.updateTempCacheWithColumns(playerData.getValue());
184+
int daysOld = plugin.getOptions().getPurgeMinimumDays();
185+
int days = user.getNumberOfDaysSinceLogin();
186+
if (days == -1) {
187+
// fix ones with no last online
188+
user.setLastOnline(System.currentTimeMillis());
189+
} else if (days > daysOld) {
190+
if (user.getTotal(TopVoter.AllTime) == 0 && user.getMilestoneCount() == 0
191+
&& user.getTotal(TopVoter.Monthly) == 0 && user.getTotal(TopVoter.Weekly) == 0
192+
&& user.getTotal(TopVoter.Daily) == 0)
193+
plugin.debug("Removing " + user.getUUID() + " because of no data purge");
194+
user.remove();
195+
}
196+
197+
user.clearTempCache();
198+
cols.put(playerData.getKey(), null);
199+
user = null;
200+
}
201+
}
202+
}
203+
}
204+
cols.clear();
205+
cols = null;
206+
207+
plugin.getUserManager().getDataManager().clearCache();
208+
plugin.setUpdate(true);
209+
}
127210
}

0 commit comments

Comments
 (0)