package cc.fascinated.account; import cc.fascinated.Aetheria; import cc.fascinated.config.Config; import cc.fascinated.config.Lang; import cc.fascinated.playercolor.PlayerColor; import cc.fascinated.utils.DiscordWebhook; import cc.fascinated.utils.Style; import lombok.Getter; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import java.io.File; import java.io.IOException; import java.util.UUID; @Getter @Log4j2 public class Account { /** * The UUID of the player. */ private final UUID uuid; /** * The first time the player joined the server. */ private long firstJoin; /** * The last time the player logged in. */ private long lastLogin; /** * The file for this account. */ private final File file; /** * The configuration for this account. */ private final FileConfiguration config; /** * Account profiles. */ private final PlayerColor playerColorProfile; public Account(UUID uuid) { //log.info("Loading account for " + uuid); boolean newAccount = false; this.uuid = uuid; File file = new File(Aetheria.INSTANCE.getDataFolder(), "accounts/" + this.uuid.toString() + ".yml"); this.file = file; if (!file.exists()) { newAccount = true; file.getParentFile().mkdirs(); try { file.createNewFile(); } catch (Exception e) { log.warn("Failed to create account file for " + this.uuid); e.printStackTrace(); } } config = YamlConfiguration.loadConfiguration(file); if (newAccount) { this.firstJoin = System.currentTimeMillis(); this.lastLogin = System.currentTimeMillis(); this.save(false); // Save default values log.info("Created new account for " + this.uuid); Bukkit.broadcast(Style.getMiniMessage().deserialize(Lang.FIRST_JOIN_MESSAGE.getAsString() .replace("%player%", this.getName()) )); 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", Bukkit.getOfflinePlayer(uuid).getName(), true); embed.addField("UUID", uuid.toString(), true); discordWebhook.addEmbed(embed); try { discordWebhook.execute(); } catch (IOException e) { throw new RuntimeException(e); } }); } this.firstJoin = config.getLong("firstJoin"); this.lastLogin = config.getLong("lastLogin"); this.lastLogin = System.currentTimeMillis(); // Update last login // Load profiles this.playerColorProfile = new PlayerColor(this, this.getProfileSection("playerColor")); //log.info("Loaded account for " + this.uuid); } /** * Get the name of the player. * * @return the name */ public String getName() { return this.getPlayer().getName(); } /** * Get the bukkit player for this account. * * @return the player */ public Player getPlayer() { return Bukkit.getPlayer(uuid); } /** * Send a message to the player. * * @param component the message to send */ public void sendMessage(Component component) { getPlayer().sendMessage(component); } /** * Send a message to the player. * * @param message the message to send */ public void sendMessage(String message) { this.sendMessage(Style.getMiniMessage().deserialize(message)); } /** * Save a profile to the configuration. * * @param profile the profile to save * @param key the key to save the profile under */ private void saveProfile(Profile profile, String key) { key = this.getProfileId(key); // append "Profile" to the key to signify it's a profile profile.save(config.getConfigurationSection(key) == null ? config.createSection(key) : config.getConfigurationSection(key)); } private ConfigurationSection getProfileSection(String key) { return this.config.getConfigurationSection(this.getProfileId(key)); } private String getProfileId(String key) { return key + "Profile"; // append "Profile" to the key to signify it's a profile } @SneakyThrows public void save(boolean saveProfiles) { this.config.set("firstJoin", this.firstJoin); this.config.set("lastLogin", this.lastLogin); if (saveProfiles) { this.saveProfile(this.playerColorProfile, "playerColor"); } this.config.save(this.file); } }