|
|
@@ -0,0 +1,91 @@
|
|
|
+package me.lethunderhawk.profile.representation;
|
|
|
+
|
|
|
+import me.lethunderhawk.profile.config.abstraction.PersistentConfig;
|
|
|
+import me.lethunderhawk.profile.config.abstraction.subconfig.ProfileSubConfig;
|
|
|
+import me.lethunderhawk.profile.config.abstraction.subconfig.ProfileSubConfigFactory;
|
|
|
+import me.lethunderhawk.profile.config.registry.ProfileModuleRegistry;
|
|
|
+import org.bukkit.configuration.ConfigurationSection;
|
|
|
+import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class FluxProfile implements PersistentConfig {
|
|
|
+
|
|
|
+ private final String playerUUID;
|
|
|
+
|
|
|
+ // key = subconfig id
|
|
|
+ private final Map<String, ProfileSubConfig> subConfigs = new HashMap<>();
|
|
|
+
|
|
|
+ public FluxProfile(String playerUUID) {
|
|
|
+ this.playerUUID = playerUUID;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Add a module dynamically.
|
|
|
+ */
|
|
|
+ public void addSubConfig(ProfileSubConfig subConfig) {
|
|
|
+ subConfigs.put(subConfig.getId(), subConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Get module by type safely.
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public <T extends ProfileSubConfig> T getSubConfig(String id, Class<T> type) {
|
|
|
+ return (T) subConfigs.get(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void serialize(YamlConfiguration cfg) {
|
|
|
+
|
|
|
+ cfg.set("uuid", playerUUID);
|
|
|
+
|
|
|
+ for (ProfileSubConfig subConfig : subConfigs.values()) {
|
|
|
+
|
|
|
+ ConfigurationSection section =
|
|
|
+ cfg.createSection(subConfig.getId());
|
|
|
+
|
|
|
+ subConfig.serialize(section);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static FluxProfile deserialize(YamlConfiguration cfg) {
|
|
|
+
|
|
|
+ String uuid = cfg.getString("uuid");
|
|
|
+
|
|
|
+ if (uuid == null) {
|
|
|
+ throw new IllegalStateException("Profile file missing UUID.");
|
|
|
+ }
|
|
|
+
|
|
|
+ FluxProfile profile = new FluxProfile(uuid);
|
|
|
+
|
|
|
+ for (String id : ProfileModuleRegistry.getRegisteredIds()) {
|
|
|
+
|
|
|
+ if (!cfg.isConfigurationSection(id)) continue;
|
|
|
+
|
|
|
+ ConfigurationSection section = cfg.getConfigurationSection(id);
|
|
|
+
|
|
|
+ ProfileSubConfigFactory<?> factory =
|
|
|
+ ProfileModuleRegistry.getFactory(id);
|
|
|
+
|
|
|
+ if (factory == null) continue;
|
|
|
+
|
|
|
+ ProfileSubConfig subConfig =
|
|
|
+ factory.deserialize(section);
|
|
|
+
|
|
|
+ profile.addSubConfig(subConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ return profile;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getFileName() {
|
|
|
+ return playerUUID;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getPlayerUUID() {
|
|
|
+ return playerUUID;
|
|
|
+ }
|
|
|
+}
|