Solved Zombies

Discussion in 'Plugin Development' started by Wantsome909, Aug 23, 2013.

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

    Wantsome909

    hello i'm trying to make the zombie only spawn on grass not stone dirt or air

    here code:
    Code:java
    1. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. Player[] players = main.this.getServer().getOnlinePlayers();
    4. for (Player p : players) {
    5. Random r = new Random();
    6. int per = r.nextInt(3);
    7. if(per == 1){
    8. Location loc = p.getLocation().add(10, 0, 10);
    9. Zombie zombie = (Zombie) p.getWorld().spawnEntity(loc, EntityType.ZOMBIE);
    10. }
    11. if(per == 2){
    12. Location loc4 = p.getLocation().add(10, 0, 10);
    13. Zombie zombie4 = (Zombie) p.getWorld().spawnEntity(loc4, EntityType.ZOMBIE);
    14. }
    15. }
    16. }
    17. }, 0, 3600);


    i'd try using something like this but it didn't work

    Code:java
    1. Material block2 = world.getBlockAt(blockX, highestBlockY - 1, blockZ).getType();
    2. if ((!block2.equals(Material.STONE)) && (!block2.equals(Material.DIRT))) {



    or is there a possible way to make them only spawn on grass?
     
  2. Offline

    Conarnar

    Listen for CreatureSpawnEvent and cancel it if the block under it isn't grass
     
  3. Offline

    Wantsome909

    Conarnar well i'm not natural spawning them i'm spawning them around players with location.
     
  4. Offline

    Conarnar

    Wantsome909 Well I thought CreatureSpawnEvent is called every time a mob spawns. It doesn't matter if it was natural or from a method.
     
  5. Offline

    Wantsome909

    Conarnar ok well, ill try it!

    Conarnar i don't see how to cancel it if the block under it isn't grass

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

    The_Doctor_123

    Code:java
    1. @EventHandler
    2. public void onCreatureSpawn(CreatureSpawnEvent event)
    3. {
    4. if (!(event.getEntity().getLocation().subtract(0, 1, 0).getBlock().getType() == Material.GRASS))
    5. {
    6. event.setCancelled(true);
    7. }
    8. }
     
  7. Offline

    Wantsome909

    The_Doctor_123
    so when a zombie spawn and if he spawn in the air it wont spawn?

    cause when i try mine then yours it did the same things
     
  8. Offline

    Axe2760

  9. Offline

    Wantsome909

    Axe2760 well im just trying to get it so my zombie don't spawn in the air or in a wall.
     
  10. Offline

    The_Doctor_123

    1. That code should work. If the block below the spawned zombie is air, then the if statement would be true.
    2. Why would a zombie spawn in air anyway..?

    In that case, replace the cancelling of the event with:

    Code:
    event.getEntity().remove();
    
     
  11. Offline

    Wantsome909

    The_Doctor_123 right. but I'm trying to stop them from spawning in the air and losing health from fall damage.
     
  12. Offline

    Axe2760

    The_Doctor_123 Read the post, that clearly would just make things worse. You have to stop the game from trying to spawn them altogether, before it generates things like AI, etc.
     
  13. Offline

    The_Doctor_123

    My code does just that.

    Meh, it's only really a problem if you're cancelling a whole lot of entities from being spawned. We just have to remember that cancelling that event is costly(for now).
     
  14. Offline

    Wantsome909

    Axe2760 & The_Doctor_123 well could i just make them so when they fall they wont take any fall damage?

    The_Doctor_123 you code really does not do what i said. when they spawn and there air under one zombie he wont spawn.

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

    The_Doctor_123

    Yes. Just set the fall distance.

    Code:
    event.getEntity().setFallDistance(-50F);
    
     
    Axe2760 likes this.
  16. Offline

    Axe2760

    Wantsome909
    You can do multiple things. The easy way out, is to add a resistance 5 potion effect for four or so seconds -- therefore mobs spawned in air will not lose health when they fall.

    You can also use a delayed task to set the health back to 20.

    Finally, (the overkill) you can store the entity in a HashMap upon spawning, set a Boolean to true. Then, when the entity takes damage, check if they're in the HashMap and have the value "true", and then if they do remove them and set their health.

    Come to think of it, an array or ArrayList would probably work just fine as well.

    Edit: Ninja'd with a far better answer. :)
     
  17. Offline

    Wantsome909

  18. Offline

    The_Doctor_123

    Axe2760
    Woah, woah, woah.. no need for complicated things, just set the fall distance of the entity.
     
  19. Offline

    Axe2760

    The_Doctor_123 Hahaha I had no idea such a method existed as well, thank you Doctor. :)

    (I'ma stay and watch the TARDIS dematerialisation)
     
  20. Offline

    Wantsome909

    The_Doctor_123 last thing! now if i get close to a wall they will spawn in there how can i stop that?
     
  21. Offline

    The_Doctor_123

  22. Offline

    Wantsome909

  23. Offline

    The_Doctor_123

    Okay, in this case.. you really should edit the code that is spawning the zombies. Can I see that code?
     
  24. Offline

    xTrollxDudex

    Naturally killing it is a better way
     
Thread Status:
Not open for further replies.

Share This Page