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;
|
2021-05-12 21:46:11 +02:00
|
|
|
import lombok.Getter;
|
2021-04-29 10:33:23 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2021-04-29 10:25:20 +02:00
|
|
|
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-30 21:02:58 +02:00
|
|
|
import yapion.parser.YAPIONParser;
|
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:33:23 +02:00
|
|
|
import java.util.logging.Level;
|
2021-04-29 10:16:28 +02:00
|
|
|
|
2021-04-29 10:25:20 +02:00
|
|
|
@Linked(LinkageType.LISTENER)
|
2021-05-12 21:46:11 +02:00
|
|
|
public class Config implements Listener {
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
private static Config instance;
|
|
|
|
|
|
|
|
{
|
|
|
|
instance = this;
|
|
|
|
}
|
2021-04-29 10:25:20 +02:00
|
|
|
|
|
|
|
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) {
|
2021-04-30 17:37:23 +02:00
|
|
|
get(event.getPlayer());
|
2021-04-29 10:25:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerQuit(PlayerQuitEvent event) {
|
2021-04-30 17:37:23 +02:00
|
|
|
save(event.getPlayer());
|
|
|
|
playerConfigurations.remove(event.getPlayer().getUniqueId());
|
2021-04-29 10:25:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 17:37:23 +02:00
|
|
|
/**
|
|
|
|
* Get a PlayerConfig, optionally loads it from the DataBase and migrates it if necessary.
|
|
|
|
*
|
|
|
|
* @param player the player from whom to get the config.
|
|
|
|
* @return the config object
|
|
|
|
*/
|
|
|
|
public YAPIONObject get(Player player) {
|
2021-04-30 21:02:58 +02:00
|
|
|
UUID uuid = player.getUniqueId();
|
|
|
|
if (!playerConfigurations.containsKey(uuid)) {
|
2021-04-30 17:37:23 +02:00
|
|
|
// TODO: Load call -> Database
|
2021-04-30 21:02:58 +02:00
|
|
|
String s = null;
|
|
|
|
YAPIONObject yapionObject;
|
|
|
|
if (s == null) {
|
|
|
|
yapionObject = ConfigCreator.createDefaultConfig();
|
|
|
|
} else {
|
|
|
|
yapionObject = YAPIONParser.parse(s);
|
|
|
|
}
|
|
|
|
playerConfigurations.put(uuid, yapionObject);
|
|
|
|
return yapionObject;
|
2021-04-29 10:25:20 +02:00
|
|
|
}
|
2021-04-30 21:02:58 +02:00
|
|
|
return playerConfigurations.get(uuid);
|
2021-04-29 10:25:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 17:37:23 +02:00
|
|
|
/**
|
|
|
|
* Save a PlayerConfig, this does not remove the key value mapping from the map.
|
|
|
|
*
|
|
|
|
* @param player the player to save the config.
|
|
|
|
*/
|
2021-04-29 10:25:20 +02:00
|
|
|
public void save(Player player) {
|
2021-04-30 17:37:23 +02:00
|
|
|
// TODO: Save call -> Database
|
2021-04-29 10:25:20 +02:00
|
|
|
}
|
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) {
|
2021-04-29 10:33:23 +02:00
|
|
|
Bukkit.getLogger().log(Level.SEVERE, "No updater found for version " + version);
|
2021-04-29 10:30:32 +02:00
|
|
|
return ConfigCreator.createDefaultConfig();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
configConverter.update(yapionObject);
|
|
|
|
} catch (Exception e) {
|
2021-04-29 10:33:23 +02:00
|
|
|
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");
|
2021-04-29 10:30:32 +02:00
|
|
|
return ConfigCreator.createDefaultConfig();
|
|
|
|
}
|
2021-04-30 17:00:43 +02:00
|
|
|
if (newVersion < version) {
|
|
|
|
Bukkit.getLogger().log(Level.SEVERE, "Version Tag was earlier after conversion");
|
|
|
|
return ConfigCreator.createDefaultConfig();
|
|
|
|
}
|
2021-04-29 10:33:23 +02:00
|
|
|
version = newVersion;
|
2021-04-29 10:30:32 +02:00
|
|
|
}
|
|
|
|
return yapionObject;
|
|
|
|
}
|
2021-04-29 10:16:28 +02:00
|
|
|
}
|