1
0
This commit is contained in:
Lee
2024-04-05 17:04:19 +01:00
parent 5e766a8eae
commit d7d2bb76ac
21 changed files with 295 additions and 73 deletions

View File

@ -5,13 +5,11 @@ import cc.fascinated.account.command.SaveAccountsCommand;
import cc.fascinated.command.CommandManager;
import cc.fascinated.config.Config;
import cc.fascinated.config.Lang;
import cc.fascinated.playercolor.PlayerColor;
import cc.fascinated.playercolor.PlayerColorProfile;
import cc.fascinated.utils.DiscordWebhook;
import cc.fascinated.utils.Manager;
import cc.fascinated.utils.Priority;
import cc.fascinated.utils.Style;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.viaversion.viaversion.api.Via;
import lombok.extern.log4j.Log4j2;
import org.bukkit.Bukkit;
@ -23,31 +21,23 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Log4j2
public class AccountManager extends Manager implements Listener {
private final long SAVE_INTERVAL = 5; // in minutes
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();
private static final Map<UUID, Account> ACCOUNTS = new HashMap<>();
public AccountManager() {
CommandManager.registerCommand(new SaveAccountsCommand());
Aetheria.INSTANCE.getServer().getPluginManager().registerEvents(this, Aetheria.INSTANCE);
for (Player player : Bukkit.getOnlinePlayers()) {
if (isAccountLoaded(player.getUniqueId())) { // Don't load the account if it's already loaded
continue;
}
loadAccount(player.getUniqueId());
}
@ -68,7 +58,7 @@ public class AccountManager extends Manager implements Listener {
* @return the account
*/
public static Account getAccount(UUID uuid) {
return ACCOUNTS.getIfPresent(uuid);
return ACCOUNTS.get(uuid);
}
/**
@ -78,7 +68,7 @@ public class AccountManager extends Manager implements Listener {
* @return true if the account is already registered, false otherwise
*/
private boolean isAccountLoaded(UUID uuid) {
return ACCOUNTS.getIfPresent(uuid) != null;
return ACCOUNTS.containsKey(uuid);
}
/**
@ -92,19 +82,23 @@ public class AccountManager extends Manager implements Listener {
}
/**
* Save all accounts to disk.
* Save dirty accounts to disk.
*/
public static void saveAccounts() {
long before = System.currentTimeMillis(),
saved = 0; // The amount of accounts that were saved
log.info("Saving accounts...");
for (Map.Entry<UUID, Account> entry : ACCOUNTS.asMap().entrySet()) {
Account account = entry.getValue();
List<Account> toRemove = new ArrayList<>();
for (Account account : ACCOUNTS.values()) {
boolean didSave = account.save(); // Save the account
if (didSave) {
saved++;
}
if (!account.isOnline()) {
toRemove.add(account);
}
}
toRemove.forEach(account -> ACCOUNTS.remove(account.getUuid())); // Unload offline accounts
log.info("Saved {}/{} accounts. ({}ms)", saved, ACCOUNTS.size(), System.currentTimeMillis() - before);
}
@ -120,7 +114,7 @@ public class AccountManager extends Manager implements Listener {
@Override
public void onPlayerJoin(Account account, PlayerJoinEvent event) {
String joinMessage = Lang.JOIN_MESSAGE.getAsString();
PlayerColor playerColorProfile = account.getPlayerColorProfile();
PlayerColorProfile playerColorProfile = account.getPlayerColorProfile();
if (!account.getPlayer().hasPlayedBefore()) {
joinMessage = Lang.FIRST_JOIN_MESSAGE.getAsString();