Skip to content

Commit

Permalink
Add new options for purging, new purge with no data check
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCodez committed Nov 26, 2024
1 parent 5b19d33 commit f90b87f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
13 changes: 12 additions & 1 deletion VotingPlugin/Resources/Config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,20 @@ CloseGUIOnShiftClick: false
# Purge settings
###########################################
# Remove old player files
# Happens on startup or /av purge
# Recommend leaving this disabled
# When disabled it also disables the commands to manually purge
PurgeOldData: false

# Enable auto purging data on startup
# Do not enable unless you know what you are doing
# Purges players with vote totals
# Requires PurgeOldData to be enabled
PurgeDataOnStartup: false

# Same as above, but checks players have no vote total before purging
# Requires PurgeOldData to be enabled
PurgeNoDataOnStartup: false

# Minimum number of days offline in order to purge
PurgeMin: 90

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,9 @@ public void onPostLoad() {

loadVersionFile();
getOptions().setServer(bungeeSettings.getServer());

// only purges if enabled in config
getVotingPluginUserManager().purgeOldPlayersNoData();

voteTester = new VoteTester(plugin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,17 @@ public void execute(CommandSender sender, String[] args) {
}
});

plugin.getAdminVoteCommand().add(new CommandHandler(plugin, new String[] { "PurgeNoData" },
"VotingPlugin.PurgeNoData",
"Purge players from database with no data and haven't been online for a number of days (Set in Config.yml)") {

@Override
public void execute(CommandSender sender, String[] args) {
plugin.getVotingPluginUserManager().purgeOldPlayersNowNoData();
sendMessage(sender, "&cPurged players with no data");
}
});

plugin.getAdminVoteCommand()
.add(new CommandHandler(plugin, new String[] { "RemoveOfflineUUIDs" },
"VotingPlugin.Commands.AdminVote.RemoveOfflineUUIDs|" + adminPerm,
Expand Down
8 changes: 2 additions & 6 deletions VotingPlugin/src/com/bencodez/votingplugin/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,9 @@ public boolean getLoadTopVoter(TopVoter top) {
@Getter
private int pointsOnVote = 1;

@ConfigDataInt(path = "PurgeMin")
@ConfigDataBoolean(path = "PurgeNoDataOnStartup")
@Getter
private int purgeMin = 90;

@ConfigDataBoolean(path = "PurgeOldData")
@Getter
private boolean purgeOldData = false;
private boolean purgeNoDataOnStartup = false;

@ConfigDataString(path = "RequestAPI.DefaultMethod")
@Getter
Expand Down
83 changes: 83 additions & 0 deletions VotingPlugin/src/com/bencodez/votingplugin/user/UserManager.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.bencodez.votingplugin.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.UUID;

import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import com.bencodez.advancedcore.api.user.AdvancedCoreUser;
import com.bencodez.advancedcore.api.user.UserStartup;
import com.bencodez.advancedcore.api.user.usercache.UserDataManager;
import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKeyBoolean;
import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKeyInt;
import com.bencodez.advancedcore.api.user.usercache.keys.UserDataKeyString;
import com.bencodez.advancedcore.api.user.userstorage.Column;
import com.bencodez.votingplugin.VotingPluginMain;
import com.bencodez.votingplugin.topvoter.TopVoter;

public class UserManager {
private VotingPluginMain plugin;
Expand Down Expand Up @@ -124,4 +130,81 @@ public VotingPluginUser getVotingPluginUser(UUID uuid) {
public VotingPluginUser getVotingPluginUser(UUID uuid, boolean loadName) {
return new VotingPluginUser(plugin, uuid, loadName);
}

public void purgeOldPlayersNoData() {
if (plugin.getOptions().isPurgeOldData() && plugin.getConfigFile().isPurgeNoDataOnStartup()) {
plugin.addUserStartup(new UserStartup() {

@Override
public void onFinish() {
plugin.debug("Finished no data purging");
}

@Override
public void onStart() {

}

@Override
public void onStartUp(AdvancedCoreUser acUser) {
VotingPluginUser user = getVotingPluginUser(acUser);
user.dontCache();
int daysOld = plugin.getOptions().getPurgeMinimumDays();
int days = user.getNumberOfDaysSinceLogin();
if (days == -1) {
// fix ones with no last online
user.setLastOnline(System.currentTimeMillis());
} else if (days > daysOld) {
if (user.getTotal(TopVoter.AllTime) == 0 && user.getMilestoneCount() == 0
&& user.getTotal(TopVoter.Monthly) == 0 && user.getTotal(TopVoter.Weekly) == 0
&& user.getTotal(TopVoter.Daily) == 0) {
plugin.debug("Removing " + user.getUUID() + " because of no data purge");
user.remove();
}
}

}
});

}

plugin.getUserManager().getDataManager().clearCache();
}

public void purgeOldPlayersNowNoData() {
HashMap<UUID, ArrayList<Column>> cols = plugin.getUserManager().getAllKeys();
for (Entry<UUID, ArrayList<Column>> playerData : cols.entrySet()) {
String uuid = playerData.getKey().toString();
if (plugin.isEnabled()) {
if (uuid != null) {
VotingPluginUser user = getVotingPluginUser(UUID.fromString(uuid), false);
if (user != null) {
user.dontCache();
user.updateTempCacheWithColumns(playerData.getValue());
int daysOld = plugin.getOptions().getPurgeMinimumDays();
int days = user.getNumberOfDaysSinceLogin();
if (days == -1) {
// fix ones with no last online
user.setLastOnline(System.currentTimeMillis());
} else if (days > daysOld) {
if (user.getTotal(TopVoter.AllTime) == 0 && user.getMilestoneCount() == 0
&& user.getTotal(TopVoter.Monthly) == 0 && user.getTotal(TopVoter.Weekly) == 0
&& user.getTotal(TopVoter.Daily) == 0)
plugin.debug("Removing " + user.getUUID() + " because of no data purge");
user.remove();
}

user.clearTempCache();
cols.put(playerData.getKey(), null);
user = null;
}
}
}
}
cols.clear();
cols = null;

plugin.getUserManager().getDataManager().clearCache();
plugin.setUpdate(true);
}
}

0 comments on commit f90b87f

Please sign in to comment.