1
0

fix account loading edge case

This commit is contained in:
Lee
2024-04-01 13:01:09 +01:00
parent 66aae8170d
commit ab3d82047a
4 changed files with 36 additions and 32 deletions

View File

@ -54,11 +54,11 @@ public class Account {
*/ */
private final PlayerColor playerColorProfile; private final PlayerColor playerColorProfile;
public Account(Player player) { public Account(UUID uuid) {
//log.info("Loading account for " + uuid); //log.info("Loading account for " + uuid);
boolean newAccount = false; boolean newAccount = false;
this.uuid = player.getUniqueId(); this.uuid = uuid;
File file = new File(Aetheria.INSTANCE.getDataFolder(), "accounts/" + this.uuid + ".yml"); File file = new File(Aetheria.INSTANCE.getDataFolder(), "accounts/" + this.uuid + ".yml");
this.file = file; this.file = file;

View File

@ -5,10 +5,14 @@ import cc.fascinated.config.Config;
import cc.fascinated.config.Lang; import cc.fascinated.config.Lang;
import cc.fascinated.utils.DiscordWebhook; import cc.fascinated.utils.DiscordWebhook;
import cc.fascinated.utils.Manager; import cc.fascinated.utils.Manager;
import cc.fascinated.utils.Priority;
import cc.fascinated.utils.Style; import cc.fascinated.utils.Style;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
@ -20,14 +24,15 @@ import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Log4j2 @Log4j2
public class AccountManager extends Manager { 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 Map<UUID, Account> ACCOUNTS = new HashMap<>();
public AccountManager() { public AccountManager() {
Aetheria.INSTANCE.getServer().getPluginManager().registerEvents(this, Aetheria.INSTANCE);
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
loadAccount(player); loadAccount(player.getUniqueId());
} }
Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> { Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> {
@ -35,40 +40,39 @@ public class AccountManager extends Manager {
}, SAVE_INTERVAL, SAVE_INTERVAL, TimeUnit.MINUTES); }, SAVE_INTERVAL, SAVE_INTERVAL, TimeUnit.MINUTES);
} }
@Override
public Priority getPriority() {
return Priority.LOWEST;
}
/** /**
* Gets the account for the specified player. * Gets the account for the specified player.
* *
* @param player the player * @param uuid the player's UUID
* @return the account * @return the account
*/ */
public static Account getAccount(Player player) { public static Account getAccount(UUID uuid) {
UUID uuid = player.getUniqueId();
if (!ACCOUNTS.containsKey(uuid)) {
return loadAccount(player);
}
return ACCOUNTS.get(uuid); return ACCOUNTS.get(uuid);
} }
/** /**
* Checks if an account is already registered for the specified player. * Checks if an account is already registered for the specified player.
* *
* @param player the player * @param uuid the player's UUID
* @return true if the account is already registered, false otherwise * @return true if the account is already registered, false otherwise
*/ */
private boolean isAccountLoaded(Player player) { private boolean isAccountLoaded(UUID uuid) {
return ACCOUNTS.containsKey(player.getUniqueId()); return ACCOUNTS.containsKey(uuid);
} }
/** /**
* Registers an account for the specified player. * Registers an account for the specified player.
* *
* @param player the player * @param uuid the player's UUID
*/ */
private static Account loadAccount(Player player) { private static void loadAccount(UUID uuid) {
UUID uuid = player.getUniqueId(); Account account = new Account(uuid);
Account account = new Account(player);
ACCOUNTS.put(uuid, account); ACCOUNTS.put(uuid, account);
return account;
} }
/** /**
@ -83,6 +87,15 @@ public class AccountManager extends Manager {
log.info("Saved {} accounts. ({}ms)", ACCOUNTS.size(), System.currentTimeMillis() - before); log.info("Saved {} accounts. ({}ms)", ACCOUNTS.size(), System.currentTimeMillis() - before);
} }
@EventHandler
public void onAsyncPlayerPreLoginEvent(AsyncPlayerPreLoginEvent event) {
UUID uuid = event.getUniqueId();
if (isAccountLoaded(uuid)) { // Account already loaded
return;
}
loadAccount(uuid);
}
@Override @Override
public void onPlayerJoin(Account account, PlayerJoinEvent event) { public void onPlayerJoin(Account account, PlayerJoinEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -114,15 +127,6 @@ public class AccountManager extends Manager {
)); ));
} }
@Override
public void onPlayerLogin(Account account, PlayerLoginEvent event) {
Player player = event.getPlayer();
if (isAccountLoaded(player)) { // Account already loaded
return;
}
loadAccount(player);
}
@Override @Override
public void onPlayerQuit(Account account, PlayerQuitEvent event) { public void onPlayerQuit(Account account, PlayerQuitEvent event) {
account.save(true); // Save the account account.save(true); // Save the account

View File

@ -38,7 +38,7 @@ public abstract class Command implements CommandExecutor {
} }
Player player = (Player) commandSender; Player player = (Player) commandSender;
Account account = AccountManager.getAccount(player); Account account = AccountManager.getAccount(player.getUniqueId());
this.execute(account, strings); this.execute(account, strings);
return true; return true;
} }

View File

@ -52,7 +52,7 @@ public class EventManager implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Account account = AccountManager.getAccount(player); Account account = AccountManager.getAccount(player.getUniqueId());
for (EventListener listener : LISTENERS) { for (EventListener listener : LISTENERS) {
listener.onPlayerJoin(account, event); listener.onPlayerJoin(account, event);
@ -62,7 +62,7 @@ public class EventManager implements Listener {
@EventHandler @EventHandler
public void onPlayerLogin(PlayerLoginEvent event) { public void onPlayerLogin(PlayerLoginEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Account account = AccountManager.getAccount(player); Account account = AccountManager.getAccount(player.getUniqueId());
for (EventListener listener : LISTENERS) { for (EventListener listener : LISTENERS) {
listener.onPlayerLogin(account, event); listener.onPlayerLogin(account, event);
@ -72,7 +72,7 @@ public class EventManager implements Listener {
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Account account = AccountManager.getAccount(player); Account account = AccountManager.getAccount(player.getUniqueId());
for (EventListener listener : LISTENERS) { for (EventListener listener : LISTENERS) {
listener.onPlayerQuit(account, event); listener.onPlayerQuit(account, event);
@ -82,7 +82,7 @@ public class EventManager implements Listener {
@EventHandler @EventHandler
public void onAsyncChat(AsyncChatEvent event) { public void onAsyncChat(AsyncChatEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Account account = AccountManager.getAccount(player); Account account = AccountManager.getAccount(player.getUniqueId());
for (EventListener listener : LISTENERS) { for (EventListener listener : LISTENERS) {
listener.onAsyncChat(account, event); listener.onAsyncChat(account, event);