Get head with player skin

Discussion in 'Plugin Development' started by Julian1Reinhardt, Sep 3, 2013.

Thread Status:
Not open for further replies.
  1. I just wanted to know, how pluggins like playerheads are able to get the head with the player skin?
     
  2. Offline

    Chinwe

    It's just like renaming items with ItemMeta, except you cast it to SkullMeta, and then can .setOwner(playerName);
    If you don't know about renaming items, read this ;)
     
  3. I'm lost with all teh skullMeta and ItemStack and stuf D: This is what I kurrently have (ITs probably totally mmixed up and wrong...)
    Code:
    Player p = (Player) sender;
                                Inventory pi = p.getInventory();   
                                ItemStack skull = new ItemStack(Material.SKULL, 1);
                                SkullMeta skullMeta = skull.getMetadata(args[1]);
                                skullMeta.setDisplayName(args[1]+"'s head");
                                skullMeta.setOwner((Player)args[1]);
                                Skull.setItemMeta(skullMeta);
                                pi.addItem(skull);
     
  4. Offline

    Chinwe

    Very almost :)

    Code:
    Player p = (Player) sender; // have you checked (sender instanceof Player) before casting?
     
    Inventory pi = p.getInventory();
     
    ItemStack skull = new ItemStack(Material.SKULL, 1);
     
    SkullMeta skullMeta = skull.getMetadata(); // no arguments needed
     
    // skullMeta.setDisplayName(args[1]+"'s head"); // this line isn't needed
     
    skullMeta.setOwner(args[1]); // takes a String for the name
     
    skull.setItemMeta(skullMeta); // lowercase s, otherwise you're using a static method from Skull.class :o
     
    pi.addItem(skull);
     
  5. Offline

    Deleted user

  6. Error: "The method getMetadata() is undefined for the type ItemStack"
     
  7. Offline

    Chinwe


    It most definitely is, are you importing the craftbukkit ItemStack instead of org.bukkit?
     
  8. Chinwe
    I imported org.bukkit.inventory.ItemStack;

    Chinwe
    so is that wrong or correct?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  9. Offline

    Chinwe

    Julian1Reinhardt
    It's correct, not sure why it can't find getItemMeta() :<
    You could try refreshing the project if you're using Eclipse, and/or restarting IDE, as sometimes errors persist even though they've been fixed :oops:
     
  10. Chinwe
    Nopee, still there.
    I got some corrections though (Suggested by eclipse):
    Either add (Object) to skull or change to .getData()
     
  11. Offline

    Chinwe

    Ah apoligies, I forgot about casting it :oops:

    Code:
    SkullMeta meta = (SkullMeta) skull.getItemMeta();
     
  12. Chinwe
    Still the same problem... :/ Would Object casting work?
     
  13. Offline

    Chinwe

    Julian1Reinhardt
    Nupe, try rewriting that line from scratch, worked for me:
    [​IMG]
    EDIT: Ohdam, why did I not notice I wrote Metadata instead of ItemMeta?
    Gg to Assult for noticing :)
     
  14. Julian1Reinhardt
    Nah, still not working. And your eclipse highlights that part, too!

    Chinwe
    You sait it worked for you, but the .getMetadata(); part is also highlighted.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  15. Offline

    Chinwe

    Julian1Reinhardt
    By that I mean that the original line did, but writing it out again on the second line worked like a charm :cool:
     
  16. Offline

    Hoolean

    Julian1Reinhardt

    No, he said to replace it with:
    Code:
    SkullMeta meta = (SkullMeta) skull.getItemMeta();
    and it would no longer be highlighted. :)

    If it still is after you've changed it, it's possibly a different error.

    Please bear in mind you're supposed to replace it; the only reason he had both of the lines there in the screenshot was to show the difference :)

    EDIT: Ninja'd by Chinwe :p
     
    Chinwe likes this.
  17. Offline

    Assult

    I think its getItemMeta(), not metadata :p

    damn, I didnt refresh the page and see that hoolean allready posted

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  18. @Chinwe
    Hoolean
    Assult

    No errors in eclipse, but when i try it, this line
    Code:
    SkullMeta meta = (SkullMeta)skull.getItemMeta();
    Throws a ClassCastException
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'th'
    in plugin Aurora Talking Heads v1.4
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    1)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerCon
    nection.java:962)
            at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.j
    ava:880)
            at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :837)
            at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
            at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :116)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R2.inventor
    y.CraftMetaItem cannot be cast to org.bukkit.inventory.meta.SkullMeta
            at me.virhonestum.talkheads.main.onCommand(main.java:228)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
     
  19. Offline

    Chinwe

    That looks like a craftbukkit ItemMeta instead of org.bukkit, org.bukkit.craftbukkit.v1_6_R2.inventory.CraftMetaItem from your imports list and reimport the Bukkit one :)
     
  20. @Chinwe
    org.bukkit.craftbukkit.v1_6_R2.inventory.CraftMetaItem is not in my imports....
    These are my imports
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.SkullMeta;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
  21. Am I the only one who finds the thread title kind of.. disturbing?

    yes, I'm childish, whatcha gonna do?
     
  22. Assist
    Haha, It would be worse if it was like "Get a players head" :D
     
  23. Offline

    Hoolean

    Julian1Reinhardt

    This means you're not using it on a Skull object, I believe :)
     
  24. Offline

    chasechocolate

    Use Material.SKULL_ITEM.
     
  25. Offline

    Chinwe

  26. Chinwe
    Now when i say
    Code:
    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1).setDurability((short) 3);
    It says "Cant convert from void to ItemStack
     
  27. Offline

    Chinwe

    Julian1Reinhardt
    To use .setDurablility(), you would have to put it on a new line, ie:
    ItemStack skull = ...;
    skull.setDurability();

    But you can just set it in the constructor ;)
    Code:
    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
     
    Julian1Reinhardt likes this.
  28. Chinwe
    Thank you sooo much :) It worked
     
Thread Status:
Not open for further replies.

Share This Page