From be8685725cb56b1415b07d0d5194a1f1a372a5c4 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 30 Oct 2016 22:57:59 +0100 Subject: [PATCH] Fixes IT WORKS! --- .../java/buttondevteam/lib/TBMCPlayer.java | 80 ++++++++++++++++--- 1 file changed, 71 insertions(+), 9 deletions(-) diff --git a/src/main/java/buttondevteam/lib/TBMCPlayer.java b/src/main/java/buttondevteam/lib/TBMCPlayer.java index 66d568e..26d78da 100644 --- a/src/main/java/buttondevteam/lib/TBMCPlayer.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayer.java @@ -51,7 +51,10 @@ public class TBMCPlayer implements AutoCloseable { String mname = st.getMethodName(); if (!mname.startsWith("get")) throw new UnsupportedOperationException("Can only use getData from a getXYZ method"); - return (T) LoadedPlayers.get(uuid).data.get(mname.substring("get".length()).toLowerCase()); + Object ret = LoadedPlayers.get(uuid).data.get(mname.substring("get".length()).toLowerCase()); + if (Integer.class.isAssignableFrom(ret.getClass())) + throw new UnsupportedOperationException("For integers use getIntData()"); + return (T) ret; } /** @@ -86,8 +89,8 @@ public class TBMCPlayer implements AutoCloseable { * *
 	 * {@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()); } + /** + *

+ * Gets a player data entry for the caller plugin returning the desired type, which is a number
+ * 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 T getIntData(Class cl) { + StackTraceElement st = new Exception().getStackTrace()[1]; + String mname = st.getMethodName(); + if (!mname.startsWith("get")) + throw new UnsupportedOperationException("Can only use getIntData from a getXYZ method"); + Object obj = LoadedPlayers.get(uuid).data.get(mname.substring("get".length()).toLowerCase()); + if (!(obj instanceof Integer)) + throw new UnsupportedOperationException("The retrieved object isn't a number: " + obj); + Integer int_ = (Integer) obj; + if (Short.class.isAssignableFrom(cl)) + return (T) (Object) int_.shortValue(); + else + return (T) (Object) int_; + } + + /** + * Sets a player data entry based on the caller method
+ * 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();