Reworked a little Users.java

This commit is contained in:
bm01
2012-06-08 19:13:17 +02:00
parent d644b4c331
commit 90c8e57ed7
7 changed files with 80 additions and 67 deletions

View File

@ -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<Player, PlayerProfile> players = new HashMap<Player, PlayerProfile>();
private static List<PlayerProfile> profiles = new ArrayList<PlayerProfile>();
/**
* 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<PlayerProfile> 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<Player, PlayerProfile> getProfiles() {
return players;
public static List<PlayerProfile> 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<PlayerProfile> 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<PlayerProfile> 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;
}
}