Browse files

Return to HashMap!

  • Loading branch information...
1 parent 0b03906 commit dc48d467f5526d6d403eb181416bfea10d15835c bm01 committed Jun 24, 2012
View
2 src/main/java/com/gmail/nossr50/mcMMO.java
@@ -285,7 +285,7 @@ public PlayerProfile getPlayerProfile(Player player) {
@Override
public void onDisable() {
//Make sure to save player information if the server shuts down
- for (PlayerProfile playerProfile : Users.getProfiles()) {
+ for (PlayerProfile playerProfile : Users.getProfiles().values()) {
playerProfile.save();
}
View
2 src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java
@@ -19,7 +19,7 @@ public void run() {
if (!Database.isConnected()) {
Database.connect();
if (Database.isConnected()) {
- for (PlayerProfile playerProfile : Users.getProfiles()) {
+ for (PlayerProfile playerProfile : Users.getProfiles().values()) {
playerProfile.save(); //Save all profiles
}
View
2 src/main/java/com/gmail/nossr50/runnables/SaveTimer.java
@@ -20,7 +20,7 @@ public void run() {
int count = 1;
BukkitScheduler bukkitScheduler = plugin.getServer().getScheduler();
- for (PlayerProfile playerProfile : Users.getProfiles()) {
+ for (PlayerProfile playerProfile : Users.getProfiles().values()) {
bukkitScheduler.scheduleSyncDelayedTask(plugin, new ProfileSaveTask(playerProfile), count);
count++;
}
View
38 src/main/java/com/gmail/nossr50/util/Users.java
@@ -3,9 +3,8 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
@@ -14,7 +13,7 @@
import com.gmail.nossr50.datatypes.PlayerProfile;
public class Users {
- private static List<PlayerProfile> profiles = new ArrayList<PlayerProfile>();
+ private static Map<String, PlayerProfile> profiles = new HashMap<String, PlayerProfile>();
/**
* Load users.
@@ -44,21 +43,18 @@ public static void loadUsers() {
*/
public static PlayerProfile addUser(Player player) {
String playerName = player.getName();
+ PlayerProfile playerProfile = profiles.get(playerName);
- for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
- PlayerProfile playerProfile = it.next();
+ if (playerProfile != null) {
+ //The player object is different on each reconnection and must be updated
+ playerProfile.setPlayer(player);
+ }
+ else {
+ playerProfile = new PlayerProfile(player, playerName, true);
- if (playerProfile.getPlayerName().equals(playerName)) {
- //The player object is different on each reconnection and must be updated
- playerProfile.setPlayer(player);
- return playerProfile;
- }
+ profiles.put(playerName, playerProfile);
}
- //New player, or already removed from the list
- PlayerProfile playerProfile = new PlayerProfile(player, playerName, true);
-
- profiles.add(playerProfile);
return playerProfile;
}
@@ -74,7 +70,7 @@ public static void clearUsers() {
*
* @return a HashMap containing the PlayerProfile of everyone in the database
*/
- public static List<PlayerProfile> getProfiles() {
+ public static Map<String, PlayerProfile> getProfiles() {
return profiles;
}
@@ -95,14 +91,6 @@ public static PlayerProfile getProfile(OfflinePlayer player) {
* @return the player's profile
*/
public static PlayerProfile getProfile(String playerName) {
- for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
- PlayerProfile playerProfile = it.next();
-
- if (playerProfile.getPlayerName().equals(playerName)) {
- return playerProfile;
- }
- }
-
- return null;
+ return profiles.get(playerName);
}
}

0 comments on commit dc48d46

Please sign in to comment.