Player data needs a default now on declaration

This commit is contained in:
Norbi Peti 2017-06-30 18:03:16 +02:00
parent 86a65899d9
commit 07a060e479
7 changed files with 53 additions and 42 deletions

View file

@ -36,8 +36,9 @@ public class MainPlugin extends JavaPlugin {
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);
TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class); TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class);
TBMCChatAPI.RegisterChatChannel(Channel.GlobalChat = new Channel("§fg§f", Color.White, "g", null)); TBMCChatAPI.RegisterChatChannel(Channel.GlobalChat = new Channel("§fg§f", Color.White, "g", null));
TBMCChatAPI.RegisterChatChannel( TBMCChatAPI.RegisterChatChannel(Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a",
Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a", s -> s.isOp() ? new RecipientTestResult(0) s -> s.isOp() //
? new RecipientTestResult(0)
: new RecipientTestResult("You need to be an admin to use this channel."))); : new RecipientTestResult("You need to be an admin to use this channel.")));
TBMCChatAPI.RegisterChatChannel(Channel.ModChat = new Channel("§9MOD§f", Color.Blue, "mod", TBMCChatAPI.RegisterChatChannel(Channel.ModChat = new Channel("§9MOD§f", Color.Blue, "mod",
s -> s.isOp() || (s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, "mod")) s -> s.isOp() || (s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, "mod"))

View file

@ -205,11 +205,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
* @return A data object with methods to get and set * @return A data object with methods to get and set
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T> PlayerData<T> data(String sectionname) { protected <T> PlayerData<T> data(String sectionname, T def) {
ThrowIfNoUser(); ThrowIfNoUser();
String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName(); String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName();
if (!datamap.containsKey(mname)) if (!datamap.containsKey(mname))
datamap.put(mname, new PlayerData<T>(mname, plugindata)); datamap.put(mname, new PlayerData<T>(mname, plugindata, def));
return datamap.get(mname); return datamap.get(mname);
} }
@ -219,11 +219,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
* @return A data object with methods to get and set * @return A data object with methods to get and set
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T> PlayerData<T> data() { protected <T> PlayerData<T> data(T def) {
ThrowIfNoUser(); ThrowIfNoUser();
String mname = new Exception().getStackTrace()[1].getMethodName(); String mname = new Exception().getStackTrace()[1].getMethodName();
if (!datamap.containsKey(mname)) if (!datamap.containsKey(mname))
datamap.put(mname, new PlayerData<T>(mname, plugindata)); datamap.put(mname, new PlayerData<T>(mname, plugindata, def));
return datamap.get(mname); return datamap.get(mname);
} }
@ -236,11 +236,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
* @return A data object with methods to get and set * @return A data object with methods to get and set
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(String sectionname, Class<T> cl) { protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(String sectionname, Class<T> cl, T def) {
ThrowIfNoUser(); ThrowIfNoUser();
String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName(); String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName();
if (!dataenummap.containsKey(mname)) if (!dataenummap.containsKey(mname))
dataenummap.put(mname, new EnumPlayerData<T>(mname, plugindata, cl)); dataenummap.put(mname, new EnumPlayerData<T>(mname, plugindata, cl, def));
return dataenummap.get(mname); return dataenummap.get(mname);
} }
@ -250,11 +250,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
* @return A data object with methods to get and set * @return A data object with methods to get and set
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl) { protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl, T def) {
ThrowIfNoUser(); ThrowIfNoUser();
String mname = new Exception().getStackTrace()[1].getMethodName(); String mname = new Exception().getStackTrace()[1].getMethodName();
if (!dataenummap.containsKey(mname)) if (!dataenummap.containsKey(mname))
dataenummap.put(mname, new EnumPlayerData<T>(mname, plugindata, cl)); dataenummap.put(mname, new EnumPlayerData<T>(mname, plugindata, cl, def));
return dataenummap.get(mname); return dataenummap.get(mname);
} }

View file

@ -5,25 +5,22 @@ import org.bukkit.configuration.file.YamlConfiguration;
public class EnumPlayerData<T extends Enum<T>> { public class EnumPlayerData<T extends Enum<T>> {
private PlayerData<String> data; private PlayerData<String> data;
private Class<T> cl; private Class<T> cl;
private T def;
public EnumPlayerData(String name, YamlConfiguration yaml, Class<T> cl) { public EnumPlayerData(String name, YamlConfiguration yaml, Class<T> cl, T def) {
data = new PlayerData<String>(name, yaml); data = new PlayerData<String>(name, yaml, "");
this.cl = cl; this.cl = cl;
this.def = def;
} }
public T get() { public T get() {
String str = data.get(); String str = data.get();
if (str == null || str.equals("")) if (str.isEmpty())
return null; return def;
return Enum.valueOf(cl, str); return Enum.valueOf(cl, str);
} }
public void set(T value) { public void set(T value) {
data.set(value.toString()); data.set(value.toString());
} }
public T getOrDefault(T def) {
T value = get();
return value == null ? def : value;
}
} }

View file

