From 4ebb526d8770b61e4d5ffbcdd223b18f011b4f7b Mon Sep 17 00:00:00 2001
From: NorbiPeti
- * Listen to the load and save events from this package to load and save plugin-specific data + * Use {@link #asPluginPlayer(Class)} to get plugin-specific data *
* * @author Norbi * */ -public class TBMCPlayer { +public class TBMCPlayer implements AutoCloseable { private static final String TBMC_PLAYERS_DIR = "TBMC/players"; - public String PlayerName; + private HashMap
+ * 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
+ * {@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()[0]; + String mname = st.getMethodName(); + if (!mname.startsWith("set")) + throw new UnsupportedOperationException("Can only use setData from a setXYZ method"); + LoadedPlayers.get(uuid).data.put(mname.substring("set".length()).toLowerCase(), value); + } + + /** + * 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; + } + + /** + * Gets the TBMCPlayer object as a specific plugin player, keeping it's data + * + * @param cl + * @return + */ + public
+ * {@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.OnlinePlayers.containsKey(p.getUniqueId())) - return TBMCPlayer.OnlinePlayers.get(p.getUniqueId()); + public static TBMCPlayer getPlayer(OfflinePlayer p) { + if (TBMCPlayer.LoadedPlayers.containsKey(p.getUniqueId())) + return TBMCPlayer.LoadedPlayers.get(p.getUniqueId()); else - return TBMCPlayer.LoadPlayer(p); + return TBMCPlayer.loadPlayer(p); } - public static TBMCPlayer LoadPlayer(OfflinePlayer p) { - if (OnlinePlayers.containsKey(p.getUniqueId())) - return OnlinePlayers.get(p.getUniqueId()); + /** + * Only intended to use from ButtonCore + */ + public static TBMCPlayer loadPlayer(OfflinePlayer p) { + if (LoadedPlayers.containsKey(p.getUniqueId())) + return LoadedPlayers.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); + return addPlayer(p); else { final YamlConfiguration yc = new YamlConfiguration(); try { @@ -87,25 +183,26 @@ public class TBMCPlayer { return null; } TBMCPlayer player = new TBMCPlayer(); - player.UUID = p.getUniqueId(); - player.PlayerName = yc.getString("playername"); - System.out.println("Player name: " + player.PlayerName); - if (player.PlayerName == null) { - player.PlayerName = p.getName(); - System.out.println("Player name saved: " + player.PlayerName); - } else if (!p.getName().equals(player.PlayerName)) { - System.out.println("Renaming " + player.PlayerName + " to " + p.getName()); + player.uuid = p.getUniqueId(); + player.data.putAll(yc.getValues(true)); + 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.PlayerName); + Resident resident = tu.getResidentMap().get(player.getPlayerName()); if (resident == null) - System.out.println("Resident not found - couldn't rename in Towny."); + Bukkit.getLogger().warning("Resident not found - couldn't rename in Towny."); else if (tu.getResidentMap().contains(p.getName())) - System.out.println("Target resident name is already in use."); // TODO: Handle + Bukkit.getLogger().warning("Target resident name is already in use."); // TODO: Handle else resident.setName(p.getName()); - player.PlayerName = p.getName(); - System.out.println("Renaming done."); + player.setPlayerName(p.getName()); + Bukkit.getLogger().info("Renaming done."); } + LoadedPlayers.put(p.getUniqueId(), player); // Load in other plugins Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(yc, player)); @@ -113,38 +210,58 @@ public class TBMCPlayer { } } - static TBMCPlayer AddPlayer(OfflinePlayer p) { + /** + * Only intended to use from ButtonCore + */ + public static TBMCPlayer addPlayer(OfflinePlayer p) { TBMCPlayer player = new TBMCPlayer(); - player.UUID = p.getUniqueId(); - player.PlayerName = p.getName(); - OnlinePlayers.put(p.getUniqueId(), player); + player.uuid = p.getUniqueId(); + player.setPlayerName(p.getName()); + LoadedPlayers.put(p.getUniqueId(), player); Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerAddEvent(player)); - SavePlayer(player); + savePlayer(player); return player; } - public static void SavePlayer(TBMCPlayer player) { + /** + * Only intended to use from ButtonCore + */ + public static void savePlayer(TBMCPlayer player) { YamlConfiguration yc = new YamlConfiguration(); - yc.set("playername", player.PlayerName); + for (Entry