Solved Skull MetaData.

Discussion in 'Plugin Development' started by InsertAPI, May 25, 2015.

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

    InsertAPI

    I'm having a problem where the plugin isn't getting the skull metadata.

    This is what I have so far.

    Code:java
    1. package apple.insertapi.events;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.BlockBreakEvent;
    9.  
    10. public class BlockBreak implements Listener {
    11.  
    12. @EventHandler
    13. public void onBlockBreak(BlockBreakEvent e){
    14. Block b = e.getBlock();
    15. if(b.getType() == Material.SKULL)
    16. if(b.hasMetadata("InsertAPI"))
    17. Bukkit.broadcastMessage("hey");
    18. }
    19. }


    What it's supposed to do is that if the skull what a player breaks has the owner of "InsertAPI" then bukkit will broadcast a message.

    It doesn't error but it doesn't work either.

    PS: The event does work as I've removed the 'hasMetadata' part before.

    Any help would be fantastic!
    Thanks.
     
  2. Offline

    Goblom

    hasMetadata is not you are looking for cause Metadatable is not the same as ItemMeta (which the skull owner is stored)....

    In order to do what you are trying to do you need to check if block is instanceof off "Skull" and then cast it to "Skull" and to getOwner on that casted object... For example..
    Code:
    Block block = event.getBlock();
    
    if (block instanceof Skull) {
        Skull skull = (Skull) block;
    
        if (skull.getOwner().equals("Goblom")) {
            // do something
        }
    }
     
  3. Offline

    InsertAPI

    Thank you for the really fast help. However it's still not working.

    I'm really not sure why.

    I also tried to do this:-

    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. Block block = event.getBlock();
    4.  
    5. if (block instanceof Skull) {
    6. Skull skull = (Skull) block;
    7.  
    8. if (skull.getOwner().equals("InsertAPI")) {
    9. Bukkit.broadcastMessage("hey");
    10. }
    11. Bukkit.broadcastMessage("no");
    12. }
    13. }
    14.  
    15. }


    It doesn't even print no!
     
  4. Offline

    I Al Istannen

    @InsertAPI It works for me. Are You sure you set the Metadata?
     
    InsertAPI likes this.
  5. Offline

    InsertAPI

    I belive so. Here's my code to give me the head:-
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if (cmd.getName().equalsIgnoreCase("apple")){
    4.  
    5. Player player = (Player) sender;
    6. PlayerInventory inventory = player.getInventory();
    7.  
    8. ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    9. SkullMeta meta = (SkullMeta) skull.getItemMeta();
    10. //meta.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.MAGIC + "iii" + ChatColor.RESET + "" + ChatColor.YELLOW + "MAGIC APPLE" + ChatColor.DARK_RED + "" + ChatColor.MAGIC + "iii");
    11. meta.setOwner("InsertAPI");
    12. skull.setItemMeta(meta);
    13.  
    14. inventory.addItem(new ItemStack(skull));
    15.  
    16. }
    17. return false;
    18. }
    19. }
     
  6. Offline

    I Al Istannen

    @InsertAPI Oh. You check in the blockBreakEvent if the Skull has the metadata "InsertAPI". You never set this. What you do is setting the owner of the skull, so you need to get the Owner again. If you do this with a block, you would need to do what Goblom mentioned.

    EDIT: One flaw in his code. You need to cast block.getState() to skull not the block itself.
     
    Goblom and InsertAPI like this.
  7. Offline

    InsertAPI

    Huh?

    In the event block, it's getting the owner and in the 'apple' command it's setting this. I have no idea what you mean. :eek:
     
  8. Offline

    I Al Istannen

    @InsertAPI In the apple command you set the owner. In the blockbreak event you try to get the METADATA. That's something totally different from ItemMeta! One is for entites and blocks, the other for itemStacks. You would need to do "Skull skull = (Skull) block.getState()", to get the meta the owner is saved in. You could do that with "skull.getOwner()".
     
    InsertAPI likes this.
  9. Offline

    InsertAPI

    Ah!

    Thank you so much! Great help.
     
  10. Offline

    I Al Istannen

    InsertAPI likes this.
  11. Offline

    Goblom

Thread Status:
Not open for further replies.

Share This Page