Fixed even more stuff, and facepalmed

This commit is contained in:
Norbi Peti 2017-04-01 23:59:59 +02:00
parent f2931ed2fd
commit ced3bd10a4
3 changed files with 27 additions and 11 deletions

View file

@ -21,6 +21,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
public static void RegisterPluginUserClass(Class<? extends ChromaGamerBase> userclass) { public static void RegisterPluginUserClass(Class<? extends ChromaGamerBase> userclass) {
if (userclass.isAnnotationPresent(UserClass.class)) if (userclass.isAnnotationPresent(UserClass.class))
playerTypes.put(userclass, userclass.getAnnotation(UserClass.class).foldername()); playerTypes.put(userclass, userclass.getAnnotation(UserClass.class).foldername());
else //<-- Really important
throw new RuntimeException("Class not registered as a user class! Use @UserClass or TBMCPlayerBase"); 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) { public static <T extends ChromaGamerBase> T getUser(String fname, Class<T> cl) {
try { try {
T obj = cl.newInstance(); 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(); file.getParentFile().mkdirs();
obj.plugindata = YamlConfiguration.loadConfiguration(file); obj.plugindata = YamlConfiguration.loadConfiguration(file);
obj.plugindata.set(folder + "_id", obj.getID());
return obj; return obj;
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", 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 * The target player class
* @return The player as a {@link T} object or null if not having an account there * @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 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); String newfolder = getFolderForType(cl);
if (newfolder == null) if (newfolder == null)
throw new RuntimeException("The specified class " + cl.getSimpleName() + " isn't registered!"); throw new RuntimeException("The specified class " + cl.getSimpleName() + " isn't registered!");
System.out.println("getAs newfolder: " + newfolder);
if (!plugindata.contains(newfolder + "_id")) if (!plugindata.contains(newfolder + "_id"))
return null; return null;
return getUser(plugindata.getString(newfolder + "_id"), cl); return getUser(plugindata.getString(newfolder + "_id"), cl);
@ -190,6 +198,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T> PlayerData<T> data() { protected <T> PlayerData<T> data() {
//System.out.println("Calling ChromaGamerBase data"); // TODO: TMP - Debugged for hours
if (!getClass().isAnnotationPresent(UserClass.class)) if (!getClass().isAnnotationPresent(UserClass.class))
throw new RuntimeException("Class not registered as a user class! Use @UserClass"); throw new RuntimeException("Class not registered as a user class! Use @UserClass");
String mname = new Exception().getStackTrace()[1].getMethodName(); String mname = new Exception().getStackTrace()[1].getMethodName();

View file

@ -12,7 +12,10 @@ public class EnumPlayerData<T extends Enum<T>> {
} }
public T get() { 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) { public void set(T value) {

View file

@ -35,7 +35,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
} }
public PlayerData<String> PlayerName() { 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 @Override
@ -50,6 +51,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
*/ */
@Override @Override
protected <T> PlayerData<T> data() { protected <T> PlayerData<T> data() {
//System.out.println("Calling TMBCPlayerBase data"); // TODO: TMP - Sigh
return super.data(pluginname); 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) { public static <T extends TBMCPlayerBase> T getPlayer(UUID uuid, Class<T> cl) {
if (playermap.containsKey(uuid + "-" + cl.getSimpleName())) if (playermap.containsKey(uuid + "-" + cl.getSimpleName()))
return (T) playermap.get(uuid + "-" + cl.getSimpleName()); return (T) playermap.get(uuid + "-" + cl.getSimpleName());
//System.out.println("A");
try { try {
T player; T player;
if (playermap.containsKey(uuid + "-" + TBMCPlayer.class.getSimpleName())) { if (playermap.containsKey(uuid + "-" + TBMCPlayer.class.getSimpleName())) {
//System.out.println("B"); - Don't program when tired
player = cl.newInstance(); player = cl.newInstance();
player.plugindata = playermap.get(uuid + "-" + TBMCPlayer.class.getSimpleName()).plugindata; 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 } else
player = ChromaGamerBase.getUser(uuid.toString(), cl); player = ChromaGamerBase.getUser(uuid.toString(), cl);
//System.out.println("C");
player.uuid = uuid; player.uuid = uuid;
return player; return player;
} catch (Exception e) { } catch (Exception e) {
@ -163,17 +168,16 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
* Only intended to use from ButtonCore * Only intended to use from ButtonCore
*/ */
public static void quitPlayer(Player p) { 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(); Iterator<Entry<String, TBMCPlayerBase>> it = playermap.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Entry<String, TBMCPlayerBase> entry = it.next(); Entry<String, TBMCPlayerBase> entry = it.next();
if (entry.getKey().startsWith(p.getUniqueId().toString())) { // Save every player data if (entry.getKey().startsWith(p.getUniqueId().toString()))
TBMCPlayerBase player = entry.getValue(); // TODO: Separate plugin data by plugin name (annotations?) it.remove();
Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player));
} }
} }
final TBMCPlayerBase player = playermap.get(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName());
player.save();
}
public static void savePlayers() { public static void savePlayers() {
playermap.values().stream().forEach(p -> { playermap.values().stream().forEach(p -> {