package de.steamwar.bausystem.configplayer; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import yapion.hierarchy.types.YAPIONObject; import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.logging.Level; @Linked(LinkageType.LISTENER) public class ConfigUpdater implements Listener { // TODO: implement everything private final Map playerConfigurations = new HashMap<>(); private static final Map CONFIG_CONVERTER_MAP = new HashMap<>(); public static void addConfigConverter(ConfigConverter configConverter) { CONFIG_CONVERTER_MAP.putIfAbsent(configConverter.version(), configConverter); } @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { // Load call -> Database } @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { // Save call -> Database } public YAPIONObject load(Player player) { if (!playerConfigurations.containsKey(player.getUniqueId())) { // Load call -> Database } return playerConfigurations.get(player.getUniqueId()); } public void save(Player player) { // Save call -> Database } private YAPIONObject update(YAPIONObject yapionObject) { int version = yapionObject.getPlainValue("@version"); while (version < ConfigCreator.currentVersion) { ConfigConverter configConverter = CONFIG_CONVERTER_MAP.getOrDefault(version, null); if (configConverter == null) { Bukkit.getLogger().log(Level.SEVERE, "No updater found for version " + version); return ConfigCreator.createDefaultConfig(); } try { configConverter.update(yapionObject); } catch (Exception e) { Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e); return ConfigCreator.createDefaultConfig(); } int newVersion = yapionObject.getPlainValue("@version"); if (version == newVersion) { Bukkit.getLogger().log(Level.SEVERE, "Version Tag was the same after conversion"); return ConfigCreator.createDefaultConfig(); } version = newVersion; } return yapionObject; } }