2012-04-27 11:47:11 +02:00
|
|
|
package com.gmail.nossr50.util;
|
|
|
|
|
|
|
|
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;
|
2012-07-06 17:57:17 +02:00
|
|
|
import com.gmail.nossr50.datatypes.McMMOPlayer;
|
2012-04-27 11:47:11 +02:00
|
|
|
import com.gmail.nossr50.datatypes.PlayerProfile;
|
|
|
|
|
|
|
|
public class Users {
|
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
|
|
|
|
2012-04-27 11:47:11 +02:00
|
|
|
/**
|
|
|
|
* Load users.
|
|
|
|
*/
|
|
|
|
public static void loadUsers() {
|
2012-06-07 00:02:22 +02:00
|
|
|
new File(mcMMO.flatFileDirectory).mkdir();
|
|
|
|
new File(mcMMO.leaderboardDirectory).mkdir();
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2012-07-04 15:45:47 +02:00
|
|
|
try {
|
|
|
|
new File(mcMMO.usersFile).createNewFile();
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the profile of a player.
|
|
|
|
*
|
|
|
|
* @param player The player whose profile to retrieve
|
|
|
|
* @return the player's profile
|
|
|
|
*/
|
|
|
|
public static PlayerProfile getProfile(OfflinePlayer player) {
|
2012-06-08 19:13:17 +02:00
|
|
|
return getProfile(player.getName());
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the profile of a player by name.
|
|
|
|
*
|
|
|
|
* @param player The name of the player whose profile to retrieve
|
|
|
|
* @return the player's profile
|
|
|
|
*/
|
2012-06-08 19:13:17 +02:00
|
|
|
public static PlayerProfile getProfile(String playerName) {
|
2012-07-06 17:57:17 +02:00
|
|
|
return players.get(playerName).getProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the McMMOPlayer of a player by name.
|
|
|
|
*
|
|
|
|
* @param player The name of the player whose McMMOPlayer to retrieve
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public static McMMOPlayer getPlayer(Player player) {
|
|
|
|
return getPlayer(player.getName());
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
}
|