2013-03-01 06:52:01 +01:00
|
|
|
package com.gmail.nossr50.util.player;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
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
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2019-06-06 06:11:44 +02:00
|
|
|
import java.util.HashSet;
|
2014-06-29 14:04:36 +02:00
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
public final class UserManager {
|
2012-05-05 19:51:54 +02:00
|
|
|
|
2019-06-06 06:11:44 +02:00
|
|
|
private static HashSet<McMMOPlayer> playerDataSet; //Used to track players for sync saves on shutdown
|
|
|
|
|
2019-06-06 06:43:07 +02:00
|
|
|
private UserManager() {}
|
2013-01-26 23:01:55 +01:00
|
|
|
|
2012-04-27 11:47:11 +02:00
|
|
|
/**
|
2014-08-01 19:35:36 +02:00
|
|
|
* Track a new user.
|
2012-04-27 11:47:11 +02:00
|
|
|
*
|
2014-08-01 19:35:36 +02:00
|
|
|
* @param mcMMOPlayer the player profile to start tracking
|
2012-04-27 11:47:11 +02:00
|
|
|
*/
|
2014-08-01 19:35:36 +02:00
|
|
|
public static void track(McMMOPlayer mcMMOPlayer) {
|
|
|
|
mcMMOPlayer.getPlayer().setMetadata(mcMMO.playerDataKey, new FixedMetadataValue(mcMMO.p, mcMMOPlayer));
|
2019-06-06 06:43:07 +02:00
|
|
|
|
|
|
|
if(playerDataSet == null)
|
|
|
|
playerDataSet = new HashSet<>();
|
|
|
|
|
2019-06-06 06:11:44 +02:00
|
|
|
playerDataSet.add(mcMMOPlayer); //for sync saves on shutdown
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void cleanupPlayer(McMMOPlayer mcMMOPlayer) {
|
|
|
|
playerDataSet.remove(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) {
|
2019-06-06 06:11:44 +02:00
|
|
|
McMMOPlayer mcMMOPlayer = getPlayer(player);
|
2014-02-07 18:50:04 +01:00
|
|
|
player.removeMetadata(mcMMO.playerDataKey, mcMMO.p);
|
2019-06-06 06:43:07 +02:00
|
|
|
|
|
|
|
if(playerDataSet != null && playerDataSet.contains(mcMMOPlayer))
|
|
|
|
playerDataSet.remove(mcMMOPlayer); //Clear sync save tracking
|
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);
|
|
|
|
}
|
2019-06-06 06:11:44 +02:00
|
|
|
|
2019-06-06 06:43:07 +02:00
|
|
|
if(playerDataSet != null)
|
|
|
|
playerDataSet.clear(); //Clear sync save tracking
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:17 +02:00
|
|
|
/**
|
2014-06-04 05:52:22 +02:00
|
|
|
* Save all users ON THIS THREAD.
|
2012-06-25 14:30:55 +02:00
|
|
|
*/
|
|
|
|
public static void saveAll() {
|
2019-06-06 06:11:44 +02:00
|
|
|
ImmutableList<McMMOPlayer> trackedSyncData = ImmutableList.copyOf(playerDataSet);
|
|
|
|
|
|
|
|
mcMMO.p.getLogger().info("Saving mcMMOPlayers... (" + trackedSyncData.size() + ")");
|
2014-02-07 18:50:04 +01:00
|
|
|
|
2019-06-06 06:11:44 +02:00
|
|
|
for (McMMOPlayer playerData : trackedSyncData) {
|
2017-09-01 06:45:22 +02:00
|
|
|
try
|
|
|
|
{
|
2019-06-06 06:11:44 +02:00
|
|
|
mcMMO.p.getLogger().info("Saving data for player: "+playerData.getPlayerName());
|
|
|
|
playerData.getProfile().save(true);
|
2017-09-01 06:45:22 +02:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2019-06-06 06:11:44 +02:00
|
|
|
mcMMO.p.getLogger().warning("Could not save mcMMO player data for player: " + playerData.getPlayerName());
|
2017-09-01 06:45:22 +02:00
|
|
|
}
|
2012-06-25 14:30:55 +02:00
|
|
|
}
|
2019-06-06 06:11:44 +02:00
|
|
|
|
|
|
|
mcMMO.p.getLogger().info("Finished save operation for "+trackedSyncData.size()+" players!");
|
2012-06-25 14:30:55 +02:00
|
|
|
}
|
|
|
|
|
2013-07-15 15:14:23 +02:00
|
|
|
public static Collection<McMMOPlayer> getPlayers() {
|
2014-06-08 18:21:13 +02:00
|
|
|
Collection<McMMOPlayer> playerCollection = new ArrayList<McMMOPlayer>();
|
2014-02-07 18:50:04 +01:00
|
|
|
|
|
|
|
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
|
2014-06-08 18:21:13 +02:00
|
|
|
if (hasPlayerDataKey(player)) {
|
|
|
|
playerCollection.add(getPlayer(player));
|
|
|
|
}
|
2014-02-07 18:50:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-04-13 00:17:05 +02:00
|
|
|
/**
|
|
|
|
* Gets the McMMOPlayer object for a player, this can be null if the player has not yet been loaded.
|
|
|
|
* @param player target player
|
|
|
|
* @return McMMOPlayer object for this player, null if Player has not been loaded
|
|
|
|
*/
|
2019-04-13 21:58:32 +02:00
|
|
|
public static McMMOPlayer getPlayer(Player player) {
|
2019-04-11 12:49:13 +02:00
|
|
|
//Avoid Array Index out of bounds
|
2019-04-14 14:39:23 +02:00
|
|
|
if(player != null && player.hasMetadata(mcMMO.playerDataKey))
|
2019-04-11 12:49:13 +02:00
|
|
|
return (McMMOPlayer) player.getMetadata(mcMMO.playerDataKey).get(0).value();
|
|
|
|
else
|
2019-04-13 21:58:32 +02:00
|
|
|
return null;
|
2014-02-07 18:50:04 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|