From be8685725cb56b1415b07d0d5194a1f1a372a5c4 Mon Sep 17 00:00:00 2001
From: NorbiPeti
+ * Gets a player data entry for the caller plugin returning the desired type, which is a number
* {@code
- * public String getPlayerName() {
- * return getData();
+ * public String getSomeEnum() {
+ * return getEnumData();
* }
*
*
@@ -97,9 +100,12 @@ public class TBMCPlayer implements AutoCloseable {
StackTraceElement st = new Exception().getStackTrace()[1];
String mname = st.getMethodName();
if (!mname.startsWith("get"))
- throw new UnsupportedOperationException("Can only use getData from a getXYZ method");
- return Enum.valueOf(cl,
- (String) LoadedPlayers.get(uuid).data.get(mname.substring("get".length()).toLowerCase()));
+ throw new UnsupportedOperationException("Can only use getEnumData from a getXYZ method");
+ final String retstr = (String) LoadedPlayers.get(uuid).data.get(mname.substring("get".length()).toLowerCase());
+ if (retstr != null)
+ return Enum.valueOf(cl, retstr);
+ else
+ return null;
}
/**
@@ -109,8 +115,8 @@ public class TBMCPlayer implements AutoCloseable {
*
*
* {@code
- * public String setPlayerName(String value) {
- * return setData(value);
+ * public String setSomeEnum(SomeEnum value) {
+ * return setEnumData(value);
* }
*
*
@@ -121,10 +127,65 @@ public class TBMCPlayer implements AutoCloseable {
StackTraceElement st = new Exception().getStackTrace()[0];
String mname = st.getMethodName();
if (!mname.startsWith("set"))
- throw new UnsupportedOperationException("Can only use setData from a setXYZ method");
+ throw new UnsupportedOperationException("Can only use setEnumData from a setXYZ method");
LoadedPlayers.get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value.toString());
}
+ /**
+ *
+ * It will automatically determine the key and the return type.
+ * Usage:
+ *
+ * {@code
+ * public short getNumber() {
+ * return getIntData();
+ * }
+ *
+ *
+ * @return The value or null if not found
+ */
+ @SuppressWarnings("unchecked")
+ protected
+ * Usage:
+ *
+ * {@code + * public String setNumber(short value) { + * return setIntData(value); + * } + *+ * + * @param value + * The value to set + */ + protected void setIntData(Number value) { + StackTraceElement st = new Exception().getStackTrace()[0]; + String mname = st.getMethodName(); + if (!mname.startsWith("set")) + throw new UnsupportedOperationException("Can only use setIntData from a setXYZ method"); + LoadedPlayers.get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value); + } + /** * Gets the player's Minecraft name * @@ -167,6 +228,7 @@ public class TBMCPlayer implements AutoCloseable { T obj = null; try { obj = cl.newInstance(); + ((TBMCPlayer) obj).uuid = uuid; ((TBMCPlayer) obj).data.putAll(data); } catch (Exception e) { e.printStackTrace();