Solved Identifying Zombie (?)

Discussion in 'Plugin Development' started by Chloe-chan, Jan 8, 2016.

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

    Chloe-chan

    Hello there, sorry for the ambiguous title, I can't think of a better name for it.

    The problem I am experiencing here is where the World#spawnEntity would call the event CreatureSpawnEvent to decide if it should be cancelled or not. However, I wanted to identify the zombie in the CreatureSpawnEvent itself, but the code to set the tag is after the World#spawnEntity method. Any idea how I might be able to achieve it ?

    Here is an extract of my code:
    PlayerDeathEvent:
    Code:
    Zombie zombie = (Zombie) player.getWorld().spawnEntity(player.getLocation(), EntityType.ZOMBIE);
    /*
    * Some other code here
    */
    zombie.setMetadata(METADATA_TAG, new FixedMetadataValue(main, true));
    Bukkit.broadcastMessage(ChatColor.BLUE + "Player Death Event:");
    Bukkit.broadcastMessage(ChatColor.BLUE + " hasMetadata: " + zombie.hasMetadata(METADATA_TAG));
    CreatureSpawnEvent:
    Code:
    Bukkit.broadcastMessage(ChatColor.BLUE + "Creature Spawn Event:");
    Bukkit.broadcastMessage(ChatColor.BLUE + " isCancelled: " + event.isCancelled());
    Bukkit.broadcastMessage(ChatColor.BLUE + " getEntityType: " + event.getEntityType());
    /*
    * Some more code here
    */
    Zombie zombie = (Zombie) event.getEntity();
    Bukkit.broadcastMessage(ChatColor.BLUE + " hasMetadata: " + zombie.hasMetadata(METADATA_TAG));
    
    The result would be as follows:
    [​IMG]
    This is to show that the CreatureSpawnEvent is called before the zombie.setMetadata (or the broadcastMessage to be precise) line could be reached.

    Thanks in advance!

    Regards,
    Chloe-chan

    EDIT: I've forgotten to mention that both event handlers will manipulate the outcome of the event, so a delayed task isn't viable.
     
    Last edited: Jan 8, 2016
  2. Offline

    aboood-bah

    i'll try i'm on my way home but you better be grateful cuz this is my first post on bukkit:'(
    i use spigot only
     
  3. Offline

    Zombie_Striker

    To solve your problem, you can create a CustomZombie that once the instance is created will receive a metadata tag.
     
  4. Offline

    Chloe-chan

    How would I go about doing this?

    I suppose I can use World#spawn instead ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 8, 2016
  5. Offline

    Zombie_Striker

  6. Offline

    aboood-bah

    there isn't a direct way but you can create your own entity and then rename them or simply add them to a list so you will simply have to use get(int) method
     
  7. Offline

    Chloe-chan

    I assume that I have to use NMS for CustomZombie ? Would there be any other way I can do it without using NMS ? I wouldn't want my plugin to break with an update.
     
  8. if you don't like NMS alternatively you could store the mobs UUID upon spawning and use that list for reference.
    of course that won't be as lightweight as nms, but it's a solution nonetheless
     
  9. Offline

    Chloe-chan

    The problem with using a list of UUID is the same as the OP, I can only store it after the CreatureSpawnEvent had triggered, which I needed it during the CreatureSpawnEvent.
     
  10. Ah, my bad.
    What is it you're specifically trying to do? perhaps we can think of another way of achieving the same result.
     
  11. Offline

    Chloe-chan

    The problem I am facing is that WorldGuard (I think it is WorldGuard, at least) prevents spawning of mobs in the defined area, including the zombie I have created in my plugin. I want to un-cancel the CreatureSpawnEvent if the creature in this event is the specific zombie I have created, thus having this issue.
     
  12. ah okay, in that case I'm afraid NMS will indeed be your only way of solving this as that's the only way to include custom data in the entity during the CreatureSpawnEvent instead of after.
     
  13. Offline

    teej107

    How so?
    What exactly are you trying to do? Is it just identifying individual zombies? Is that it?
     
  14. Offline

    Chloe-chan

    The 1st event (PlayerDeathEvent) will alter the drops of the player, whereas the 2nd event (CreatureSpawnEvent) will un-cancel the event if the creature spawned is the one I've created in the first event.
     
  15. Offline

    teej107

    Got it!
    Huh? Why are you listening in the CreatureSpawnEvent? Are you cancelling every creature spawn except for the one created in the PlayerDeathEvent?
     
  16. Offline

    Chloe-chan

    No, but WorldGuard is.
     
  17. Offline

    teej107

    Chloe-chan likes this.
  18. Offline

    Chloe-chan

    Sure, I'll look it up. Thanks anyways, and go get a good sleep. :)

    EDIT: I just realised that I still can't identify the creature even I'm using event manipulation, as any attempts to "tag" the zombie will happen after the event has completed. :p
     
    Last edited: Jan 9, 2016
    teej107 likes this.
  19. Offline

    Chloe-chan

    Anyways, sorry for not updating this thread.
    I managed to solve this ungracefully by creating a boolean variable and setting it to true before spawning the zombie.
    In the CreatureSpawnEvent that follows, I checked if the variable is true, and do simple creature type checking.
    If it passes, I'll change the variable back to false and handle the zombie.

    I know it isn't graceful and prone to bugs, but it's a workaround without the use of NMS. :p
     
Thread Status:
Not open for further replies.

Share This Page