mcMMO/src/main/java/com/gmail/nossr50/util/Users.java

128 lines
3.2 KiB
Java
Raw Normal View History

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;
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;
2013-01-26 23:01:55 +01:00
public final class Users {
2012-07-06 17:57:17 +02:00
private static Map<String, McMMOPlayer> players = new HashMap<String, McMMOPlayer>();
2013-01-26 23:01:55 +01:00
private Users() {};
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) {
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
*/
@Deprecated
2012-04-27 11:47:11 +02:00
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 playerName The name of the player whose profile to retrieve
2012-04-27 11:47:11 +02:00
* @return the player's profile
*/
@Deprecated
2012-06-08 19:13:17 +02:00
public static PlayerProfile getProfile(String playerName) {
2012-07-08 00:02:40 +02:00
McMMOPlayer mcmmoPlayer = players.get(playerName);
return (mcmmoPlayer != null) ? mcmmoPlayer.getProfile() : null;
2012-07-06 17:57:17 +02:00
}
/**
* Get the McMMOPlayer of a player by name.
*
* @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
*/
public static McMMOPlayer getPlayer(OfflinePlayer player) {
return players.get(player.getName());
2012-04-27 11:47:11 +02:00
}
}