Player data moving is in progress...

This commit is contained in:
Norbi Peti 2016-07-05 00:20:39 +02:00
parent 7195ed1338
commit e9fc7457e3
7 changed files with 152 additions and 7 deletions

View file

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

View file

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

View file

@ -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!");
}
}

View file

@ -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<UUID, TBMCPlayer> AllPlayers = new HashMap<>();
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)) {
@ -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;
}
}

View file

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

View file

@ -0,0 +1,31 @@
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

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