From e9fc7457e33cf74d40024f3d53c61c0765937f66 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 5 Jul 2016 00:20:39 +0200 Subject: [PATCH] Player data moving is in progress... --- src/buttondevteam/MainPlugin.java | 4 ++ src/buttondevteam/player/PlayerListener.java | 17 ++++++ src/buttondevteam/player/PlayerSubPlugin.java | 15 +++++ src/buttondevteam/player/TBMCPlayer.java | 56 +++++++++++++++++-- .../player/TBMCPlayerAddEvent.java | 8 ++- src/buttondevteam/player/TBMCPlayerBase.java | 31 ++++++++++ .../player/TBMCPlayerLoadEvent.java | 28 ++++++++++ 7 files changed, 152 insertions(+), 7 deletions(-) create mode 100644 src/buttondevteam/player/PlayerListener.java create mode 100644 src/buttondevteam/player/PlayerSubPlugin.java create mode 100644 src/buttondevteam/player/TBMCPlayerBase.java create mode 100644 src/buttondevteam/player/TBMCPlayerLoadEvent.java diff --git a/src/buttondevteam/MainPlugin.java b/src/buttondevteam/MainPlugin.java index 3ff1ca0..7f48398 100644 --- a/src/buttondevteam/MainPlugin.java +++ b/src/buttondevteam/MainPlugin.java @@ -6,11 +6,13 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; import buttondevteam.alisolarflare.aliarrow.AliArrowSubPlugin; +import buttondevteam.player.PlayerSubPlugin; public class MainPlugin extends JavaPlugin { private PluginDescriptionFile pdfFile; private Logger logger; private AliArrowSubPlugin aliArrowSubPlugin; + private PlayerSubPlugin playerSubPlugin; public void onEnable(){ //Logs "Plugin Enabled", registers commands @@ -27,6 +29,8 @@ public class MainPlugin extends JavaPlugin { private void registerSubPlugins() { aliArrowSubPlugin = new AliArrowSubPlugin(this); aliArrowSubPlugin.register(); + playerSubPlugin = new PlayerSubPlugin(this); + playerSubPlugin.register(); } private void registerCommands() { // TODO Auto-generated method stub diff --git a/src/buttondevteam/player/PlayerListener.java b/src/buttondevteam/player/PlayerListener.java new file mode 100644 index 0000000..fef533d --- /dev/null +++ b/src/buttondevteam/player/PlayerListener.java @@ -0,0 +1,17 @@ +package buttondevteam.player; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class PlayerListener implements Listener { + + @EventHandler + public void OnPlayerJoin(PlayerJoinEvent event) { + try { + TBMCPlayer.LoadPlayer(event.getPlayer().getUniqueId()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/buttondevteam/player/PlayerSubPlugin.java b/src/buttondevteam/player/PlayerSubPlugin.java new file mode 100644 index 0000000..a3b638e --- /dev/null +++ b/src/buttondevteam/player/PlayerSubPlugin.java @@ -0,0 +1,15 @@ +package buttondevteam.player; + +import org.bukkit.plugin.Plugin; + +public class PlayerSubPlugin { + public static Plugin plugin; + + public PlayerSubPlugin(Plugin plugin) { + this.plugin = plugin; + } + + public void register() { + System.out.println("Players subplugin registered!"); + } +} diff --git a/src/buttondevteam/player/TBMCPlayer.java b/src/buttondevteam/player/TBMCPlayer.java index f48b416..2af937c 100644 --- a/src/buttondevteam/player/TBMCPlayer.java +++ b/src/buttondevteam/player/TBMCPlayer.java @@ -1,5 +1,8 @@ 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; @@ -8,15 +11,19 @@ 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; public UUID UUID; - public static HashMap AllPlayers = new HashMap<>(); + public static HashMap OnlinePlayers = new HashMap<>(); + + public HashMap Settings = new HashMap<>(); public static TBMCPlayer AddPlayerIfNeeded(UUID uuid) { if (!AllPlayers.containsKey(uuid)) { @@ -31,23 +38,28 @@ public class TBMCPlayer { return AllPlayers.get(uuid); } - public static void Load(YamlConfiguration yc) { + 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"))); + TBMCPlayer mp = AddPlayerIfNeeded(java.util.UUID.fromString(cs2.getString("uuid"))); } } - public static void Save(YamlConfiguration yc) { + 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 + * @return The {@link TBMCPlayer} object for the player + */ public static TBMCPlayer GetFromName(String name) { + @SuppressWarnings("deprecation") Player p = Bukkit.getPlayer(name); if (p != null) return AllPlayers.get(p.getUniqueId()); @@ -55,7 +67,39 @@ public class TBMCPlayer { return null; } - public static TBMCPlayer GetFromPlayer(Player p) { + /** + * @param p + * The Player object + * @return The {@link TBMCPlayer} object for the player + */ + public static TBMCPlayer GetPlayer(Player p) { return TBMCPlayer.AllPlayers.get(p.getUniqueId()); } + + static TBMCPlayer LoadPlayer(UUID uuid) throws Exception { + if (OnlinePlayers.containsKey(uuid)) + return OnlinePlayers.get(uuid); + File file = new File("TBMC/players"); + file.mkdirs(); + file = new File("TBMC/players", uuid.toString() + ".yml"); + if (!file.exists()) + return AddPlayer(uuid); + else { + final YamlConfiguration yc = new YamlConfiguration(); + yc.load(file); + + } + } + + static TBMCPlayer AddPlayer(UUID uuid) { + if (OnlinePlayers.containsKey(uuid)) + return OnlinePlayers.get(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; + } } diff --git a/src/buttondevteam/player/TBMCPlayerAddEvent.java b/src/buttondevteam/player/TBMCPlayerAddEvent.java index 9d6e49d..cfcf5c8 100644 --- a/src/buttondevteam/player/TBMCPlayerAddEvent.java +++ b/src/buttondevteam/player/TBMCPlayerAddEvent.java @@ -6,11 +6,17 @@ import org.bukkit.event.HandlerList; public class TBMCPlayerAddEvent extends Event { private static final HandlerList handlers = new HandlerList(); - public TBMCPlayerAddEvent(TBMCPlayer tbmcplayer) { + 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 } + public TBMCPlayer GetPlayer() { + return player; + } + @Override public HandlerList getHandlers() { return handlers; diff --git a/src/buttondevteam/player/TBMCPlayerBase.java b/src/buttondevteam/player/TBMCPlayerBase.java new file mode 100644 index 0000000..1a92e4f --- /dev/null +++ b/src/buttondevteam/player/TBMCPlayerBase.java @@ -0,0 +1,31 @@ +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 new file mode 100644 index 0000000..f68b548 --- /dev/null +++ b/src/buttondevteam/player/TBMCPlayerLoadEvent.java @@ -0,0 +1,28 @@ +package buttondevteam.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 config; + + public TBMCPlayerLoadEvent(YamlConfiguration yc) { + this.config = yc; + } + + public YamlConfiguration GetPlayerConfig() { + return config; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +}