2012-04-27 11:47:11 +02:00
|
|
|
package com.gmail.nossr50.util;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2012-06-06 20:49:07 +02:00
|
|
|
import com.gmail.nossr50.McMMO;
|
2012-04-27 11:47:11 +02:00
|
|
|
import com.gmail.nossr50.datatypes.PlayerProfile;
|
|
|
|
|
|
|
|
public class Users {
|
2012-06-06 01:19:39 +02:00
|
|
|
private static HashMap<Player, PlayerProfile> players = new HashMap<Player, PlayerProfile>();
|
2012-05-05 19:51:54 +02:00
|
|
|
|
2012-04-27 11:47:11 +02:00
|
|
|
/**
|
|
|
|
* Load users.
|
|
|
|
*/
|
|
|
|
public static void loadUsers() {
|
2012-06-06 20:49:07 +02:00
|
|
|
new File(McMMO.flatFileDirectory).mkdir();
|
|
|
|
new File(McMMO.leaderboardDirectory).mkdir();
|
2012-06-06 01:19:39 +02:00
|
|
|
|
2012-06-06 20:49:07 +02:00
|
|
|
File theDir = new File(McMMO.usersFile);
|
2012-04-27 11:47:11 +02:00
|
|
|
|
|
|
|
if (!theDir.exists()) {
|
|
|
|
try {
|
|
|
|
FileWriter writer = new FileWriter(theDir);
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new user.
|
|
|
|
*
|
|
|
|
* @param player The player to create a user record for
|
|
|
|
*/
|
|
|
|
public static void addUser(Player player) {
|
2012-06-06 01:19:39 +02:00
|
|
|
if (!players.containsKey(player)) {
|
|
|
|
players.put(player, new PlayerProfile(player, true));
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all users.
|
|
|
|
*/
|
|
|
|
public static void clearUsers() {
|
|
|
|
players.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all PlayerProfiles.
|
|
|
|
*
|
|
|
|
* @return a HashMap containing the PlayerProfile of everyone in the database
|
|
|
|
*/
|
2012-06-06 01:19:39 +02:00
|
|
|
public static HashMap<Player, PlayerProfile> getProfiles() {
|
2012-04-27 11:47:11 +02:00
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-06-06 01:19:39 +02:00
|
|
|
if (!player.isOnline() && players.containsKey(player)) {
|
|
|
|
players.remove(player);
|
2012-04-27 11:47:11 +02: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-06-06 20:49:07 +02:00
|
|
|
players.remove(McMMO.p.getServer().getOfflinePlayer(playerName));
|
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-06 01:19:39 +02:00
|
|
|
return players.get(player);
|
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
|
|
|
|
*/
|
|
|
|
public static PlayerProfile getProfileByName(String playerName) {
|
2012-06-06 20:49:07 +02:00
|
|
|
Player player = McMMO.p.getServer().getPlayer(playerName);
|
2012-06-06 01:19:39 +02:00
|
|
|
PlayerProfile profile = players.get(player);
|
|
|
|
|
|
|
|
if (profile == null) {
|
|
|
|
if (player != null) {
|
|
|
|
PlayerProfile newProfile = new PlayerProfile(player, true);
|
|
|
|
|
|
|
|
players.put(player, newProfile);
|
|
|
|
return newProfile;
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-06-06 20:49:07 +02:00
|
|
|
McMMO.p.getLogger().severe("getProfileByName(" + playerName + ") just returned null :(");
|
2012-06-06 01:19:39 +02:00
|
|
|
|
|
|
|
for (StackTraceElement ste : new Throwable().getStackTrace()) {
|
|
|
|
System.out.println(ste);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-06-06 01:19:39 +02:00
|
|
|
return profile;
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|