@ -5,15 +5,24 @@ import org.bukkit.configuration.file.YamlConfiguration;
public class PlayerData<T> { public class PlayerData<T> {
private String name; private String name;
private YamlConfiguration yaml; private YamlConfiguration yaml;
private T def;
public PlayerData(String name, YamlConfiguration yaml) { public PlayerData(String name, YamlConfiguration yaml, T def) {
this.name = name; this.name = name;
this.yaml = yaml; this.yaml = yaml;
this.def = def;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
// @Deprecated - What was once enforced (2 days ago from now) vanished now
public T get() { public T get() {
Object value = yaml.get(name); Object value = yaml.get(name, def);
if (value instanceof Integer) {
if (def instanceof Short) // If the default is Short the value must be as well because both are T
return (T) (Short) ((Integer) value).shortValue();
if (def instanceof Long)
return (T) (Long) ((Integer) value).longValue();
}
return (T) value; return (T) value;
} }
@ -21,11 +30,6 @@ public class PlayerData<T> {
yaml.set(name, value); yaml.set(name, value);
} }
public T getOrDefault(T def) {
T value = get();
return value == null ? def : value;
}
@Override @Override
public String toString() { public String toString() {
return get().toString(); return get().toString();

View file

@ -38,7 +38,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
} }
public PlayerData<String> PlayerName() { public PlayerData<String> PlayerName() {
return super.data(); return super.data(null);
} }
/** /**
@ -47,8 +47,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
* @return A data object with methods to get and set * @return A data object with methods to get and set
*/ */
@Override @Override
protected <T> PlayerData<T> data() { protected <T> PlayerData<T> data(T def) {
return super.data(pluginname); return super.data(pluginname, def);
} }
/** /**
@ -57,8 +57,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
* @return A data object with methods to get and set * @return A data object with methods to get and set
*/ */
@Override @Override
protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl) { protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl, T def) {
return super.dataEnum(pluginname, cl); return super.dataEnum(pluginname, cl, def);
} }
/** /**

View file

@ -1,7 +1,12 @@
package buttondevteam.core; package buttondevteam.core;
import java.io.File;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.io.FileUtils;
import buttondevteam.core.TestPlayerClass.TestEnum; import buttondevteam.core.TestPlayerClass.TestEnum;
import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.TBMCPlayerBase; import buttondevteam.lib.player.TBMCPlayerBase;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -19,28 +24,32 @@ public class PlayerDataTest extends TestCase {
return new TestSuite(PlayerDataTest.class); return new TestSuite(PlayerDataTest.class);
} }
public void testConfig() { public void testConfig() throws Exception {
TestPrepare.PrepareServer(); TestPrepare.PrepareServer();
FileUtils.deleteDirectory(new File(ChromaGamerBase.TBMC_PLAYERS_DIR));
UUID uuid = new UUID(0L, 0L); UUID uuid = new UUID(0L, 0L);
try (TestPlayerClass p = TBMCPlayerBase.getPlayer(uuid, TestPlayerClass.class)) { try (TestPlayerClass p = TBMCPlayerBase.getPlayer(uuid, TestPlayerClass.class)) {
p.PlayerName().set("Test"); p.PlayerName().set("Test");
assertEquals("Test", p.PlayerName().get()); assertEquals("Test", p.PlayerName().get());
p.testenum().set(TestEnum.A);
assertEquals(TestEnum.A, p.testenum().get()); assertEquals(TestEnum.A, p.testenum().get());
// p.TestShort().set((short) 5); assertEquals((short) 0, (short) p.TestShort().get());
// assertEquals((short) 5, (short) (int) p.TestShort().get()); assertEquals(false, (boolean) p.TestBool().get());
p.testenum().set(TestEnum.B);
assertEquals(TestEnum.B, p.testenum().get());
p.TestShort().set((short) 5);
assertEquals((short) 5, (short) p.TestShort().get());
p.TestBool().set(true); p.TestBool().set(true);
assertEquals(true, (boolean) p.TestBool().get()); assertEquals(true, (boolean) p.TestBool().get());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); throw e;
} }
try (TestPlayerClass p = TBMCPlayerBase.getPlayer(uuid, TestPlayerClass.class)) { try (TestPlayerClass p = TBMCPlayerBase.getPlayer(uuid, TestPlayerClass.class)) {
assertEquals("Test", p.PlayerName().get()); assertEquals("Test", p.PlayerName().get());
assertEquals(TestEnum.A, p.testenum().get()); assertEquals(TestEnum.B, p.testenum().get());
// assertEquals((short) 5, (short) p.TestShort().get()); assertEquals((short) 5, (short) p.TestShort().get());
assertEquals(true, (boolean) p.TestBool().get()); assertEquals(true, (boolean) p.TestBool().get());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); throw e;
} }
} }
} }

View file

@ -8,7 +8,7 @@ import buttondevteam.lib.player.TBMCPlayerBase;
@PlayerClass(pluginname = "TestPlugin") @PlayerClass(pluginname = "TestPlugin")
public class TestPlayerClass extends TBMCPlayerBase { public class TestPlayerClass extends TBMCPlayerBase {
public EnumPlayerData<TestEnum> testenum() { public EnumPlayerData<TestEnum> testenum() {
return dataEnum(TestEnum.class); return dataEnum(TestEnum.class, TestEnum.A);
} }
public enum TestEnum { public enum TestEnum {
@ -16,10 +16,10 @@ public class TestPlayerClass extends TBMCPlayerBase {
} }
public PlayerData<Short> TestShort() { public PlayerData<Short> TestShort() {
return data(); return data((short) 0);
} }
public PlayerData<Boolean> TestBool() { public PlayerData<Boolean> TestBool() {
return data(); return data(false);
} }
} }