Plugin updater refactored, player data needs a default on declaration now, color codes added #35

Merged
NorbiPeti merged 5 commits from dev into master 2017-07-03 21:58:11 +00:00
7 changed files with 53 additions and 42 deletions
Showing only changes of commit 07a060e479 - Show all commits

View file

@ -36,8 +36,9 @@ public class MainPlugin extends JavaPlugin {
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);
TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class);
TBMCChatAPI.RegisterChatChannel(Channel.GlobalChat = new Channel("§fg§f", Color.White, "g", null));
TBMCChatAPI.RegisterChatChannel(
Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a", s -> s.isOp() ? new RecipientTestResult(0)
TBMCChatAPI.RegisterChatChannel(Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a",
s -> s.isOp() //
? new RecipientTestResult(0)
: new RecipientTestResult("You need to be an admin to use this channel.")));
TBMCChatAPI.RegisterChatChannel(Channel.ModChat = new Channel("§9MOD§f", Color.Blue, "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
*/
@SuppressWarnings("unchecked")
protected <T> PlayerData<T> data(String sectionname) {
protected <T> PlayerData<T> data(String sectionname, T def) {
ThrowIfNoUser();
String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName();
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);
}
@ -219,11 +219,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
* @return A data object with methods to get and set
*/
@SuppressWarnings("unchecked")
protected <T> PlayerData<T> data() {
protected <T> PlayerData<T> data(T def) {
ThrowIfNoUser();
String mname = new Exception().getStackTrace()[1].getMethodName();
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);
}
@ -236,11 +236,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
* @return A data object with methods to get and set
*/
@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();
String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName();
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);
}
@ -250,11 +250,11 @@ public abstract class ChromaGamerBase implements AutoCloseable {
* @return A data object with methods to get and set
*/
@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();
String mname = new Exception().getStackTrace()[1].getMethodName();
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);
}

View file

@ -5,25 +5,22 @@ import org.bukkit.configuration.file.YamlConfiguration;
public class EnumPlayerData<T extends Enum<T>> {
private PlayerData<String> data;
private Class<T> cl;
private T def;
public EnumPlayerData(String name, YamlConfiguration yaml, Class<T> cl) {
data = new PlayerData<String>(name, yaml);
public EnumPlayerData(String name, YamlConfiguration yaml, Class<T> cl, T def) {
data = new PlayerData<String>(name, yaml, "");
this.cl = cl;
this.def = def;
}
public T get() {
String str = data.get();
if (str == null || str.equals(""))
return null;
if (str.isEmpty())
return def;
return Enum.valueOf(cl, str);
}
public void set(T value) {
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> {
private String name;
private YamlConfiguration yaml;
private T def;
public PlayerData(String name, YamlConfiguration yaml) {
public PlayerData(String name, YamlConfiguration yaml, T def) {
this.name = name;
this.yaml = yaml;
this.def = def;
}
@SuppressWarnings("unchecked")
// @Deprecated - What was once enforced (2 days ago from now) vanished now
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;
}
@ -21,11 +30,6 @@ public class PlayerData<T> {
yaml.set(name, value);
}
public T getOrDefault(T def) {
T value = get();
return value == null ? def : value;
}
@Override
public String toString() {
return get().toString();

View file

@ -38,7 +38,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
}
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
*/
@Override
protected <T> PlayerData<T> data() {
return super.data(pluginname);
protected <T> PlayerData<T> data(T def) {
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
*/
@Override
protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl) {
return super.dataEnum(pluginname, cl);
protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl, T def) {
return super.dataEnum(pluginname, cl, def);
}
/**

View file

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

View file

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