Remade user data managing, added a test #30
3 changed files with 27 additions and 11 deletions
|
@ -21,7 +21,8 @@ public abstract class ChromaGamerBase implements AutoCloseable {
|
|||
public static void RegisterPluginUserClass(Class<? extends ChromaGamerBase> userclass) {
|
||||
if (userclass.isAnnotationPresent(UserClass.class))
|
||||
playerTypes.put(userclass, userclass.getAnnotation(UserClass.class).foldername());
|
||||
throw new RuntimeException("Class not registered as a user class! Use @UserClass or TBMCPlayerBase");
|
||||
else //<-- Really important
|
||||
throw new RuntimeException("Class not registered as a user class! Use @UserClass or TBMCPlayerBase");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,9 +65,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
|
|||
public static <T extends ChromaGamerBase> T getUser(String fname, Class<T> cl) {
|
||||
try {
|
||||
T obj = cl.newInstance();
|
||||
final File file = new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname + ".yml");
|
||||
final String folder = getFolderForType(cl);
|
||||
final File file = new File(TBMC_PLAYERS_DIR + folder, fname + ".yml");
|
||||
file.getParentFile().mkdirs();
|
||||
obj.plugindata = YamlConfiguration.loadConfiguration(file);
|
||||
obj.plugindata.set(folder + "_id", obj.getID());
|
||||
return obj;
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e);
|
||||
|
@ -152,10 +155,15 @@ public abstract class ChromaGamerBase implements AutoCloseable {
|
|||
* The target player class
|
||||
* @return The player as a {@link T} object or null if not having an account there
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends ChromaGamerBase> T getAs(Class<T> cl) { // TODO: Provide a way to use TBMCPlayerBase's loaded players
|
||||
//System.out.println("getAs cls: " + cl.getSimpleName() + " =? " + getClass().getSimpleName()); // TODO: TMP - Don't be tired when programming
|
||||
if (cl.getSimpleName().equals(getClass().getSimpleName()))
|
||||
return (T) this;
|
||||
String newfolder = getFolderForType(cl);
|
||||
if (newfolder == null)
|
||||
throw new RuntimeException("The specified class " + cl.getSimpleName() + " isn't registered!");
|
||||
System.out.println("getAs newfolder: " + newfolder);
|
||||
if (!plugindata.contains(newfolder + "_id"))
|
||||
return null;
|
||||
return getUser(plugindata.getString(newfolder + "_id"), cl);
|
||||
|
@ -190,6 +198,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
|
|||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T> PlayerData<T> data() {
|
||||
//System.out.println("Calling ChromaGamerBase data"); // TODO: TMP - Debugged for hours
|
||||
if (!getClass().isAnnotationPresent(UserClass.class))
|
||||
throw new RuntimeException("Class not registered as a user class! Use @UserClass");
|
||||
String mname = new Exception().getStackTrace()[1].getMethodName();
|
||||
|
|
|
@ -12,7 +12,10 @@ public class EnumPlayerData<T extends Enum<T>> {
|
|||
}
|
||||
|
||||
public T get() {
|
||||
return Enum.valueOf(cl, data.get());
|
||||
String str = data.get();
|
||||
if (str == null || str.equals(""))
|
||||
return null;
|
||||
return Enum.valueOf(cl, str);
|
||||
}
|
||||
|
||||
public void set(T value) {
|
||||
|
|
|
@ -35,7 +35,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
}
|
||||
|
||||
public PlayerData<String> PlayerName() {
|
||||
return data();
|
||||
//System.out.println("Calling playername"); // TODO: TMP - The data will only get stored if it's changed
|
||||
return super.data();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -50,6 +51,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
*/
|
||||
@Override
|
||||
protected <T> PlayerData<T> data() {
|
||||
//System.out.println("Calling TMBCPlayerBase data"); // TODO: TMP - Sigh
|
||||
return super.data(pluginname);
|
||||
}
|
||||
|
||||
|
@ -76,14 +78,17 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
public static <T extends TBMCPlayerBase> T getPlayer(UUID uuid, Class<T> cl) {
|
||||
if (playermap.containsKey(uuid + "-" + cl.getSimpleName()))
|
||||
return (T) playermap.get(uuid + "-" + cl.getSimpleName());
|
||||
//System.out.println("A");
|
||||
try {
|
||||
T player;
|
||||
if (playermap.containsKey(uuid + "-" + TBMCPlayer.class.getSimpleName())) {
|
||||
//System.out.println("B"); - Don't program when tired
|
||||
player = cl.newInstance();
|
||||
player.plugindata = playermap.get(uuid + "-" + TBMCPlayer.class.getSimpleName()).plugindata;
|
||||
playermap.put(player.uuid + "-" + cl.getSimpleName(), player); // It will get removed on player quit
|
||||
playermap.put(uuid + "-" + cl.getSimpleName(), player); // It will get removed on player quit
|
||||
} else
|
||||
player = ChromaGamerBase.getUser(uuid.toString(), cl);
|
||||
//System.out.println("C");
|
||||
player.uuid = uuid;
|
||||
return player;
|
||||
} catch (Exception e) {
|
||||
|
@ -163,16 +168,15 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
* Only intended to use from ButtonCore
|
||||
*/
|
||||
public static void quitPlayer(Player p) {
|
||||
final TBMCPlayerBase player = playermap.get(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName());
|
||||
player.save();
|
||||
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player));
|
||||
Iterator<Entry<String, TBMCPlayerBase>> it = playermap.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Entry<String, TBMCPlayerBase> entry = it.next();
|
||||
if (entry.getKey().startsWith(p.getUniqueId().toString())) { // Save every player data
|
||||
TBMCPlayerBase player = entry.getValue(); // TODO: Separate plugin data by plugin name (annotations?)
|
||||
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player));
|
||||
}
|
||||
if (entry.getKey().startsWith(p.getUniqueId().toString()))
|
||||
it.remove();
|
||||
}
|
||||
final TBMCPlayerBase player = playermap.get(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName());
|
||||
player.save();
|
||||
}
|
||||
|
||||
public static void savePlayers() {
|
||||
|
|
Loading…
Reference in a new issue