1
0

fix logs and first join message

This commit is contained in:
Lee
2024-03-30 09:57:01 +00:00
parent 641e40b35d
commit e3bd0b7c15
5 changed files with 37 additions and 23 deletions

View File

@ -11,6 +11,7 @@ import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -52,13 +53,13 @@ public class Account {
*/ */
private final PlayerColor playerColorProfile; private final PlayerColor playerColorProfile;
public Account(UUID uuid) { public Account(Player player) {
//log.info("Loading account for " + uuid); //log.info("Loading account for " + uuid);
boolean newAccount = false; boolean newAccount = false;
this.uuid = uuid; this.uuid = player.getUniqueId();
File file = new File(Aetheria.INSTANCE.getDataFolder(), "accounts/" + this.uuid.toString() + ".yml"); File file = new File(Aetheria.INSTANCE.getDataFolder(), "accounts/" + this.uuid + ".yml");
this.file = file; this.file = file;
if (!file.exists()) { if (!file.exists()) {
newAccount = true; newAccount = true;
@ -79,7 +80,7 @@ public class Account {
log.info("Created new account for " + this.uuid); log.info("Created new account for " + this.uuid);
Bukkit.broadcast(Style.getMiniMessage().deserialize(Lang.FIRST_JOIN_MESSAGE.getAsString() Bukkit.broadcast(Style.getMiniMessage().deserialize(Lang.FIRST_JOIN_MESSAGE.getAsString()
.replace("%player%", this.getName()) .replace("%player%", player.getName())
)); ));
Aetheria.EXECUTOR.execute(() -> { Aetheria.EXECUTOR.execute(() -> {
@ -87,7 +88,7 @@ public class Account {
DiscordWebhook discordWebhook = new DiscordWebhook(Config.DISCORD_LOG_WEBHOOK.getAsString()); DiscordWebhook discordWebhook = new DiscordWebhook(Config.DISCORD_LOG_WEBHOOK.getAsString());
DiscordWebhook.EmbedObject embed = new DiscordWebhook.EmbedObject(); DiscordWebhook.EmbedObject embed = new DiscordWebhook.EmbedObject();
embed.setTitle("New Player Joined"); embed.setTitle("New Player Joined");
embed.addField("Name", Bukkit.getOfflinePlayer(uuid).getName(), true); embed.addField("Name", player.getName(), true);
embed.addField("UUID", uuid.toString(), true); embed.addField("UUID", uuid.toString(), true);
discordWebhook.addEmbed(embed); discordWebhook.addEmbed(embed);
@ -128,6 +129,15 @@ public class Account {
return Bukkit.getPlayer(uuid); return Bukkit.getPlayer(uuid);
} }
/**
* Get the offline player for this account.
*
* @return the offline player
*/
public OfflinePlayer getOfflinePlayer() {
return Bukkit.getOfflinePlayer(uuid);
}
/** /**
* Send a message to the player. * Send a message to the player.
* *

View File

@ -23,7 +23,7 @@ public class AccountManager extends Manager {
public AccountManager() { public AccountManager() {
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers()) {
registerAccount(player.getUniqueId()); registerAccount(player);
} }
Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> { Bukkit.getAsyncScheduler().runAtFixedRate(Aetheria.INSTANCE, (task) -> {
@ -34,12 +34,13 @@ public class AccountManager extends Manager {
/** /**
* Gets the account for the specified player. * Gets the account for the specified player.
* *
* @param uuid the UUID of the player * @param player the player
* @return the account * @return the account
*/ */
public static Account getAccount(UUID uuid) { public static Account getAccount(Player player) {
UUID uuid = player.getUniqueId();
if (!ACCOUNTS.containsKey(uuid)) { if (!ACCOUNTS.containsKey(uuid)) {
return registerAccount(uuid); return registerAccount(player);
} }
return ACCOUNTS.get(uuid); return ACCOUNTS.get(uuid);
} }
@ -47,20 +48,21 @@ public class AccountManager extends Manager {
/** /**
* Checks if an account is already registered for the specified player. * Checks if an account is already registered for the specified player.
* *
* @param uuid the UUID of the player * @param player the player
* @return true if the account is already registered, false otherwise * @return true if the account is already registered, false otherwise
*/ */
private boolean accountRegistered(UUID uuid) { private boolean accountRegistered(Player player) {
return ACCOUNTS.containsKey(uuid); return ACCOUNTS.containsKey(player.getUniqueId());
} }
/** /**
* Registers an account for the specified player. * Registers an account for the specified player.
* *
* @param uuid the UUID of the player * @param player the player
*/ */
private static Account registerAccount(UUID uuid) { private static Account registerAccount(Player player) {
Account account = new Account(uuid); UUID uuid = player.getUniqueId();
Account account = new Account(player);
ACCOUNTS.put(uuid, account); ACCOUNTS.put(uuid, account);
return account; return account;
} }
@ -79,18 +81,20 @@ public class AccountManager extends Manager {
@EventHandler @EventHandler
public void onJoin(PlayerJoinEvent event) { public void onJoin(PlayerJoinEvent event) {
if (accountRegistered(event.getPlayer().getUniqueId())) { // Account already registered Player player = event.getPlayer();
if (accountRegistered(player)) { // Account already registered
return; return;
} }
registerAccount(event.getPlayer().getUniqueId()); registerAccount(player);
} }
@EventHandler @EventHandler
public void onQuit(PlayerQuitEvent event) { public void onQuit(PlayerQuitEvent event) {
Account account = getAccount(event.getPlayer().getUniqueId()); Player player = event.getPlayer();
Account account = getAccount(player);
account.save(true); account.save(true);
ACCOUNTS.remove(event.getPlayer().getUniqueId()); ACCOUNTS.remove(player.getUniqueId());
} }
@EventHandler @EventHandler

View File

@ -36,7 +36,7 @@ public class ChatManager extends Manager {
@EventHandler @EventHandler
public void onChat(AsyncChatEvent event) { public void onChat(AsyncChatEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Account account = AccountManager.getAccount(player.getUniqueId()); Account account = AccountManager.getAccount(player);
NamedTextColor color = account.getPlayerColorProfile().getColor(); NamedTextColor color = account.getPlayerColorProfile().getColor();
BlockReason blockReason = null; BlockReason blockReason = null;

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.getUniqueId()); Account account = AccountManager.getAccount(player);
this.execute(account, strings); this.execute(account, strings);
return true; return true;
} }

View File

@ -83,7 +83,7 @@ public class PlayerColorManager extends Manager {
@EventHandler @EventHandler
public void onLogin(PlayerLoginEvent event) { public void onLogin(PlayerLoginEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Account account = AccountManager.getAccount(player.getUniqueId()); Account account = AccountManager.getAccount(player);
PlayerColor playerColor = account.getPlayerColorProfile(); PlayerColor playerColor = account.getPlayerColorProfile();
Team team = PlayerColorManager.getScoreboardTeam(playerColor.getColor()); Team team = PlayerColorManager.getScoreboardTeam(playerColor.getColor());
@ -93,7 +93,7 @@ public class PlayerColorManager extends Manager {
@EventHandler @EventHandler
public void onQuit(PlayerQuitEvent event) { public void onQuit(PlayerQuitEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Account account = AccountManager.getAccount(player.getUniqueId()); Account account = AccountManager.getAccount(player);
PlayerColor playerColor = account.getPlayerColorProfile(); PlayerColor playerColor = account.getPlayerColorProfile();
Team team = PlayerColorManager.getScoreboardTeam(playerColor.getColor()); Team team = PlayerColorManager.getScoreboardTeam(playerColor.getColor());