From 0b9aab4c9c9d6507b791becec829f300d13621e7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 17 Jul 2016 02:27:19 +0200 Subject: [PATCH] Various fixes --- plugin.yml | 4 ++ .../core/player/CoreSubPlugin.java | 6 ++- .../core/player/PlayerListener.java | 16 ++++--- src/buttondevteam/core/player/TBMCPlayer.java | 48 ++++++++++++------- .../core/player/TBMCPlayerAddEvent.java | 11 ++++- 5 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 plugin.yml diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..04bcd2c --- /dev/null +++ b/plugin.yml @@ -0,0 +1,4 @@ +name: ButtonPluginBucket +main: buttondevteam.MainPlugin +version: 1.0 +author: TBMCPlugins \ No newline at end of file diff --git a/src/buttondevteam/core/player/CoreSubPlugin.java b/src/buttondevteam/core/player/CoreSubPlugin.java index ba2afd5..0e8fbcd 100644 --- a/src/buttondevteam/core/player/CoreSubPlugin.java +++ b/src/buttondevteam/core/player/CoreSubPlugin.java @@ -1,5 +1,8 @@ package buttondevteam.core.player; +import java.util.logging.Level; + +import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; public class CoreSubPlugin { @@ -10,6 +13,7 @@ public class CoreSubPlugin { } public void register() { - System.out.println("Players subplugin registered!"); + Bukkit.getPluginManager().registerEvents(new PlayerListener(), plugin); + plugin.getLogger().log(Level.INFO, "Core subplugin registered!"); } } diff --git a/src/buttondevteam/core/player/PlayerListener.java b/src/buttondevteam/core/player/PlayerListener.java index 27c8a65..7eb3d55 100644 --- a/src/buttondevteam/core/player/PlayerListener.java +++ b/src/buttondevteam/core/player/PlayerListener.java @@ -1,17 +1,21 @@ package buttondevteam.core.player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; public class PlayerListener implements Listener { - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST) public void OnPlayerJoin(PlayerJoinEvent event) { - try { - TBMCPlayer.LoadPlayer(event.getPlayer().getUniqueId()); - } catch (Exception e) { - e.printStackTrace(); - } + if (TBMCPlayer.LoadPlayer(event.getPlayer()) == null) + event.getPlayer().sendMessage("§c[TBMC] Failed to load player data! Please contact a mod."); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void OnPlayerLeave(PlayerQuitEvent event) { + TBMCPlayer.SavePlayer(TBMCPlayer.GetPlayer(event.getPlayer())); } } diff --git a/src/buttondevteam/core/player/TBMCPlayer.java b/src/buttondevteam/core/player/TBMCPlayer.java index aca04ce..2044d5d 100644 --- a/src/buttondevteam/core/player/TBMCPlayer.java +++ b/src/buttondevteam/core/player/TBMCPlayer.java @@ -9,6 +9,11 @@ import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import com.palmergames.bukkit.towny.Towny; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.ResidentModes; +import com.palmergames.bukkit.towny.object.TownyUniverse; + /** *

* The class for holding data common to all TBMC plugins @@ -22,6 +27,8 @@ import org.bukkit.entity.Player; * */ public class TBMCPlayer { + private static final String TBMC_PLAYERS_DIR = "TBMC/players"; + public String PlayerName; public UUID UUID; @@ -51,20 +58,31 @@ public class TBMCPlayer { return TBMCPlayer.OnlinePlayers.get(p.getUniqueId()); } - static TBMCPlayer LoadPlayer(UUID uuid) throws Exception { - if (OnlinePlayers.containsKey(uuid)) - return OnlinePlayers.get(uuid); - File file = new File("TBMC/players"); + static TBMCPlayer LoadPlayer(Player p) { + if (OnlinePlayers.containsKey(p.getUniqueId())) + return OnlinePlayers.get(p.getUniqueId()); + File file = new File(TBMC_PLAYERS_DIR); file.mkdirs(); - file = new File("TBMC/players", uuid.toString() + ".yml"); + file = new File(TBMC_PLAYERS_DIR, p.getUniqueId().toString() + ".yml"); if (!file.exists()) - return AddPlayer(uuid); + return AddPlayer(p); else { final YamlConfiguration yc = new YamlConfiguration(); - yc.load(file); + try { + yc.load(file); + } catch (Exception e) { + new Exception("Failed to load player data for " + p.getUniqueId(), e).printStackTrace(); + return null; + } TBMCPlayer player = new TBMCPlayer(); - player.UUID = uuid; + player.UUID = p.getUniqueId(); player.PlayerName = yc.getString("playername"); + if (!p.getName().equals(player.PlayerName)) { + System.out.println("Renaming " + player.PlayerName + " to " + p.getName()); + TownyUniverse tu = Towny.getPlugin(Towny.class).getTownyUniverse(); + tu.getResidentMap().get(player.PlayerName).setName(p.getName()); + System.out.println("Renaming done."); + } // Load in other plugins Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerLoadEvent(yc, player)); @@ -72,15 +90,11 @@ public class TBMCPlayer { } } - static TBMCPlayer AddPlayer(UUID uuid) { - if (OnlinePlayers.containsKey(uuid)) - return OnlinePlayers.get(uuid); + static TBMCPlayer AddPlayer(Player p) { TBMCPlayer player = new TBMCPlayer(); - player.UUID = uuid; - Player p = Bukkit.getPlayer(uuid); - if (p != null) - player.PlayerName = p.getName(); - OnlinePlayers.put(uuid, player); + player.UUID = p.getUniqueId(); + player.PlayerName = p.getName(); + OnlinePlayers.put(p.getUniqueId(), player); Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerAddEvent(player)); SavePlayer(player); return player; @@ -91,7 +105,7 @@ public class TBMCPlayer { yc.set("playername", player.PlayerName); Bukkit.getServer().getPluginManager().callEvent(new TBMCPlayerSaveEvent(yc, player)); try { - yc.save("tbmcplayers/" + player.UUID + ".yml"); + yc.save(TBMC_PLAYERS_DIR + "/" + player.UUID + ".yml"); } catch (IOException e) { new Exception("Failed to save player data for " + player.PlayerName, e).printStackTrace(); } diff --git a/src/buttondevteam/core/player/TBMCPlayerAddEvent.java b/src/buttondevteam/core/player/TBMCPlayerAddEvent.java index e7b4f75..08c595a 100644 --- a/src/buttondevteam/core/player/TBMCPlayerAddEvent.java +++ b/src/buttondevteam/core/player/TBMCPlayerAddEvent.java @@ -3,13 +3,22 @@ package buttondevteam.core.player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +/** + *

+ * This event gets called when a new player joins. After this event, the + * {@link TBMCPlayerSaveEvent} will be called. + *

+ * + * @author Norbi + * + */ public class TBMCPlayerAddEvent extends Event { private static final HandlerList handlers = new HandlerList(); private TBMCPlayer player; public TBMCPlayerAddEvent(TBMCPlayer player) { - //TODO: Convert player configs + // TODO: Convert player configs this.player = player; }