stuff
This commit is contained in:
@ -1,12 +1,16 @@
|
||||
package cc.fascinated.account;
|
||||
|
||||
import cc.fascinated.Aetheria;
|
||||
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.utils.DiscordWebhook;
|
||||
import cc.fascinated.utils.Manager;
|
||||
import cc.fascinated.utils.Priority;
|
||||
import cc.fascinated.utils.Style;
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -14,7 +18,6 @@ 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;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -30,13 +33,15 @@ public class AccountManager extends Manager implements Listener {
|
||||
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()) {
|
||||
loadAccount(player.getUniqueId());
|
||||
}
|
||||
|
||||
Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> {
|
||||
this.saveAccounts();
|
||||
saveAccounts();
|
||||
}, SAVE_INTERVAL, SAVE_INTERVAL, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
@ -78,13 +83,17 @@ public class AccountManager extends Manager implements Listener {
|
||||
/**
|
||||
* Save all accounts to disk.
|
||||
*/
|
||||
private void saveAccounts() {
|
||||
long before = System.currentTimeMillis();
|
||||
public static void saveAccounts() {
|
||||
long before = System.currentTimeMillis(),
|
||||
saved = 0; // The amount of accounts that were saved
|
||||
log.info("Saving accounts...");
|
||||
for (Account account : ACCOUNTS.values()) {
|
||||
account.save(true); // Save the account
|
||||
boolean didSave = account.save(); // Save the account
|
||||
if (didSave) {
|
||||
saved++;
|
||||
}
|
||||
}
|
||||
log.info("Saved {} accounts. ({}ms)", ACCOUNTS.size(), System.currentTimeMillis() - before);
|
||||
log.info("Saved {}/{} accounts. ({}ms)", saved, ACCOUNTS.size(), System.currentTimeMillis() - before);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@ -93,15 +102,15 @@ public class AccountManager extends Manager implements Listener {
|
||||
if (isAccountLoaded(uuid)) { // Account already loaded
|
||||
return;
|
||||
}
|
||||
loadAccount(uuid);
|
||||
loadAccount(uuid); // Load the account into memory
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerJoin(Account account, PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
String joinMessage = Lang.JOIN_MESSAGE.getAsString();
|
||||
PlayerColor playerColorProfile = account.getPlayerColorProfile();
|
||||
|
||||
if (!player.hasPlayedBefore()) {
|
||||
if (!account.getPlayer().hasPlayedBefore()) {
|
||||
joinMessage = Lang.FIRST_JOIN_MESSAGE.getAsString();
|
||||
|
||||
// Send a notification to the discord log channel
|
||||
@ -110,8 +119,8 @@ public class AccountManager extends Manager implements Listener {
|
||||
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);
|
||||
embed.addField("Name", account.getName(), true);
|
||||
embed.addField("UUID", account.getUuid().toString(), true);
|
||||
|
||||
discordWebhook.addEmbed(embed);
|
||||
try {
|
||||
@ -122,25 +131,30 @@ public class AccountManager extends Manager implements Listener {
|
||||
});
|
||||
}
|
||||
event.joinMessage(Style.getMiniMessage().deserialize(joinMessage
|
||||
.replace("%player%", player.getName())
|
||||
.replace("player-color", account.getPlayerColorProfile().getColor().toString())
|
||||
.replace("%player%", account.getName())
|
||||
.replace("player-color", playerColorProfile.getColor().toString())
|
||||
));
|
||||
|
||||
// Check if the player is using an outdated version and send a warning message
|
||||
int version = Via.getAPI().getPlayerVersion(account.getUuid());
|
||||
if (version < Config.VERSION_WARNING_VERSION.getAsInt()) {
|
||||
account.sendMessage(Style.getMiniMessage().deserialize(Config.VERSION_WARNING_MESSAGE.getAsString()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerQuit(Account account, PlayerQuitEvent event) {
|
||||
account.save(true); // Save the account
|
||||
|
||||
ACCOUNTS.remove(account.getUuid());
|
||||
event.quitMessage(Style.getMiniMessage().deserialize(Lang.QUIT_MESSAGE.getAsString()
|
||||
.replace("%player%", event.getPlayer().getName())
|
||||
.replace("player-color", account.getPlayerColorProfile().getColor().toString())
|
||||
));
|
||||
account.save(); // Save the account
|
||||
ACCOUNTS.remove(account.getUuid()); // Remove the account from the cache
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAetheriaDisable() {
|
||||
this.saveAccounts(); // Save the accounts to disk
|
||||
saveAccounts(); // Save the accounts to disk
|
||||
ACCOUNTS.clear(); // Remove the accounts from the cache
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user