From f2931ed2fdf4db52375df1a03204cf93fac074da Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 1 Apr 2017 19:57:43 +0200 Subject: [PATCH] Fixed a bunch of stuff --- .../buttondevteam/lib/player/ChromaGamerBase.java | 2 +- .../buttondevteam/lib/player/EnumPlayerData.java | 5 +++++ .../java/buttondevteam/lib/player/PlayerData.java | 13 ++++++++++++- .../buttondevteam/lib/player/TBMCPlayerBase.java | 2 ++ .../java/buttondevteam/core/PlayerDataTest.java | 6 ++++++ .../java/buttondevteam/core/TestPlayerClass.java | 9 +++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 1282ddb..91a4b70 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -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 getAs(Class cl) { + public T getAs(Class 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!"); diff --git a/src/main/java/buttondevteam/lib/player/EnumPlayerData.java b/src/main/java/buttondevteam/lib/player/EnumPlayerData.java index 586e845..b7b0e5c 100644 --- a/src/main/java/buttondevteam/lib/player/EnumPlayerData.java +++ b/src/main/java/buttondevteam/lib/player/EnumPlayerData.java @@ -18,4 +18,9 @@ public class EnumPlayerData> { public void set(T value) { data.set(value.toString()); } + + public T getOrDefault(T def) { + T value = get(); + return value == null ? def : value; + } } diff --git a/src/main/java/buttondevteam/lib/player/PlayerData.java b/src/main/java/buttondevteam/lib/player/PlayerData.java index fb5326b..05d68ca 100644 --- a/src/main/java/buttondevteam/lib/player/PlayerData.java +++ b/src/main/java/buttondevteam/lib/player/PlayerData.java @@ -13,10 +13,21 @@ public class PlayerData { @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(); + } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 3a174c5..33bcef2 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -48,6 +48,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { * * @return A data object with methods to get and set */ + @Override protected PlayerData 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 > EnumPlayerData dataEnum(Class cl) { return super.dataEnum(pluginname, cl); } diff --git a/src/test/java/buttondevteam/core/PlayerDataTest.java b/src/test/java/buttondevteam/core/PlayerDataTest.java index 4abde02..87fa5f7 100644 --- a/src/test/java/buttondevteam/core/PlayerDataTest.java +++ b/src/test/java/buttondevteam/core/PlayerDataTest.java @@ -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(); } diff --git a/src/test/java/buttondevteam/core/TestPlayerClass.java b/src/test/java/buttondevteam/core/TestPlayerClass.java index 42010db..7f51c4d 100644 --- a/src/test/java/buttondevteam/core/TestPlayerClass.java +++ b/src/test/java/buttondevteam/core/TestPlayerClass.java @@ -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 TestShort() { + return data(); + } + + public PlayerData TestBool() { + return data(); + } }