2013-03-01 06:52:01 +01:00
|
|
|
package com.gmail.nossr50.util.player;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2014-02-07 18:50:04 +01:00
|
|
|
import java.util.ArrayList;
|
2013-07-15 15:14:23 +02:00
|
|
|
import java.util.Collection;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
|
|
|
import org.bukkit.OfflinePlayer;
|
2014-02-07 18:50:04 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
2012-04-27 11:47:11 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2014-02-07 18:50:04 +01:00
|
|
|
import org.bukkit.metadata.FixedMetadataValue;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2013-08-20 20:10:49 +02:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2013-03-01 06:52:01 +01:00
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
public final class UserManager {
|
2012-05-05 19:51:54 +02:00
|
|
|
|
2013-08-22 00:37:48 +02:00
|
|
|
private UserManager() {}
|
2013-01-26 23:01:55 +01:00
|
|
|
|
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) {
|
2014-02-07 18:50:04 +01:00
|
|
|
McMMOPlayer mcMMOPlayer = new McMMOPlayer(player);
|
|
|
|
player.setMetadata(mcMMO.playerDataKey, new FixedMetadataValue(mcMMO.p, mcMMOPlayer));
|
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
|
|
|
*
|
2014-02-07 18:50:04 +01:00
|
|
|
* @param player The Player object
|
2012-06-25 14:30:55 +02:00
|
|
|
*/
|
2014-02-07 18:50:04 +01:00
|
|
|
public static void remove(Player player) {
|
|
|
|
player.removeMetadata(mcMMO.playerDataKey, mcMMO.p);
|
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() {
|
2014-02-07 18:50:04 +01:00
|
|
|
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
|
|
|
|
remove(player);
|
|
|
|
}
|
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() {
|
2014-02-07 18:50:04 +01:00
|
|
|
Player[] onlinePlayers = mcMMO.p.getServer().getOnlinePlayers();
|
|
|
|
mcMMO.p.debug("Saving mcMMOPlayers... (" + onlinePlayers.length + ")");
|
|
|
|
|
|
|
|
for (Player player : onlinePlayers) {
|
|
|
|
getPlayer(player).getProfile().save();
|
2012-06-25 14:30:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 15:14:23 +02:00
|
|
|
public static Collection<McMMOPlayer> getPlayers() {
|
2014-02-07 18:50:04 +01:00
|
|
|
Collection<McMMOPlayer> playerCollection = new ArrayList<McMMOPlayer>();
|
|
|
|
|
|
|
|
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
|
|
|
|
playerCollection.add(getPlayer(player));
|
|
|
|
}
|
|
|
|
|
|
|
|
return playerCollection;
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
/**
|
2013-08-20 20:10:49 +02:00
|
|
|
* Get the McMMOPlayer of a player by name.
|
2012-07-06 17:57:17 +02:00
|
|
|
*
|
2013-08-20 20:10:49 +02:00
|
|
|
* @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) {
|
2013-08-21 16:20:12 +02:00
|
|
|
return retrieveMcMMOPlayer(playerName, false);
|
2013-08-19 10:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-02-27 16:56:21 +01:00
|
|
|
public static McMMOPlayer getOfflinePlayer(OfflinePlayer player) {
|
2014-02-07 18:50:04 +01:00
|
|
|
if (player instanceof Player) {
|
|
|
|
return getPlayer((Player) player);
|
|
|
|
}
|
2013-08-20 20:10:49 +02:00
|
|
|
|
2014-02-27 16:56:21 +01:00
|
|
|
return retrieveMcMMOPlayer(player.getName(), true);
|
2013-11-01 14:12:06 +01:00
|
|
|
}
|
|
|
|
|
2014-02-27 16:56:21 +01:00
|
|
|
public static McMMOPlayer getOfflinePlayer(String playerName) {
|
|
|
|
return retrieveMcMMOPlayer(playerName, true);
|
2013-08-21 16:20:12 +02:00
|
|
|
}
|
2013-08-22 00:37:48 +02:00
|
|
|
|
2014-02-07 18:50:04 +01:00
|
|
|
public static McMMOPlayer getPlayer(Player player) {
|
|
|
|
return (McMMOPlayer) player.getMetadata(mcMMO.playerDataKey).get(0).value();
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:20:12 +02:00
|
|
|
private static McMMOPlayer retrieveMcMMOPlayer(String playerName, boolean offlineValid) {
|
2014-02-07 18:50:04 +01:00
|
|
|
Player player = mcMMO.p.getServer().getPlayerExact(playerName);
|
2013-08-20 20:10:49 +02:00
|
|
|
|
2014-02-07 18:50:04 +01:00
|
|
|
if (player == null) {
|
|
|
|
if (!offlineValid) {
|
|
|
|
mcMMO.p.getLogger().warning("A valid mcMMOPlayer object could not be found for " + playerName + ".");
|
|
|
|
}
|
2013-08-20 20:10:49 +02:00
|
|
|
|
2014-02-07 18:50:04 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-08-21 16:20:12 +02:00
|
|
|
|
2014-02-07 18:50:04 +01:00
|
|
|
return getPlayer(player);
|
|
|
|
}
|
2013-08-20 20:10:49 +02:00
|
|
|
|
2014-02-07 18:50:04 +01:00
|
|
|
public static boolean hasPlayerDataKey(Entity entity) {
|
2014-02-27 16:56:21 +01:00
|
|
|
return entity != null && entity.hasMetadata(mcMMO.playerDataKey);
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
}
|