2012-01-09 20:00:13 +01:00
|
|
|
package com.gmail.nossr50;
|
|
|
|
|
2012-03-12 22:28:13 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
2012-01-09 20:00:13 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2012-03-08 23:26:13 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2012-01-09 20:00:13 +01:00
|
|
|
import com.gmail.nossr50.datatypes.PlayerProfile;
|
|
|
|
|
|
|
|
public class Users {
|
|
|
|
private static volatile Users instance;
|
2012-03-08 23:26:13 +01:00
|
|
|
|
2012-01-09 20:00:13 +01:00
|
|
|
String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
|
|
|
|
String directory = "plugins/mcMMO/FlatFileStuff/";
|
|
|
|
String directoryb = "plugins/mcMMO/FlatFileStuff/Leaderboards/";
|
2012-03-08 23:26:13 +01:00
|
|
|
|
2012-03-22 21:42:44 +01:00
|
|
|
public static HashMap<String, PlayerProfile> players = new HashMap<String, PlayerProfile>();
|
2012-03-08 23:26:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load users.
|
|
|
|
*/
|
|
|
|
public void loadUsers() {
|
|
|
|
new File(directory).mkdir();
|
|
|
|
new File(directoryb).mkdir();
|
2012-01-09 20:00:13 +01:00
|
|
|
File theDir = new File(location);
|
2012-03-08 23:26:13 +01:00
|
|
|
|
|
|
|
if (!theDir.exists()) {
|
|
|
|
try {
|
|
|
|
FileWriter writer = new FileWriter(theDir);
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 20:00:13 +01:00
|
|
|
}
|
|
|
|
|
2012-03-08 23:26:13 +01:00
|
|
|
/**
|
|
|
|
* Add a new user.
|
|
|
|
*
|
|
|
|
* @param player The player to create a user record for
|
|
|
|
*/
|
|
|
|
public static void addUser(Player player) {
|
2012-03-22 21:42:44 +01:00
|
|
|
if (!players.containsKey(player.getName().toLowerCase())) {
|
2012-03-22 22:19:35 +01:00
|
|
|
players.put(player.getName().toLowerCase(), new PlayerProfile(player.getName()));
|
2012-03-08 23:26:13 +01:00
|
|
|
}
|
2012-01-09 20:00:13 +01:00
|
|
|
}
|
2012-03-08 23:26:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all users.
|
|
|
|
*/
|
|
|
|
public static void clearUsers() {
|
|
|
|
players.clear();
|
2012-01-09 20:00:13 +01:00
|
|
|
}
|
2012-03-08 23:26:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all PlayerProfiles.
|
|
|
|
*
|
|
|
|
* @return a HashMap containing the PlayerProfile of everyone in the database
|
|
|
|
*/
|
2012-03-22 21:42:44 +01:00
|
|
|
public static HashMap<String, PlayerProfile> getProfiles() {
|
2012-03-08 23:26:13 +01:00
|
|
|
return players;
|
2012-01-09 20:00:13 +01:00
|
|
|
}
|
2012-03-08 23:26:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a user from the database.
|
|
|
|
*
|
|
|
|
* @param player The player to remove
|
|
|
|
*/
|
|
|
|
public static void removeUser(Player player) {
|
|
|
|
|
|
|
|
//Only remove PlayerProfile if user is offline and we have it in memory
|
2012-03-22 21:42:44 +01:00
|
|
|
if (!player.isOnline() && players.containsKey(player.getName().toLowerCase())) {
|
|
|
|
players.get(player.getName().toLowerCase()).save();
|
|
|
|
players.remove(player.getName().toLowerCase());
|
2012-03-08 23:26:13 +01:00
|
|
|
}
|
2012-01-09 20:00:13 +01:00
|
|
|
}
|
2012-03-08 23:26:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a user from the DB by name.
|
|
|
|
*
|
|
|
|
* @param playerName The name of the player to remove
|
|
|
|
*/
|
|
|
|
public static void removeUserByName(String playerName) {
|
2012-03-22 21:42:44 +01:00
|
|
|
players.remove(playerName.toLowerCase());
|
2012-02-28 21:16:23 +01:00
|
|
|
}
|
2012-01-09 20:00:13 +01:00
|
|
|
|
2012-03-08 23:26:13 +01:00
|
|
|
/**
|
|
|
|
* Get the profile of an online player.
|
|
|
|
*
|
|
|
|
* @param player The player whose profile to retrieve
|
|
|
|
* @return the player's profile
|
|
|
|
*/
|
|
|
|
public static PlayerProfile getProfile(Player player) {
|
2012-03-22 21:42:44 +01:00
|
|
|
if(players.get(player.getName().toLowerCase()) != null) {
|
|
|
|
return players.get(player.getName().toLowerCase());
|
|
|
|
}
|
|
|
|
else {
|
2012-03-22 22:19:35 +01:00
|
|
|
players.put(player.getName().toLowerCase(), new PlayerProfile(player.getName()));
|
2012-03-22 21:42:44 +01:00
|
|
|
return players.get(player.getName().toLowerCase());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the profile of an online player.
|
|
|
|
*
|
|
|
|
* @param player The player whose profile to retrieve
|
|
|
|
* @return the player's profile
|
|
|
|
*/
|
|
|
|
public static PlayerProfile getProfile(String playerName) {
|
|
|
|
if(players.get(playerName.toLowerCase()) != null) {
|
|
|
|
return players.get(playerName.toLowerCase());
|
2012-03-08 23:26:13 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-03-22 22:19:35 +01:00
|
|
|
players.put(playerName.toLowerCase(), new PlayerProfile(playerName));
|
2012-03-22 21:42:44 +01:00
|
|
|
return players.get(playerName.toLowerCase());
|
2012-03-08 23:26:13 +01:00
|
|
|
}
|
2012-01-09 20:00:13 +01:00
|
|
|
}
|
2012-03-08 23:26:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the profile of an offline player.
|
|
|
|
*
|
|
|
|
* @param playerName Name of the player whose profile to retrieve
|
|
|
|
* @return the player's profile
|
|
|
|
*/
|
|
|
|
public static PlayerProfile getOfflineProfile(String playerName) {
|
2012-03-22 22:31:48 +01:00
|
|
|
return new PlayerProfile(playerName, false);
|
2012-02-29 21:33:33 +01:00
|
|
|
}
|
2012-01-09 20:00:13 +01:00
|
|
|
|
2012-03-08 23:26:13 +01:00
|
|
|
/**
|
|
|
|
* Get an instance of this class.
|
|
|
|
*
|
|
|
|
* @return an instance of this class
|
|
|
|
*/
|
|
|
|
public static Users getInstance() {
|
|
|
|
if (instance == null) {
|
|
|
|
instance = new Users();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
}
|