From 9ea151596abcf5c17cc5e7c0c5d6504f5ef6ce7a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 2 Jan 2017 00:47:47 +0100 Subject: [PATCH 01/22] Renamed TBMCPlayerBase to ChromaGamerBase --- .../lib/{TBMCPlayerBase.java => ChromaGamerBase.java} | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) rename src/main/java/buttondevteam/lib/{TBMCPlayerBase.java => ChromaGamerBase.java} (90%) diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/ChromaGamerBase.java similarity index 90% rename from src/main/java/buttondevteam/lib/TBMCPlayerBase.java rename to src/main/java/buttondevteam/lib/ChromaGamerBase.java index cac3068..c186686 100644 --- a/src/main/java/buttondevteam/lib/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/ChromaGamerBase.java @@ -1,6 +1,6 @@ package buttondevteam.lib; -public abstract class TBMCPlayerBase { +public abstract class ChromaGamerBase { /** * This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc. */ @@ -10,6 +10,4 @@ public abstract class TBMCPlayerBase { * This method returns the folder the file is in. For example, for Minecraft data, this should be "minecraft", for Discord, "discord", etc. */ public abstract String getFolder(); - - } -- 2.30.2 From a187170fddb64a662cdd7ccc780058042a501ca6 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 2 Jan 2017 01:09:11 +0100 Subject: [PATCH 02/22] Slowly progressing with player data --- .../buttondevteam/lib/ChromaGamerBase.java | 4 ++++ .../buttondevteam/lib/TBMCPlayerBase.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/main/java/buttondevteam/lib/TBMCPlayerBase.java diff --git a/src/main/java/buttondevteam/lib/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/ChromaGamerBase.java index c186686..19abff1 100644 --- a/src/main/java/buttondevteam/lib/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/ChromaGamerBase.java @@ -1,5 +1,7 @@ package buttondevteam.lib; +import org.bukkit.configuration.ConfigurationSection; + public abstract class ChromaGamerBase { /** * This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc. @@ -10,4 +12,6 @@ public abstract class ChromaGamerBase { * This method returns the folder the file is in. For example, for Minecraft data, this should be "minecraft", for Discord, "discord", etc. */ public abstract String getFolder(); + + protected ConfigurationSection plugindata; } diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/TBMCPlayerBase.java new file mode 100644 index 0000000..d3212fa --- /dev/null +++ b/src/main/java/buttondevteam/lib/TBMCPlayerBase.java @@ -0,0 +1,19 @@ +package buttondevteam.lib; + +import java.util.UUID; + +public abstract class TBMCPlayerBase extends ChromaGamerBase { + public abstract UUID getUUID(); + + public abstract String getPluginName(); + + @Override + public String getFileName() { + return getUUID().toString(); + } + + @Override + public String getFolder() { + return "minecraft"; + } +} -- 2.30.2 From 37ef10da2af1220abc5c9dc4fc765e17442e1944 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 2 Jan 2017 01:19:09 +0100 Subject: [PATCH 03/22] Organized stuff --- src/main/java/buttondevteam/lib/{ => player}/ChromaGamerBase.java | 0 src/main/java/buttondevteam/lib/{ => player}/TBMCPlayer.java | 0 .../java/buttondevteam/lib/{ => player}/TBMCPlayerAddEvent.java | 0 src/main/java/buttondevteam/lib/{ => player}/TBMCPlayerBase.java | 0 .../buttondevteam/lib/{ => player}/TBMCPlayerGetInfoEvent.java | 0 .../java/buttondevteam/lib/{ => player}/TBMCPlayerJoinEvent.java | 0 .../java/buttondevteam/lib/{ => player}/TBMCPlayerLoadEvent.java | 0 .../java/buttondevteam/lib/{ => player}/TBMCPlayerQuitEvent.java | 0 .../java/buttondevteam/lib/{ => player}/TBMCPlayerSaveEvent.java | 0 src/main/java/buttondevteam/lib/{ => player}/TBMCYEEHAWEvent.java | 0 src/main/java/buttondevteam/lib/{ => potato}/DebugPotato.java | 0 src/main/java/buttondevteam/lib/{ => potato}/Gary.java | 0 12 files changed, 0 insertions(+), 0 deletions(-) rename src/main/java/buttondevteam/lib/{ => player}/ChromaGamerBase.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCPlayer.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCPlayerAddEvent.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCPlayerBase.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCPlayerGetInfoEvent.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCPlayerJoinEvent.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCPlayerLoadEvent.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCPlayerQuitEvent.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCPlayerSaveEvent.java (100%) rename src/main/java/buttondevteam/lib/{ => player}/TBMCYEEHAWEvent.java (100%) rename src/main/java/buttondevteam/lib/{ => potato}/DebugPotato.java (100%) rename src/main/java/buttondevteam/lib/{ => potato}/Gary.java (100%) diff --git a/src/main/java/buttondevteam/lib/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java similarity index 100% rename from src/main/java/buttondevteam/lib/ChromaGamerBase.java rename to src/main/java/buttondevteam/lib/player/ChromaGamerBase.java diff --git a/src/main/java/buttondevteam/lib/TBMCPlayer.java b/src/main/java/buttondevteam/lib/player/TBMCPlayer.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCPlayer.java rename to src/main/java/buttondevteam/lib/player/TBMCPlayer.java diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCPlayerAddEvent.java rename to src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCPlayerBase.java rename to src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCPlayerGetInfoEvent.java rename to src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCPlayerJoinEvent.java rename to src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCPlayerLoadEvent.java rename to src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCPlayerQuitEvent.java rename to src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java diff --git a/src/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCPlayerSaveEvent.java rename to src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java diff --git a/src/main/java/buttondevteam/lib/TBMCYEEHAWEvent.java b/src/main/java/buttondevteam/lib/player/TBMCYEEHAWEvent.java similarity index 100% rename from src/main/java/buttondevteam/lib/TBMCYEEHAWEvent.java rename to src/main/java/buttondevteam/lib/player/TBMCYEEHAWEvent.java diff --git a/src/main/java/buttondevteam/lib/DebugPotato.java b/src/main/java/buttondevteam/lib/potato/DebugPotato.java similarity index 100% rename from src/main/java/buttondevteam/lib/DebugPotato.java rename to src/main/java/buttondevteam/lib/potato/DebugPotato.java diff --git a/src/main/java/buttondevteam/lib/Gary.java b/src/main/java/buttondevteam/lib/potato/Gary.java similarity index 100% rename from src/main/java/buttondevteam/lib/Gary.java rename to src/main/java/buttondevteam/lib/potato/Gary.java -- 2.30.2 From 201ae7c8f647ad16b6ad1b9177429a6aa547b57c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 2 Jan 2017 01:19:19 +0100 Subject: [PATCH 04/22] Organized stuff --- src/main/java/buttondevteam/core/MainPlugin.java | 2 +- src/main/java/buttondevteam/core/PlayerListener.java | 2 +- src/main/java/buttondevteam/lib/TBMCCoreAPI.java | 1 + src/main/java/buttondevteam/lib/player/ChromaGamerBase.java | 2 +- src/main/java/buttondevteam/lib/player/TBMCPlayer.java | 2 +- .../java/buttondevteam/lib/player/TBMCPlayerAddEvent.java | 2 +- src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java | 2 +- .../java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java | 4 ++-- .../java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java | 2 +- .../java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java | 2 +- .../java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java | 2 +- .../java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java | 2 +- src/main/java/buttondevteam/lib/player/TBMCYEEHAWEvent.java | 2 +- src/main/java/buttondevteam/lib/potato/DebugPotato.java | 2 +- src/main/java/buttondevteam/lib/potato/Gary.java | 2 +- 15 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index e3f5f06..c084692 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -9,8 +9,8 @@ import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.lib.TBMCPlayer; import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.player.TBMCPlayer; import net.milkbowl.vault.permission.Permission; public class MainPlugin extends JavaPlugin { diff --git a/src/main/java/buttondevteam/core/PlayerListener.java b/src/main/java/buttondevteam/core/PlayerListener.java index 4f7fc95..9261b21 100644 --- a/src/main/java/buttondevteam/core/PlayerListener.java +++ b/src/main/java/buttondevteam/core/PlayerListener.java @@ -6,7 +6,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import buttondevteam.lib.TBMCPlayer; +import buttondevteam.lib.player.TBMCPlayer; public class PlayerListener implements Listener { diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index 9e2f639..dcb7d60 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -18,6 +18,7 @@ import org.bukkit.plugin.Plugin; import com.google.gson.*; import buttondevteam.core.MainPlugin; +import buttondevteam.lib.potato.DebugPotato; public final class TBMCCoreAPI { static List coders = new ArrayList() { diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 19abff1..c2b78c0 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import org.bukkit.configuration.ConfigurationSection; diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayer.java b/src/main/java/buttondevteam/lib/player/TBMCPlayer.java index da1e582..954027b 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayer.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayer.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import java.io.File; import java.io.IOException; diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java index 46ccb4f..6613ff3 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index d3212fa..f6c0fc7 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import java.util.UUID; diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java index ded8f2e..b8e5b5e 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import java.util.ArrayList; import java.util.List; @@ -7,7 +7,7 @@ import java.util.stream.Collectors; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -import buttondevteam.lib.TBMCPlayer.InfoTarget; +import buttondevteam.lib.player.TBMCPlayer.InfoTarget; /** *

diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java index 499f8b1..0422530 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java index 71d35a6..67c178d 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.Event; diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java index 5883365..c77b272 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java index f538641..5c5f873 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.Event; diff --git a/src/main/java/buttondevteam/lib/player/TBMCYEEHAWEvent.java b/src/main/java/buttondevteam/lib/player/TBMCYEEHAWEvent.java index edf3f56..a54a1b7 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCYEEHAWEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCYEEHAWEvent.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.player; import org.bukkit.command.CommandSender; import org.bukkit.event.Event; diff --git a/src/main/java/buttondevteam/lib/potato/DebugPotato.java b/src/main/java/buttondevteam/lib/potato/DebugPotato.java index 69a1b55..4c5269f 100644 --- a/src/main/java/buttondevteam/lib/potato/DebugPotato.java +++ b/src/main/java/buttondevteam/lib/potato/DebugPotato.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.potato; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/buttondevteam/lib/potato/Gary.java b/src/main/java/buttondevteam/lib/potato/Gary.java index 1e109cd..c2cebee 100644 --- a/src/main/java/buttondevteam/lib/potato/Gary.java +++ b/src/main/java/buttondevteam/lib/potato/Gary.java @@ -1,4 +1,4 @@ -package buttondevteam.lib; +package buttondevteam.lib.potato; import java.util.List; -- 2.30.2 From 27a92b164e2049f22334558a1a3a8426c7d1d08d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 16 Jan 2017 19:49:43 +0100 Subject: [PATCH 05/22] Making progress on player data stuff --- .../lib/player/ChromaGamerBase.java | 53 +++++++++++++++++-- .../lib/player/TBMCPlayerBase.java | 26 ++++++++- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index c2b78c0..dcb2865 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -1,8 +1,27 @@ package buttondevteam.lib.player; -import org.bukkit.configuration.ConfigurationSection; +import java.io.File; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.configuration.file.YamlConfiguration; + +import buttondevteam.lib.TBMCCoreAPI; + +public abstract class ChromaGamerBase implements AutoCloseable { + private static final String TBMC_PLAYERS_DIR = "TBMC/players/"; + + private static final HashMap, String> playerTypes = new HashMap<>(); + + public static Map, String> getPlayerTypes() { + return Collections.unmodifiableMap(playerTypes); + } + + public static void addPlayerType(Class cl, String folder) { + playerTypes.put(cl, folder); + } -public abstract class ChromaGamerBase { /** * This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc. */ @@ -13,5 +32,33 @@ public abstract class ChromaGamerBase { */ public abstract String getFolder(); - protected ConfigurationSection plugindata; + protected YamlConfiguration plugindata; + + public YamlConfiguration getData() { + return plugindata; + } + + // protected void load() { + /* + * public static void load() { try { plugindata = YamlConfiguration.loadConfiguration(new File(getFolder(), getFileName())); } catch (Exception e) { + * TBMCCoreAPI.SendException("An error occured while loading gamer data", e); } } protected void save() { try { plugindata.save(new File(getFolder(), getFileName())); } catch (Exception e) { + * TBMCCoreAPI.SendException("An error occured while saving gamer data", e); } } + */ + + protected static T getUser(String fname, Class cl) { + try { + T obj = cl.newInstance(); + obj.plugindata = YamlConfiguration // TODO: Put all IDs + .loadConfiguration(new File(TBMC_PLAYERS_DIR + playerTypes.get(cl), fname)); + return obj; + } catch (Exception e) { + TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e); + } + return null; + } + + @Override + public void close() throws Exception { + plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName())); + } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index f6c0fc7..ae7620a 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -3,7 +3,12 @@ package buttondevteam.lib.player; import java.util.UUID; public abstract class TBMCPlayerBase extends ChromaGamerBase { - public abstract UUID getUUID(); + private static final String FOLDER_NAME = "minecraft"; + protected UUID uuid; + + public UUID getUUID() { + return uuid; + } public abstract String getPluginName(); @@ -14,6 +19,23 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { @Override public String getFolder() { - return "minecraft"; + return FOLDER_NAME; + } + + // protected ConfigurationSection plugindata; + + /* + * public static void load() { super.load(); plugindata = super.plugindata.getConfigurationSection(getPluginName()); if (plugindata == null) plugindata = + * super.plugindata.createSection(getPluginName()); } protected void save() { plugindata = super.plugindata.createSection(getPluginName(), plugindata.getValues(true)); super.save(); } + */ + + static { + addPlayerType(TBMCPlayerBase.class, FOLDER_NAME); + } + + public static T getPlayer(UUID uuid, Class cl) { + T obj = ChromaGamerBase.getUser(uuid.toString(), cl); + obj.uuid = uuid; + return obj; } } -- 2.30.2 From 4aa7cccb5c18b618b6499c2270c7320f984c20ce Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 20 Jan 2017 23:35:05 +0100 Subject: [PATCH 06/22] Made progress on user data --- .../lib/player/ChromaGamerBase.java | 40 ++++++++++++------- .../lib/player/TBMCPlayerBase.java | 5 --- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index dcb2865..71147d8 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -4,6 +4,9 @@ import java.io.File; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; +import java.util.function.BiConsumer; +import java.util.function.Consumer; import org.bukkit.configuration.file.YamlConfiguration; @@ -14,41 +17,36 @@ public abstract class ChromaGamerBase implements AutoCloseable { private static final HashMap, String> playerTypes = new HashMap<>(); - public static Map, String> getPlayerTypes() { - return Collections.unmodifiableMap(playerTypes); - } - public static void addPlayerType(Class cl, String folder) { playerTypes.put(cl, folder); } + public static String getFolderForType(Class cl) { + return playerTypes.get(cl); + } + /** * This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc. */ public abstract String getFileName(); /** - * This method returns the folder the file is in. For example, for Minecraft data, this should be "minecraft", for Discord, "discord", etc. + * The 'id' must be always set */ - public abstract String getFolder(); - protected YamlConfiguration plugindata; public YamlConfiguration getData() { return plugindata; } - // protected void load() { - /* - * public static void load() { try { plugindata = YamlConfiguration.loadConfiguration(new File(getFolder(), getFileName())); } catch (Exception e) { - * TBMCCoreAPI.SendException("An error occured while loading gamer data", e); } } protected void save() { try { plugindata.save(new File(getFolder(), getFileName())); } catch (Exception e) { - * TBMCCoreAPI.SendException("An error occured while saving gamer data", e); } } - */ + public String getID() { + return plugindata != null ? plugindata.getString("id") : null; + } protected static T getUser(String fname, Class cl) { try { T obj = cl.newInstance(); - obj.plugindata = YamlConfiguration // TODO: Put all IDs + obj.plugindata = YamlConfiguration .loadConfiguration(new File(TBMC_PLAYERS_DIR + playerTypes.get(cl), fname)); return obj; } catch (Exception e) { @@ -59,6 +57,18 @@ public abstract class ChromaGamerBase implements AutoCloseable { @Override public void close() throws Exception { - plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName())); + plugindata.save(new File(TBMC_PLAYERS_DIR + getFolderForType(getClass()), getFileName())); + } + + public void connectWith(T user) { + // Set the ID, go through all linked files and connect them as well + plugindata.set(playerTypes.get(user.getClass()) + "_id", user.plugindata.getString("id")); + final String ownFolder = getFolderForType(getClass()); + user.plugindata.set(ownFolder + "_id", plugindata.getString("id")); + BiConsumer sync = (pdata1, pdata2) -> { + for (Entry, String> entry : playerTypes.entrySet()) + if (pdata1.contains(entry.getValue() + "_id", false)) + pdata2.set(entry.getValue() + "_id", pdata1.getString(entry.getValue() + "_id")); + }; // ... } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index ae7620a..3b0ce71 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -17,11 +17,6 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { return getUUID().toString(); } - @Override - public String getFolder() { - return FOLDER_NAME; - } - // protected ConfigurationSection plugindata; /* -- 2.30.2 From 28fcb2f8d89e2ef3d7201a53a401f72dc66f1a3f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 21 Jan 2017 19:45:54 +0100 Subject: [PATCH 07/22] Making progress on player data --- .../lib/player/ChromaGamerBase.java | 80 +++++++++++++++---- .../lib/player/TBMCPlayerBase.java | 21 +++++ 2 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 71147d8..c50be0a 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -1,11 +1,8 @@ package buttondevteam.lib.player; import java.io.File; -import java.util.Collections; import java.util.HashMap; -import java.util.Map; import java.util.Map.Entry; -import java.util.function.BiConsumer; import java.util.function.Consumer; import org.bukkit.configuration.file.YamlConfiguration; @@ -17,12 +14,30 @@ public abstract class ChromaGamerBase implements AutoCloseable { private static final HashMap, String> playerTypes = new HashMap<>(); + /** + * Use only if outside Minecraft, and use it to register your plugin's class. + * + * @param cl + * The custom player class + * @param folder + * The folder to store the data in (like "discord") + */ public static void addPlayerType(Class cl, String folder) { playerTypes.put(cl, folder); } + /** + * Returns the folder name for the given player class. If a direct match is not found, it'll look for superclasses. + * + * @param cl + * The class to get the folder from (like {@link TBMCPlayerBase} or one of it's subclasses + * @return The folder name for the given type + */ public static String getFolderForType(Class cl) { - return playerTypes.get(cl); + if (playerTypes.containsKey(cl)) + return playerTypes.get(cl); + return playerTypes.entrySet().stream().filter(e -> e.getKey().isAssignableFrom(cl)).findAny() + .orElseThrow(() -> new RuntimeException("Type not registered as a player type!")).getValue(); } /** @@ -35,10 +50,6 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ protected YamlConfiguration plugindata; - public YamlConfiguration getData() { - return plugindata; - } - public String getID() { return plugindata != null ? plugindata.getString("id") : null; } @@ -47,7 +58,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { try { T obj = cl.newInstance(); obj.plugindata = YamlConfiguration - .loadConfiguration(new File(TBMC_PLAYERS_DIR + playerTypes.get(cl), fname)); + .loadConfiguration(new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname)); return obj; } catch (Exception e) { TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e); @@ -60,15 +71,54 @@ public abstract class ChromaGamerBase implements AutoCloseable { plugindata.save(new File(TBMC_PLAYERS_DIR + getFolderForType(getClass()), getFileName())); } + /** + * Connect two accounts. Do not use for connecting two Minecraft accounts or similar. Also make sure you have the "id" tag set + * + * @param user + * The account to connect with + */ public void connectWith(T user) { // Set the ID, go through all linked files and connect them as well - plugindata.set(playerTypes.get(user.getClass()) + "_id", user.plugindata.getString("id")); + plugindata.set(getFolderForType(user.getClass()) + "_id", user.plugindata.getString("id")); final String ownFolder = getFolderForType(getClass()); user.plugindata.set(ownFolder + "_id", plugindata.getString("id")); - BiConsumer sync = (pdata1, pdata2) -> { - for (Entry, String> entry : playerTypes.entrySet()) - if (pdata1.contains(entry.getValue() + "_id", false)) - pdata2.set(entry.getValue() + "_id", pdata1.getString(entry.getValue() + "_id")); - }; // ... + Consumer sync = sourcedata -> { + final String sourcefolder = sourcedata == plugindata ? ownFolder : getFolderForType(user.getClass()); + final String id = sourcedata.getString("id"); + for (Entry, String> entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones + final String otherid = sourcedata.getString(entry.getValue() + "_id"); + if (otherid == null) + continue; + try (@SuppressWarnings("unchecked") + ChromaGamerBase cg = getUser(otherid, (Class) entry.getKey())) { + cg.plugindata.set(sourcefolder + "_id", id); // Set new IDs + for (Entry, String> item : playerTypes.entrySet()) + if (sourcedata.contains(item.getValue() + "_id")) + cg.plugindata.set(item.getValue() + "_id", sourcedata.getString(item.getValue() + "_id")); // Set all existing IDs + } catch (Exception e) { + TBMCCoreAPI.SendException("Failed to update " + sourcefolder + " ID in player files for " + id + + " in folder with " + entry.getValue() + " id " + otherid + "!", e); + } + } + }; + sync.accept(plugindata); + sync.accept(user.plugindata); + } + + /** + * Returns this player as a plugin player. This will return a new instance unless the player is online.
+ * Make sure to close both the returned and this object. A try-with-resources block or two can help.
+ * + * @param cl + * The target player class + * @return The player as a {@link T} object or null if not having an account there + */ + public T getAs(Class cl) { + String newfolder = getFolderForType(cl); + if (newfolder == null) + throw new RuntimeException("The specified class " + cl.getSimpleName() + " isn't registered!"); + if (!plugindata.contains(newfolder + "_id")) + return null; + return getUser(plugindata.getString(newfolder + "_id"), cl); } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 3b0ce71..56360d3 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -1,6 +1,7 @@ package buttondevteam.lib.player; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; public abstract class TBMCPlayerBase extends ChromaGamerBase { private static final String FOLDER_NAME = "minecraft"; @@ -28,9 +29,29 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { addPlayerType(TBMCPlayerBase.class, FOLDER_NAME); } + @SuppressWarnings("unchecked") public static T getPlayer(UUID uuid, Class cl) { + if (playermap.containsKey(uuid + "-" + cl.getSimpleName())) + return (T) playermap.get(uuid + "-" + cl.getSimpleName()); T obj = ChromaGamerBase.getUser(uuid.toString(), cl); obj.uuid = uuid; return obj; } + + /** + * Key: UUID-Class + */ + static final ConcurrentHashMap playermap = new ConcurrentHashMap<>(); + + /** + * Gets the TBMCPlayer object as a specific plugin player, keeping it's data * + * + * @param p + * Player to get + * @param cl + * The TBMCPlayer subclass + */ + public T asPluginPlayer(Class cl) { + return getPlayer(uuid, cl); + } } -- 2.30.2 From 88f85d2b15932dbe9816da07fa031c4fbdc28970 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 21 Jan 2017 23:01:58 +0100 Subject: [PATCH 08/22] Fixed rename method, player data and other stuff --- .../buttondevteam/core/PlayerListener.java | 8 +- .../core/ScheduledRestartCommand.java | 4 +- .../lib/player/ChromaGamerBase.java | 19 ++-- .../lib/player/TBMCPlayerAddEvent.java | 36 -------- .../lib/player/TBMCPlayerBase.java | 89 +++++++++++++++++++ .../lib/player/TBMCPlayerJoinEvent.java | 6 +- .../lib/player/TBMCPlayerLoadEvent.java | 13 +-- .../lib/player/TBMCPlayerQuitEvent.java | 6 +- .../lib/player/TBMCPlayerSaveEvent.java | 12 +-- 9 files changed, 119 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java diff --git a/src/main/java/buttondevteam/core/PlayerListener.java b/src/main/java/buttondevteam/core/PlayerListener.java index 9261b21..0328dac 100644 --- a/src/main/java/buttondevteam/core/PlayerListener.java +++ b/src/main/java/buttondevteam/core/PlayerListener.java @@ -6,6 +6,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.player.TBMCPlayer; public class PlayerListener implements Listener { @@ -13,9 +14,10 @@ public class PlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void OnPlayerJoin(PlayerJoinEvent event) { TBMCPlayer player = TBMCPlayer.loadPlayer(event.getPlayer()); - if (player == null) - event.getPlayer().sendMessage("§c[TBMC] Failed to load player data! Please contact a mod."); - else + if (player == null) { + TBMCCoreAPI.SendException("Error on player join!", new Exception("Player is null")); + event.getPlayer().sendMessage("§c[TBMC] Failed to load player data! The error has been sent to the devs."); + } else TBMCPlayer.joinPlayer(player); } diff --git a/src/main/java/buttondevteam/core/ScheduledRestartCommand.java b/src/main/java/buttondevteam/core/ScheduledRestartCommand.java index 2bec03b..77bc185 100644 --- a/src/main/java/buttondevteam/core/ScheduledRestartCommand.java +++ b/src/main/java/buttondevteam/core/ScheduledRestartCommand.java @@ -1,7 +1,5 @@ package buttondevteam.core; -import java.util.stream.Collectors; - import org.bukkit.Bukkit; import org.bukkit.boss.BarColor; import org.bukkit.boss.BarFlag; @@ -51,7 +49,7 @@ public class ScheduledRestartCommand extends TBMCCommandBase { if (restartcounter % 200 == 0) Bukkit.broadcastMessage("§c-- The server is restarting in " + restartcounter / 20 + " seconds!"); restartbar.setProgress(restartcounter / (double) restarttime); - restartbar.setTitle("Server restart in " + restartcounter / 20f); + restartbar.setTitle(String.format("Server restart in %f.2", restartcounter / 20f)); /* * if (restartcounter % 20 == 0) System.out.println("Progress: " + restartbar.getProgress()); */ diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index c50be0a..e8740c1 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -10,7 +10,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import buttondevteam.lib.TBMCCoreAPI; public abstract class ChromaGamerBase implements AutoCloseable { - private static final String TBMC_PLAYERS_DIR = "TBMC/players/"; + public static final String TBMC_PLAYERS_DIR = "TBMC/players/"; private static final HashMap, String> playerTypes = new HashMap<>(); @@ -57,8 +57,9 @@ public abstract class ChromaGamerBase implements AutoCloseable { protected static T getUser(String fname, Class cl) { try { T obj = cl.newInstance(); - obj.plugindata = YamlConfiguration - .loadConfiguration(new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname)); + final File file = new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname + ".yml"); + file.mkdirs(); + obj.plugindata = YamlConfiguration.loadConfiguration(file); return obj; } catch (Exception e) { TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e); @@ -68,7 +69,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { @Override public void close() throws Exception { - plugindata.save(new File(TBMC_PLAYERS_DIR + getFolderForType(getClass()), getFileName())); + plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName() + ".yml")); } /** @@ -79,11 +80,11 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ public void connectWith(T user) { // Set the ID, go through all linked files and connect them as well - plugindata.set(getFolderForType(user.getClass()) + "_id", user.plugindata.getString("id")); - final String ownFolder = getFolderForType(getClass()); + plugindata.set(user.getFolder() + "_id", user.plugindata.getString("id")); + final String ownFolder = user.getFolder(); user.plugindata.set(ownFolder + "_id", plugindata.getString("id")); Consumer sync = sourcedata -> { - final String sourcefolder = sourcedata == plugindata ? ownFolder : getFolderForType(user.getClass()); + final String sourcefolder = sourcedata == plugindata ? ownFolder : user.getFolder(); final String id = sourcedata.getString("id"); for (Entry, String> entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones final String otherid = sourcedata.getString(entry.getValue() + "_id"); @@ -121,4 +122,8 @@ public abstract class ChromaGamerBase implements AutoCloseable { return null; return getUser(plugindata.getString(newfolder + "_id"), cl); } + + public String getFolder() { + return getFolderForType(getClass()); + } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java deleted file mode 100644 index 6613ff3..0000000 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerAddEvent.java +++ /dev/null @@ -1,36 +0,0 @@ -package buttondevteam.lib.player; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -/** - *

- * This event gets called when a new player joins. After this event, the - * {@link TBMCPlayerSaveEvent} will be called. - *

- * - * @author Norbi - * - */ -public class TBMCPlayerAddEvent extends Event { - private static final HandlerList handlers = new HandlerList(); - - private TBMCPlayer player; - - public TBMCPlayerAddEvent(TBMCPlayer player) { - this.player = player; - } - - public TBMCPlayer GetPlayer() { - return player; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} \ No newline at end of file diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 56360d3..bfc56ab 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -3,6 +3,16 @@ package buttondevteam.lib.player; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import com.palmergames.bukkit.towny.Towny; +import com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.TownyUniverse; + +import buttondevteam.lib.TBMCCoreAPI; + public abstract class TBMCPlayerBase extends ChromaGamerBase { private static final String FOLDER_NAME = "minecraft"; protected UUID uuid; @@ -11,6 +21,14 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { return uuid; } + public String getPlayerName() { + return plugindata.getString("playername", ""); + } + + public void setPlayerName(String value) { + plugindata.set("playername", value); + } + public abstract String getPluginName(); @Override @@ -29,6 +47,15 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { addPlayerType(TBMCPlayerBase.class, FOLDER_NAME); } + /** + * Get player as a plugin player + * + * @param uuid + * The UUID of the player to get + * @param cl + * The type of the player + * @return The requested player object + */ @SuppressWarnings("unchecked") public static T getPlayer(UUID uuid, Class cl) { if (playermap.containsKey(uuid + "-" + cl.getSimpleName())) @@ -54,4 +81,66 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { public T asPluginPlayer(Class cl) { return getPlayer(uuid, cl); } + + /** + * Only intended to use from ButtonCore + */ + public static T loadPlayer(OfflinePlayer p, Class cl) { + T player = getPlayer(p.getUniqueId(), cl); + Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); + if (player.getPlayerName() == null) { + player.setPlayerName(p.getName()); + Bukkit.getLogger().info("Player name saved: " + player.getPlayerName()); + } else if (!p.getName().equals(player.getPlayerName())) { + Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName()); + TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); + Resident resident = tu.getResidentMap().get(player.getPlayerName()); + if (resident == null) + Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); + else if (tu.getResidentMap().contains(p.getName())) + Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle + else + try { + TownyUniverse.getDataSource().renamePlayer(resident, p.getName()); + } catch (AlreadyRegisteredException e) { + TBMCCoreAPI.SendException("Failed to rename resident, there's already one with this name.", e); + } catch (NotRegisteredException e) { + TBMCCoreAPI.SendException("Failed to rename resident, the resident isn't registered.", e); + } + player.setPlayerName(p.getName()); + Bukkit.getLogger().info("Renaming done."); + } + + // Load in other plugins + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(player)); + return player; + } + + /** + * Only intended to use from ButtonCore + */ + public static void savePlayer(TBMCPlayerBase player) { + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(player)); + try { + player.close(); + } catch (Exception e) { + new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace(); + } + } + + /** + * Only intended to use from ButtonCore + */ + public static void joinPlayer(TBMCPlayerBase player) { + playermap.put(player.uuid + "-" + player.getFolder(), player); + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player)); + } + + /** + * Only intended to use from ButtonCore + */ + public static void quitPlayer(TBMCPlayerBase player) { + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); + playermap.remove(player.uuid + "-" + player.getFolder()); + } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java index 0422530..f63c002 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerJoinEvent.java @@ -6,13 +6,13 @@ import org.bukkit.event.HandlerList; public class TBMCPlayerJoinEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private TBMCPlayer player; + private TBMCPlayerBase player; - public TBMCPlayerJoinEvent(TBMCPlayer player) { + public TBMCPlayerJoinEvent(TBMCPlayerBase player) { this.player = player; } - public TBMCPlayer GetPlayer() { + public TBMCPlayerBase GetPlayer() { return player; } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java index 67c178d..80ad7da 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerLoadEvent.java @@ -1,25 +1,18 @@ package buttondevteam.lib.player; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; public class TBMCPlayerLoadEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private YamlConfiguration yaml; - private TBMCPlayer player; + private TBMCPlayerBase player; - public TBMCPlayerLoadEvent(YamlConfiguration yaml, TBMCPlayer player) { - this.yaml = yaml; + public TBMCPlayerLoadEvent(TBMCPlayerBase player) { this.player = player; } - public YamlConfiguration GetPlayerConfig() { - return yaml; - } - - public TBMCPlayer GetPlayer() { + public TBMCPlayerBase GetPlayer() { return player; } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java index c77b272..a228d02 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerQuitEvent.java @@ -6,13 +6,13 @@ import org.bukkit.event.HandlerList; public class TBMCPlayerQuitEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private TBMCPlayer player; + private TBMCPlayerBase player; - public TBMCPlayerQuitEvent(TBMCPlayer player) { + public TBMCPlayerQuitEvent(TBMCPlayerBase player) { this.player = player; } - public TBMCPlayer GetPlayer() { + public TBMCPlayerBase GetPlayer() { return player; } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java index 5c5f873..67b74a8 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java @@ -7,19 +7,13 @@ import org.bukkit.event.HandlerList; public class TBMCPlayerSaveEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private YamlConfiguration yaml; - private TBMCPlayer player; + private TBMCPlayerBase player; - public TBMCPlayerSaveEvent(YamlConfiguration yaml, TBMCPlayer player) { - this.yaml = yaml; + public TBMCPlayerSaveEvent(TBMCPlayerBase player) { this.player = player; } - public YamlConfiguration GetPlayerConfig() { - return yaml; - } - - public TBMCPlayer GetPlayer() { + public TBMCPlayerBase GetPlayer() { return player; } -- 2.30.2 From 5e4128afe73699406547e84777c8680d6a3ad892 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 23 Jan 2017 20:54:43 +0100 Subject: [PATCH 09/22] Moved the last bits over and deleted old class --- .../java/buttondevteam/core/MainPlugin.java | 8 +- .../buttondevteam/lib/player/TBMCPlayer.java | 510 ------------------ .../lib/player/TBMCPlayerBase.java | 66 ++- .../lib/player/TBMCPlayerGetInfoEvent.java | 12 +- .../lib/player/TBMCPlayerSaveEvent.java | 1 - 5 files changed, 70 insertions(+), 527 deletions(-) delete mode 100644 src/main/java/buttondevteam/lib/player/TBMCPlayer.java diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index c084692..c09b698 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -1,7 +1,5 @@ package buttondevteam.core; -import java.util.Map.Entry; -import java.util.UUID; import java.util.logging.Logger; import org.bukkit.plugin.PluginDescriptionFile; @@ -10,7 +8,7 @@ import org.bukkit.plugin.java.JavaPlugin; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.TBMCChatAPI; -import buttondevteam.lib.player.TBMCPlayer; +import buttondevteam.lib.player.TBMCPlayerBase; import net.milkbowl.vault.permission.Permission; public class MainPlugin extends JavaPlugin { @@ -39,9 +37,7 @@ public class MainPlugin extends JavaPlugin { @Override public void onDisable() { logger.info("Saving player data..."); - for (Entry entry : TBMCPlayer.getLoadedPlayers().entrySet()) { - TBMCPlayer.savePlayer(entry.getValue()); - } + TBMCPlayerBase.savePlayers(); logger.info("Player data saved."); } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayer.java b/src/main/java/buttondevteam/lib/player/TBMCPlayer.java deleted file mode 100644 index 954027b..0000000 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayer.java +++ /dev/null @@ -1,510 +0,0 @@ -package buttondevteam.lib.player; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; - -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; - -import com.palmergames.bukkit.towny.Towny; -import com.palmergames.bukkit.towny.object.Resident; -import com.palmergames.bukkit.towny.object.TownyUniverse; - -/** - *

- * The class for holding data common to all TBMC plugins - *

- *

- * Use {@link #asPluginPlayer(Class)} to get plugin-specific data - *

- * - * @author Norbi - * - */ -public class TBMCPlayer implements AutoCloseable { - private static final String TBMC_PLAYERS_DIR = "TBMC/players"; - - private ConcurrentHashMap data = new ConcurrentHashMap<>(); - - /** - *

- * Gets a player data entry for the caller plugin returning the desired type.
- * 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 - */ - @SuppressWarnings("unchecked") - protected T getData() { - 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"); - Object ret = getLoadedPlayers().get(uuid).data.get(mname.substring("get".length()).toLowerCase()); - if (ret != null && Integer.class.isAssignableFrom(ret.getClass())) - throw new UnsupportedOperationException("For integers use getIntData()"); - if (ret != null && Boolean.class.isAssignableFrom(ret.getClass())) - throw new UnsupportedOperationException("For booleans use getBoolData()"); - return (T) ret; - } - - /** - * 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 setData(Object value) { - StackTraceElement st = new Exception().getStackTrace()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("set")) - throw new UnsupportedOperationException("Can only use setData from a setXYZ method"); - getLoadedPlayers().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 getSomeEnum() {
-	 * 	return getEnumData();
-	 * }
-	 * 
- * - * @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 getEnumData from a getXYZ method"); - final String retstr = (String) getLoadedPlayers().get(uuid).data - .get(mname.substring("get".length()).toLowerCase()); - if (retstr != null) - return Enum.valueOf(cl, retstr); - else - return null; - } - - /** - * Sets a player data entry based on the caller method
- * Usage: - *

- * - *
-	 * {@code
-	 * public String setSomeEnum(SomeEnum value) {
-	 * 	return setEnumData(value);
-	 * }
-	 * 
- * - * @param value - * The value to set - */ - protected void setEnumData(Enum value) { - StackTraceElement st = new Exception().getStackTrace()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("set")) - throw new UnsupportedOperationException("Can only use setEnumData from a setXYZ method"); - getLoadedPlayers().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 Optional 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 = getLoadedPlayers().get(uuid).data.get(mname.substring("get".length()).toLowerCase()); - if (obj == null) - return Optional.empty(); - if (obj instanceof Short) - return Optional.of((T) obj); - if (!(Integer.class.isAssignableFrom(obj.getClass()))) - throw new UnsupportedOperationException("The retrieved object isn't an integer: " + obj); - Integer int_ = (Integer) obj; - if (Short.class.isAssignableFrom(cl)) - return Optional.of((T) (Object) int_.shortValue()); - else - return Optional.of((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()[1]; - String mname = st.getMethodName(); - if (!mname.startsWith("set")) - throw new UnsupportedOperationException("Can only use setIntData from a setXYZ method"); - getLoadedPlayers().get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value); - } - - /** - *

- * Gets a player data entry for the caller plugin returning a boolean.
- * Usage: - *

- * - *
-	 * {@code
-	 * public String getFlairCheater() {
-	 * 	return getBoolData();
-	 * }
-	 * 
- * - * @return The value or false if not found - */ - protected boolean getBoolData() { - 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"); - Object ret = getLoadedPlayers().get(uuid).data.get(mname.substring("get".length()).toLowerCase()); - if (ret != null && !Boolean.class.isAssignableFrom(ret.getClass())) - throw new UnsupportedOperationException("Not a boolean!"); - if (ret == null) - return false; - return (boolean) ret; - } - - /** - * Gets the player's Minecraft name - * - * @return The player's Minecraft name - */ - public String getPlayerName() { - return getData(); - } - - /** - * Sets the player's Minecraft name - * - * @param playerName - * the new name - */ - public void setPlayerName(String playerName) { - setData(playerName); - } - - private UUID uuid; // Do not save it in the file - - /** - * Get the player's UUID - * - * @return The Minecraft UUID of the player - */ - public UUID getUuid() { - return uuid; - } - - private ConcurrentHashMap, TBMCPlayer> playermap = new ConcurrentHashMap<>(); // TODO - - /** - * Gets the TBMCPlayer object as a specific plugin player, keeping it's data * - * - * @param p - * Player to get - * @param cl - * The TBMCPlayer subclass - */ - @SuppressWarnings("unchecked") - public T asPluginPlayer(Class cl) { - T obj = null; - if (playermap.containsKey(cl)) - return (T) playermap.get(cl); - try { - obj = cl.newInstance(); - ((TBMCPlayer) obj).uuid = uuid; - // ((TBMCPlayer) obj).data.putAll(data); - playermap.put(cl, obj); - } catch (Exception e) { - e.printStackTrace(); - } - return obj; - } - - private static HashMap LoadedPlayers = new HashMap<>(); - - /** - * This method returns a TBMC player from their name. Calling this method may return an offline player which will load it, therefore it's highly recommended to use {@link #close()} to unload the - * player data. Using try-with-resources may be the easiest way to achieve this. Example: - * - *
-	 * {@code
-	 * try(TBMCPlayer player = getFromName(p))
-	 * {
-	 * 	...
-	 * }
-	 * 
- * - * @param name - * The player's name - * @return The {@link TBMCPlayer} object for the player - */ - public static TBMCPlayer getFromName(String name) { - @SuppressWarnings("deprecation") - OfflinePlayer p = Bukkit.getOfflinePlayer(name); - if (p != null) - return getPlayer(p); - else - return null; - } - - /** - * This method returns a TBMC player from a Bukkit player. Calling this method may return an offline player, therefore it's highly recommended to use {@link #close()} to unload the player data. - * Using try-with-resources may be the easiest way to achieve this. Example: - * - *
-	 * {@code
-	 * try(TBMCPlayer player = getPlayer(p))
-	 * {
-	 * 	...
-	 * }
-	 * 
- * - * @param p - * The Player object - * @return The {@link TBMCPlayer} object for the player - */ - public static TBMCPlayer getPlayer(OfflinePlayer p) { - if (TBMCPlayer.getLoadedPlayers().containsKey(p.getUniqueId())) - return TBMCPlayer.getLoadedPlayers().get(p.getUniqueId()); - else - return TBMCPlayer.loadPlayer(p); - } - - /** - * This method returns a TBMC player from a player UUID. Calling this method may return an offline player, therefore it's highly recommended to use {@link #close()} to unload the player data. - * Using try-with-resources may be the easiest way to achieve this. Example: - * - *
-	 * {@code
-	 * try(TBMCPlayer player = getPlayer(p))
-	 * {
-	 * 	...
-	 * }
-	 * 
- * - * @param p - * The Player object - * @return The {@link TBMCPlayer} object for the player - */ - public static TBMCPlayer getPlayer(UUID uuid) { - if (TBMCPlayer.getLoadedPlayers().containsKey(uuid)) - return TBMCPlayer.getLoadedPlayers().get(uuid); - else - return TBMCPlayer.loadPlayer(Bukkit.getOfflinePlayer(uuid)); - } - - /** - * This is a convenience method for {@link #getPlayer(OfflinePlayer)}.{@link #asPluginPlayer(Class)}. - * - * See those methods for more information. - * - * @param p - * Player to get - * @param cl - * The TBMCPlayer subclass - * @return The player as a subtype of TBMCPlayer - */ - public static T getPlayerAs(OfflinePlayer p, Class cl) { - return getPlayer(p).asPluginPlayer(cl); - } - - /** - * This is a convenience method for {@link #getPlayer(UUID)}.{@link #asPluginPlayer(Class)} - * - * See those methods for more information. - * - * @param uuid - * The UUID of the player to get - * @param cl - * The TBMCPlayer subclass - * @return The player as a subtype of TBMCPlayer - */ - public static T getPlayerAs(UUID uuid, Class cl) { - return getPlayer(uuid).asPluginPlayer(cl); - } - - /** - * Only intended to use from ButtonCore - */ - public static TBMCPlayer loadPlayer(OfflinePlayer p) { - if (getLoadedPlayers().containsKey(p.getUniqueId())) - return getLoadedPlayers().get(p.getUniqueId()); - File file = new File(TBMC_PLAYERS_DIR); - file.mkdirs(); - file = new File(TBMC_PLAYERS_DIR, p.getUniqueId().toString() + ".yml"); - if (!file.exists()) - return addPlayer(p); - else { - final YamlConfiguration yc = new YamlConfiguration(); - try { - yc.load(file); - } catch (Exception e) { - new Exception("Failed to load player data for " + p.getUniqueId(), e).printStackTrace(); - return null; - } - TBMCPlayer player = new TBMCPlayer(); - player.uuid = p.getUniqueId(); - player.data.putAll(yc.getValues(true)); - getLoadedPlayers().put(p.getUniqueId(), player); // Accessing any value requires it to be in the map - Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); - if (player.getPlayerName() == null) { - player.setPlayerName(p.getName()); - Bukkit.getLogger().info("Player name saved: " + player.getPlayerName()); - } else if (!p.getName().equals(player.getPlayerName())) { - Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName()); - TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); - Resident resident = tu.getResidentMap().get(player.getPlayerName()); - if (resident == null) - Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); - else if (tu.getResidentMap().contains(p.getName())) - Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle - else - resident.setName(p.getName()); - player.setPlayerName(p.getName()); - Bukkit.getLogger().info("Renaming done."); - } - - // Load in other plugins - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(yc, player)); - return player; - } - } - - /** - * Only intended to use from ButtonCore - */ - public static TBMCPlayer addPlayer(OfflinePlayer p) { - TBMCPlayer player = new TBMCPlayer(); - player.uuid = p.getUniqueId(); - getLoadedPlayers().put(p.getUniqueId(), player); // Accessing any value requires it to be in the map - player.setPlayerName(p.getName()); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerAddEvent(player)); - savePlayer(player); - return player; - } - - /** - * Only intended to use from ButtonCore - */ - public static void savePlayer(TBMCPlayer player) { - YamlConfiguration yc = new YamlConfiguration(); - for (Entry item : player.data.entrySet()) - yc.set(item.getKey(), item.getValue()); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(yc, player)); - try { - yc.save(TBMC_PLAYERS_DIR + "/" + player.uuid + ".yml"); - } catch (IOException e) { - new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace(); - } - } - - /** - * Only intended to use from ButtonCore - */ - public static void joinPlayer(TBMCPlayer player) { - getLoadedPlayers().put(player.uuid, player); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player)); - } - - /** - * Only intended to use from ButtonCore - */ - public static void quitPlayer(TBMCPlayer player) { - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); - getLoadedPlayers().remove(player.uuid); - } - - /** - * By default the player data will only get cleaned from memory when the player quits. Therefore this method must be called when accessing an offline player to clean the player data up. Calling - * this method will have no effect on online players.
- * Therefore, the recommended use is to call it when using {@link #GetPlayer} or use try-with-resources. - */ - @Override - public void close() { - final Player player = Bukkit.getPlayer(uuid); - if (player == null || !player.isOnline()) - getLoadedPlayers().remove(uuid); - } - - public static HashMap getLoadedPlayers() { - return LoadedPlayers; - } - - /** - * Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins. - * - * @param target - * The {@link InfoTarget} to return the info for. - * @return The player information. - */ - public String getInfo(InfoTarget target) { - TBMCPlayerGetInfoEvent event = new TBMCPlayerGetInfoEvent(this, target); - Bukkit.getServer().getPluginManager().callEvent(event); - return event.getResult(); - } - - public enum InfoTarget { - MCHover, MCCommand, Discord - } -} diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index bfc56ab..d66efc2 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -71,10 +71,9 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { static final ConcurrentHashMap playermap = new ConcurrentHashMap<>(); /** - * Gets the TBMCPlayer object as a specific plugin player, keeping it's data * + * Gets the TBMCPlayer object as a specific plugin player, keeping it's data
+ * Make sure to use try-with-resources with this to save the data, as it may need to load the file * - * @param p - * Player to get * @param cl * The TBMCPlayer subclass */ @@ -85,7 +84,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { /** * Only intended to use from ButtonCore */ - public static T loadPlayer(OfflinePlayer p, Class cl) { + public static T loadPlayer(OfflinePlayer p, Class cl) { // TODO: Load player files and get player classes backed by the YAML T player = getPlayer(p.getUniqueId(), cl); Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); if (player.getPlayerName() == null) { @@ -142,5 +141,64 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { public static void quitPlayer(TBMCPlayerBase player) { Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); playermap.remove(player.uuid + "-" + player.getFolder()); + try { + player.close(); + } catch (Exception e) { + TBMCCoreAPI.SendException("Error while saving quitting player " + player.getPlayerName() + " (" + + player.getFolder() + "/" + player.getFileName() + ")!", e); + } + } + + public static void savePlayers() { + playermap.values().stream().forEach(p -> { + try { + p.close(); + } catch (Exception e) { + TBMCCoreAPI.SendException("Error while saving player " + p.getPlayerName() + " (" + p.getFolder() + "/" + + p.getFileName() + ")!", e); + } + }); + } + + /** + * This method returns a TBMC player from their name. Calling this method may return an offline player which will load it, therefore it's highly recommended to use {@link #close()} to unload the + * player data. Using try-with-resources may be the easiest way to achieve this. Example: + * + *
+	 * {@code
+	 * try(TBMCPlayer player = getFromName(p))
+	 * {
+	 * 	...
+	 * }
+	 * 
+ * + * @param name + * The player's name + * @return The {@link TBMCPlayer} object for the player + */ + public static T getFromName(String name, Class cl) { + @SuppressWarnings("deprecation") + OfflinePlayer p = Bukkit.getOfflinePlayer(name); + if (p != null) + return getPlayer(p.getUniqueId(), cl); + else + return null; + } + + /** + * Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins. + * + * @param target + * The {@link InfoTarget} to return the info for. + * @return The player information. + */ + public String getInfo(InfoTarget target) { + TBMCPlayerGetInfoEvent event = new TBMCPlayerGetInfoEvent(this, target); + Bukkit.getServer().getPluginManager().callEvent(event); + return event.getResult(); + } + + public enum InfoTarget { + MCHover, MCCommand, Discord } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java index b8e5b5e..ba2bc74 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java @@ -7,7 +7,7 @@ import java.util.stream.Collectors; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -import buttondevteam.lib.player.TBMCPlayer.InfoTarget; +import buttondevteam.lib.player.TBMCPlayerBase.InfoTarget; /** *

@@ -20,11 +20,11 @@ import buttondevteam.lib.player.TBMCPlayer.InfoTarget; public class TBMCPlayerGetInfoEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private TBMCPlayer player; + private TBMCPlayerBase player; private List infolines; - private TBMCPlayer.InfoTarget target; + private InfoTarget target; - TBMCPlayerGetInfoEvent(TBMCPlayer player, TBMCPlayer.InfoTarget target) { + TBMCPlayerGetInfoEvent(TBMCPlayerBase player, InfoTarget target) { this.player = player; infolines = new ArrayList<>(); this.target = target; @@ -35,7 +35,7 @@ public class TBMCPlayerGetInfoEvent extends Event { * * @return A player object */ - public TBMCPlayer getPlayer() { + public TBMCPlayerBase getPlayer() { return player; } @@ -54,7 +54,7 @@ public class TBMCPlayerGetInfoEvent extends Event { * * @return The target of the information. */ - public TBMCPlayer.InfoTarget getTarget() { + public InfoTarget getTarget() { return target; } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java index 67b74a8..2f4ea4c 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerSaveEvent.java @@ -1,6 +1,5 @@ package buttondevteam.lib.player; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -- 2.30.2 From 5f62df97dee674df44b0112ba95e9c80cc3a62ea Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 27 Jan 2017 22:54:24 +0100 Subject: [PATCH 10/22] Added some code and some TODOs --- .../buttondevteam/core/PlayerListener.java | 5 +- .../lib/player/ChromaGamerBase.java | 3 +- .../buttondevteam/lib/player/PlayerClass.java | 12 +++ .../buttondevteam/lib/player/TBMCPlayer.java | 6 ++ .../lib/player/TBMCPlayerBase.java | 91 ++++++++++++------- 5 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 src/main/java/buttondevteam/lib/player/PlayerClass.java create mode 100644 src/main/java/buttondevteam/lib/player/TBMCPlayer.java diff --git a/src/main/java/buttondevteam/core/PlayerListener.java b/src/main/java/buttondevteam/core/PlayerListener.java index 0328dac..e6fd5c9 100644 --- a/src/main/java/buttondevteam/core/PlayerListener.java +++ b/src/main/java/buttondevteam/core/PlayerListener.java @@ -7,11 +7,10 @@ import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.lib.player.TBMCPlayer; public class PlayerListener implements Listener { - @EventHandler(priority = EventPriority.HIGHEST) + /*@EventHandler(priority = EventPriority.HIGHEST) public void OnPlayerJoin(PlayerJoinEvent event) { TBMCPlayer player = TBMCPlayer.loadPlayer(event.getPlayer()); if (player == null) { @@ -26,5 +25,5 @@ public class PlayerListener implements Listener { TBMCPlayer player = TBMCPlayer.getPlayer(event.getPlayer()); TBMCPlayer.savePlayer(player); TBMCPlayer.quitPlayer(player); - } + }*/ } diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index e8740c1..beeaa27 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -41,7 +41,8 @@ public abstract class ChromaGamerBase implements AutoCloseable { } /** - * This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc. + * This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc.
+ * Does not include .yml */ public abstract String getFileName(); diff --git a/src/main/java/buttondevteam/lib/player/PlayerClass.java b/src/main/java/buttondevteam/lib/player/PlayerClass.java new file mode 100644 index 0000000..a543849 --- /dev/null +++ b/src/main/java/buttondevteam/lib/player/PlayerClass.java @@ -0,0 +1,12 @@ +package buttondevteam.lib.player; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface PlayerClass { + String pluginname(); +} diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayer.java b/src/main/java/buttondevteam/lib/player/TBMCPlayer.java new file mode 100644 index 0000000..badbe8e --- /dev/null +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayer.java @@ -0,0 +1,6 @@ +package buttondevteam.lib.player; + +@PlayerClass(pluginname = "ButtonCore") +public class TBMCPlayer extends TBMCPlayerBase { + +} diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index d66efc2..b6c44fa 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -1,10 +1,16 @@ package buttondevteam.lib.player; +import java.io.File; +import java.util.Iterator; +import java.util.Map.Entry; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + import com.palmergames.bukkit.towny.Towny; import com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; @@ -29,8 +35,6 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { plugindata.set("playername", value); } - public abstract String getPluginName(); - @Override public String getFileName() { return getUUID().toString(); @@ -60,9 +64,21 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { public static T getPlayer(UUID uuid, Class cl) { if (playermap.containsKey(uuid + "-" + cl.getSimpleName())) return (T) playermap.get(uuid + "-" + cl.getSimpleName()); - T obj = ChromaGamerBase.getUser(uuid.toString(), cl); - obj.uuid = uuid; - return obj; + try { + T player; + if (playerfiles.containsKey(uuid)) { + player = cl.newInstance(); + player.plugindata = playerfiles.get(uuid); + playermap.put(player.uuid + "-" + player.getFolder(), player); // It will get removed on player quit + } else + player = ChromaGamerBase.getUser(uuid.toString(), cl); + player.uuid = uuid; + return player; + } catch (Exception e) { + TBMCCoreAPI.SendException( + "Failed to get player with UUID " + uuid + " and class " + cl.getSimpleName() + "!", e); + return null; + } } /** @@ -70,6 +86,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { */ static final ConcurrentHashMap playermap = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap playerfiles = new ConcurrentHashMap<>(); + /** * Gets the TBMCPlayer object as a specific plugin player, keeping it's data
* Make sure to use try-with-resources with this to save the data, as it may need to load the file @@ -84,9 +102,25 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { /** * Only intended to use from ButtonCore */ - public static T loadPlayer(OfflinePlayer p, Class cl) { // TODO: Load player files and get player classes backed by the YAML - T player = getPlayer(p.getUniqueId(), cl); - Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); + public static void savePlayer(TBMCPlayerBase player) { + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(player)); + try { + player.close(); + } catch (Exception e) { + new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace(); + } + } + + /** + * Only intended to use from ButtonCore + */ + public static void joinPlayer(UUID uuid) { + YamlConfiguration yc; + if (playerfiles.containsKey(uuid)) + yc = playerfiles.get(uuid); + else + playerfiles.put(uuid, yc = YamlConfiguration.loadConfiguration(new File("minecraft", uuid + ".yml"))); + /*Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); if (player.getPlayerName() == null) { player.setPlayerName(p.getName()); Bukkit.getLogger().info("Player name saved: " + player.getPlayerName()); @@ -112,40 +146,27 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { // Load in other plugins Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(player)); - return player; + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player));*/ } /** * Only intended to use from ButtonCore */ - public static void savePlayer(TBMCPlayerBase player) { - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(player)); - try { - player.close(); - } catch (Exception e) { - new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace(); + public static void quitPlayer(Player p) { + Iterator> it = playermap.entrySet().iterator(); + while (it.hasNext()) { + Entry entry = it.next(); + if (entry.getKey().startsWith(p.getUniqueId().toString())) { // Save every player data + TBMCPlayerBase player = entry.getValue(); // TODO: Separate plugin data by plugin name (annotations?) + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); + } } - } - - /** - * Only intended to use from ButtonCore - */ - public static void joinPlayer(TBMCPlayerBase player) { - playermap.put(player.uuid + "-" + player.getFolder(), player); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player)); - } - - /** - * Only intended to use from ButtonCore - */ - public static void quitPlayer(TBMCPlayerBase player) { - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); - playermap.remove(player.uuid + "-" + player.getFolder()); - try { - player.close(); + final YamlConfiguration playerfile = playerfiles.get(p.getUniqueId()); + try { // Only save to file once, not for each plugin + playerfile.save(p.getUniqueId().toString() + ".yml"); // TODO: Bring this together with the close() method, like a common method or something } catch (Exception e) { - TBMCCoreAPI.SendException("Error while saving quitting player " + player.getPlayerName() + " (" - + player.getFolder() + "/" + player.getFileName() + ")!", e); + TBMCCoreAPI.SendException("Error while saving quitting player " + playerfile.getString("playername") + " (" + + "minecraft/" + p.getUniqueId() + ".yml)!", e); } } -- 2.30.2 From 5a559aa4c398b0845ddc68ab4b395ff0162cfc57 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 28 Jan 2017 12:43:55 +0100 Subject: [PATCH 11/22] Started using TBMCPlayer and added save(); --- .../lib/player/ChromaGamerBase.java | 19 +++++++++++++++++++ .../lib/player/TBMCPlayerBase.java | 13 +++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index beeaa27..24a1bcb 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -1,6 +1,7 @@ package buttondevteam.lib.player; import java.io.File; +import java.io.IOException; import java.util.HashMap; import java.util.Map.Entry; import java.util.function.Consumer; @@ -68,8 +69,26 @@ public abstract class ChromaGamerBase implements AutoCloseable { return null; } + /** + * Saves the player. It'll pass all exceptions to the caller. To automatically handle the exception, use {@link #save()} instead. + */ @Override public void close() throws Exception { + save_(); + } + + /** + * Saves the player. It'll send all exceptions that may happen. To catch the exception, use {@link #close()} instead. + */ + public void save() { + try { + save_(); + } catch (Exception e) { + TBMCCoreAPI.SendException("Error while saving player data!", e); + } + } + + private void save_() throws IOException { plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName() + ".yml")); } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index b6c44fa..a910020 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -114,13 +114,9 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { /** * Only intended to use from ButtonCore */ - public static void joinPlayer(UUID uuid) { - YamlConfiguration yc; - if (playerfiles.containsKey(uuid)) - yc = playerfiles.get(uuid); - else - playerfiles.put(uuid, yc = YamlConfiguration.loadConfiguration(new File("minecraft", uuid + ".yml"))); - /*Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); + public static void joinPlayer(Player p) { + TBMCPlayer player = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class); + Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); if (player.getPlayerName() == null) { player.setPlayerName(p.getName()); Bukkit.getLogger().info("Player name saved: " + player.getPlayerName()); @@ -146,7 +142,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { // Load in other plugins Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(player)); - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player));*/ + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player)); + player.save(); } /** -- 2.30.2 From 159cef01ff205e6f8824d3a89ef97dbc5dee84cf Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 28 Jan 2017 21:05:07 +0100 Subject: [PATCH 12/22] #28 Using TBMCPlayer instead of YamlConfig --- .../lib/player/ChromaGamerBase.java | 2 +- .../lib/player/TBMCPlayerBase.java | 27 +++++++------------ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 24a1bcb..fab002f 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -84,7 +84,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { try { save_(); } catch (Exception e) { - TBMCCoreAPI.SendException("Error while saving player data!", e); + TBMCCoreAPI.SendException("Error while saving player to " + getFolder() + "/" + getFileName() + ".yml!", e); } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index a910020..2bc6420 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -1,6 +1,5 @@ package buttondevteam.lib.player; -import java.io.File; import java.util.Iterator; import java.util.Map.Entry; import java.util.UUID; @@ -8,7 +7,6 @@ import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import com.palmergames.bukkit.towny.Towny; @@ -66,10 +64,10 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { return (T) playermap.get(uuid + "-" + cl.getSimpleName()); try { T player; - if (playerfiles.containsKey(uuid)) { + if (playermap.containsKey(uuid + "-" + TBMCPlayer.class.getSimpleName())) { player = cl.newInstance(); - player.plugindata = playerfiles.get(uuid); - playermap.put(player.uuid + "-" + player.getFolder(), player); // It will get removed on player quit + player.plugindata = playermap.get(uuid + "-" + TBMCPlayer.class.getSimpleName()).plugindata; + playermap.put(player.uuid + "-" + cl.getSimpleName(), player); // It will get removed on player quit } else player = ChromaGamerBase.getUser(uuid.toString(), cl); player.uuid = uuid; @@ -86,8 +84,6 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { */ static final ConcurrentHashMap playermap = new ConcurrentHashMap<>(); - private static final ConcurrentHashMap playerfiles = new ConcurrentHashMap<>(); - /** * Gets the TBMCPlayer object as a specific plugin player, keeping it's data
* Make sure to use try-with-resources with this to save the data, as it may need to load the file @@ -124,11 +120,13 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName()); TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); Resident resident = tu.getResidentMap().get(player.getPlayerName()); - if (resident == null) + if (resident == null) { Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); - else if (tu.getResidentMap().contains(p.getName())) + TBMCCoreAPI.sendDebugMessage("Resident not found - couldn't rename in Towny."); + } else if (tu.getResidentMap().contains(p.getName())) { Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle - else + TBMCCoreAPI.sendDebugMessage("Target resident name is already in use."); + } else try { TownyUniverse.getDataSource().renamePlayer(resident, p.getName()); } catch (AlreadyRegisteredException e) { @@ -158,13 +156,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); } } - final YamlConfiguration playerfile = playerfiles.get(p.getUniqueId()); - try { // Only save to file once, not for each plugin - playerfile.save(p.getUniqueId().toString() + ".yml"); // TODO: Bring this together with the close() method, like a common method or something - } catch (Exception e) { - TBMCCoreAPI.SendException("Error while saving quitting player " + playerfile.getString("playername") + " (" - + "minecraft/" + p.getUniqueId() + ".yml)!", e); - } + final TBMCPlayerBase player = playermap.get(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName()); + player.save(); } public static void savePlayers() { -- 2.30.2 From ef42d811b0061f5ff356ce62f1b20593dad587d6 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 28 Jan 2017 23:53:08 +0100 Subject: [PATCH 13/22] A few important fixes --- .../java/buttondevteam/core/CommandCaller.java | 2 -- .../java/buttondevteam/core/PlayerListener.java | 17 +++++------------ .../lib/player/ChromaGamerBase.java | 2 +- .../lib/player/TBMCPlayerBase.java | 8 +------- 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/src/main/java/buttondevteam/core/CommandCaller.java b/src/main/java/buttondevteam/core/CommandCaller.java index 29ce030..cb1b050 100644 --- a/src/main/java/buttondevteam/core/CommandCaller.java +++ b/src/main/java/buttondevteam/core/CommandCaller.java @@ -1,11 +1,9 @@ package buttondevteam.core; import java.util.Arrays; -import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; diff --git a/src/main/java/buttondevteam/core/PlayerListener.java b/src/main/java/buttondevteam/core/PlayerListener.java index e6fd5c9..0a2ecb3 100644 --- a/src/main/java/buttondevteam/core/PlayerListener.java +++ b/src/main/java/buttondevteam/core/PlayerListener.java @@ -6,24 +6,17 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.player.TBMCPlayerBase; public class PlayerListener implements Listener { - /*@EventHandler(priority = EventPriority.HIGHEST) + @EventHandler(priority = EventPriority.HIGHEST) public void OnPlayerJoin(PlayerJoinEvent event) { - TBMCPlayer player = TBMCPlayer.loadPlayer(event.getPlayer()); - if (player == null) { - TBMCCoreAPI.SendException("Error on player join!", new Exception("Player is null")); - event.getPlayer().sendMessage("§c[TBMC] Failed to load player data! The error has been sent to the devs."); - } else - TBMCPlayer.joinPlayer(player); + TBMCPlayerBase.joinPlayer(event.getPlayer()); } @EventHandler(priority = EventPriority.HIGHEST) public void OnPlayerLeave(PlayerQuitEvent event) { - TBMCPlayer player = TBMCPlayer.getPlayer(event.getPlayer()); - TBMCPlayer.savePlayer(player); - TBMCPlayer.quitPlayer(player); - }*/ + TBMCPlayerBase.quitPlayer(event.getPlayer()); + } } diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index fab002f..79037f7 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -60,7 +60,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { try { T obj = cl.newInstance(); final File file = new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname + ".yml"); - file.mkdirs(); + file.getParentFile().mkdirs(); obj.plugindata = YamlConfiguration.loadConfiguration(file); return obj; } catch (Exception e) { diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 2bc6420..c24c1fd 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -38,13 +38,6 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { return getUUID().toString(); } - // protected ConfigurationSection plugindata; - - /* - * public static void load() { super.load(); plugindata = super.plugindata.getConfigurationSection(getPluginName()); if (plugindata == null) plugindata = - * super.plugindata.createSection(getPluginName()); } protected void save() { plugindata = super.plugindata.createSection(getPluginName(), plugindata.getValues(true)); super.save(); } - */ - static { addPlayerType(TBMCPlayerBase.class, FOLDER_NAME); } @@ -137,6 +130,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { player.setPlayerName(p.getName()); Bukkit.getLogger().info("Renaming done."); } + playermap.put(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName(), player); // Load in other plugins Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(player)); -- 2.30.2 From 11f13af8cc8ef27ed523555c4c48e381419a0761 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 29 Jan 2017 01:48:53 +0100 Subject: [PATCH 14/22] Player data fixes --- .../lib/player/ChromaGamerBase.java | 31 ++++++++++++++++++- .../buttondevteam/lib/player/TBMCPlayer.java | 2 +- .../lib/player/TBMCPlayerBase.java | 17 ---------- .../lib/player/TBMCPlayerGetInfoEvent.java | 8 ++--- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 79037f7..2239694 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.Map.Entry; import java.util.function.Consumer; +import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import buttondevteam.lib.TBMCCoreAPI; @@ -56,7 +57,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { return plugindata != null ? plugindata.getString("id") : null; } - protected static T getUser(String fname, Class cl) { + public static T getUser(String fname, Class cl) { try { T obj = cl.newInstance(); final File file = new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname + ".yml"); @@ -126,6 +127,17 @@ public abstract class ChromaGamerBase implements AutoCloseable { sync.accept(user.plugindata); } + /** + * Retunrs the ID for the T typed player object connected with this one or null if no connection found. + * + * @param cl + * The player class to get the ID from + * @return The ID or null if not found + */ + public String getConnectedID(Class cl) { + return plugindata.getString(getFolderForType(cl) + "_id"); + } + /** * Returns this player as a plugin player. This will return a new instance unless the player is online.
* Make sure to close both the returned and this object. A try-with-resources block or two can help.
@@ -146,4 +158,21 @@ public abstract class ChromaGamerBase implements AutoCloseable { public String getFolder() { return getFolderForType(getClass()); } + + /** + * Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins. + * + * @param target + * The {@link InfoTarget} to return the info for. + * @return The player information. + */ + public String getInfo(InfoTarget target) { + TBMCPlayerGetInfoEvent event = new TBMCPlayerGetInfoEvent(this, target); + Bukkit.getServer().getPluginManager().callEvent(event); + return event.getResult(); + } + + public enum InfoTarget { + MCHover, MCCommand, Discord + } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayer.java b/src/main/java/buttondevteam/lib/player/TBMCPlayer.java index badbe8e..617af5e 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayer.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayer.java @@ -1,6 +1,6 @@ package buttondevteam.lib.player; @PlayerClass(pluginname = "ButtonCore") -public class TBMCPlayer extends TBMCPlayerBase { +public final class TBMCPlayer extends TBMCPlayerBase { } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index c24c1fd..954b3e0 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -189,21 +189,4 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { else return null; } - - /** - * Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins. - * - * @param target - * The {@link InfoTarget} to return the info for. - * @return The player information. - */ - public String getInfo(InfoTarget target) { - TBMCPlayerGetInfoEvent event = new TBMCPlayerGetInfoEvent(this, target); - Bukkit.getServer().getPluginManager().callEvent(event); - return event.getResult(); - } - - public enum InfoTarget { - MCHover, MCCommand, Discord - } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java index ba2bc74..6e310a0 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerGetInfoEvent.java @@ -7,7 +7,7 @@ import java.util.stream.Collectors; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; -import buttondevteam.lib.player.TBMCPlayerBase.InfoTarget; +import buttondevteam.lib.player.ChromaGamerBase.InfoTarget; /** *

@@ -20,11 +20,11 @@ import buttondevteam.lib.player.TBMCPlayerBase.InfoTarget; public class TBMCPlayerGetInfoEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private TBMCPlayerBase player; + private ChromaGamerBase player; private List infolines; private InfoTarget target; - TBMCPlayerGetInfoEvent(TBMCPlayerBase player, InfoTarget target) { + TBMCPlayerGetInfoEvent(ChromaGamerBase player, InfoTarget target) { this.player = player; infolines = new ArrayList<>(); this.target = target; @@ -35,7 +35,7 @@ public class TBMCPlayerGetInfoEvent extends Event { * * @return A player object */ - public TBMCPlayerBase getPlayer() { + public ChromaGamerBase getPlayer() { return player; } -- 2.30.2 From 153425be5ee3018961988ce7e2e44ac307bf646a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 1 Apr 2017 14:40:54 +0200 Subject: [PATCH 15/22] Probably done with player data (#28) - Figured out and finished the annotation - Grouped data by plugin names - Player data is saved as an object automatically determining the type --- .../java/buttondevteam/core/MainPlugin.java | 1 + .../java/buttondevteam/lib/TBMCCoreAPI.java | 8 ++- .../lib/player/ChromaGamerBase.java | 67 +++++++++++++++---- .../buttondevteam/lib/player/PlayerClass.java | 9 +++ .../buttondevteam/lib/player/PlayerData.java | 22 ++++++ .../lib/player/TBMCPlayerBase.java | 50 ++++++++------ .../buttondevteam/lib/player/UserClass.java | 21 ++++++ 7 files changed, 142 insertions(+), 36 deletions(-) create mode 100644 src/main/java/buttondevteam/lib/player/PlayerData.java create mode 100644 src/main/java/buttondevteam/lib/player/UserClass.java diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index c09b698..1afd61e 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -31,6 +31,7 @@ public class MainPlugin extends JavaPlugin { TBMCChatAPI.AddCommand(this, UpdatePluginCommand.class); TBMCChatAPI.AddCommand(this, ScheduledRestartCommand.class); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); + TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ")."); } diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index dcb7d60..62c0e37 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -18,6 +18,7 @@ import org.bukkit.plugin.Plugin; import com.google.gson.*; import buttondevteam.core.MainPlugin; +import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.potato.DebugPotato; public final class TBMCCoreAPI { @@ -83,8 +84,7 @@ public final class TBMCCoreAPI { info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get()); URL url; final boolean isWindows = System.getProperty("os.name").contains("Windows"); - File result = new File( - "plugins/" + correctname + (isWindows ? ".jar" : ".jar_tmp")); + File result = new File("plugins/" + correctname + (isWindows ? ".jar" : ".jar_tmp")); File finalresult = new File("plugins/" + correctname + ".jar"); try { url = new URL("https://jitpack.io/com/github/TBMCPlugins/" @@ -250,6 +250,10 @@ public final class TBMCCoreAPI { EventExceptionHandler.registerEvents(listener, plugin, new EventExceptionCoreHandler()); } + public static void RegisterUserClass(Class userclass) { + ChromaGamerBase.RegisterPluginUserClass(userclass); + } + /** * Send exceptions that haven't been sent (their events didn't get handled). This method is used by the DiscordPlugin's ready event */ diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 2239694..aa64747 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -8,7 +8,6 @@ import java.util.function.Consumer; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; - import buttondevteam.lib.TBMCCoreAPI; public abstract class ChromaGamerBase implements AutoCloseable { @@ -17,29 +16,27 @@ public abstract class ChromaGamerBase implements AutoCloseable { private static final HashMap, String> playerTypes = new HashMap<>(); /** - * Use only if outside Minecraft, and use it to register your plugin's class. - * - * @param cl - * The custom player class - * @param folder - * The folder to store the data in (like "discord") + * Used for connecting with every type of user ({@link #connectWith(ChromaGamerBase)}) */ - public static void addPlayerType(Class cl, String folder) { - playerTypes.put(cl, folder); + public static void RegisterPluginUserClass(Class userclass) { + if (userclass.isAnnotationPresent(UserClass.class)) + playerTypes.put(userclass, userclass.getAnnotation(UserClass.class).foldername()); + throw new RuntimeException("Class not registered as a user class! Use @UserClass or TBMCPlayerBase"); } /** - * Returns the folder name for the given player class. If a direct match is not found, it'll look for superclasses. + * Returns the folder name for the given player class. * * @param cl * The class to get the folder from (like {@link TBMCPlayerBase} or one of it's subclasses * @return The folder name for the given type + * @throws RuntimeException + * If the class doesn't have the {@link UserClass} annotation. */ public static String getFolderForType(Class cl) { - if (playerTypes.containsKey(cl)) - return playerTypes.get(cl); - return playerTypes.entrySet().stream().filter(e -> e.getKey().isAssignableFrom(cl)).findAny() - .orElseThrow(() -> new RuntimeException("Type not registered as a player type!")).getValue(); + if (cl.isAnnotationPresent(UserClass.class)) + return cl.getAnnotation(UserClass.class).foldername(); + throw new RuntimeException("Class not registered as a user class! Use @UserClass"); } /** @@ -57,6 +54,13 @@ public abstract class ChromaGamerBase implements AutoCloseable { return plugindata != null ? plugindata.getString("id") : null; } + /*** + * Loads a user from disk and returns the user object. Make sure to use the subclasses' methods, where possible, like {@link TBMCPlayerBase#getPlayer(java.util.UUID, Class)} + * + * @param fname + * @param cl + * @return + */ public static T getUser(String fname, Class cl) { try { T obj = cl.newInstance(); @@ -101,6 +105,8 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ public void connectWith(T user) { // Set the ID, go through all linked files and connect them as well + if (!playerTypes.containsKey(getClass())) + throw new RuntimeException("Class not registered as a user class! Use TBMCCoreAPI.RegisterUserClass"); plugindata.set(user.getFolder() + "_id", user.plugindata.getString("id")); final String ownFolder = user.getFolder(); user.plugindata.set(ownFolder + "_id", plugindata.getString("id")); @@ -159,6 +165,39 @@ public abstract class ChromaGamerBase implements AutoCloseable { return getFolderForType(getClass()); } + @SuppressWarnings("rawtypes") + private HashMap datamap = 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 PlayerData 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(); + if (!datamap.containsKey(mname)) + datamap.put(mname, new PlayerData(mname, plugindata)); + return datamap.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 PlayerData 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(); + if (!datamap.containsKey(mname)) + datamap.put(mname, new PlayerData(mname, plugindata)); + return datamap.get(mname); + } + /** * Get player information. This method calls the {@link TBMCPlayerGetInfoEvent} to get all the player information across the TBMC plugins. * diff --git a/src/main/java/buttondevteam/lib/player/PlayerClass.java b/src/main/java/buttondevteam/lib/player/PlayerClass.java index a543849..4c22784 100644 --- a/src/main/java/buttondevteam/lib/player/PlayerClass.java +++ b/src/main/java/buttondevteam/lib/player/PlayerClass.java @@ -5,8 +5,17 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * Specifies a {@link TBMCPlayerBase} direct subclass. For Minecraft data, use {@link UserClass} + * + * @author NorbiPeti + * + */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface PlayerClass { + /** + * Indicates the plugin's name which this player class belongs to. Used to create a section for each plugin. + */ String pluginname(); } diff --git a/src/main/java/buttondevteam/lib/player/PlayerData.java b/src/main/java/buttondevteam/lib/player/PlayerData.java new file mode 100644 index 0000000..f729907 --- /dev/null +++ b/src/main/java/buttondevteam/lib/player/PlayerData.java @@ -0,0 +1,22 @@ +package buttondevteam.lib.player; + +import org.bukkit.configuration.file.YamlConfiguration; + +public class PlayerData { + private String name; + private YamlConfiguration yaml; + + public PlayerData(String name, YamlConfiguration yaml) { + this.name = name; + this.yaml = yaml; + } + + @SuppressWarnings("unchecked") + public T get() { + return (T) yaml.get(name); + } + + public void set(String value) { + yaml.set(name, value); + } +} diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 954b3e0..87382a5 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -17,20 +17,24 @@ import com.palmergames.bukkit.towny.object.TownyUniverse; import buttondevteam.lib.TBMCCoreAPI; +@UserClass(foldername = "minecraft") public abstract class TBMCPlayerBase extends ChromaGamerBase { - private static final String FOLDER_NAME = "minecraft"; protected UUID uuid; + private String pluginname; + + protected TBMCPlayerBase() { + if (getClass().isAnnotationPresent(PlayerClass.class)) + pluginname = getClass().getAnnotation(PlayerClass.class).pluginname(); + throw new RuntimeException("Class not defined as player class! Use @PlayerClass"); + } + public UUID getUUID() { return uuid; } - public String getPlayerName() { - return plugindata.getString("playername", ""); - } - - public void setPlayerName(String value) { - plugindata.set("playername", value); + public PlayerData PlayerName() { + return data(); } @Override @@ -38,8 +42,14 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { return getUUID().toString(); } - static { - addPlayerType(TBMCPlayerBase.class, FOLDER_NAME); + + /** + * 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 PlayerData data() { + return super.data(pluginname); } /** @@ -96,7 +106,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { try { player.close(); } catch (Exception e) { - new Exception("Failed to save player data for " + player.getPlayerName(), e).printStackTrace(); + new Exception("Failed to save player data for " + player.PlayerName().get(), e).printStackTrace(); } } @@ -105,14 +115,14 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { */ public static void joinPlayer(Player p) { TBMCPlayer player = TBMCPlayerBase.getPlayer(p.getUniqueId(), TBMCPlayer.class); - Bukkit.getLogger().info("Loaded player: " + player.getPlayerName()); - if (player.getPlayerName() == null) { - player.setPlayerName(p.getName()); - Bukkit.getLogger().info("Player name saved: " + player.getPlayerName()); - } else if (!p.getName().equals(player.getPlayerName())) { - Bukkit.getLogger().info("Renaming " + player.getPlayerName() + " to " + p.getName()); + Bukkit.getLogger().info("Loaded player: " + player.PlayerName().get()); + if (player.PlayerName().get() == null) { + player.PlayerName().set(p.getName()); + Bukkit.getLogger().info("Player name saved: " + player.PlayerName().get()); + } else if (!p.getName().equals(player.PlayerName().get())) { + Bukkit.getLogger().info("Renaming " + player.PlayerName().get() + " to " + p.getName()); TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); - Resident resident = tu.getResidentMap().get(player.getPlayerName()); + Resident resident = tu.getResidentMap().get(player.PlayerName().get()); if (resident == null) { Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); TBMCCoreAPI.sendDebugMessage("Resident not found - couldn't rename in Towny."); @@ -127,7 +137,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { } catch (NotRegisteredException e) { TBMCCoreAPI.SendException("Failed to rename resident, the resident isn't registered.", e); } - player.setPlayerName(p.getName()); + player.PlayerName().set(p.getName()); Bukkit.getLogger().info("Renaming done."); } playermap.put(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName(), player); @@ -159,8 +169,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { try { p.close(); } catch (Exception e) { - TBMCCoreAPI.SendException("Error while saving player " + p.getPlayerName() + " (" + p.getFolder() + "/" - + p.getFileName() + ")!", e); + TBMCCoreAPI.SendException("Error while saving player " + p.PlayerName().get() + " (" + p.getFolder() + + "/" + p.getFileName() + ")!", e); } }); } diff --git a/src/main/java/buttondevteam/lib/player/UserClass.java b/src/main/java/buttondevteam/lib/player/UserClass.java new file mode 100644 index 0000000..2f389ac --- /dev/null +++ b/src/main/java/buttondevteam/lib/player/UserClass.java @@ -0,0 +1,21 @@ +package buttondevteam.lib.player; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Specifies a {@link ChromaGamerBase} direct subclass. For Minecraft data, use {@link PlayerClass} + * + * @author NorbiPeti + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface UserClass { + /** + * Indicates which folder should the player files be saved in. + */ + String foldername(); +} -- 2.30.2 From b9738837a15f733010959439a9f028450e2d7d38 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 1 Apr 2017 18:02:24 +0200 Subject: [PATCH 16/22] Green across the board! Finished player data and added a test! (#28) --- pom.xml | 5 ++ .../java/buttondevteam/lib/TBMCCoreAPI.java | 2 +- .../lib/player/ChromaGamerBase.java | 39 +++++++++++- .../lib/player/EnumPlayerData.java | 21 +++++++ .../buttondevteam/lib/player/PlayerData.java | 2 +- .../lib/player/TBMCPlayerBase.java | 13 +++- .../buttondevteam/lib/player/UserClass.java | 2 + .../buttondevteam/core/PlayerDataTest.java | 63 +++++++++++++++++++ .../buttondevteam/core/TestPlayerClass.java | 16 +++++ 9 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 src/main/java/buttondevteam/lib/player/EnumPlayerData.java create mode 100644 src/test/java/buttondevteam/core/PlayerDataTest.java create mode 100644 src/test/java/buttondevteam/core/TestPlayerClass.java diff --git a/pom.xml b/pom.xml index b855faa..8609c02 100644 --- a/pom.xml +++ b/pom.xml @@ -138,6 +138,11 @@ javassist 3.20.0-GA + + org.mockito + mockito-core + 2.7.20 + TBMCPlugins diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index 62c0e37..469095e 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -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 coders = new ArrayList() { private static final long serialVersionUID = -4462159250738367334L; { diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index aa64747..1282ddb 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -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 PlayerData 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(mname, plugindata)); return datamap.get(mname); @@ -192,12 +192,45 @@ public abstract class ChromaGamerBase implements AutoCloseable { protected PlayerData 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(mname, plugindata)); return datamap.get(mname); } + @SuppressWarnings("rawtypes") + private HashMap 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 > EnumPlayerData dataEnum(String sectionname, Class 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(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 > EnumPlayerData dataEnum(Class 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(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. * diff --git a/src/main/java/buttondevteam/lib/player/EnumPlayerData.java b/src/main/java/buttondevteam/lib/player/EnumPlayerData.java new file mode 100644 index 0000000..586e845 --- /dev/null +++ b/src/main/java/buttondevteam/lib/player/EnumPlayerData.java @@ -0,0 +1,21 @@ +package buttondevteam.lib.player; + +import org.bukkit.configuration.file.YamlConfiguration; + +public class EnumPlayerData> { + private PlayerData data; + private Class cl; + + public EnumPlayerData(String name, YamlConfiguration yaml, Class cl) { + data = new PlayerData(name, yaml); + this.cl = cl; + } + + public T get() { + return Enum.valueOf(cl, data.get()); + } + + public void set(T value) { + data.set(value.toString()); + } +} diff --git a/src/main/java/buttondevteam/lib/player/PlayerData.java b/src/main/java/buttondevteam/lib/player/PlayerData.java index f729907..fb5326b 100644 --- a/src/main/java/buttondevteam/lib/player/PlayerData.java +++ b/src/main/java/buttondevteam/lib/player/PlayerData.java @@ -16,7 +16,7 @@ public class PlayerData { return (T) yaml.get(name); } - public void set(String value) { + public void set(T value) { yaml.set(name, value); } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 87382a5..3a174c5 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -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 > EnumPlayerData dataEnum(Class cl) { + return super.dataEnum(pluginname, cl); + } + /** * Get player as a plugin player * diff --git a/src/main/java/buttondevteam/lib/player/UserClass.java b/src/main/java/buttondevteam/lib/player/UserClass.java index 2f389ac..bbe9b59 100644 --- a/src/main/java/buttondevteam/lib/player/UserClass.java +++ b/src/main/java/buttondevteam/lib/player/UserClass.java @@ -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. diff --git a/src/test/java/buttondevteam/core/PlayerDataTest.java b/src/test/java/buttondevteam/core/PlayerDataTest.java new file mode 100644 index 0000000..4abde02 --- /dev/null +++ b/src/test/java/buttondevteam/core/PlayerDataTest.java @@ -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() { + + @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(); + } + } +} diff --git a/src/test/java/buttondevteam/core/TestPlayerClass.java b/src/test/java/buttondevteam/core/TestPlayerClass.java new file mode 100644 index 0000000..42010db --- /dev/null +++ b/src/test/java/buttondevteam/core/TestPlayerClass.java @@ -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() { + return dataEnum(TestEnum.class); + } + + public enum TestEnum { + A, B + } +} -- 2.30.2 From f2931ed2fdf4db52375df1a03204cf93fac074da Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 1 Apr 2017 19:57:43 +0200 Subject: [PATCH 17/22] Fixed a bunch of stuff --- .../buttondevteam/lib/player/ChromaGamerBase.java | 2 +- .../buttondevteam/lib/player/EnumPlayerData.java | 5 +++++ .../java/buttondevteam/lib/player/PlayerData.java | 13 ++++++++++++- .../buttondevteam/lib/player/TBMCPlayerBase.java | 2 ++ .../java/buttondevteam/core/PlayerDataTest.java | 6 ++++++ .../java/buttondevteam/core/TestPlayerClass.java | 9 +++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 1282ddb..91a4b70 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -152,7 +152,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { * The target player class * @return The player as a {@link T} object or null if not having an account there */ - public T getAs(Class cl) { + public T getAs(Class cl) { // TODO: Provide a way to use TBMCPlayerBase's loaded players String newfolder = getFolderForType(cl); if (newfolder == null) throw new RuntimeException("The specified class " + cl.getSimpleName() + " isn't registered!"); diff --git a/src/main/java/buttondevteam/lib/player/EnumPlayerData.java b/src/main/java/buttondevteam/lib/player/EnumPlayerData.java index 586e845..b7b0e5c 100644 --- a/src/main/java/buttondevteam/lib/player/EnumPlayerData.java +++ b/src/main/java/buttondevteam/lib/player/EnumPlayerData.java @@ -18,4 +18,9 @@ public class EnumPlayerData> { public void set(T value) { data.set(value.toString()); } + + public T getOrDefault(T def) { + T value = get(); + return value == null ? def : value; + } } diff --git a/src/main/java/buttondevteam/lib/player/PlayerData.java b/src/main/java/buttondevteam/lib/player/PlayerData.java index fb5326b..05d68ca 100644 --- a/src/main/java/buttondevteam/lib/player/PlayerData.java +++ b/src/main/java/buttondevteam/lib/player/PlayerData.java @@ -13,10 +13,21 @@ public class PlayerData { @SuppressWarnings("unchecked") public T get() { - return (T) yaml.get(name); + Object value = yaml.get(name); + return (T) value; } public void set(T value) { yaml.set(name, value); } + + public T getOrDefault(T def) { + T value = get(); + return value == null ? def : value; + } + + @Override + public String toString() { + return get().toString(); + } } diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 3a174c5..33bcef2 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -48,6 +48,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { * * @return A data object with methods to get and set */ + @Override protected PlayerData data() { return super.data(pluginname); } @@ -57,6 +58,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { * * @return A data object with methods to get and set */ + @Override protected > EnumPlayerData dataEnum(Class cl) { return super.dataEnum(pluginname, cl); } diff --git a/src/test/java/buttondevteam/core/PlayerDataTest.java b/src/test/java/buttondevteam/core/PlayerDataTest.java index 4abde02..87fa5f7 100644 --- a/src/test/java/buttondevteam/core/PlayerDataTest.java +++ b/src/test/java/buttondevteam/core/PlayerDataTest.java @@ -50,12 +50,18 @@ public class PlayerDataTest extends TestCase { assertEquals("Test", p.PlayerName().get()); p.testenum().set(TestEnum.A); // TODO: Fix enum saving assertEquals(TestEnum.A, p.testenum().get()); + // p.TestShort().set((short) 5); + // assertEquals((short) 5, (short) (int) p.TestShort().get()); + p.TestBool().set(true); + assertEquals(true, (boolean) p.TestBool().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()); + // assertEquals((short) 5, (short) p.TestShort().get()); + assertEquals(true, (boolean) p.TestBool().get()); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/buttondevteam/core/TestPlayerClass.java b/src/test/java/buttondevteam/core/TestPlayerClass.java index 42010db..7f51c4d 100644 --- a/src/test/java/buttondevteam/core/TestPlayerClass.java +++ b/src/test/java/buttondevteam/core/TestPlayerClass.java @@ -2,6 +2,7 @@ package buttondevteam.core; import buttondevteam.lib.player.EnumPlayerData; import buttondevteam.lib.player.PlayerClass; +import buttondevteam.lib.player.PlayerData; import buttondevteam.lib.player.TBMCPlayerBase; @PlayerClass(pluginname = "TestPlugin") @@ -13,4 +14,12 @@ public class TestPlayerClass extends TBMCPlayerBase { public enum TestEnum { A, B } + + public PlayerData TestShort() { + return data(); + } + + public PlayerData TestBool() { + return data(); + } } -- 2.30.2 From ced3bd10a4ba5bfbc800ec67702ccd1a1be9869c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 1 Apr 2017 23:59:59 +0200 Subject: [PATCH 18/22] Fixed even more stuff, and facepalmed --- .../lib/player/ChromaGamerBase.java | 13 ++++++++++-- .../lib/player/EnumPlayerData.java | 5 ++++- .../lib/player/TBMCPlayerBase.java | 20 +++++++++++-------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 91a4b70..89cca93 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -21,7 +21,8 @@ public abstract class ChromaGamerBase implements AutoCloseable { public static void RegisterPluginUserClass(Class userclass) { if (userclass.isAnnotationPresent(UserClass.class)) playerTypes.put(userclass, userclass.getAnnotation(UserClass.class).foldername()); - throw new RuntimeException("Class not registered as a user class! Use @UserClass or TBMCPlayerBase"); + else //<-- Really important + throw new RuntimeException("Class not registered as a user class! Use @UserClass or TBMCPlayerBase"); } /** @@ -64,9 +65,11 @@ public abstract class ChromaGamerBase implements AutoCloseable { public static T getUser(String fname, Class cl) { try { T obj = cl.newInstance(); - final File file = new File(TBMC_PLAYERS_DIR + getFolderForType(cl), fname + ".yml"); + final String folder = getFolderForType(cl); + final File file = new File(TBMC_PLAYERS_DIR + folder, fname + ".yml"); file.getParentFile().mkdirs(); obj.plugindata = YamlConfiguration.loadConfiguration(file); + obj.plugindata.set(folder + "_id", obj.getID()); return obj; } catch (Exception e) { TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e); @@ -152,10 +155,15 @@ public abstract class ChromaGamerBase implements AutoCloseable { * The target player class * @return The player as a {@link T} object or null if not having an account there */ + @SuppressWarnings("unchecked") public T getAs(Class cl) { // TODO: Provide a way to use TBMCPlayerBase's loaded players + //System.out.println("getAs cls: " + cl.getSimpleName() + " =? " + getClass().getSimpleName()); // TODO: TMP - Don't be tired when programming + if (cl.getSimpleName().equals(getClass().getSimpleName())) + return (T) this; String newfolder = getFolderForType(cl); if (newfolder == null) throw new RuntimeException("The specified class " + cl.getSimpleName() + " isn't registered!"); + System.out.println("getAs newfolder: " + newfolder); if (!plugindata.contains(newfolder + "_id")) return null; return getUser(plugindata.getString(newfolder + "_id"), cl); @@ -190,6 +198,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ @SuppressWarnings("unchecked") protected PlayerData data() { + //System.out.println("Calling ChromaGamerBase data"); // TODO: TMP - Debugged for hours if (!getClass().isAnnotationPresent(UserClass.class)) throw new RuntimeException("Class not registered as a user class! Use @UserClass"); String mname = new Exception().getStackTrace()[1].getMethodName(); diff --git a/src/main/java/buttondevteam/lib/player/EnumPlayerData.java b/src/main/java/buttondevteam/lib/player/EnumPlayerData.java index b7b0e5c..fcc31af 100644 --- a/src/main/java/buttondevteam/lib/player/EnumPlayerData.java +++ b/src/main/java/buttondevteam/lib/player/EnumPlayerData.java @@ -12,7 +12,10 @@ public class EnumPlayerData> { } public T get() { - return Enum.valueOf(cl, data.get()); + String str = data.get(); + if (str == null || str.equals("")) + return null; + return Enum.valueOf(cl, str); } public void set(T value) { diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 33bcef2..269a41f 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -35,7 +35,8 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { } public PlayerData PlayerName() { - return data(); + //System.out.println("Calling playername"); // TODO: TMP - The data will only get stored if it's changed + return super.data(); } @Override @@ -50,6 +51,7 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { */ @Override protected PlayerData data() { + //System.out.println("Calling TMBCPlayerBase data"); // TODO: TMP - Sigh return super.data(pluginname); } @@ -76,14 +78,17 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { public static T getPlayer(UUID uuid, Class cl) { if (playermap.containsKey(uuid + "-" + cl.getSimpleName())) return (T) playermap.get(uuid + "-" + cl.getSimpleName()); + //System.out.println("A"); try { T player; if (playermap.containsKey(uuid + "-" + TBMCPlayer.class.getSimpleName())) { + //System.out.println("B"); - Don't program when tired player = cl.newInstance(); player.plugindata = playermap.get(uuid + "-" + TBMCPlayer.class.getSimpleName()).plugindata; - playermap.put(player.uuid + "-" + cl.getSimpleName(), player); // It will get removed on player quit + playermap.put(uuid + "-" + cl.getSimpleName(), player); // It will get removed on player quit } else player = ChromaGamerBase.getUser(uuid.toString(), cl); + //System.out.println("C"); player.uuid = uuid; return player; } catch (Exception e) { @@ -163,16 +168,15 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { * Only intended to use from ButtonCore */ public static void quitPlayer(Player p) { + final TBMCPlayerBase player = playermap.get(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName()); + player.save(); + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); Iterator> it = playermap.entrySet().iterator(); while (it.hasNext()) { Entry entry = it.next(); - if (entry.getKey().startsWith(p.getUniqueId().toString())) { // Save every player data - TBMCPlayerBase player = entry.getValue(); // TODO: Separate plugin data by plugin name (annotations?) - Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player)); - } + if (entry.getKey().startsWith(p.getUniqueId().toString())) + it.remove(); } - final TBMCPlayerBase player = playermap.get(p.getUniqueId() + "-" + TBMCPlayer.class.getSimpleName()); - player.save(); } public static void savePlayers() { -- 2.30.2 From 1497a487a41e6cb6c84c6771fc4287370572a3e9 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 11 Apr 2017 21:15:22 +0200 Subject: [PATCH 19/22] Added AbstractUserClass, fixes - Fixed ID problem - Fixed connectWith - Other fixes and improvements --- .../lib/player/AbstractUserClass.java | 28 +++++++++ .../lib/player/ChromaGamerBase.java | 57 +++++++++++-------- .../lib/player/TBMCPlayerBase.java | 17 ++---- .../buttondevteam/lib/player/UserClass.java | 2 +- .../buttondevteam/core/PlayerDataTest.java | 2 +- 5 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 src/main/java/buttondevteam/lib/player/AbstractUserClass.java diff --git a/src/main/java/buttondevteam/lib/player/AbstractUserClass.java b/src/main/java/buttondevteam/lib/player/AbstractUserClass.java new file mode 100644 index 0000000..f6554e1 --- /dev/null +++ b/src/main/java/buttondevteam/lib/player/AbstractUserClass.java @@ -0,0 +1,28 @@ +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; + +/** + * Specifies a {@link ChromaGamerBase} direct subclass which's abstract. For Minecraft data, use {@link PlayerClass} + * + * @author NorbiPeti + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface AbstractUserClass { + /** + * Indicates which folder should the player files be saved in. + */ + String foldername(); + + /** + * Indicates the class to create when connecting accounts. + */ + Class prototype(); +} diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 89cca93..0daf543 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -21,7 +21,10 @@ public abstract class ChromaGamerBase implements AutoCloseable { public static void RegisterPluginUserClass(Class userclass) { if (userclass.isAnnotationPresent(UserClass.class)) playerTypes.put(userclass, userclass.getAnnotation(UserClass.class).foldername()); - else //<-- Really important + else if (userclass.isAnnotationPresent(AbstractUserClass.class)) + playerTypes.put(userclass.getAnnotation(AbstractUserClass.class).prototype(), + userclass.getAnnotation(AbstractUserClass.class).foldername()); + else // <-- Really important throw new RuntimeException("Class not registered as a user class! Use @UserClass or TBMCPlayerBase"); } @@ -37,24 +40,24 @@ public abstract class ChromaGamerBase implements AutoCloseable { public static String getFolderForType(Class cl) { if (cl.isAnnotationPresent(UserClass.class)) return cl.getAnnotation(UserClass.class).foldername(); - throw new RuntimeException("Class not registered as a user class! Use @UserClass"); + else if (cl.isAnnotationPresent(AbstractUserClass.class)) + return cl.getAnnotation(AbstractUserClass.class).foldername(); + throw new RuntimeException("Class not registered as a user class! Use @UserClass or @AbstractUserClass"); } /** - * This method returns the filename for this player data. For example, for Minecraft-related data, use MC UUIDs, for Discord data, use Discord IDs, etc.
+ * This method returns the filename for this player data. For example, for Minecraft-related data, MC UUIDs, for Discord data, use Discord IDs, etc.
* Does not include .yml */ - public abstract String getFileName(); + public final String getFileName() { + return plugindata.getString(getFolder() + "_id"); + } /** * Use {@link #data()} or {@link #data(String)} where possible; the 'id' must be always set */ protected YamlConfiguration plugindata; - public String getID() { - return plugindata != null ? plugindata.getString("id") : null; - } - /*** * Loads a user from disk and returns the user object. Make sure to use the subclasses' methods, where possible, like {@link TBMCPlayerBase#getPlayer(java.util.UUID, Class)} * @@ -69,7 +72,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { final File file = new File(TBMC_PLAYERS_DIR + folder, fname + ".yml"); file.getParentFile().mkdirs(); obj.plugindata = YamlConfiguration.loadConfiguration(file); - obj.plugindata.set(folder + "_id", obj.getID()); + obj.plugindata.set(folder + "_id", fname); return obj; } catch (Exception e) { TBMCCoreAPI.SendException("An error occured while loading a " + cl.getSimpleName() + "!", e); @@ -110,13 +113,16 @@ public abstract class ChromaGamerBase implements AutoCloseable { // Set the ID, go through all linked files and connect them as well if (!playerTypes.containsKey(getClass())) throw new RuntimeException("Class not registered as a user class! Use TBMCCoreAPI.RegisterUserClass"); - plugindata.set(user.getFolder() + "_id", user.plugindata.getString("id")); - final String ownFolder = user.getFolder(); - user.plugindata.set(ownFolder + "_id", plugindata.getString("id")); + final String ownFolder = getFolder(); + user.plugindata.set(ownFolder + "_id", plugindata.getString(ownFolder + "_id")); + final String userFolder = user.getFolder(); + plugindata.set(userFolder + "_id", user.plugindata.getString(userFolder + "_id")); Consumer sync = sourcedata -> { - final String sourcefolder = sourcedata == plugindata ? ownFolder : user.getFolder(); - final String id = sourcedata.getString("id"); + final String sourcefolder = sourcedata == plugindata ? ownFolder : userFolder; + final String id = sourcedata.getString(sourcefolder + "_id"); for (Entry, String> entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones + if (entry.getKey() == getClass() || entry.getKey() == user.getClass()) + continue; final String otherid = sourcedata.getString(entry.getValue() + "_id"); if (otherid == null) continue; @@ -157,13 +163,13 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ @SuppressWarnings("unchecked") public T getAs(Class cl) { // TODO: Provide a way to use TBMCPlayerBase's loaded players - //System.out.println("getAs cls: " + cl.getSimpleName() + " =? " + getClass().getSimpleName()); // TODO: TMP - Don't be tired when programming if (cl.getSimpleName().equals(getClass().getSimpleName())) return (T) this; String newfolder = getFolderForType(cl); if (newfolder == null) throw new RuntimeException("The specified class " + cl.getSimpleName() + " isn't registered!"); - System.out.println("getAs newfolder: " + newfolder); + if (newfolder.equals(getFolder())) // If in the same folder, the same filename is used + return getUser(getFileName(), cl); if (!plugindata.contains(newfolder + "_id")) return null; return getUser(plugindata.getString(newfolder + "_id"), cl); @@ -173,6 +179,12 @@ public abstract class ChromaGamerBase implements AutoCloseable { return getFolderForType(getClass()); } + private void ThrowIfNoUser() { + if (!getClass().isAnnotationPresent(UserClass.class) + && !getClass().isAnnotationPresent(AbstractUserClass.class)) + throw new RuntimeException("Class not registered as a user class! Use @UserClass"); + } + @SuppressWarnings("rawtypes") private HashMap datamap = new HashMap<>(); @@ -183,8 +195,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ @SuppressWarnings("unchecked") protected PlayerData data(String sectionname) { - if (!getClass().isAnnotationPresent(UserClass.class)) - throw new RuntimeException("Class not registered as a user class! Use @UserClass"); + ThrowIfNoUser(); String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName(); if (!datamap.containsKey(mname)) datamap.put(mname, new PlayerData(mname, plugindata)); @@ -198,9 +209,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ @SuppressWarnings("unchecked") protected PlayerData data() { - //System.out.println("Calling ChromaGamerBase data"); // TODO: TMP - Debugged for hours - if (!getClass().isAnnotationPresent(UserClass.class)) - throw new RuntimeException("Class not registered as a user class! Use @UserClass"); + ThrowIfNoUser(); String mname = new Exception().getStackTrace()[1].getMethodName(); if (!datamap.containsKey(mname)) datamap.put(mname, new PlayerData(mname, plugindata)); @@ -217,8 +226,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ @SuppressWarnings("unchecked") protected > EnumPlayerData dataEnum(String sectionname, Class cl) { - if (!getClass().isAnnotationPresent(UserClass.class)) - throw new RuntimeException("Class not registered as a user class! Use @UserClass"); + ThrowIfNoUser(); String mname = sectionname + "." + new Exception().getStackTrace()[2].getMethodName(); if (!dataenummap.containsKey(mname)) dataenummap.put(mname, new EnumPlayerData(mname, plugindata, cl)); @@ -232,8 +240,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ @SuppressWarnings("unchecked") protected > EnumPlayerData dataEnum(Class cl) { - if (!getClass().isAnnotationPresent(UserClass.class)) - throw new RuntimeException("Class not registered as a user class! Use @UserClass"); + ThrowIfNoUser(); String mname = new Exception().getStackTrace()[1].getMethodName(); if (!dataenummap.containsKey(mname)) dataenummap.put(mname, new EnumPlayerData(mname, plugindata, cl)); diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 269a41f..0ff465d 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -17,7 +17,7 @@ import com.palmergames.bukkit.towny.object.TownyUniverse; import buttondevteam.lib.TBMCCoreAPI; -@UserClass(foldername = "minecraft") +@AbstractUserClass(foldername = "minecraft", prototype = TBMCPlayer.class) public abstract class TBMCPlayerBase extends ChromaGamerBase { protected UUID uuid; @@ -31,19 +31,15 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { } public UUID getUUID() { + if (uuid == null) + uuid = UUID.fromString(getFileName()); return uuid; } public PlayerData PlayerName() { - //System.out.println("Calling playername"); // TODO: TMP - The data will only get stored if it's changed return super.data(); } - @Override - public String getFileName() { - 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" * @@ -51,7 +47,6 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { */ @Override protected PlayerData data() { - //System.out.println("Calling TMBCPlayerBase data"); // TODO: TMP - Sigh return super.data(pluginname); } @@ -78,17 +73,17 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { public static T getPlayer(UUID uuid, Class cl) { if (playermap.containsKey(uuid + "-" + cl.getSimpleName())) return (T) playermap.get(uuid + "-" + cl.getSimpleName()); - //System.out.println("A"); + // System.out.println("A"); try { T player; if (playermap.containsKey(uuid + "-" + TBMCPlayer.class.getSimpleName())) { - //System.out.println("B"); - Don't program when tired + // System.out.println("B"); - Don't program when tired player = cl.newInstance(); player.plugindata = playermap.get(uuid + "-" + TBMCPlayer.class.getSimpleName()).plugindata; playermap.put(uuid + "-" + cl.getSimpleName(), player); // It will get removed on player quit } else player = ChromaGamerBase.getUser(uuid.toString(), cl); - //System.out.println("C"); + // System.out.println("C"); player.uuid = uuid; return player; } catch (Exception e) { diff --git a/src/main/java/buttondevteam/lib/player/UserClass.java b/src/main/java/buttondevteam/lib/player/UserClass.java index bbe9b59..cae2326 100644 --- a/src/main/java/buttondevteam/lib/player/UserClass.java +++ b/src/main/java/buttondevteam/lib/player/UserClass.java @@ -7,7 +7,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Specifies a {@link ChromaGamerBase} direct subclass. For Minecraft data, use {@link PlayerClass} + * Specifies a {@link ChromaGamerBase} direct subclass which can be instantiated. For Minecraft data, use {@link PlayerClass} * * @author NorbiPeti * diff --git a/src/test/java/buttondevteam/core/PlayerDataTest.java b/src/test/java/buttondevteam/core/PlayerDataTest.java index 87fa5f7..a512e82 100644 --- a/src/test/java/buttondevteam/core/PlayerDataTest.java +++ b/src/test/java/buttondevteam/core/PlayerDataTest.java @@ -48,7 +48,7 @@ public class PlayerDataTest extends TestCase { 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 + p.testenum().set(TestEnum.A); assertEquals(TestEnum.A, p.testenum().get()); // p.TestShort().set((short) 5); // assertEquals((short) 5, (short) (int) p.TestShort().get()); -- 2.30.2 From 7c8a47e4b280eb24a230d9387ffb2196445c4dae Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 12 Apr 2017 17:47:17 +0200 Subject: [PATCH 20/22] Added TBMMC dir to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 77d9874..1917a1a 100644 --- a/.gitignore +++ b/.gitignore @@ -222,3 +222,5 @@ TheButtonAutoFlair/out/artifacts/Autoflair/Autoflair.jar *.name .idea/compiler.xml *.xml + +TBMC/ -- 2.30.2 From 0160b4146febcfcf929eee8e5e873709251620a3 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 13 Apr 2017 00:50:05 +0200 Subject: [PATCH 21/22] Added player data converter and fixes --- .../java/buttondevteam/core/MainPlugin.java | 49 +++++++++++++++++++ .../lib/player/ChromaGamerBase.java | 12 ++--- .../lib/player/TBMCPlayerBase.java | 8 +++ 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index 1afd61e..9d48e81 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -1,13 +1,22 @@ package buttondevteam.core; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; import java.util.logging.Logger; +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.TBMCPlayerBase; import net.milkbowl.vault.permission.Permission; @@ -18,6 +27,7 @@ public class MainPlugin extends JavaPlugin { private PluginDescriptionFile pdfFile; private Logger logger; + private int C = 0, keep = 0; @Override public void onEnable() { @@ -33,6 +43,45 @@ public class MainPlugin extends JavaPlugin { TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ")."); + Arrays.stream(new File(ChromaGamerBase.TBMC_PLAYERS_DIR).listFiles(f -> !f.isDirectory())).map(f -> { + YamlConfiguration yc = new YamlConfiguration(); + try { + yc.load(f); + } catch (IOException | InvalidConfigurationException e) { + TBMCCoreAPI.SendException("Error while converting player data!", e); + } + f.delete(); + return yc; + }).forEach(yc -> { + try { + int flairtime = yc.getInt("flairtime"), fcount = yc.getInt("fcount"), fdeaths = yc.getInt("fdeaths"); + String flairstate = yc.getString("flairstate"); + List usernames = yc.getStringList("usernames"); + boolean flaircheater = yc.getBoolean("flaircheater"); + final String uuid = yc.getString("uuid"); + C++; + if ((fcount == 0 || fdeaths == 0) + && (flairstate == null || "NoComment".equals(flairstate) || flairtime <= 0)) + return; // Those who received no Fs yet will also get their stats reset if no flair + final File file = new File(ChromaGamerBase.TBMC_PLAYERS_DIR + "minecraft", uuid + ".yml"); + YamlConfiguration targetyc = YamlConfiguration.loadConfiguration(file); + targetyc.set("PlayerName", yc.getString("playername")); + targetyc.set("minecraft_id", uuid); + ConfigurationSection bc = targetyc.createSection("ButtonChat"); + bc.set("FlairTime", "NoComment".equals(flairstate) ? -3 : flairtime); // FlairTimeNone: -3 + bc.set("FCount", fcount); + bc.set("FDeaths", fdeaths); + bc.set("FlairState", flairstate); + bc.set("UserNames", usernames); + bc.set("FlairCheater", flaircheater); + targetyc.save(file); + keep++; + } catch (Exception e) { + TBMCCoreAPI.SendException("Error while converting player data!", e); + } + }); + Bukkit.getScheduler().runTask(this, () -> logger.info("Converted " + keep + " player data from " + C)); + //TODO: Remove once ran it at least once } @Override diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 0daf543..944b676 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -1,7 +1,6 @@ package buttondevteam.lib.player; import java.io.File; -import java.io.IOException; import java.util.HashMap; import java.util.Map.Entry; import java.util.function.Consumer; @@ -85,24 +84,21 @@ public abstract class ChromaGamerBase implements AutoCloseable { */ @Override public void close() throws Exception { - save_(); + if (plugindata.getKeys(false).size() > 0) + plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName() + ".yml")); } /** - * Saves the player. It'll send all exceptions that may happen. To catch the exception, use {@link #close()} instead. + * Saves the player. It'll handle all exceptions that may happen. To catch the exception, use {@link #close()} instead. */ public void save() { try { - save_(); + close(); } catch (Exception e) { TBMCCoreAPI.SendException("Error while saving player to " + getFolder() + "/" + getFileName() + ".yml!", e); } } - private void save_() throws IOException { - plugindata.save(new File(TBMC_PLAYERS_DIR + getFolder(), getFileName() + ".yml")); - } - /** * Connect two accounts. Do not use for connecting two Minecraft accounts or similar. Also make sure you have the "id" tag set * diff --git a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java index 0ff465d..e4f38c2 100644 --- a/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java +++ b/src/main/java/buttondevteam/lib/player/TBMCPlayerBase.java @@ -2,6 +2,7 @@ package buttondevteam.lib.player; import java.util.Iterator; import java.util.Map.Entry; +import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -209,4 +210,11 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase { else return null; } + + @Override + public void close() throws Exception { + Set keys = plugindata.getKeys(false); + if (keys.size() > 1) // PlayerName is always saved, but we don't need a file for just that + super.close(); + } } -- 2.30.2 From 3bab090fc7741c0c172a78b5ef09d14d4ebcef25 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 14 Apr 2017 22:40:25 +0200 Subject: [PATCH 22/22] Sharing test preparation --- .../java/buttondevteam/core/TestPrepare.java | 38 +++++++++++++++++++ .../buttondevteam/core/PlayerDataTest.java | 25 +----------- 2 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 src/main/java/buttondevteam/core/TestPrepare.java diff --git a/src/main/java/buttondevteam/core/TestPrepare.java b/src/main/java/buttondevteam/core/TestPrepare.java new file mode 100644 index 0000000..e135a6a --- /dev/null +++ b/src/main/java/buttondevteam/core/TestPrepare.java @@ -0,0 +1,38 @@ +package buttondevteam.core; + +import java.util.Collection; +import java.util.Collections; +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; + +public class TestPrepare { + public static void PrepareServer() { + Bukkit.setServer(Mockito.mock(Server.class, new Answer() { + + @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 (returns(invocation, String.class)) + return "test"; + if (returns(invocation, Logger.class)) + return Logger.getAnonymousLogger(); + if (returns(invocation, PluginManager.class)) + return Mockito.mock(PluginManager.class); + if (returns(invocation, Collection.class)) + return Collections.EMPTY_LIST; + return null; + } + + boolean returns(InvocationOnMock invocation, Class cl) { + return cl.isAssignableFrom(invocation.getMethod().getReturnType()); + } + })); + } +} diff --git a/src/test/java/buttondevteam/core/PlayerDataTest.java b/src/test/java/buttondevteam/core/PlayerDataTest.java index a512e82..7bf9ed0 100644 --- a/src/test/java/buttondevteam/core/PlayerDataTest.java +++ b/src/test/java/buttondevteam/core/PlayerDataTest.java @@ -1,15 +1,6 @@ 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; @@ -29,21 +20,7 @@ public class PlayerDataTest extends TestCase { } public void testConfig() { - Bukkit.setServer(Mockito.mock(Server.class, new Answer() { - - @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; - } - })); + TestPrepare.PrepareServer(); UUID uuid = new UUID(0L, 0L); try (TestPlayerClass p = TBMCPlayerBase.getPlayer(uuid, TestPlayerClass.class)) { p.PlayerName().set("Test"); -- 2.30.2