Remade user data managing, added a test #30

Merged
NorbiPeti merged 22 commits from dev into master 2017-04-15 11:54:06 +00:00
2 changed files with 74 additions and 5 deletions
Showing only changes of commit 27a92b164e - Show all commits

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;
}
}