package leigh.minecraft.link.bukkit;

import leigh.minecraft.link.PlayerNameProvider;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;

import java.util.UUID;

/**
 * Implementation of {@link PlayerNameProvider} backed by Bukkit.
 *
 * @author Alex Leigh
 */
public class BukkitPlayerNameProvider implements PlayerNameProvider {
    @Override
    /**
     * Return the name of the player. If the lookup fails for some reason (like that ID has never logged into
     * the server), then the ID is returned in string form.
     */
    public String getPlayerName(UUID playerId) {
        OfflinePlayer p = Bukkit.getOfflinePlayer(playerId);
        if(p==null) return null;
        return p.getName();
    }
}
