From d6017f1bf688f9f8fccbcc5f4374d0f92c95a946 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 16 Jul 2016 23:48:11 +0200 Subject: [PATCH] Finished LoadPlayer, AddPlayer and SavePlayer methods --- src/buttondevteam/player/TBMCPlayer.java | 64 +++++++------------ .../player/TBMCPlayerAddEvent.java | 8 ++- src/buttondevteam/player/TBMCPlayerBase.java | 31 --------- .../player/TBMCPlayerLoadEvent.java | 15 +++-- .../player/TBMCPlayerSaveEvent.java | 35 ++++++++++ 5 files changed, 75 insertions(+), 78 deletions(-) delete mode 100644 src/buttondevteam/player/TBMCPlayerBase.java create mode 100644 src/buttondevteam/player/TBMCPlayerSaveEvent.java diff --git a/src/buttondevteam/player/TBMCPlayer.java b/src/buttondevteam/player/TBMCPlayer.java index 2af937c..e171bc6 100644 --- a/src/buttondevteam/player/TBMCPlayer.java +++ b/src/buttondevteam/player/TBMCPlayer.java @@ -1,20 +1,13 @@ package buttondevteam.player; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.UUID; import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; public class TBMCPlayer { public String PlayerName; @@ -23,36 +16,6 @@ public class TBMCPlayer { public static HashMap OnlinePlayers = new HashMap<>(); - public HashMap Settings = new HashMap<>(); - - public static TBMCPlayer AddPlayerIfNeeded(UUID uuid) { - if (!AllPlayers.containsKey(uuid)) { - TBMCPlayer player = new TBMCPlayer(); - player.UUID = uuid; - Player p = Bukkit.getPlayer(uuid); - if (p != null) - player.PlayerName = p.getName(); - AllPlayers.put(uuid, player); - return player; - } - return AllPlayers.get(uuid); - } - - public static void Load(YamlConfiguration yc) { //OLD - ConfigurationSection cs = yc.getConfigurationSection("players"); - for (String key : cs.getKeys(false)) { - ConfigurationSection cs2 = cs.getConfigurationSection(key); - TBMCPlayer mp = AddPlayerIfNeeded(java.util.UUID.fromString(cs2.getString("uuid"))); - } - } - - public static void Save(YamlConfiguration yc) { //OLD - ConfigurationSection cs = yc.createSection("players"); - for (TBMCPlayer mp : TBMCPlayer.AllPlayers.values()) { - ConfigurationSection cs2 = cs.createSection(mp.UUID.toString()); - } - } - /** * @param name * The player's name @@ -62,7 +25,7 @@ public class TBMCPlayer { @SuppressWarnings("deprecation") Player p = Bukkit.getPlayer(name); if (p != null) - return AllPlayers.get(p.getUniqueId()); + return GetPlayer(p); // TODO: Put playernames into filenames else return null; } @@ -73,7 +36,7 @@ public class TBMCPlayer { * @return The {@link TBMCPlayer} object for the player */ public static TBMCPlayer GetPlayer(Player p) { - return TBMCPlayer.AllPlayers.get(p.getUniqueId()); + return TBMCPlayer.OnlinePlayers.get(p.getUniqueId()); } static TBMCPlayer LoadPlayer(UUID uuid) throws Exception { @@ -87,7 +50,13 @@ public class TBMCPlayer { else { final YamlConfiguration yc = new YamlConfiguration(); yc.load(file); - + TBMCPlayer player = new TBMCPlayer(); + player.UUID = uuid; + player.PlayerName = yc.getString("playername"); + + // Load in other plugins + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(yc, player)); + return player; } } @@ -99,7 +68,20 @@ public class TBMCPlayer { Player p = Bukkit.getPlayer(uuid); if (p != null) player.PlayerName = p.getName(); - AllPlayers.put(uuid, player); + OnlinePlayers.put(uuid, player); + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerAddEvent(player)); + SavePlayer(player); return player; } + + static void SavePlayer(TBMCPlayer player) { + YamlConfiguration yc = new YamlConfiguration(); + yc.set("playername", player.PlayerName); + Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(yc, player)); + try { + yc.save("tbmcplayers/" + player.UUID + ".yml"); + } catch (IOException e) { + new Exception("Failed to save player data for " + player.PlayerName, e).printStackTrace(); + } + } } diff --git a/src/buttondevteam/player/TBMCPlayerAddEvent.java b/src/buttondevteam/player/TBMCPlayerAddEvent.java index cfcf5c8..86a35f8 100644 --- a/src/buttondevteam/player/TBMCPlayerAddEvent.java +++ b/src/buttondevteam/player/TBMCPlayerAddEvent.java @@ -1,7 +1,11 @@ package buttondevteam.player; +import java.util.UUID; + +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.yaml.snakeyaml.Yaml; public class TBMCPlayerAddEvent extends Event { private static final HandlerList handlers = new HandlerList(); @@ -9,8 +13,8 @@ public class TBMCPlayerAddEvent extends Event { private TBMCPlayer player; public TBMCPlayerAddEvent(TBMCPlayer player) { - // TODO: Separate player configs, figure out how to make one TBMCPlayer - // object have all the other plugin properties + //TODO: Convert player configs + this.player = player; } public TBMCPlayer GetPlayer() { diff --git a/src/buttondevteam/player/TBMCPlayerBase.java b/src/buttondevteam/player/TBMCPlayerBase.java deleted file mode 100644 index 1a92e4f..0000000 --- a/src/buttondevteam/player/TBMCPlayerBase.java +++ /dev/null @@ -1,31 +0,0 @@ -package buttondevteam.player; - -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; - -/** - *

- * Base class for other plugins' player data. - *

- *
    - *
  1. Add extra data fields
  2. - *
  3. Add the extra data to the load/save methods
  4. - *
- * - * @author Norbi - * - */ -public abstract class TBMCPlayerBase { - public abstract void OnPlayerAdd(TBMCPlayerAddEvent event); - - public abstract void OnPlayerLoad(); - - public abstract void OnPlayerSave(); - - public abstract T GetPlayerAs(TBMCPlayer player); - - public T GetPlayerAs(Player player) { - return GetPlayerAs(TBMCPlayer.GetPlayer(player)); - } -} diff --git a/src/buttondevteam/player/TBMCPlayerLoadEvent.java b/src/buttondevteam/player/TBMCPlayerLoadEvent.java index f68b548..9285f1c 100644 --- a/src/buttondevteam/player/TBMCPlayerLoadEvent.java +++ b/src/buttondevteam/player/TBMCPlayerLoadEvent.java @@ -7,14 +7,21 @@ import org.bukkit.event.HandlerList; public class TBMCPlayerLoadEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private YamlConfiguration config; + private YamlConfiguration yaml; + private TBMCPlayer player; - public TBMCPlayerLoadEvent(YamlConfiguration yc) { - this.config = yc; + public TBMCPlayerLoadEvent(YamlConfiguration yaml, TBMCPlayer player) { + //TODO: Convert player configs + this.yaml = yaml; + this.player = player; } public YamlConfiguration GetPlayerConfig() { - return config; + return yaml; + } + + public TBMCPlayer GetPlayer() { + return player; } @Override diff --git a/src/buttondevteam/player/TBMCPlayerSaveEvent.java b/src/buttondevteam/player/TBMCPlayerSaveEvent.java new file mode 100644 index 0000000..ffd2a1c --- /dev/null +++ b/src/buttondevteam/player/TBMCPlayerSaveEvent.java @@ -0,0 +1,35 @@ +package buttondevteam.player; + +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class TBMCPlayerSaveEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private YamlConfiguration yaml; + private TBMCPlayer player; + + public TBMCPlayerSaveEvent(YamlConfiguration yaml, TBMCPlayer player) { + // TODO: Convert player configs + this.yaml = yaml; + this.player = player; + } + + public YamlConfiguration GetYaml() { + return yaml; + } + + public TBMCPlayer GetPlayer() { + return player; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +}