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;
|
2013-03-21 23:45:10 +01:00
|
|
|
import java.util.List;
|
2012-06-24 22:46:45 +02:00
|
|
|
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-03-01 06:52:01 +01:00
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
2013-08-19 10:37:04 +02:00
|
|
|
import com.gmail.nossr50.util.Misc;
|
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-03-01 06:52:01 +01: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-03-21 23:45:10 +01:00
|
|
|
* Get the McMMOPlayer of a player by a partial name.
|
2012-07-06 17:57:17 +02:00
|
|
|
*
|
2013-03-21 23:45:10 +01:00
|
|
|
* @param playerName The partial 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-19 10:37:04 +02:00
|
|
|
List<String> matches = Misc.matchPlayer(playerName);
|
2013-05-28 19:33:20 +02:00
|
|
|
|
2013-03-21 23:45:10 +01:00
|
|
|
if (matches.size() == 1) {
|
2013-08-19 10:37:04 +02:00
|
|
|
playerName = matches.get(0);
|
2013-03-21 23:45:10 +01:00
|
|
|
}
|
2013-05-28 19:33:20 +02:00
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
return players.get(playerName);
|
|
|
|
}
|
|
|
|
|
2013-08-19 10:37:04 +02:00
|
|
|
/**
|
|
|
|
* Get the McMMOPlayer of a player by the exact name.
|
|
|
|
*
|
|
|
|
* @param playerName The exact name of the player whose McMMOPlayer to retrieve
|
|
|
|
* @return the player's McMMOPlayer object
|
|
|
|
*/
|
|
|
|
public static McMMOPlayer getPlayerExact(String playerName) {
|
|
|
|
return players.get(playerName);
|
|
|
|
}
|
|
|
|
|
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-02-01 10:18:57 +01:00
|
|
|
return players.get(player.getName());
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
}
|