About mob spawning ticks.

Discussion in 'Spigot Discussion' started by blackone1991, Jul 18, 2018.

  1. Hello, I have been trying spawn mob and set potion effect for a hour but when server spawn a mob. Mob haven't potion effect.

    When I change when add potion effect for mob. It's work but It's bullshit.

    Example; (IT'S NOT WORK)
    Code (Java):
    Zombie entity = (Zombie) Bukkit.getWorld("world").spawnEntity(loce, EntityType.ZOMBIE);
    entity.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 9));

    Example 2; (IT'S WORK)
    Code (Text):
    Zombie entity = (Zombie) Bukkit.getWorld("world").spawnEntity(loce, EntityType.ZOMBIE);
    new BukkitRunnable(){
        int ticks = 0;
        public void run(){
            ticks++;
            if (ticks > 1) {
                entity.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 9));
            }
        }
    }.runTaskTimer(MainSystem.plugin, 1L, 1L);
    Thanks for help.
     
  2. I think this is because the entity needs to be spawned in the world first, which happens after it's been ticked once. I faced a similar issue where I tried to equip a zombie with armor, but I had to add a delay to it (1 tick seemed fine).

    Also; just a new task is fine. Maybe delay it by one ticket. You really don't need to use such a repeating task.
     
    • Informative Informative x 1
  3. Can you make example without repeating task?
     
  4. Either the runTask or runTaskLater should do. I'm using the runTaskLater with a delay of 1 tick and that seems to do the job. Possible that the runTask also works, but you'd have to play around with that.
     
    • Informative Informative x 1
  5. As Dienns said something like this:

    Code (Text):
            Zombie entity = (Zombie) Bukkit.getWorld("world").spawnEntity(loce, EntityType.ZOMBIE);
            Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {

                @Override
                public void run() {
                    entity.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 9));
                 
                }
             
            }, 1);
    PS: You could also create a custom entity using NMS
     
    • Informative Informative x 1
  6. or something like this (with runTask() ):

    Code (Java):

            Zombie entity = (Zombie) Bukkit.getWorld("world").spawnEntity(loce, EntityType.ZOMBIE);
            Bukkit.getScheduler().runTask(pl, new Runnable() {

                @Override
                public void run() {
                    entity.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 9));
                }
         
            });
    PS: this run with a delay of 1 tick.
     
    • Informative Informative x 1
  7. i did not know that a new task always has one tick delay on default ^^
     
    • Informative Informative x 1
    • Informative Informative x 1