Making progress on player data stuff

This commit is contained in:
Norbi Peti 2017-01-16 19:49:43 +01:00
parent 201ae7c8f6
commit 27a92b164e
2 changed files with 74 additions and 5 deletions

View file

@ -1,8 +1,27 @@
package buttondevteam.lib.player;
import org.bukkit.configuration.ConfigurationSection;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.configuration.file.YamlConfiguration;
import buttondevteam.lib.TBMCCoreAPI;
public abstract class ChromaGamerBase implements AutoCloseable {
private static final String TBMC_PLAYERS_DIR = "TBMC/players/";
private static final HashMap<Class<?>, String> playerTypes = new HashMap<>();
public static Map<Class<?>, String> getPlayerTypes() {
return Collections.unmodifiableMap(playerTypes);
}
public static <T extends ChromaGamerBase> void addPlayerType(Class<T> cl, String folder) {
playerTypes.put(cl, folder);
}
public abstract class ChromaGamerBase {
/**
* This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc.
*/
@ -13,5 +32,33 @@ public abstract class ChromaGamerBase {
*/
public abstract String getFolder();
protected ConfigurationSection plugindata;
protected YamlConfiguration plugindata;
public YamlConfiguration getData() {
return plugindata;
}
// protected void load() {
/*
* public static void load() { try { plugindata = YamlConfiguration.loadConfiguration(new File(getFolder(), getFileName())); } catch (Exception e) {
* TBMCCoreAPI.SendException("An error occured while loading gamer data", e); } } protected void save() { try { plugindata.save(new File(getFolder(), getFileName())); } catch (Exception e) {
* TBMCCoreAPI.SendException("An error occured while saving gamer data", e); } }
*/
protected static <T extends ChromaGamerBase> T getUser(String fname, Class<T> cl) {
try {
T obj = cl.newInstance();
obj.plugindata = YamlConfiguration // TODO: Put all IDs
.loadConfiguration(new File(TBMC_PLAYERS_DIR + playerTypes.get(cl), fname));
return obj;
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e);
}
return null;
}
@Override
public void close() throws Exception {
plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName()));
}
}

View file

@ -3,7 +3,12 @@ package buttondevteam.lib.player;
import java.util.UUID;
public abstract class TBMCPlayerBase extends ChromaGamerBase {
public abstract UUID getUUID();
private static final String FOLDER_NAME = "minecraft";
protected UUID uuid;
public UUID getUUID() {
return uuid;
}
public abstract String getPluginName();
@ -14,6 +19,23 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
@Override
public String getFolder() {
return "minecraft";
return FOLDER_NAME;
}
// protected ConfigurationSection plugindata;
/*
* public static void load() { super.load(); plugindata = super.plugindata.getConfigurationSection(getPluginName()); if (plugindata == null) plugindata =
* super.plugindata.createSection(getPluginName()); } protected void save() { plugindata = super.plugindata.createSection(getPluginName(), plugindata.getValues(true)); super.save(); }
*/
static {
addPlayerType(TBMCPlayerBase.class, FOLDER_NAME);
}
public static <T extends TBMCPlayerBase> T getPlayer(UUID uuid, Class<T> cl) {
T obj = ChromaGamerBase.getUser(uuid.toString(), cl);
obj.uuid = uuid;
return obj;
}
}