2024-03-26 09:00:12 +00:00
|
|
|
package cc.fascinated.account;
|
|
|
|
|
|
|
|
import cc.fascinated.Aetheria;
|
2024-04-01 12:34:28 +01:00
|
|
|
import cc.fascinated.config.Config;
|
|
|
|
import cc.fascinated.config.Lang;
|
|
|
|
import cc.fascinated.utils.DiscordWebhook;
|
2024-03-26 09:00:12 +00:00
|
|
|
import cc.fascinated.utils.Manager;
|
2024-04-01 12:34:28 +01:00
|
|
|
import cc.fascinated.utils.Style;
|
2024-03-26 09:00:12 +00:00
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
2024-04-01 12:34:28 +01:00
|
|
|
import org.bukkit.event.player.PlayerLoginEvent;
|
2024-03-26 09:00:12 +00:00
|
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
|
|
|
2024-04-01 12:34:28 +01:00
|
|
|
import java.io.IOException;
|
2024-03-26 09:00:12 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
@Log4j2
|
|
|
|
public class AccountManager extends Manager {
|
|
|
|
|
2024-03-29 17:46:05 +00:00
|
|
|
private final long SAVE_INTERVAL = 5; // in minutes
|
2024-03-26 09:00:12 +00:00
|
|
|
private static final Map<UUID, Account> ACCOUNTS = new HashMap<>();
|
|
|
|
|
|
|
|
public AccountManager() {
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
2024-04-01 12:34:28 +01:00
|
|
|
loadAccount(player);
|
2024-03-26 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> {
|
|
|
|
this.saveAccounts();
|
2024-03-29 17:46:05 +00:00
|
|
|
}, SAVE_INTERVAL, SAVE_INTERVAL, TimeUnit.MINUTES);
|
2024-03-26 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the account for the specified player.
|
|
|
|
*
|
2024-03-30 09:57:01 +00:00
|
|
|
* @param player the player
|
2024-03-26 09:00:12 +00:00
|
|
|
* @return the account
|
|
|
|
*/
|
2024-03-30 09:57:01 +00:00
|
|
|
public static Account getAccount(Player player) {
|
|
|
|
UUID uuid = player.getUniqueId();
|
2024-03-26 09:04:54 +00:00
|
|
|
if (!ACCOUNTS.containsKey(uuid)) {
|
2024-04-01 12:34:28 +01:00
|
|
|
return loadAccount(player);
|
2024-03-26 09:04:54 +00:00
|
|
|
}
|
|
|
|
return ACCOUNTS.get(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if an account is already registered for the specified player.
|
|
|
|
*
|
2024-03-30 09:57:01 +00:00
|
|
|
* @param player the player
|
2024-03-26 09:04:54 +00:00
|
|
|
* @return true if the account is already registered, false otherwise
|
|
|
|
*/
|
2024-04-01 12:34:28 +01:00
|
|
|
private boolean isAccountLoaded(Player player) {
|
2024-03-30 09:57:01 +00:00
|
|
|
return ACCOUNTS.containsKey(player.getUniqueId());
|
2024-03-26 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers an account for the specified player.
|
|
|
|
*
|
2024-03-30 09:57:01 +00:00
|
|
|
* @param player the player
|
2024-03-26 09:00:12 +00:00
|
|
|
*/
|
2024-04-01 12:34:28 +01:00
|
|
|
private static Account loadAccount(Player player) {
|
2024-03-30 09:57:01 +00:00
|
|
|
UUID uuid = player.getUniqueId();
|
|
|
|
Account account = new Account(player);
|
2024-03-26 09:04:54 +00:00
|
|
|
ACCOUNTS.put(uuid, account);
|
|
|
|
return account;
|
2024-03-26 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save all accounts to disk.
|
|
|
|
*/
|
|
|
|
private void saveAccounts() {
|
2024-03-28 18:13:15 +00:00
|
|
|
long before = System.currentTimeMillis();
|
2024-03-26 09:00:12 +00:00
|
|
|
log.info("Saving accounts...");
|
|
|
|
for (Account account : ACCOUNTS.values()) {
|
|
|
|
account.save(true); // Save the account
|
|
|
|
}
|
2024-03-28 18:13:15 +00:00
|
|
|
log.info("Saved {} accounts. ({}ms)", ACCOUNTS.size(), System.currentTimeMillis() - before);
|
2024-03-26 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 12:34:28 +01:00
|
|
|
@Override
|
|
|
|
public void onPlayerJoin(Account account, PlayerJoinEvent event) {
|
2024-03-30 09:57:01 +00:00
|
|
|
Player player = event.getPlayer();
|
2024-04-01 12:34:28 +01:00
|
|
|
if (player.hasPlayedBefore()) {
|
2024-03-26 09:04:54 +00:00
|
|
|
return;
|
2024-03-26 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 12:34:28 +01:00
|
|
|
event.joinMessage(null); // Remove the join message so we can send our own
|
|
|
|
Bukkit.broadcast(Style.getMiniMessage().deserialize(Lang.FIRST_JOIN_MESSAGE.getAsString()
|
|
|
|
.replace("%player%", player.getName())
|
|
|
|
));
|
2024-03-26 09:00:12 +00:00
|
|
|
|
2024-04-01 12:34:28 +01:00
|
|
|
Aetheria.EXECUTOR.execute(() -> {
|
|
|
|
// todo: re-code this it's so ugly
|
|
|
|
DiscordWebhook discordWebhook = new DiscordWebhook(Config.DISCORD_LOG_WEBHOOK.getAsString());
|
|
|
|
DiscordWebhook.EmbedObject embed = new DiscordWebhook.EmbedObject();
|
|
|
|
embed.setTitle("New Player Joined");
|
|
|
|
embed.addField("Name", player.getName(), true);
|
|
|
|
embed.addField("UUID", player.getUniqueId().toString(), true);
|
|
|
|
|
|
|
|
discordWebhook.addEmbed(embed);
|
|
|
|
try {
|
|
|
|
discordWebhook.execute();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
});
|
2024-03-26 09:00:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 12:34:28 +01:00
|
|
|
@Override
|
|
|
|
public void onPlayerLogin(Account account, PlayerLoginEvent event) {
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
if (isAccountLoaded(player)) { // Account already loaded
|
2024-03-26 09:00:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-01 12:34:28 +01:00
|
|
|
loadAccount(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlayerQuit(Account account, PlayerQuitEvent event) {
|
|
|
|
account.save(true);
|
|
|
|
|
|
|
|
ACCOUNTS.remove(account.getUuid());
|
|
|
|
}
|
2024-03-26 09:00:12 +00:00
|
|
|
|
2024-04-01 12:34:28 +01:00
|
|
|
@Override
|
|
|
|
public void onAetheriaDisable() {
|
2024-03-29 17:46:05 +00:00
|
|
|
this.saveAccounts(); // Save the accounts to disk
|
|
|
|
ACCOUNTS.clear(); // Remove the accounts from the cache
|
2024-03-26 09:00:12 +00:00
|
|
|
}
|
|
|
|
}
|