From f90b87fa5abc3e7fb57690fc8388a46fb1360ed3 Mon Sep 17 00:00:00 2001 From: BenCodez Date: Mon, 25 Nov 2024 20:43:05 -0600 Subject: [PATCH] Add new options for purging, new purge with no data check --- VotingPlugin/Resources/Config.yml | 13 ++- .../votingplugin/VotingPluginMain.java | 3 + .../votingplugin/commands/CommandLoader.java | 11 +++ .../bencodez/votingplugin/config/Config.java | 8 +- .../votingplugin/user/UserManager.java | 83 +++++++++++++++++++ 5 files changed, 111 insertions(+), 7 deletions(-) diff --git a/VotingPlugin/Resources/Config.yml b/VotingPlugin/Resources/Config.yml index 02405823c..f285dea43 100644 --- a/VotingPlugin/Resources/Config.yml +++ b/VotingPlugin/Resources/Config.yml @@ -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 diff --git a/VotingPlugin/src/com/bencodez/votingplugin/VotingPluginMain.java b/VotingPlugin/src/com/bencodez/votingplugin/VotingPluginMain.java index 563096c60..72094ff1c 100644 --- a/VotingPlugin/src/com/bencodez/votingplugin/VotingPluginMain.java +++ b/VotingPlugin/src/com/bencodez/votingplugin/VotingPluginMain.java @@ -1063,6 +1063,9 @@ public void onPostLoad() { loadVersionFile(); getOptions().setServer(bungeeSettings.getServer()); + + // only purges if enabled in config + getVotingPluginUserManager().purgeOldPlayersNoData(); voteTester = new VoteTester(plugin); diff --git a/VotingPlugin/src/com/bencodez/votingplugin/commands/CommandLoader.java b/VotingPlugin/src/com/bencodez/votingplugin/commands/CommandLoader.java index cc2ba1af6..62808648f 100644 --- a/VotingPlugin/src/com/bencodez/votingplugin/commands/CommandLoader.java +++ b/VotingPlugin/src/com/bencodez/votingplugin/commands/CommandLoader.java @@ -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, diff --git a/VotingPlugin/src/com/bencodez/votingplugin/config/Config.java b/VotingPlugin/src/com/bencodez/votingplugin/config/Config.java index b11af59d3..26a4b95ca 100644 --- a/VotingPlugin/src/com/bencodez/votingplugin/config/Config.java +++ b/VotingPlugin/src/com/bencodez/votingplugin/config/Config.java @@ -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 diff --git a/VotingPlugin/src/com/bencodez/votingplugin/user/UserManager.java b/VotingPlugin/src/com/bencodez/votingplugin/user/UserManager.java index e1381392d..a2d058d59 100644 --- a/VotingPlugin/src/com/bencodez/votingplugin/user/UserManager.java +++ b/VotingPlugin/src/com/bencodez/votingplugin/user/UserManager.java @@ -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; @@ -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> cols = plugin.getUserManager().getAllKeys(); + for (Entry> 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); + } }