From 90c8e57ed78ae992760c3741c33b61edb5471f04 Mon Sep 17 00:00:00 2001 From: bm01 Date: Fri, 8 Jun 2012 19:13:17 +0200 Subject: [PATCH] Reworked a little Users.java --- .../nossr50/commands/mc/McremoveCommand.java | 3 +- .../nossr50/datatypes/PlayerProfile.java | 9 +- src/main/java/com/gmail/nossr50/mcMMO.java | 26 ++---- .../java/com/gmail/nossr50/party/Party.java | 5 +- .../RemoveProfileFromMemoryTask.java | 12 ++- .../gmail/nossr50/runnables/SQLReconnect.java | 10 +-- .../java/com/gmail/nossr50/util/Users.java | 82 +++++++++++-------- 7 files changed, 80 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java b/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java index 38f9c1f27..f946c822d 100644 --- a/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java @@ -98,7 +98,8 @@ public class McremoveCommand implements CommandExecutor { //Force PlayerProfile stuff to update Player player = plugin.getServer().getPlayer(playerName); - if (player != null && Users.getProfiles().containsKey(player)) { + //TODO fix this + if (player != null /*&& Users.getProfiles().containsKey(player)*/) { Users.removeUser(player); Users.addUser(player); } diff --git a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java index 60c525e8b..ec9f0b6bf 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java @@ -93,11 +93,18 @@ public class PlayerProfile { lastlogin = ((Long) (System.currentTimeMillis() / 1000)).intValue(); } - //This method is actually never used public Player getPlayer() { return player; } + public String getPlayerName() { + return playerName; + } + + public void setPlayer(Player player) { + this.player = player; + } + public boolean loadMySQL() { int id = 0; id = mcMMO.database.getInt("SELECT id FROM "+Config.getInstance().getMySQLTablePrefix()+"users WHERE user = '" + playerName + "'"); diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 3d3aa5f9c..e78c59fbc 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -236,18 +236,6 @@ public class mcMMO extends JavaPlugin { modDirectory = mainDirectory + "ModConfigs" + File.separator; } - /** - * Get profile of the player. - *
- * This function is designed for API usage. - * - * @param player Player whose profile to get - * @return the PlayerProfile object - */ - public PlayerProfile getPlayerProfile(Player player) { - return Users.getProfile(player); - } - /** * Get profile of the player by name. *
@@ -256,19 +244,19 @@ public class mcMMO extends JavaPlugin { * @param playerName Name of player whose profile to get * @return the PlayerProfile object */ - public PlayerProfile getPlayerProfileByName(String playerName) { - return Users.getProfileByName(playerName); + public PlayerProfile getPlayerProfile(String playerName) { + return Users.getProfile(playerName); } /** - * Get profile of the offline player. + * Get profile of the player. *
* This function is designed for API usage. * - * @param player Offline player whose profile to get + * @param player player whose profile to get * @return the PlayerProfile object */ - public PlayerProfile getOfflinePlayerProfile(OfflinePlayer player) { + public PlayerProfile getPlayerProfile(OfflinePlayer player) { return Users.getProfile(player); } @@ -278,8 +266,8 @@ public class mcMMO extends JavaPlugin { @Override public void onDisable() { //Make sure to save player information if the server shuts down - for (PlayerProfile x : Users.getProfiles().values()) { - x.save(); + for (PlayerProfile playerProfile : Users.getProfiles()) { + playerProfile.save(); } getServer().getScheduler().cancelTasks(this); //This removes our tasks diff --git a/src/main/java/com/gmail/nossr50/party/Party.java b/src/main/java/com/gmail/nossr50/party/Party.java index 9a3490c94..0b5443800 100644 --- a/src/main/java/com/gmail/nossr50/party/Party.java +++ b/src/main/java/com/gmail/nossr50/party/Party.java @@ -148,10 +148,11 @@ public class Party { */ public ArrayList getAllMembers(Player player) { ArrayList players = new ArrayList(); - HashMap profiles = Users.getProfiles(); if (player != null) { - for (Player otherPlayer : profiles.keySet()) { + for (PlayerProfile playerProfile : Users.getProfiles()) { + Player otherPlayer = playerProfile.getPlayer(); + if (otherPlayer != null && inSameParty(otherPlayer, player)) { players.add(otherPlayer); } diff --git a/src/main/java/com/gmail/nossr50/runnables/RemoveProfileFromMemoryTask.java b/src/main/java/com/gmail/nossr50/runnables/RemoveProfileFromMemoryTask.java index 8a410acd8..d7c1a1dbb 100644 --- a/src/main/java/com/gmail/nossr50/runnables/RemoveProfileFromMemoryTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/RemoveProfileFromMemoryTask.java @@ -2,6 +2,7 @@ package com.gmail.nossr50.runnables; import org.bukkit.entity.Player; +import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.util.Users; public class RemoveProfileFromMemoryTask implements Runnable { @@ -13,10 +14,15 @@ public class RemoveProfileFromMemoryTask implements Runnable { @Override public void run() { + PlayerProfile playerProfile = Users.getProfile(player); + //Check if the profile still exists (stuff like MySQL reconnection removes profiles) - if (Users.getProfiles().containsKey(player)) { - Users.getProfile(player).save(); //We save here so players don't quit/reconnect to cause lag - Users.removeUser(player); + if (playerProfile != null) { + playerProfile.save(); //We save here so players don't quit/reconnect to cause lag + + if (!player.isOnline()) { + Users.removeUser(playerProfile); + } } } } diff --git a/src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java b/src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java index 4beb7eae2..574d92dc0 100644 --- a/src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java +++ b/src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java @@ -19,13 +19,13 @@ public class SQLReconnect implements Runnable { if (!Database.isConnected()) { Database.connect(); if (Database.isConnected()) { - for (PlayerProfile x : Users.getProfiles().values()) { - x.save(); //Save all profiles + for (PlayerProfile playerProfile : Users.getProfiles()) { + playerProfile.save(); //Save all profiles } - Users.getProfiles().clear(); //Clear the profiles - for (Player x : plugin.getServer().getOnlinePlayers()) { - Users.addUser(x); //Add in new profiles, forcing them to 'load' again from MySQL + Users.clearUsers(); //Clear the profiles + for (Player player : plugin.getServer().getOnlinePlayers()) { + Users.addUser(player); //Add in new profiles, forcing them to 'load' again from MySQL } } } diff --git a/src/main/java/com/gmail/nossr50/util/Users.java b/src/main/java/com/gmail/nossr50/util/Users.java index c6642bc2c..f3164ee95 100644 --- a/src/main/java/com/gmail/nossr50/util/Users.java +++ b/src/main/java/com/gmail/nossr50/util/Users.java @@ -3,7 +3,9 @@ package com.gmail.nossr50.util; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -12,7 +14,7 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.PlayerProfile; public class Users { - private static HashMap players = new HashMap(); + private static List profiles = new ArrayList(); /** * Load users. @@ -40,16 +42,27 @@ public class Users { * @param player The player to create a user record for */ public static void addUser(Player player) { - if (!players.containsKey(player)) { - players.put(player, new PlayerProfile(player, true)); + String playerName = player.getName(); + + for (Iterator it = profiles.iterator() ; it.hasNext() ; ) { + PlayerProfile playerProfile = it.next(); + + if (playerProfile.getPlayerName().equals(playerName)) { + //The player object is different on each reconnection and must be updated + playerProfile.setPlayer(player); + return; + } } + + //New player, or already removed from the list + profiles.add(new PlayerProfile(player, true)); } /** * Clear all users. */ public static void clearUsers() { - players.clear(); + profiles.clear(); } /** @@ -57,8 +70,8 @@ public class Users { * * @return a HashMap containing the PlayerProfile of everyone in the database */ - public static HashMap getProfiles() { - return players; + public static List getProfiles() { + return profiles; } /** @@ -66,11 +79,8 @@ public class Users { * * @param player The player to remove */ - public static void removeUser(Player player) { - //Only remove PlayerProfile if user is offline and we have it in memory - if (!player.isOnline() && players.containsKey(player)) { - players.remove(player); - } + public static void removeUser(OfflinePlayer player) { + removeUser(player.getName()); } /** @@ -78,8 +88,22 @@ public class Users { * * @param playerName The name of the player to remove */ - public static void removeUserByName(String playerName) { - players.remove(mcMMO.p.getServer().getOfflinePlayer(playerName)); + public static void removeUser(String playerName) { + for (Iterator it = profiles.iterator() ; it.hasNext() ; ) { + if (it.next().getPlayer().getName().equals(playerName)) { + it.remove(); + return; + } + } + } + + /** + * Remove a user from the DB by its profile. + * + * @param playerProfile the profile of the player to remove + */ + public static void removeUser(PlayerProfile playerProfile) { + profiles.remove(playerProfile); } /** @@ -89,7 +113,7 @@ public class Users { * @return the player's profile */ public static PlayerProfile getProfile(OfflinePlayer player) { - return players.get(player); + return getProfile(player.getName()); } /** @@ -98,29 +122,15 @@ public class Users { * @param player The name of the player whose profile to retrieve * @return the player's profile */ - public static PlayerProfile getProfileByName(String playerName) { - Player player = mcMMO.p.getServer().getPlayer(playerName); - PlayerProfile profile = players.get(player); + public static PlayerProfile getProfile(String playerName) { + for (Iterator it = profiles.iterator() ; it.hasNext() ; ) { + PlayerProfile playerProfile = it.next(); - if (profile == null) { - if (player != null) { - PlayerProfile newProfile = new PlayerProfile(player, true); - - players.put(player, newProfile); - return newProfile; - } - else { - mcMMO.p.getLogger().severe("getProfileByName(" + playerName + ") just returned null :("); - - for (StackTraceElement ste : new Throwable().getStackTrace()) { - System.out.println(ste); - } - - return null; + if (playerProfile.getPlayerName().equals(playerName)) { + return playerProfile; } } - else { - return profile; - } + + return null; } }