2013-03-01 06:52:01 +01:00
|
|
|
package com.gmail.nossr50.util.player;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2013-07-15 15:14:23 +02:00
|
|
|
import java.util.Collection;
|
2012-06-24 22:46:45 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2013-07-15 15:14:23 +02:00
|
|
|
import java.util.Set;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2013-08-20 20:10:49 +02:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2013-03-01 06:52:01 +01:00
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
public final class UserManager {
|
2013-04-02 17:17:40 +02:00
|
|
|
private final static Map<String, McMMOPlayer> players = new HashMap<String, McMMOPlayer>();
|
2012-05-05 19:51:54 +02:00
|
|
|
|
2013-08-22 00:37:48 +02:00
|
|
|
private UserManager() {}
|
2013-01-26 23:01:55 +01:00
|
|
|
|
2012-04-27 11:47:11 +02:00
|
|
|
/**
|
|
|
|
* Add a new user.
|
|
|
|
*
|
|
|
|
* @param player The player to create a user record for
|
2012-07-06 17:57:17 +02:00
|
|
|
* @return the player's {@link McMMOPlayer} object
|
2012-04-27 11:47:11 +02:00
|
|
|
*/
|
2012-07-06 17:57:17 +02:00
|
|
|
public static McMMOPlayer addUser(Player player) {
|
2012-06-08 19:13:17 +02:00
|
|
|
String playerName = player.getName();
|
2012-07-06 17:57:17 +02:00
|
|
|
McMMOPlayer mcMMOPlayer = players.get(playerName);
|
2012-06-08 19:13:17 +02:00
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
if (mcMMOPlayer != null) {
|
2013-03-01 06:52:01 +01:00
|
|
|
mcMMOPlayer.setPlayer(player); // The player object is different on each reconnection and must be updated
|
2012-06-24 22:46:45 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-07-06 17:57:17 +02:00
|
|
|
mcMMOPlayer = new McMMOPlayer(player);
|
|
|
|
players.put(playerName, mcMMOPlayer);
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
2012-06-08 19:13:17 +02:00
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
return mcMMOPlayer;
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
/**
|
2012-06-25 14:30:55 +02:00
|
|
|
* Remove a user.
|
2012-07-06 17:57:17 +02:00
|
|
|
*
|
2012-06-25 14:30:55 +02:00
|
|
|
* @param playerName The name of the player to remove
|
|
|
|
*/
|
|
|
|
public static void remove(String playerName) {
|
2012-07-06 17:57:17 +02:00
|
|
|
players.remove(playerName);
|
2012-06-25 14:30:55 +02:00
|
|
|
}
|
|
|
|
|
2012-04-27 11:47:11 +02:00
|
|
|
/**
|
|
|
|
* Clear all users.
|
|
|
|
*/
|
2012-06-25 14:30:55 +02:00
|
|
|
public static void clearAll() {
|
2012-07-06 17:57:17 +02:00
|
|
|
players.clear();
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
/**
|
2012-06-25 14:30:55 +02:00
|
|
|
* Save all users.
|
|
|
|
*/
|
|
|
|
public static void saveAll() {
|
2012-07-06 17:57:17 +02:00
|
|
|
for (McMMOPlayer mcMMOPlayer : players.values()) {
|
|
|
|
mcMMOPlayer.getProfile().save();
|
2012-06-25 14:30:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 15:14:23 +02:00
|
|
|
public static Set<String> getPlayerNames() {
|
|
|
|
return players.keySet();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Collection<McMMOPlayer> getPlayers() {
|
|
|
|
return players.values();
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
/**
|
2013-08-20 20:10:49 +02:00
|
|
|
* Get the McMMOPlayer of a player by name.
|
2012-07-06 17:57:17 +02:00
|
|
|
*
|
2013-08-20 20:10:49 +02:00
|
|
|
* @param playerName The name of the player whose McMMOPlayer to retrieve
|
2012-07-06 17:57:17 +02:00
|
|
|
* @return the player's McMMOPlayer object
|
|
|
|
*/
|
|
|
|
public static McMMOPlayer getPlayer(String playerName) {
|
2013-08-21 16:20:12 +02:00
|
|
|
return retrieveMcMMOPlayer(playerName, false);
|
2013-08-19 10:37:04 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
/**
|
|
|
|
* Get the McMMOPlayer of a player.
|
|
|
|
*
|
|
|
|
* @param player The player whose McMMOPlayer to retrieve
|
|
|
|
* @return the player's McMMOPlayer object
|
|
|
|
*/
|
2013-02-11 14:14:43 +01:00
|
|
|
public static McMMOPlayer getPlayer(OfflinePlayer player) {
|
2013-08-21 16:20:12 +02:00
|
|
|
return retrieveMcMMOPlayer(player.getName(), false);
|
2013-08-20 20:10:49 +02:00
|
|
|
}
|
|
|
|
|
2013-08-21 16:20:12 +02:00
|
|
|
public static McMMOPlayer getPlayer(String playerName, boolean offlineValid) {
|
2013-08-22 00:37:48 +02:00
|
|
|
return retrieveMcMMOPlayer(playerName, offlineValid);
|
2013-08-21 16:20:12 +02:00
|
|
|
}
|
2013-08-22 00:37:48 +02:00
|
|
|
|
2013-08-21 16:20:12 +02:00
|
|
|
private static McMMOPlayer retrieveMcMMOPlayer(String playerName, boolean offlineValid) {
|
2013-08-20 20:10:49 +02:00
|
|
|
McMMOPlayer mcMMOPlayer = players.get(playerName);
|
|
|
|
|
|
|
|
if (mcMMOPlayer == null) {
|
|
|
|
Player player = mcMMO.p.getServer().getPlayerExact(playerName);
|
|
|
|
|
|
|
|
if (player == null) {
|
2013-08-22 00:37:48 +02:00
|
|
|
if (!offlineValid) {
|
2013-08-21 16:20:12 +02:00
|
|
|
mcMMO.p.getLogger().warning("A valid mcMMOPlayer object could not be found for " + playerName + ".");
|
2013-08-22 00:37:48 +02:00
|
|
|
}
|
2013-08-21 16:20:12 +02:00
|
|
|
|
2013-08-20 20:10:49 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
mcMMOPlayer = new McMMOPlayer(player);
|
|
|
|
players.put(playerName, mcMMOPlayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mcMMOPlayer;
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
}
|