Help with player skulls?

Discussion in 'Plugin Development' started by Erestor1999, May 11, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Erestor1999

    Ok, so here is my code.

    Code:
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            if (e.getEntity() instanceof Player) {
            e.getDrops().remove(new ItemStack(Material.IRON_PICKAXE));
          
            ItemStack PlayerBones = new ItemStack(Material.BONE, 5);
            ItemMeta pb = PlayerBones.getItemMeta();
            pb.setDisplayName("" + ChatColor.GRAY + ChatColor.BOLD + "Player Bones");
            PlayerBones.setItemMeta(pb);
          
            ItemStack Gem = new ItemStack(Material.EMERALD, 10);
            ItemMeta gem = Gem.getItemMeta();
            gem.setDisplayName("" + ChatColor.GREEN + ChatColor.BOLD + "Gem");
            Gem.setItemMeta(gem);
          
        
          
           ItemStack Head = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
           SkullMeta head = (SkullMeta) Head.getItemMeta();
           ItemMeta head1 =  Head.getItemMeta();
           head.setOwner(((Player) e).getName());
           head1.setLore(Arrays.asList(ChatColor.RED + "Right Click" + ChatColor.GRAY + " to heal!",        ChatColor.GRAY + "Heals " + ChatColor.RED + "100" + ChatColor.GRAY + "HP"));
           head1.setDisplayName("" + ChatColor.AQUA + ChatColor.BOLD + ((Player) e).getName() + "'s Head");
           Head.setItemMeta(head1);
           Head.setItemMeta(head);
       
          
            e.getDrops().add(Head);
            e.getDrops().add(Gem);
            e.getDrops().add(PlayerBones);
            }
        }
    It doesnt bring up any errors. However, it simple doesnt work at all in game. If I change it to this it works flawlessly, but players dont drop their skulls.

    Code:
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            if (e.getEntity() instanceof Player) {
            e.getDrops().remove(new ItemStack(Material.IRON_PICKAXE));
          
            ItemStack PlayerBones = new ItemStack(Material.BONE, 5);
            ItemMeta pb = PlayerBones.getItemMeta();
            pb.setDisplayName("" + ChatColor.GRAY + ChatColor.BOLD + "Player Bones");
            PlayerBones.setItemMeta(pb);
          
            ItemStack Gem = new ItemStack(Material.EMERALD, 10);
            ItemMeta gem = Gem.getItemMeta();
            gem.setDisplayName("" + ChatColor.GREEN + ChatColor.BOLD + "Gem");
            Gem.setItemMeta(gem);
          
     
            e.getDrops().add(Gem);
            e.getDrops().add(PlayerBones);
            }
        }
    Whats going on? Im so confused please help ;-;
     
  2. @Erestor1999 Get rid of Head.setItemMeta(head);
    You're setting the item meta back to the default (which is a blank skull).
     
  3. @Erestor1999 Also couple of tips: Firstly, how often do you think the entity in a PlayerDeathEvent won't be a Player? ;) It literally can't be false unless it's null... which it won't be :p

    Secondly, please follow Oracle's Java naming conventions - your code is confusing to read otherwise.
     
  4. Offline

    Abs0rbed

    @Erestor1999 @DJSkepter also you shouldn't have to create an entirely new meta to set the display name. Since SkullMeta extends ItemMeta, you can call the methods for that on the 'head' object
     
  5. Offline

    Erestor1999

    Yeah, I had it set to EntityDeathEvent, or something like that before, and I forgot to take out the e.getEntity() instanceof Player :p

    Also sorry about the code being hard to read. I have never taken a programming coarse and I have only been playing with plugins for about 3 weeks, so Imma MASSIVE noob at Java :p
    Im trying to learn what I can through the internet.
     
  6. Offline

    Zombie_Striker

  7. @Erestor1999 You don't need to take a programming course to know the conventions of a particular language - here's Oracle official page of Java conventions :) But yeah, as above, you really need to learn Java before learning Bukkit, otherwise it doesn't work properly.


    @Zombie_Striker Java isn't in all-caps ;)
     
Thread Status:
Not open for further replies.

Share This Page