How to get head skin of offline player?

Discussion in 'Spigot Plugin Development' started by CrystalDev, May 12, 2015.

  1. How to get head skin of offline player?

    My Code:

    Code (Text):
      public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("head"))
        {
          Player p = (Player)sender;
          if (p.hasPermission("spawnhead.use"))
              if(args.length == 0){
                    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short)SkullType.PLAYER.ordinal());

                    SkullMeta meta = (SkullMeta)skull.getItemMeta();
                    meta.setOwner(p.getName());
                    meta.setDisplayName(ChatColor.GREEN + p.getName() + "'s Head");
                    skull.setItemMeta(meta);

                    p.getPlayer().getInventory().addItem(new ItemStack[] { skull });
                    p.getPlayer().sendMessage(ChatColor.GREEN + "Spawn your head in your inventory!");
              } else if(args.length == 1){
                 
                  Player targetPlayer = p.getServer().getPlayer(args[0]);
                 
                   ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short)SkullType.PLAYER.ordinal());

                    SkullMeta meta = (SkullMeta)skull.getItemMeta();
                    meta.setOwner(targetPlayer.getName());
                    meta.setDisplayName(ChatColor.GREEN + targetPlayer.getName() + "'s Head");
                    skull.setItemMeta(meta);

                    p.getPlayer().getInventory().addItem(new ItemStack[] { skull });
                    p.getPlayer().sendMessage(ChatColor.GREEN + "Spawn " + targetPlayer.getName() + "'s Head in your inventory!");
                 
              }
          return true;
        }
        return false;
      }

    IN " if(args.length == 1) " player type /head <Player> ( error if the player is not online )

    How I fix this?
     
  2. Use a OfflinePlayer?
     
    • Like Like x 1
    • Agree Agree x 1
  3. Change getPlayer() to getOfflinePlayer(). Javadocs.

    You really don't even have to get the player, just pass args[0] to the skull meta. Very unnecessary to get the Player object.
     
    • Like Like x 1
  4. Thank you I will try
     
  5. Not Work ! I'm doing something wrong

    [​IMG]

    Help :1
     
  6. @CrystalDev well getOfflinePlayer returns an OfflinePlayer, which is not a Player. Change the field type of targetPlayer to OfflinePlayer.
     
    • Like Like x 1
  7. OfflinePlayer targetPlayer = p.getServer().getOfflinePlayer(args[0]);
     
    • Like Like x 1
  8. Thanks :) Work perfectly