From 1a01e9e9048d34080438ae486438b82534872165 Mon Sep 17 00:00:00 2001
From: NorbiPeti
Date: Mon, 22 Aug 2016 16:17:44 +0200
Subject: [PATCH] Fixes
---
src/buttondevteam/core/TBMCPlayer.java | 46 +++++++++++++++----
.../core/TBMCPlayerJoinEvent.java | 27 +++++++++++
.../core/TBMCPlayerQuitEvent.java | 27 +++++++++++
3 files changed, 92 insertions(+), 8 deletions(-)
create mode 100644 src/buttondevteam/core/TBMCPlayerJoinEvent.java
create mode 100644 src/buttondevteam/core/TBMCPlayerQuitEvent.java
diff --git a/src/buttondevteam/core/TBMCPlayer.java b/src/buttondevteam/core/TBMCPlayer.java
index dd0cc47..db3ee4e 100644
--- a/src/buttondevteam/core/TBMCPlayer.java
+++ b/src/buttondevteam/core/TBMCPlayer.java
@@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit;
+import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
@@ -17,8 +18,7 @@ import com.palmergames.bukkit.towny.object.TownyUniverse;
* The class for holding data common to all TBMC plugins
*
*
- * Listen to the load and save events from this package to load and save
- * plugin-specific data
+ * Listen to the load and save events from this package to load and save plugin-specific data
*
*
* @author Norbi
@@ -31,6 +31,18 @@ public class TBMCPlayer {
public UUID UUID;
+ public T AsPluginPlayer(Class cl) {
+ T obj = null;
+ try {
+ obj = cl.newInstance();
+ obj.UUID = UUID;
+ obj.PlayerName = PlayerName;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return obj;
+ }
+
public static HashMap OnlinePlayers = new HashMap<>();
/**
@@ -40,9 +52,9 @@ public class TBMCPlayer {
*/
public static TBMCPlayer GetFromName(String name) {
@SuppressWarnings("deprecation")
- Player p = Bukkit.getPlayer(name);
+ OfflinePlayer p = Bukkit.getOfflinePlayer(name);
if (p != null)
- return GetPlayer(p); // TODO: Put playernames into filenames
+ return GetPlayer(p);
else
return null;
}
@@ -52,11 +64,14 @@ public class TBMCPlayer {
* The Player object
* @return The {@link TBMCPlayer} object for the player
*/
- public static TBMCPlayer GetPlayer(Player p) {
- return TBMCPlayer.OnlinePlayers.get(p.getUniqueId());
+ public static TBMCPlayer GetPlayer(OfflinePlayer p) {
+ if (TBMCPlayer.OnlinePlayers.containsKey(p.getUniqueId()))
+ return TBMCPlayer.OnlinePlayers.get(p.getUniqueId());
+ else
+ return TBMCPlayer.LoadPlayer(p);
}
- static TBMCPlayer LoadPlayer(Player p) {
+ protected static TBMCPlayer LoadPlayer(OfflinePlayer p) {
if (OnlinePlayers.containsKey(p.getUniqueId()))
return OnlinePlayers.get(p.getUniqueId());
File file = new File(TBMC_PLAYERS_DIR);
@@ -75,6 +90,7 @@ public class TBMCPlayer {
TBMCPlayer player = new TBMCPlayer();
player.UUID = p.getUniqueId();
player.PlayerName = yc.getString("playername");
+ System.out.println("Player name: " + player.PlayerName);
if (!p.getName().equals(player.PlayerName)) {
System.out.println("Renaming " + player.PlayerName + " to " + p.getName());
TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse();
@@ -88,7 +104,7 @@ public class TBMCPlayer {
}
}
- static TBMCPlayer AddPlayer(Player p) {
+ static TBMCPlayer AddPlayer(OfflinePlayer p) {
TBMCPlayer player = new TBMCPlayer();
player.UUID = p.getUniqueId();
player.PlayerName = p.getName();
@@ -108,4 +124,18 @@ public class TBMCPlayer {
new Exception("Failed to save player data for " + player.PlayerName, e).printStackTrace();
}
}
+
+ static void JoinPlayer(TBMCPlayer player) {
+ OnlinePlayers.put(player.UUID, player);
+ Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerJoinEvent(player));
+ }
+
+ static void QuitPlayer(TBMCPlayer player) {
+ OnlinePlayers.remove(player.UUID);
+ Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerQuitEvent(player));
+ }
+
+ T GetAs(Class cl) { // TODO: Serialize player classes
+ return null;
+ }
}
diff --git a/src/buttondevteam/core/TBMCPlayerJoinEvent.java b/src/buttondevteam/core/TBMCPlayerJoinEvent.java
new file mode 100644
index 0000000..1427bb4
--- /dev/null
+++ b/src/buttondevteam/core/TBMCPlayerJoinEvent.java
@@ -0,0 +1,27 @@
+package buttondevteam.core;
+
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+
+public class TBMCPlayerJoinEvent extends Event {
+ private static final HandlerList handlers = new HandlerList();
+
+ private TBMCPlayer player;
+
+ public TBMCPlayerJoinEvent(TBMCPlayer player) {
+ this.player = player;
+ }
+
+ public TBMCPlayer GetPlayer() {
+ return player;
+ }
+
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
diff --git a/src/buttondevteam/core/TBMCPlayerQuitEvent.java b/src/buttondevteam/core/TBMCPlayerQuitEvent.java
new file mode 100644
index 0000000..4b4bab8
--- /dev/null
+++ b/src/buttondevteam/core/TBMCPlayerQuitEvent.java
@@ -0,0 +1,27 @@
+package buttondevteam.core;
+
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+
+public class TBMCPlayerQuitEvent extends Event {
+ private static final HandlerList handlers = new HandlerList();
+
+ private TBMCPlayer player;
+
+ public TBMCPlayerQuitEvent(TBMCPlayer player) {
+ this.player = player;
+ }
+
+ public TBMCPlayer GetPlayer() {
+ return player;
+ }
+
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}