fix account loading edge case
This commit is contained in:
@ -54,11 +54,11 @@ public class Account {
|
||||
*/
|
||||
private final PlayerColor playerColorProfile;
|
||||
|
||||
public Account(Player player) {
|
||||
public Account(UUID uuid) {
|
||||
//log.info("Loading account for " + uuid);
|
||||
boolean newAccount = false;
|
||||
|
||||
this.uuid = player.getUniqueId();
|
||||
this.uuid = uuid;
|
||||
|
||||
File file = new File(Aetheria.INSTANCE.getDataFolder(), "accounts/" + this.uuid + ".yml");
|
||||
this.file = file;
|
||||
|
@ -5,10 +5,14 @@ import cc.fascinated.config.Config;
|
||||
import cc.fascinated.config.Lang;
|
||||
import cc.fascinated.utils.DiscordWebhook;
|
||||
import cc.fascinated.utils.Manager;
|
||||
import cc.fascinated.utils.Priority;
|
||||
import cc.fascinated.utils.Style;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.bukkit.Bukkit;
|
||||
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.PlayerLoginEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
@ -20,14 +24,15 @@ import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Log4j2
|
||||
public class AccountManager extends Manager {
|
||||
public class AccountManager extends Manager implements Listener {
|
||||
|
||||
private final long SAVE_INTERVAL = 5; // in minutes
|
||||
private static final Map<UUID, Account> ACCOUNTS = new HashMap<>();
|
||||
|
||||
public AccountManager() {
|
||||
Aetheria.INSTANCE.getServer().getPluginManager().registerEvents(this, Aetheria.INSTANCE);
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
loadAccount(player);
|
||||
loadAccount(player.getUniqueId());
|
||||
}
|
||||
|
||||
Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> {
|
||||
@ -35,40 +40,39 @@ public class AccountManager extends Manager {
|
||||
}, SAVE_INTERVAL, SAVE_INTERVAL, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Priority getPriority() {
|
||||
return Priority.LOWEST;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the account for the specified player.
|
||||
*
|
||||
* @param player the player
|
||||
* @param uuid the player's UUID
|
||||
* @return the account
|
||||
*/
|
||||
public static Account getAccount(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
if (!ACCOUNTS.containsKey(uuid)) {
|
||||
return loadAccount(player);
|
||||
}
|
||||
public static Account getAccount(UUID uuid) {
|
||||
return ACCOUNTS.get(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private boolean isAccountLoaded(Player player) {
|
||||
return ACCOUNTS.containsKey(player.getUniqueId());
|
||||
private boolean isAccountLoaded(UUID uuid) {
|
||||
return ACCOUNTS.containsKey(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an account for the specified player.
|
||||
*
|
||||
* @param player the player
|
||||
* @param uuid the player's UUID
|
||||
*/
|
||||
private static Account loadAccount(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
Account account = new Account(player);
|
||||
private static void loadAccount(UUID uuid) {
|
||||
Account account = new Account(uuid);
|
||||
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);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onAsyncPlayerPreLoginEvent(AsyncPlayerPreLoginEvent event) {
|
||||
UUID uuid = event.getUniqueId();
|
||||
if (isAccountLoaded(uuid)) { // Account already loaded
|
||||
return;
|
||||
}
|
||||
loadAccount(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerJoin(Account account, PlayerJoinEvent event) {
|
||||
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
|
||||
public void onPlayerQuit(Account account, PlayerQuitEvent event) {
|
||||
account.save(true); // Save the account
|
||||
|
@ -38,7 +38,7 @@ public abstract class Command implements CommandExecutor {
|
||||
}
|
||||
|
||||
Player player = (Player) commandSender;
|
||||
Account account = AccountManager.getAccount(player);
|
||||
Account account = AccountManager.getAccount(player.getUniqueId());
|
||||
this.execute(account, strings);
|
||||
return true;
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ public class EventManager implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Account account = AccountManager.getAccount(player);
|
||||
Account account = AccountManager.getAccount(player.getUniqueId());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onPlayerJoin(account, event);
|
||||
@ -62,7 +62,7 @@ public class EventManager implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerLogin(PlayerLoginEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Account account = AccountManager.getAccount(player);
|
||||
Account account = AccountManager.getAccount(player.getUniqueId());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onPlayerLogin(account, event);
|
||||
@ -72,7 +72,7 @@ public class EventManager implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Account account = AccountManager.getAccount(player);
|
||||
Account account = AccountManager.getAccount(player.getUniqueId());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onPlayerQuit(account, event);
|
||||
@ -82,7 +82,7 @@ public class EventManager implements Listener {
|
||||
@EventHandler
|
||||
public void onAsyncChat(AsyncChatEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Account account = AccountManager.getAccount(player);
|
||||
Account account = AccountManager.getAccount(player.getUniqueId());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onAsyncChat(account, event);
|
||||
|
Reference in New Issue
Block a user