2021-04-29 10:16:28 +02:00
|
|
|
package de.steamwar.bausystem.configplayer;
|
|
|
|
|
2021-04-29 10:25:20 +02:00
|
|
|
import de.steamwar.bausystem.linkage.LinkageType;
|
|
|
|
import de.steamwar.bausystem.linkage.Linked;
|
|
|
|
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;
|
2021-04-29 10:16:28 +02:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2021-04-29 10:25:20 +02:00
|
|
|
import java.util.UUID;
|
2021-04-29 10:16:28 +02:00
|
|
|
|
2021-04-29 10:25:20 +02:00
|
|
|
@Linked(LinkageType.LISTENER)
|
|
|
|
public class ConfigUpdater implements Listener {
|
|
|
|
|
|
|
|
private final Map<UUID, YAPIONObject> playerConfigurations = new HashMap<>();
|
2021-04-29 10:16:28 +02:00
|
|
|
|
|
|
|
private static final Map<Integer, ConfigConverter> CONFIG_CONVERTER_MAP = new HashMap<>();
|
|
|
|
|
|
|
|
public static void addConfigConverter(ConfigConverter configConverter) {
|
|
|
|
CONFIG_CONVERTER_MAP.putIfAbsent(configConverter.version(), configConverter);
|
|
|
|
}
|
|
|
|
|
2021-04-29 10:25:20 +02:00
|
|
|
@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
|
|
|
|
}
|
2021-04-29 10:30:32 +02:00
|
|
|
|
|
|
|
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) {
|
|
|
|
return ConfigCreator.createDefaultConfig();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
configConverter.update(yapionObject);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return ConfigCreator.createDefaultConfig();
|
|
|
|
}
|
|
|
|
version = yapionObject.getPlainValue("@version");
|
|
|
|
}
|
|
|
|
return yapionObject;
|
|
|
|
}
|
2021-04-29 10:16:28 +02:00
|
|
|
}
|