package cc.fascinated.account; import cc.fascinated.Aetheria; import cc.fascinated.playercolor.PlayerColor; import lombok.Getter; import lombok.SneakyThrows; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import java.io.File; import java.util.UUID; @Getter 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) { 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) { e.printStackTrace(); } } config = YamlConfiguration.loadConfiguration(file); if (newAccount) { this.firstJoin = System.currentTimeMillis(); this.lastLogin = System.currentTimeMillis(); save(false); // Save default values } this.firstJoin = config.getLong("firstJoin"); this.lastLogin = config.getLong("lastLogin"); // Load profiles this.playerColorProfile = config.contains("playerColor") ? new PlayerColor(this, config.getConfigurationSection("playerColor")) : new PlayerColor(this); } /** * Get the name of the player. * * @return the name */ public String getName() { return getPlayer().getName(); } /** * Get the bukkit player for this account. * * @return the player */ public Player getPlayer() { return org.bukkit.Bukkit.getPlayer(uuid); } /** * Send a message to the player. * * @param message the message to send */ public void sendMessage(String message) { getPlayer().sendPlainMessage(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 += "Profile"; // append "Profile" to the key to signify it's a profile profile.save(config.getConfigurationSection(key) == null ? config.createSection(key) : config.getConfigurationSection(key)); } @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); } }