Finished LoadPlayer, AddPlayer and SavePlayer methods

This commit is contained in:
Norbi Peti 2016-07-16 23:48:11 +02:00
parent e9fc7457e3
commit d6017f1bf6
5 changed files with 75 additions and 78 deletions

View file

@ -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<UUID, TBMCPlayer> OnlinePlayers = new HashMap<>();
public HashMap<String, String> 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();
}
}
}

View file

@ -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() {

View file

@ -1,31 +0,0 @@
package buttondevteam.player;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
/**
* <p>
* Base class for other plugins' player data.
* </p>
* <ol>
* <li>Add extra data fields</li>
* <li>Add the extra data to the load/save methods</li>
* </ol>
*
* @author Norbi
*
*/
public abstract class TBMCPlayerBase {
public abstract void OnPlayerAdd(TBMCPlayerAddEvent event);
public abstract void OnPlayerLoad();
public abstract void OnPlayerSave();
public abstract <T extends TBMCPlayerBase> T GetPlayerAs(TBMCPlayer player);
public <T extends TBMCPlayerBase> T GetPlayerAs(Player player) {
return GetPlayerAs(TBMCPlayer.GetPlayer(player));
}
}

View file

@ -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

View file

@ -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;
}
}