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

137 lines
3.6 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-08 19:13:17 +02:00
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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-08 19:13:17 +02:00
private static List<PlayerProfile> profiles = new ArrayList<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();
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-08 19:13:17 +02:00
String playerName = player.getName();
for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
PlayerProfile playerProfile = it.next();
if (playerProfile.getPlayerName().equals(playerName)) {
//The player object is different on each reconnection and must be updated
playerProfile.setPlayer(player);
return;
}
2012-04-27 11:47:11 +02:00
}
2012-06-08 19:13:17 +02:00
//New player, or already removed from the list
profiles.add(new PlayerProfile(player, true));
2012-04-27 11:47:11 +02:00
}
/**
* Clear all users.
*/
public static void clearUsers() {
2012-06-08 19:13:17 +02:00
profiles.clear();
2012-04-27 11:47:11 +02:00
}
/**
* Get all PlayerProfiles.
*
* @return a HashMap containing the PlayerProfile of everyone in the database
*/
2012-06-08 19:13:17 +02:00
public static List<PlayerProfile> getProfiles() {
return profiles;
2012-04-27 11:47:11 +02:00
}
/**
* Remove a user from the database.
*
* @param player The player to remove
*/
2012-06-08 19:13:17 +02:00
public static void removeUser(OfflinePlayer player) {
removeUser(player.getName());
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
*/
2012-06-08 19:13:17 +02:00
public static void removeUser(String playerName) {
for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
if (it.next().getPlayer().getName().equals(playerName)) {
it.remove();
return;
}
}
}
/**
* Remove a user from the DB by its profile.
*
* @param playerProfile the profile of the player to remove
*/
public static void removeUser(PlayerProfile playerProfile) {
profiles.remove(playerProfile);
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) {
for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
PlayerProfile playerProfile = it.next();
2012-06-08 19:13:17 +02:00
if (playerProfile.getPlayerName().equals(playerName)) {
return playerProfile;
2012-04-27 11:47:11 +02:00
}
}
2012-06-08 19:13:17 +02:00
return null;
2012-04-27 11:47:11 +02:00
}
}