The logistics behind onCreatureSpawn...? [HELP]

Discussion in 'Plugin Development' started by DrBoweNur, Jul 14, 2011.

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

    DrBowe

    Code:java
    1.  
    2. public void onCreatureSpawn(CreatureSpawnEvent event) {
    3. Entity e = event.getEntity();
    4. Location eLoc = event.getLocation();
    5. /*
    6. * Everything that spawns will be a zombie
    7. */
    8. if((e instanceof Animals || e instanceof Monster) && (!(e instanceof Zombie) && event.getSpawnReason() == SpawnReason.NATURAL)){
    9. event.setCancelled(true);
    10. e.getWorld().spawnCreature(eLoc, CreatureType.ZOMBIE);
    11. }
    12.  
    13.  
    14. }
    15.  


    Alright, so in theory, this should replace any NATURAL SPAWN with a zombie spawn, instead. HOWEVER, I've been getting some rather bizarre results from this.

    It works...ummmm...a little 'too' well.
    Zombies spawn everywhere, and continue to multiply. I'd assume some sort of endless loop, but its not that.
    I also considered the possibility that spawnCreature was re-firing the event, so I added the NATURAL check. Still not fixed.

    If you cancel a creatureSpawn event, will it simply attempt to re-fire it until it gets through? I can't see any other explanation for this.
     
  2. Offline

    Kohle

    Wouldnt this make it so ALL animals are zombies?
    So all sheep are zombies.​
    All pigs are zombies.​
    So that's probably your problem (maybe)​
     
  3. Offline

    DrBowe

    No, that's EXACTLY my intent.
    However, it's almost as if it were caught in an endless loop, as there are so many zombies spawning, that the server crashes.

    If you use spawnCreature, does that not add it to the server's total mobs (so a CreatureSpawnEvent won't occur)?
     
  4. Offline

    bleachisback

    add the LivingEntity that is returned with world.spawnCreature() to an ArrayList and then check with your if command do see if the entity that spawned is in the ArrayList
     
  5. Offline

    DrBowe

    I'm not sure I follow. How would this help me prevent what's occurring?
     
  6. Offline

    bleachisback

    It makes it so that zombies won't spawn on your zombies. I don't know if it is your actual problem, but it might help.
     
  7. Offline

    DrBowe

    @bleachisback
    Well, I don't think that's the actual problem though.

    I *believe* that the CreatureSpawnEvent (if cancelled) continues to fire until it successfully gets through.

    So, what I *believe* is happening is something along the lines of this:

    - Event fires, if it's not a zombie, the event cancells
    - Zombie spawns where the previous entity WOULD have been
    - The event, having been cancelled, then fires off again (in the same area), trying to meet what I believe to be some sort of 'quota' for CreatureSpawnEvents
    - The process rinses, washes, and repeats. (Thus causing a seemingly infinite amount of zombies to start spawning)

    Has anyone else tried something similar, and gotten a result like this?

    EDIT:
    Changed title, trying to clarify that the issues isn't actually getting things to spawn.
     
  8. Instead of cancelling try doing:

    Code:java
    1. e.remove();
    2. //your spawn code
    3.  
     
  9. Offline

    DrBowe

    @Adamki11s
    Alright, I'll give that a shot.

    EDIT:
    Nope, ends up firing the same issue.
    I'll try setting the health of the spawned entity to 0 to see if that works next, I suppose.

    EDIT 2:
    Same issues.

    I feel like the main issue here is that spawnCreature() doesn't add the creature it spawns to the server's 'database', so to speak.
    Thus, the server thinks there are 0 mobs, even though zombies keep spawning on each event fire.

    Is there any method to trick the server into thinking every zombie that replaces a natural spawn...IS a natural spawn?

    I'm going to give this one shameless bump...and a little bit of an update:

    I've found that if I simply cancell the event if it's not a zombie, so that only zombie spawns get through...
    That it works (kind of)

    My issue with this is that they still can't spawn in any lit area, or during the daytime.

    So, I suppose I'll ask for the last time:
    Does anyone know a way to prevent a CreatureSpawnEvent from firing multiple times (if cancelled)?

    I need the server to know about the mobs that I spawn using spawnCreature(), so that the spawn events don't keep firing as if the server had no mobs at all

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  10. Did you try debugging the event? Like writing the hashCode of the spawning Entity to the console everytime it is called?
    Then you could figure out what exactly is being called recursively.
     
Thread Status:
Not open for further replies.

Share This Page