Tutorial How to get the player head texture in your GUI or hand! [1.9+]

Discussion in 'Resources' started by Proxygames14, Jul 17, 2017.

Thread Status:
Not open for further replies.
  1. Since update 1.9 came out the normal skull method didnt worked anymore! You would get an alex/steve head and when you place it it will turn into the real texture! I made a new method for it!
    It works for me and if you got any errors! Please tell me and I will try to fix it!
    But here is the turtorial!

    When you place the block it will be updated! So why dont you first place the block with the targetted texture!
    Then break it and you can get the ItemStack from the method (ItemSpawnEvent)
    So you can store the ItemStack and use it in the GUI
    Here is an example

    Code:
       
        @SuppressWarnings("deprecation")
        public void CreateSkull(Player p) {  
              
               String owner = p.getName();
               name = owner;
               Location loc = p.getLocation();
               loc.getBlock().setType(Material.SKULL);
               loc.getBlock().setData((byte) 3);
               Skull s = (Skull)loc.getBlock().getState();
               s.setOwner(owner);
               s.update();
               loc.getBlock().breakNaturally();          
               
        }
    
    First you create the good textured player head!
    It will automaticly add the texture to the skull!
    And then it will break the skull so we can go to the second part

    Code:
        public void checkItem(ItemSpawnEvent event) {
               ItemStack itemStack = event.getEntity().getItemStack();
               if(itemStack.getTypeId() != 397) return;
               if(itemStack.getDurability() != 3) return;
               skull = itemStack;
               if(itemStack.toString().contains("skull-owner=" + name))
               event.getEntity().remove();
               name = null;
        }
        
    Second you track the dropped item and store the ItemStack!
    In a static ItemStack!

    Code:
    public static ItemStack skull;
    You also store the name of the Player so we can delete the custom player head skull after break!

    Code:
    public static String name;
    The last thing you do is using the code to convert it and you are done

    Code:
            (Convert class) skull = new (Convert class)();
            skull.CreateSkull(player);
            ItemStack skull1 = ConvertSkull.skull;
            ItemMeta sk1 = skull1.getItemMeta();
            sk1.setDisplayName(color("&3&l" + player.getName() + "'s &9&lStats"));
            sk1.setLore(lore);
            skull1.setItemMeta(sk1);
            mystatsinv.setItem(4, skull1);
            OpenInv(mystatsinv, player);

    So here is the complete example!


    Code:
    public class ConvertSkull {
    public static ItemStack skull;
    public static String name;
        @SuppressWarnings("deprecation")
        public void checkItem(ItemSpawnEvent event) {
               ItemStack itemStack = event.getEntity().getItemStack();
               if(itemStack.getTypeId() != 397) return;
               if(itemStack.getDurability() != 3) return;
               skull = itemStack;
               if(itemStack.toString().contains("skull-owner=" + name))
               event.getEntity().remove();
               name = null;
        }
       
    
        @SuppressWarnings("deprecation")
        public void CreateSkull(Player p) {  
              
               String owner = p.getName();
               name = owner;
               Location loc = p.getLocation();
               loc.getBlock().setType(Material.SKULL);
               loc.getBlock().setData((byte) 3);
               Skull s = (Skull)loc.getBlock().getState();
               s.setOwner(owner);
               s.update();
               loc.getBlock().breakNaturally();          
               
        }
    }
    

    And to run the code


    Code:
            ConvertSkull skull = new ConvertSkull();
            skull.CreateSkull(player);
            ItemStack skull1 = ConvertSkull.skull;
            ItemMeta sk1 = skull1.getItemMeta();
            sk1.setDisplayName(color("&3&l" + player.getName() + "'s &9&lStats"));
            sk1.setLore(lore);
            skull1.setItemMeta(sk1);
            mystatsinv.setItem(4, skull1);
            OpenInv(mystatsinv, player);


    So I all hoped you learned something about my selfmade method!
    if you got any questions or tips! Just tell it in the comments down below
     
  2. Offline

    Zombie_Striker

    @Proxygames14
    There is actually a simpler way of doing it:
    Code:
    
    //Only here for simplicity
    String owner = p.getName();
    
    //In case the player was standing in water/vines/halfstep, this will save the material type and data.
    Material tempfix = p.getLocation().getBlock().getType();
    byte tempfixD = p.getLocation().getBlock().getData();
    
    //Update the skin
    p.getLocation().getBlock().setType(Material.SKULL);
    p.getLocation().getBlock().setData((byte) 3);
    Skull s = (Skull)loc.getBlock().getState();
    s.setOwner(owner);
    s.update();
    p.getLocation().getBlock().setType(tempfix);
    p.getLocation().getBlock().setData(tempfixD);
    
    //Create the skull itemstack
    ItemStack skull1 = new Itemstack(Material.SKULL_ITEM);
    SkullMeta sk1 = (SkullMeta)skull1.getItemMeta();
    sk1.setDisplayName(color("&3&l" + player.getName() + "'s &9&lStats"));
    sk1.setLore(lore);
    sk1.setOwner(owner);
    skull1.setItemMeta(sk1);
    
    //Setts the item in the inventory.
    mystatsinv.setItem(4, skull1);
    OpenInv(mystatsinv, player);
    
    
    Also, you're code requires an event, which means that class needs to be registered for each instance that is created. Since the skin works regardless of if it is from a block (a block just needs to be placed), create a new item stack instance that has the owner set instead of creating an event.

    Also, don't use static variables. That means there can be only one skull saved at a time for the entire server.
     


  3. Well I didnt looked that direction! But thank you! I forgot that you can get a datatype from a block instead of breaking xD

    Thank you

    But how to create an Instance of an event!
    Could you give me an example?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 26, 2017
  4. Offline

    Machine Maker

    I'm fairly sure you could also do this with reflection getting the Game Profile property "textures" and grabbing the texture URL and adding it to an new item stacks Game Profile. This doesn't require any placing of a block, but is probably much slower because it requires getting fields through reflection.
     
  5. @Proxygames14
    Well, getting that data properly is a bit tricky (especially since you should do it async). Can you not just include the MojangAPIUtil class in your project?

    If you wanted to try to create a down-sized version of the class, you could strip out everything except for the makeAsyncGetRequest, getSkinData, getUUIDFromString and getSkinDataAsync methods, the SkinData class, and the PARSER static field.
     
    Last edited: Sep 9, 2017
Thread Status:
Not open for further replies.

Share This Page