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

114 lines
2.8 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.FileWriter;
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-04-27 11:47:11 +02:00
import com.gmail.nossr50.datatypes.PlayerProfile;
public class Users {
2012-06-24 22:46:45 +02:00
private static Map<String, PlayerProfile> profiles = new HashMap<String, PlayerProfile>();
2012-04-27 11:47:11 +02:00
/**
* Load users.
*/
public static void loadUsers() {
new File(mcMMO.flatFileDirectory).mkdir();
new File(mcMMO.leaderboardDirectory).mkdir();
2012-07-04 03:23:32 +02:00
new File(mcMMO.usersFile).createNewFile();
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
2012-06-12 05:32:56 +02:00
* @return the player's profile
2012-04-27 11:47:11 +02:00
*/
2012-06-12 05:32:56 +02:00
public static PlayerProfile addUser(Player player) {
2012-06-08 19:13:17 +02:00
String playerName = player.getName();
2012-06-24 22:46:45 +02:00
PlayerProfile playerProfile = profiles.get(playerName);
2012-06-08 19:13:17 +02:00
2012-06-24 22:46:45 +02:00
if (playerProfile != null) {
//The player object is different on each reconnection and must be updated
playerProfile.setPlayer(player);
}
else {
playerProfile = new PlayerProfile(player, playerName, true);
2012-06-08 19:13:17 +02:00
2012-06-24 22:46:45 +02:00
profiles.put(playerName, playerProfile);
2012-04-27 11:47:11 +02:00
}
2012-06-08 19:13:17 +02:00
2012-06-12 05:32:56 +02:00
return playerProfile;
2012-04-27 11:47:11 +02:00
}
2012-06-25 14:30:55 +02:00
/*
* Remove a user.
*
* @param playerName The name of the player to remove
*/
public static void remove(String playerName) {
profiles.remove(playerName);
}
2012-04-27 11:47:11 +02:00
/**
* Clear all users.
*/
2012-06-25 14:30:55 +02:00
public static void clearAll() {
2012-06-08 19:13:17 +02:00
profiles.clear();
2012-04-27 11:47:11 +02:00
}
2012-06-25 14:30:55 +02:00
/*
* Save all users.
*/
public static void saveAll() {
for (PlayerProfile playerProfile : profiles.values()) {
playerProfile.save();
}
}
2012-04-27 11:47:11 +02:00
/**
* Get all PlayerProfiles.
*
* @return a HashMap containing the PlayerProfile of everyone in the database
*/
2012-06-24 22:46:45 +02:00
public static Map<String, PlayerProfile> getProfiles() {
2012-06-08 19:13:17 +02:00
return profiles;
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-06-24 22:46:45 +02:00
return profiles.get(playerName);
2012-04-27 11:47:11 +02:00
}
}