1
0
Files
aetheria-anarchy-plugin/src/main/java/cc/fascinated/account/Account.java

180 lines
5.2 KiB
Java
Raw Normal View History

package cc.fascinated.account;
import cc.fascinated.Aetheria;
2024-03-29 17:46:05 +00:00
import cc.fascinated.config.Config;
2024-03-28 19:54:03 +00:00
import cc.fascinated.config.Lang;
import cc.fascinated.playercolor.PlayerColor;
2024-03-29 17:46:05 +00:00
import cc.fascinated.utils.DiscordWebhook;
2024-03-28 18:13:15 +00:00
import cc.fascinated.utils.Style;
import lombok.Getter;
import lombok.SneakyThrows;
2024-03-27 01:27:23 +00:00
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;
2024-03-29 17:46:05 +00:00
import java.io.IOException;
import java.util.UUID;
2024-03-27 01:27:23 +00:00
@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) {
2024-03-28 18:13:15 +00:00
//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) {
2024-03-28 18:13:15 +00:00
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();
2024-03-27 01:27:23 +00:00
this.save(false); // Save default values
log.info("Created new account for " + this.uuid);
2024-03-28 19:54:03 +00:00
2024-03-29 17:46:05 +00:00
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");
2024-03-28 14:00:15 +00:00
this.lastLogin = System.currentTimeMillis(); // Update last login
// Load profiles
2024-03-27 01:27:23 +00:00
this.playerColorProfile = new PlayerColor(this, this.getProfileSection("playerColor"));
2024-03-28 18:13:15 +00:00
//log.info("Loaded account for " + this.uuid);
}
/**
* Get the name of the player.
*
* @return the name
*/
public String getName() {
2024-03-27 01:27:23 +00:00
return this.getPlayer().getName();
}
/**
* Get the bukkit player for this account.
*
* @return the player
*/
public Player getPlayer() {
2024-03-27 01:27:23 +00:00
return Bukkit.getPlayer(uuid);
}
/**
* Send a message to the player.
*
2024-03-28 15:12:18 +00:00
* @param component the message to send
*/
2024-03-28 15:12:18 +00:00
public void sendMessage(Component component) {
getPlayer().sendMessage(component);
}
2024-03-27 01:27:23 +00:00
/**
* Send a message to the player.
*
2024-03-28 15:12:18 +00:00
* @param message the message to send
2024-03-27 01:27:23 +00:00
*/
2024-03-28 15:12:18 +00:00
public void sendMessage(String message) {
2024-03-28 18:13:15 +00:00
this.sendMessage(Style.getMiniMessage().deserialize(message));
2024-03-27 01:27:23 +00:00
}
/**
* 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) {
2024-03-27 01:27:23 +00:00
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));
}
2024-03-27 01:27:23 +00:00
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);
}
}