1
0

use expiring map for accounts

This commit is contained in:
Lee
2024-04-03 19:26:54 +01:00
parent 6524429b5b
commit 5e766a8eae
4 changed files with 27 additions and 8 deletions

View File

@ -171,6 +171,12 @@
<version>4.9.2</version> <version>4.9.2</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.1.0-jre</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -4,12 +4,12 @@ import cc.fascinated.account.AccountManager;
import cc.fascinated.chat.ChatManager; import cc.fascinated.chat.ChatManager;
import cc.fascinated.command.CommandManager; import cc.fascinated.command.CommandManager;
import cc.fascinated.commandspy.CommandSpyManager; import cc.fascinated.commandspy.CommandSpyManager;
import cc.fascinated.commandspy.command.CommandSpyCommand;
import cc.fascinated.event.EventManager; import cc.fascinated.event.EventManager;
import cc.fascinated.metrics.MetricManager; import cc.fascinated.metrics.MetricManager;
import cc.fascinated.motd.MotdManager; import cc.fascinated.motd.MotdManager;
import cc.fascinated.placeholder.PlaceholderManager; import cc.fascinated.placeholder.PlaceholderManager;
import cc.fascinated.playercolor.PlayerColorManager; import cc.fascinated.playercolor.PlayerColorManager;
import cc.fascinated.renderdistance.RenderDistanceManager;
import cc.fascinated.utils.BuildData; import cc.fascinated.utils.BuildData;
import cc.fascinated.worldsize.WorldSizeManager; import cc.fascinated.worldsize.WorldSizeManager;
import lombok.Getter; import lombok.Getter;
@ -49,5 +49,6 @@ public class Aetheria extends JavaPlugin {
new ChatManager(); new ChatManager();
new MotdManager(); new MotdManager();
new CommandSpyManager(); new CommandSpyManager();
new RenderDistanceManager();
} }
} }

View File

@ -10,6 +10,8 @@ import cc.fascinated.utils.DiscordWebhook;
import cc.fascinated.utils.Manager; import cc.fascinated.utils.Manager;
import cc.fascinated.utils.Priority; import cc.fascinated.utils.Priority;
import cc.fascinated.utils.Style; import cc.fascinated.utils.Style;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -30,7 +32,16 @@ import java.util.concurrent.TimeUnit;
public class AccountManager extends Manager implements Listener { public class AccountManager extends Manager implements Listener {
private final long SAVE_INTERVAL = 5; // in minutes private final long SAVE_INTERVAL = 5; // in minutes
private static final Map<UUID, Account> ACCOUNTS = new HashMap<>(); private static final Cache<UUID, Account> ACCOUNTS = CacheBuilder.newBuilder()
.expireAfterAccess(30, TimeUnit.MINUTES)
.removalListener(notification -> {
Account account = (Account) notification.getValue();
if (account != null) {
account.save(); // Save the account when it is removed from the cache
log.info("Saved account for {} because it was removed from the cache.", account.getName());
}
})
.build();
public AccountManager() { public AccountManager() {
CommandManager.registerCommand(new SaveAccountsCommand()); CommandManager.registerCommand(new SaveAccountsCommand());
@ -57,7 +68,7 @@ public class AccountManager extends Manager implements Listener {
* @return the account * @return the account
*/ */
public static Account getAccount(UUID uuid) { public static Account getAccount(UUID uuid) {
return ACCOUNTS.get(uuid); return ACCOUNTS.getIfPresent(uuid);
} }
/** /**
@ -67,7 +78,7 @@ public class AccountManager extends Manager implements Listener {
* @return true if the account is already registered, false otherwise * @return true if the account is already registered, false otherwise
*/ */
private boolean isAccountLoaded(UUID uuid) { private boolean isAccountLoaded(UUID uuid) {
return ACCOUNTS.containsKey(uuid); return ACCOUNTS.getIfPresent(uuid) != null;
} }
/** /**
@ -87,7 +98,8 @@ public class AccountManager extends Manager implements Listener {
long before = System.currentTimeMillis(), long before = System.currentTimeMillis(),
saved = 0; // The amount of accounts that were saved saved = 0; // The amount of accounts that were saved
log.info("Saving accounts..."); log.info("Saving accounts...");
for (Account account : ACCOUNTS.values()) { for (Map.Entry<UUID, Account> entry : ACCOUNTS.asMap().entrySet()) {
Account account = entry.getValue();
boolean didSave = account.save(); // Save the account boolean didSave = account.save(); // Save the account
if (didSave) { if (didSave) {
saved++; saved++;
@ -148,13 +160,10 @@ public class AccountManager extends Manager implements Listener {
.replace("%player%", event.getPlayer().getName()) .replace("%player%", event.getPlayer().getName())
.replace("player-color", account.getPlayerColorProfile().getColor().toString()) .replace("player-color", account.getPlayerColorProfile().getColor().toString())
)); ));
account.save(); // Save the account
ACCOUNTS.remove(account.getUuid()); // Remove the account from the cache
} }
@Override @Override
public void onAetheriaDisable() { public void onAetheriaDisable() {
saveAccounts(); // Save the accounts to disk saveAccounts(); // Save the accounts to disk
ACCOUNTS.clear(); // Remove the accounts from the cache
} }
} }

View File

@ -41,6 +41,9 @@ public class CommandSpyManager extends Manager {
@Override @Override
public void onCommandPreProcess(Account account, PlayerCommandPreprocessEvent event) { public void onCommandPreProcess(Account account, PlayerCommandPreprocessEvent event) {
for (Account commandSpyAccount : COMMAND_SPY_ENABLED) { for (Account commandSpyAccount : COMMAND_SPY_ENABLED) {
if (account.equals(commandSpyAccount)) { // Don't send the command to the account that executed it
continue;
}
commandSpyAccount.sendMessage(Lang.COMMAND_SPY_FORMAT.getAsString() commandSpyAccount.sendMessage(Lang.COMMAND_SPY_FORMAT.getAsString()
.replace("player-color", account.getPlayerColorProfile().getColor().toString()) .replace("player-color", account.getPlayerColorProfile().getColor().toString())
.replace("%player%", account.getName()) .replace("%player%", account.getName())