Green across the board!

Finished player data and added a test! (#28)
This commit is contained in:
Norbi Peti 2017-04-01 18:02:24 +02:00
parent 153425be5e
commit b9738837a1
9 changed files with 156 additions and 7 deletions

View file

@ -138,6 +138,11 @@
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.20</version>
</dependency>
</dependencies>
<organization>
<name>TBMCPlugins</name>

View file

@ -21,7 +21,7 @@ import buttondevteam.core.MainPlugin;
import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.potato.DebugPotato;
public final class TBMCCoreAPI {
public class TBMCCoreAPI {
static List<String> coders = new ArrayList<String>() {
private static final long serialVersionUID = -4462159250738367334L;
{

View file

@ -46,7 +46,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
public abstract String getFileName();
/**
* The 'id' must be always set
* Use {@link #data()} or {@link #data(String)} where possible; the 'id' must be always set
*/
protected YamlConfiguration plugindata;
@ -177,7 +177,7 @@ public abstract class ChromaGamerBase implements AutoCloseable {
protected <T> PlayerData<T> data(String sectionname) {
if (!getClass().isAnnotationPresent(UserClass.class))
throw new RuntimeException("Class not registered as a user class! Use @UserClass");
String mname = sectionname + "." + new Exception().getStackTrace()[1].getMethodName();
String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName();
if (!datamap.containsKey(mname))
datamap.put(mname, new PlayerData<T>(mname, plugindata));
return datamap.get(mname);
@ -192,12 +192,45 @@ public abstract class ChromaGamerBase implements AutoCloseable {
protected <T> PlayerData<T> data() {
if (!getClass().isAnnotationPresent(UserClass.class))
throw new RuntimeException("Class not registered as a user class! Use @UserClass");
String mname = new Exception().getStackTrace()[2].getMethodName();
String mname = new Exception().getStackTrace()[1].getMethodName();
if (!datamap.containsKey(mname))
datamap.put(mname, new PlayerData<T>(mname, plugindata));
return datamap.get(mname);
}
@SuppressWarnings("rawtypes")
private HashMap<String, EnumPlayerData> dataenummap = new HashMap<>();
/**
* Use from a data() method, which is in a method with the name of the key. For example, use flair() for the enclosing method of the outer data() to save to and load from "flair"
*
* @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) {
if (!getClass().isAnnotationPresent(UserClass.class))
throw new RuntimeException("Class not registered as a user class! Use @UserClass");
String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName();
if (!dataenummap.containsKey(mname))
dataenummap.put(mname, new EnumPlayerData<T>(mname, plugindata, cl));
return dataenummap.get(mname);
}
/**
* Use from a method with the name of the key. For example, use flair() for the enclosing method to save to and load from "flair"
*
* @return A data object with methods to get and set
*/
@SuppressWarnings("unchecked")
protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl) {
if (!getClass().isAnnotationPresent(UserClass.class))
throw new RuntimeException("Class not registered as a user class! Use @UserClass");
String mname = new Exception().getStackTrace()[1].getMethodName();
if (!dataenummap.containsKey(mname))
dataenummap.put(mname, new EnumPlayerData<T>(mname, plugindata, cl));
return dataenummap.get(mname);
}
/**
* Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins.
*

View file

@ -0,0 +1,21 @@
package buttondevteam.lib.player;
import org.bukkit.configuration.file.YamlConfiguration;
public class EnumPlayerData<T extends Enum<T>> {
private PlayerData<String> data;
private Class<T> cl;
public EnumPlayerData(String name, YamlConfiguration yaml, Class<T> cl) {
data = new PlayerData<String>(name, yaml);
this.cl = cl;
}
public T get() {
return Enum.valueOf(cl, data.get());
}
public void set(T value) {
data.set(value.toString());
}
}

View file

@ -16,7 +16,7 @@ public class PlayerData<T> {
return (T) yaml.get(name);
}
public void set(String value) {
public void set(T value) {
yaml.set(name, value);
}
}

View file

@ -26,7 +26,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
protected TBMCPlayerBase() {
if (getClass().isAnnotationPresent(PlayerClass.class))
pluginname = getClass().getAnnotation(PlayerClass.class).pluginname();
throw new RuntimeException("Class not defined as player class! Use @PlayerClass");
else
throw new RuntimeException("Class not defined as player class! Use @PlayerClass");
}
public UUID getUUID() {
@ -42,7 +43,6 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
return getUUID().toString();
}
/**
* Use from a method with the name of the key. For example, use flair() for the enclosing method to save to and load from "flair"
*
@ -52,6 +52,15 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
return super.data(pluginname);
}
/**
* Use from a method with the name of the key. For example, use flair() for the enclosing method to save to and load from "flair"
*
* @return A data object with methods to get and set
*/
protected <T extends Enum<T>> EnumPlayerData<T> dataEnum(Class<T> cl) {
return super.dataEnum(pluginname, cl);
}
/**
* Get player as a plugin player
*

View file

@ -1,6 +1,7 @@
package buttondevteam.lib.player;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@ -13,6 +14,7 @@ import java.lang.annotation.Target;
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface UserClass {
/**
* Indicates which folder should the player files be saved in.

View file

@ -0,0 +1,63 @@
package buttondevteam.core;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.plugin.PluginManager;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import buttondevteam.core.TestPlayerClass.TestEnum;
import buttondevteam.lib.player.TBMCPlayerBase;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class PlayerDataTest extends TestCase {
public PlayerDataTest() {
super("Player data test");
}
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite(PlayerDataTest.class);
}
public void testConfig() {
Bukkit.setServer(Mockito.mock(Server.class, new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
// System.out.println("Return type: " + invocation.getMethod().getReturnType());
// System.out.println(String.class.isAssignableFrom(invocation.getMethod().getReturnType()));
if (String.class.isAssignableFrom(invocation.getMethod().getReturnType()))
return "";
if (Logger.class.isAssignableFrom(invocation.getMethod().getReturnType()))
return Logger.getAnonymousLogger();
if (PluginManager.class.isAssignableFrom(invocation.getMethod().getReturnType()))
return Mockito.mock(PluginManager.class);
return null;
}
}));
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); // TODO: Fix enum saving
assertEquals(TestEnum.A, p.testenum().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());
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,16 @@
package buttondevteam.core;
import buttondevteam.lib.player.EnumPlayerData;
import buttondevteam.lib.player.PlayerClass;
import buttondevteam.lib.player.TBMCPlayerBase;
@PlayerClass(pluginname = "TestPlugin")
public class TestPlayerClass extends TBMCPlayerBase {
public EnumPlayerData<TestEnum> testenum() {
return dataEnum(TestEnum.class);
}
public enum TestEnum {
A, B
}
}