From 44109db61f12c3b9f07b165f5e36d1374fedfe7a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 30 Oct 2016 21:44:56 +0100 Subject: [PATCH] Added Enum data handling --- .../java/buttondevteam/lib/TBMCPlayer.java | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/buttondevteam/lib/TBMCPlayer.java b/src/main/java/buttondevteam/lib/TBMCPlayer.java index f2c2c06..66d568e 100644 --- a/src/main/java/buttondevteam/lib/TBMCPlayer.java +++ b/src/main/java/buttondevteam/lib/TBMCPlayer.java @@ -47,7 +47,7 @@ public class TBMCPlayer implements AutoCloseable { */ @SuppressWarnings("unchecked") protected T getData() { - StackTraceElement st = new Exception().getStackTrace()[0]; + 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"); @@ -77,6 +77,54 @@ public class TBMCPlayer implements AutoCloseable { LoadedPlayers.get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value); } + /** + *

+ * Gets a player data entry for the caller plugin returning the desired type, which is an enum
+ * It will automatically determine the key and the return type.
+ * Usage: + *

+ * + *
+	 * {@code
+	 * public String getPlayerName() {
+	 * 	return getData();
+	 * }
+	 * 
+ * + * @return The value or null if not found + */ + protected > T getEnumData(Class cl) { + 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())); + } + + /** + * Sets a player data entry based on the caller method
+ * Usage: + *

+ * + *
+	 * {@code
+	 * public String setPlayerName(String value) {
+	 * 	return setData(value);
+	 * }
+	 * 
+ * + * @param value + * The value to set + */ + protected void setEnumData(Enum value) { + 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"); + LoadedPlayers.get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value.toString()); + } + /** * Gets the player's Minecraft name *