How to check what type of spawn egg it is?

Discussion in 'Plugin Development' started by NoobDoesMC, Jun 21, 2016.

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

    NoobDoesMC

    I'd like to find out how I can use bukkit to check the type of spawn egg the player clicks.

    My code so far:

    Code:
    if (player.getItemInHand().getType() == Material.MONSTER_EGG) {
      player.sendMessage("You clicked a " + "" + " spawn egg.");
    }
    How can I tell the player what type of spawn egg they've clicked?
     
  2. Offline

    timtower Moderator Moderator

    Moved to plugin development.
    @NoobDoesMC Check the data/durability value. They can tell what mob it is.
    They at least give you more information.
     
  3. Offline

    NoobDoesMC

    I tried this, and no matter what spawn egg I am holding, both the Data and Durability seem to return 0. Data actually returns "SPAWN EGG(null)"

    i.e.
    Code:
    player.sendMessage("You clicked a "+player.getItemInHand().getData()+" spawn egg.");
    Says: "You clicked a SPAWN EGG{null} spawn egg." and
    Code:
    player.sendMessage("You clicked a "+player.getItemInHand().getDurability()+" spawn egg.");
    Says: "You clicked a 0 spawn egg."

    These values do not change no matter what spawn egg I am holding.
     
  4. Offline

    timtower Moderator Moderator

  5. Offline

    NoobDoesMC

    According to the Minecraft Wiki article on spawn eggs the data value has no effect on the spawn egg type.

    Which is weird, because in-game, I can run:
    Code:
    /give @p minecraft:spawn_egg 1 50
    To give myself a creeper spawn egg.

    I've tried to use that, but I can't figure out how to cast player.getItemInHand() to type SpawnEgg, and then I couldn't get the spawn egg type either.

    Could you post an example?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  6. Offline

    timtower Moderator Moderator

    @NoobDoesMC Has to do with MaterialData, can't write examples now.
     
  7. Offline

    NoobDoesMC

    Thank you, that's very helpful. It's not like I spent almost 6 hours yesterday trying to figure this out already, having already come across MaterialData, CraftItemStack, NBTTagCompund, getItemMeta amongst many other obscure classes and methods.
     
  8. Offline

    timtower Moderator Moderator

    @NoobDoesMC My apologies that I am not on a machine with Bukkit etc on it.
    Get the materialdata, see if it is an instance of spawnegg, cast, check mob.
     
  9. Offline

    NoobDoesMC

    I tried this before:

    Code:
    player.sendMessage("You clicked a "+((SpawnEgg)player.getItemInHand().getData()).getSpawnedType()+" spawn egg.");
    But it says:

    "You clicked a null spawn egg."
     
  10. Offline

    timtower Moderator Moderator

    @NoobDoesMC Please try it without sending the message right away but with checking everything first.
     
  11. Offline

    CeramicTitan Retired Staff

    A few things; What happens if you click with other spawn eggs? Does it still get you the null value?
     
  12. Offline

    NoobDoesMC

    Here:

    Code:
    ((SpawnEgg)player.getItemInHand().getData()).getClass().getName() // "org.bukkit.Material.SpawnEgg"
    player.getItemInHand().getData().getClass().getName()             // "org.bukkit.Material.SpawnEgg"
    ((SpawnEgg)player.getItemInHand().getData()).getSpawnedType()     // null
    player.getItemInHand().getData()                                  // SPAWN EGG{null}
    The values I put in my post above are the same for every spawn egg. The spawn egg type does not change them.

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

    ArsenArsen

    Okay, so
    Code:
    variable d is data cast to class spawn egg.
    variable et is entity type you get using d.getSpawnedType()
    send message to player containing string value of et.
    Use the SpawnEgg.getSpawnedType, so lets assume that 'is' is your ItemStack

    ((SpawnEgg)is.getData()).getSpawnedType().toString()

    Returns: CREEPER

    You can use that in a conjuction with a switch/case to convert it to 'pretty' names. For example CREEPER to Creeper
     
  14. Offline

    NoobDoesMC

    Please ArsenArsen read earlier messages before posting a response. As you can see, your suggested method won't work because getSpawnedType returns null.
     
  15. Offline

    I Al Istannen

    @NoobDoesMC
    What bukkit/spigot version are you using?
     
  16. Offline

    NoobDoesMC

    Craftbukkit 1.10
     
  17. Offline

    elian1203

    You may have to get the NBT tag using NMS. Look for EntityTag.
     
  18. Offline

    I Al Istannen

    @NoobDoesMC
    See here.
    And the Javadoc here.

    Seems like you have a little problem... . Maybe somebody does know the solution though (not involving NBT data reading).
     
  19. Offline

    CeramicTitan Retired Staff

    I dont think the javadocs have been updated to support the new methods yet.
     
  20. Offline

    I Al Istannen

    @CeramicTitan
    The iusse is still unresolved, but apart from that, you are hopefully right!
     
    CeramicTitan likes this.
  21. Offline

    CeramicTitan Retired Staff

    Lol, I was just about to edit my comment
     
  22. Offline

    bwfcwalshy Retired Staff

    It changed to NBT tags, you will have to use these. There is no method yet for changing the egg type but it will be added in ItemMeta when it is.

    For now, you can do something like this:
    Code:
    //Make an ItemStack
    ItemStack is = new ItemStack(Material.MONSTER_EGG);
    //Get the NMS itemstack
    net.minecraft.blah.ItemStack nmsIs = CraftItemStack.asNMSCopy(is);
    //Get the NBT
    NBTTagCompound tag = stack.getTag();
    if(tag == null){
        tag = new NBTTagCompound();
    }
    //Create id tag
    NBTTagCompound id = new NBTTagCompound();
    //Set the id and entity
    id.setString("id", EntityType.INSERT_HERE);
    tag.set("EntityTag", id);
    //Set the mob tag to the new one.
    nmsIs.setTag(tag);
    To then get this as a normal Bukkit ItemStack again just do CraftItemStack.asBukkitCopy(nmsIs);
     
    elian1203 likes this.
  23. Offline

    NoobDoesMC

    I w
    I would like to get the NBT tag so that I can check the {EntityTag:{id:_____}} key, not set anything.

    How can I do this instead? I do not care at all about setting any NBT tags.
     
  24. Offline

    I Al Istannen

    @NoobDoesMC
    The same.
    Some get method called on "tag" with the key "EntityTag" should do it. Can't look right now, may do it later if I don't forget it.
     
    bwfcwalshy likes this.
  25. Offline

    bwfcwalshy Retired Staff

    Simple with what I provided, you could have had a quick look around and found it.

    Either way,
    As @I Al Istannen said you just need to get the NBT, get the compound EntityTag and get the id.

    Code:
    EntityType type = EntityType.fromName(tag.getCompound("EntityTag").getString("id"));
    This will return the EntityType, not going to post the code to get the NBT and tag again. You can look above and work it out.
     
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page