Solved obtain data from a skull on the floor as his name when I break

Discussion in 'Spigot Plugin Development' started by KohakuSaintCrown, May 18, 2016.

  1. how obtain data from a skull on the floor as his name when I break, i need know how get that, help please.
     
  2. Well it was kind of hard trying to figure out exactly what you wanted because I couldn't really figure out what you said but this is what I got from what I did understand.

    [TUTORIAL]

    First we would listen for the BlockBreakEvent assuming that is when we want to retrieve the information

    Code (Text):
    @EventHandler
        public void onBreak(BlockBreakEvent event){}
    Then check the block type to make sure that the block being broken is a skull

    Code (Text):

    // Then we make sure the block being broken is a skull
    if(event.getBlock().getType().equals(Material.SKULL)){

    }
     
    There's probably a more efficient way of doing what I'm going to do next but I wanted to stick conventional so

    Code (Text):
     // There's a collection of items a block drops I assume this is for dispensers, furnaces etc but we go through it to
                // check if it drops a skull item
                for(ItemStack drop : event.getBlock().getDrops()){

    }
    After that in my case I checked the drops of the block to make sure it actually drops a head, basically just to make sure

    Code (Text):
     // If any of the drops are a skull then we can retrieve any information from it
                    if(drop.getType().equals(Material.SKULL_ITEM)){
                        SkullMeta meta = (SkullMeta) drop.getItemMeta();

                    }
    And then we retrieve any data that we want from the head for example the owner

    Code (Text):
     String name = meta.getOwner();
    And that's about it for this tutorial for the full tested code with example there's a spoiler below.

    [TESTED]

    Code (Text):
    @EventHandler
        public void onBreak(BlockBreakEvent event){
            // Then we make sure the block being broken is a skull
            if(event.getBlock().getType().equals(Material.SKULL)){

                // There's a collection of items a block drops I assume this is for dispensers, furnaces etc but we go through it to
                // check if it drops a skull item
                for(ItemStack drop : event.getBlock().getDrops()){

                    // If any of the drops are a skull then we can retrieve any information from it
                    if(drop.getType().equals(Material.SKULL_ITEM)){
                        SkullMeta meta = (SkullMeta) drop.getItemMeta();

                        // Just a little test I conducted :P
                        String name = meta.getOwner();
                        event.getPlayer().sendMessage("You just broke " + name + "'s skull!");
                    }
                }
            }
        }
     
    • Informative Informative x 1
    • Useful Useful x 1
  3. Cool man you are awesome !!! you save me!! :D
     
    • Friendly Friendly x 1