From c80d1318d61b8459236a3de36d72e46465fc4e51 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 27 Mar 2024 01:27:23 +0000 Subject: [PATCH] fuck me components are ugly --- .../java/cc/fascinated/account/Account.java | 38 +++++++++++++++---- .../java/cc/fascinated/account/Profile.java | 9 ----- .../fascinated/playercolor/PlayerColor.java | 12 +++--- .../command/PlayerColorCommand.java | 20 +++++----- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/main/java/cc/fascinated/account/Account.java b/src/main/java/cc/fascinated/account/Account.java index f618db5..354285e 100644 --- a/src/main/java/cc/fascinated/account/Account.java +++ b/src/main/java/cc/fascinated/account/Account.java @@ -4,13 +4,17 @@ import cc.fascinated.Aetheria; import cc.fascinated.playercolor.PlayerColor; 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.util.UUID; -@Getter +@Getter @Log4j2 public class Account { /** @@ -44,6 +48,7 @@ public class Account { private final PlayerColor playerColorProfile; public Account(UUID uuid) { + log.info("Loading account for " + uuid); boolean newAccount = false; this.uuid = uuid; @@ -64,15 +69,17 @@ public class Account { if (newAccount) { this.firstJoin = System.currentTimeMillis(); this.lastLogin = System.currentTimeMillis(); - save(false); // Save default values + this.save(false); // Save default values + log.info("Created new account for " + this.uuid); } 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); + this.playerColorProfile = new PlayerColor(this, this.getProfileSection("playerColor")); + + log.info("Loaded account for " + this.uuid); } /** @@ -81,7 +88,7 @@ public class Account { * @return the name */ public String getName() { - return getPlayer().getName(); + return this.getPlayer().getName(); } /** @@ -90,7 +97,7 @@ public class Account { * @return the player */ public Player getPlayer() { - return org.bukkit.Bukkit.getPlayer(uuid); + return Bukkit.getPlayer(uuid); } /** @@ -102,6 +109,15 @@ public class Account { getPlayer().sendPlainMessage(message); } + /** + * Send a message to the player. + * + * @param component the message to send + */ + public void sendMessage(Component component) { + getPlayer().sendMessage(component); + } + /** * Save a profile to the configuration. * @@ -109,10 +125,18 @@ public class Account { * @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 + 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); diff --git a/src/main/java/cc/fascinated/account/Profile.java b/src/main/java/cc/fascinated/account/Profile.java index bf9cb4e..8cb78b0 100644 --- a/src/main/java/cc/fascinated/account/Profile.java +++ b/src/main/java/cc/fascinated/account/Profile.java @@ -22,15 +22,6 @@ public abstract class Profile extends Manager { this.account = account; } - /** - * This constructor is used for creating a new profile. - * - * @param account the account to create the profile for - */ - public Profile(Account account) { - this.account = account; - } - /** * Saves this profile to file. * diff --git a/src/main/java/cc/fascinated/playercolor/PlayerColor.java b/src/main/java/cc/fascinated/playercolor/PlayerColor.java index 60aad9c..a87c8f2 100644 --- a/src/main/java/cc/fascinated/playercolor/PlayerColor.java +++ b/src/main/java/cc/fascinated/playercolor/PlayerColor.java @@ -16,16 +16,14 @@ public class PlayerColor extends Profile { */ private NamedTextColor color; - public PlayerColor(Account account) { - super(account); - - this.color = PlayerColorManager.getRandomColor(); - } - public PlayerColor(Account account, ConfigurationSection section) { super(account, section); - this.color = NamedTextColor.namedColor(section.getInt("color")); + if (section == null) { + this.color = PlayerColorManager.getRandomColor(); + } else { + this.color = NamedTextColor.namedColor(section.getInt("color")); + } } /** diff --git a/src/main/java/cc/fascinated/playercolor/command/PlayerColorCommand.java b/src/main/java/cc/fascinated/playercolor/command/PlayerColorCommand.java index ce30b3d..fbeab02 100644 --- a/src/main/java/cc/fascinated/playercolor/command/PlayerColorCommand.java +++ b/src/main/java/cc/fascinated/playercolor/command/PlayerColorCommand.java @@ -4,6 +4,7 @@ import cc.fascinated.account.Account; import cc.fascinated.command.Command; import cc.fascinated.playercolor.PlayerColor; import cc.fascinated.playercolor.PlayerColorManager; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import java.util.List; @@ -16,13 +17,20 @@ public class PlayerColorCommand extends Command { @Override public void execute(Account account, String[] args) { + List validColors = PlayerColorManager.getValidColors(); PlayerColor playerColorProfile = account.getPlayerColorProfile(); + if (args.length == 0) { - account.sendMessage("§b§lPLAYERCOLOR §7» §fYour current color is: " + playerColorProfile.getColor() + "§f."); + account.sendMessage(Component.text("§b§lPLAYERCOLOR §7» §fYour current color is: ").append(Component.text(playerColorProfile.getColor().toString()).color(playerColorProfile.getColor()))); + Component validColorsComponent = Component.text("§fValid colors: "); + for (NamedTextColor validColor : validColors) { + validColorsComponent = validColorsComponent.append(Component.text(validColor.toString() + ", ").color(validColor)); + } + validColorsComponent = validColorsComponent.append(Component.text("random.").color(NamedTextColor.WHITE)); + account.sendMessage(validColorsComponent); return; } - List validColors = PlayerColorManager.getValidColors(); if (args[0].equalsIgnoreCase("random")) { playerColorProfile.setColor(validColors.get((int) (Math.random() * validColors.size()))); account.sendMessage("§b§lPLAYERCOLOR §7» §fYour color has been set to: " + playerColorProfile.getColor() + "§f."); @@ -32,16 +40,10 @@ public class PlayerColorCommand extends Command { NamedTextColor color = PlayerColorManager.getColor(args[0]); if (color == null) { account.sendMessage("§b§lPLAYERCOLOR §7» §cInvalid color."); - - StringBuilder builder = new StringBuilder("§fValid colors: "); - for (NamedTextColor validColor : validColors) { - builder.append(validColor).append(", "); - } - account.sendMessage(builder.substring(0, builder.length() - 2) + "."); return; } playerColorProfile.setColor(color); - account.sendMessage("§b§lPLAYERCOLOR §7» §fYour color has been set to: " + playerColorProfile.getColor() + "§f."); + account.sendMessage(Component.text("§b§lPLAYERCOLOR §7» §fYour color has been set to: ").append(Component.text(playerColorProfile.getColor().toString()).color(playerColorProfile.getColor()))); } }