Solved What is an OfflinePlayer?

Discussion in 'Spigot Plugin Development' started by Kingskew, Jun 10, 2021.

  1. I am trying to make a plugin that involves needing a head of a player, and I did the code to do that.
    Code (Java):
    ItemStack skull = new ItemStack(Material.PLAYER_HEAD);
    SkullMeta meta = (SkullMeta) skull.getItemMeta();
    but for setOwningPlayer(); It says I need an OfflinePlayer inside the (); but I have no clue what that is.
  2. Hello,

    An OfflinePlayer is an object which store information about a Player, regardless if they are online or offline.
    The Player object extends OfflinePlayer, which means you can put a Player object into the SKullMeta#setOwningPlayer method.
    You can create your own OfflinePlayer, based on the UUID of the player, with Server#getOfflinePlayer(uuid).
  3. An OfflinePlayer is, unexpectedly, a Player that is offline. An online player, however, can also be OfflinePlayer. So let me elaborate:
    Player - only an online player.
    OfflinePlayer - either an online or an offline player.
    You can cast it like this:
    Code (Java):

    OfflinePlayer myOffPlayer = (Player)myPlayer;
    As Andross mentioned, you can get that player with Server.getOfflinePlayer(UUID uuid). There's also a deprecated method to get it with Server.getOfflinePlayer(String name), but I don't recommend it since the name can change. So if you got a name only, get the OfflinePlayer like this:
    Code (Java):

    String name = "the_name_";
    OfflinePlayer namedPlayerBad = Bukkit.getOfflinePlayer(name);
    OfflinePlayer uuidPlayerGood = Bukkit.getOfflinePlayer(namedPlayerBad.getUniqueId());
    or, more shortly:
    Code (Java):

    OfflinePlayer uuidPlayerGood = Bukkit.getOfflinePlayer(Bukkit.getOfflinePlayer("the_name_").getUniqueId());
    It isn't that inefficient, since you are getting a Player that isn't going to change, since changing UUID isn't an option currently.
  4. thank you
  5. I would advice taking a look at JavaDocs.
    • Agree Agree x 1