2013-03-01 06:52:01 +01:00
|
|
|
package com.gmail.nossr50.util.player;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2012-06-24 22:46:45 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2012-06-07 00:02:22 +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 {
|
2012-07-06 17:57:17 +02:00
|
|
|
private 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
|
|
|
/**
|
|
|
|
* Load users.
|
|
|
|
*/
|
|
|
|
public static void loadUsers() {
|
2012-07-09 16:55:33 +02:00
|
|
|
new File(mcMMO.getFlatFileDirectory()).mkdir();
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2012-07-04 15:45:47 +02:00
|
|
|
try {
|
2013-01-26 23:01:55 +01:00
|
|
|
new File(mcMMO.getUsersFilePath()).createNewFile();
|
2012-07-04 15:45:47 +02:00
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
public static Map<String, McMMOPlayer> getPlayers() {
|
|
|
|
return players;
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
/**
|
|
|
|
* Get the McMMOPlayer of a player by name.
|
|
|
|
*
|
2013-01-31 01:42:40 +01: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) {
|
|
|
|
return players.get(playerName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
}
|