Solved Faster loading of playerheads?

Discussion in 'Spigot Plugin Development' started by Goobsy, Jan 26, 2016.

  1. Hello everyone! I want to know if there is a way to render the skin of a player head in an inventory faster. I used the normal way to create an playerhead using the ItemStack, SkullMeta and the setOwner() method. It often tooks ages to load the playerhead. I found a way to create an Inventory when the player joins the server. When I open this Inventory everything works fine, the skins are loading instantly. The problem is That i am working on a friend System and the heads have to update when the player opens the Inventory. So I Can't create an static inventory. And the problem with the playerhead skin rendering Returns. Does anyone have an idea to fix this?
    With kind regards/Mit freundlichen Grüßen,
    Goobsy

    PS: I hope that my english is not too bad...
     
  2. Add the player head behind the player(s) so the skin will load on their client. It's all about caches.
     
    • Like Like x 1
  3. Thank you for your quick reply but i don't know exactly what you mean with 'Add the player head behind the player(s)".
     
    • Like Like x 1
    • Agree Agree x 1
  4. Place it as a block somewhere hidden. Maybe far above them but in their chunk so it renders (downloads the texture).
     
  5. Pretty certain that won't draw, but it may download the texture. Unsure, someone would need to test or verify that.
     
    • Agree Agree x 1
  6. Thank you all for your replies. But there are two Problems with this: 1. I have to save the position of the head, get the block there, transfer it into an ItemStack, because everytime I use the setOwner Method it will cache the head again. So I need exactly this placed Block. And this would make the whole thing too complicated. 2. This is not so important, but my friend-system runs on a bungee-Server with a MySQL Database. It might be that a player, from who I want a head, hasn't been online before. (Multi-Lobbys). And when I place a Head of an offline player the skin won't render. I can update the block but nothing happens.

    The only solution that i found is very server-unfriendly. Everytime a player joins the bungee-network or changes a server, for all his online friends will an inventory created and put into a HashMap. now the skins have time to render before a player opens the inventory. But this is not very ressource-friendly.
     
  7. Doesn't seem too bad tbh. If it actually works.
     
    • Like Like x 1
  8. It will work but i hoped that there is a better, more ressource friendly, solution.
     
    #8 Goobsy, Jan 27, 2016
    Last edited: Jan 27, 2016
  9. You don't need to get the head from the block.

    Just place it with the skin you want and the client should download it.

    You can then create your ItemStack as you normally would and since the skin is already cached clientside, it will load almost instantly ;D

    I actually never though of this solution before, so +1 for AniSkywalker for thinking this through.

    Cheers,
    Moulberry
     
  10. I placed the players head one block over the Player, but i still the problem with the player head in the inventory. It seems that everytime I create the ItemStack the Skin gets downloaded...
     

    Attached Files:

  11. How are you exactly creating those heads? The skins are downloaded by the client and it doesn't do it twice.
     
  12. Code (Text):
                        ItemStack s = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                        SkullMeta friendMeta = (SkullMeta) s.getItemMeta();
                        friendMeta.setOwner(name);
                        friendMeta.setDisplayName("§5§l" + name);
                        friendMeta.setLore(Arrays.asList(Main.l.IS_ONLINE_ON + FriendMySQLManager.getServer(uuid)));
                        s.setItemMeta(friendMeta);
                        online.add(s);
    That's how I create the player heads.
     
  13. Hey!

    I finally found a really easy solution for this. When a player joins the server, i get for every friend the player head.

    Code (Text):
    for(Player player : FriendManger.getFriends(p)) {
      ItemStack s = new ItemStack(Material.SKULL_ITEM, 1, 3);
      SkullMeta meta = (SkullMeta) s.getItemMeta();
      meta.setOwner(players.getName());
      s.setItemMeta(meta);
      skulls.put(players, CraftItemStack.asNMSCopy(s);
    }
    'skulls' is a simple HashMap<Player, ItemStack>. But you have to import ItemStack from net.minecraft.server.

    If you want to add the skull to the Inventory, you can get the skull with:

    Code (Text):
    ItemStack toAdd = CraftItemStack.asBukkitCopy(skulls.get(players);
    inventory.setItem(0, toAdd);
    You can also edit the ItemMeta after getting the player head and the skull will load instantly!

    Thank you all for your support!
    With kind regards, mit freundlichen Grüßen,
    Goobsy
     
    • Like Like x 1
    • Agree Agree x 1
    • Creative Creative x 1
  14. Humph. I'm surprised that works.