Remade user data managing, added a test #30
6 changed files with 35 additions and 2 deletions
|
@ -152,7 +152,7 @@ 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
|
||||
*/
|
||||
public <T extends ChromaGamerBase> T getAs(Class<T> cl) {
|
||||
public <T extends ChromaGamerBase> T getAs(Class<T> cl) { // TODO: Provide a way to use TBMCPlayerBase's loaded players
|
||||
String newfolder = getFolderForType(cl);
|
||||
if (newfolder == null)
|
||||
throw new RuntimeException("The specified class " + cl.getSimpleName() + " isn't registered!");
|
||||
|
|
|
@ -18,4 +18,9 @@ public class EnumPlayerData<T extends Enum<T>> {
|
|||
public void set(T value) {
|
||||
data.set(value.toString());
|
||||
}
|
||||
|
||||
public T getOrDefault(T def) {
|
||||
T value = get();
|
||||
return value == null ? def : value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,10 +13,21 @@ public class PlayerData<T> {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T get() {
|
||||
return (T) yaml.get(name);
|
||||
Object value = yaml.get(name);
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
public void set(T value) {
|
||||
yaml.set(name, value);
|
||||
}
|
||||
|
||||
public T getOrDefault(T def) {
|
||||
T value = get();
|
||||
return value == null ? def : value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return get().toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
*
|
||||
* @return A data object with methods to get and set
|
||||
*/
|
||||
@Override
|
||||
protected <T> PlayerData<T> data() {
|
||||
return super.data(pluginname);
|
||||
}
|
||||
|
@ -57,6 +58,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
|
|||
*
|
||||
* @return A data object with methods to get and set
|
||||
*/
|
||||
@Override
|
||||
protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl) {
|
||||
return super.dataEnum(pluginname, cl);
|
||||
}
|
||||
|
|
|
@ -50,12 +50,18 @@ public class PlayerDataTest extends TestCase {
|
|||
assertEquals("Test", p.PlayerName().get());
|
||||
p.testenum().set(TestEnum.A); // TODO: Fix enum saving
|
||||
assertEquals(TestEnum.A, p.testenum().get());
|
||||
// p.TestShort().set((short) 5);
|
||||
// assertEquals((short) 5, (short) (int) p.TestShort().get());
|
||||
p.TestBool().set(true);
|
||||
assertEquals(true, (boolean) p.TestBool().get());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try (TestPlayerClass p = TBMCPlayerBase.getPlayer(uuid, TestPlayerClass.class)) {
|
||||
assertEquals("Test", p.PlayerName().get());
|
||||
assertEquals(TestEnum.A, p.testenum().get());
|
||||
// assertEquals((short) 5, (short) p.TestShort().get());
|
||||
assertEquals(true, (boolean) p.TestBool().get());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package buttondevteam.core;
|
|||
|
||||
import buttondevteam.lib.player.EnumPlayerData;
|
||||
import buttondevteam.lib.player.PlayerClass;
|
||||
import buttondevteam.lib.player.PlayerData;
|
||||
import buttondevteam.lib.player.TBMCPlayerBase;
|
||||
|
||||
@PlayerClass(pluginname = "TestPlugin")
|
||||
|
@ -13,4 +14,12 @@ public class TestPlayerClass extends TBMCPlayerBase {
|
|||
public enum TestEnum {
|
||||
A, B
|
||||
}
|
||||
|
||||
public PlayerData<Short> TestShort() {
|
||||
return data();
|
||||
}
|
||||
|
||||
public PlayerData<Boolean> TestBool() {
|
||||
return data();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue