Solved How to make mob have anti-kb for a little bit.

Discussion in 'Spigot Plugin Development' started by NocturnalDev, Feb 3, 2020.

  1. Hello,

    How can I give my mob anti knockback for a little bit?


    Thank you!
     
  2. Make a list of entitys (or add a tag to each entity you want no kb). On EntityDamageByEntity event, check if they're in the list or have the proper tag, and set the entity's velocity to 0. Then create a task that will run X ticks later, which removes them from the list or removes the tag.
     
  3. Maybe It can be work

    Code (Java):
           
            @EventHandler
            public void entityDamagedByEntity(EntityDamageByEntityEvent e){
                if (e.getDamager() instanceof Player) {
                    Player damager = (Player) e.getDamager();

                    Entity entity = e.getEntity();

                    entity.setVelocity(damager.getLocation().getDirection().setY(0).normalize().multiply(5));
                }
            }
     
     
  4. Code (Text):
     @EventHandler
        public void onHit(EntityDamageByEntityEvent e) {
            if (e.getEntity().equals(boss)) {  //Checks if the damaged entity is a monster (zombie, skeleton etc.)

                if (antiKb >= 4 && antiKb <= 7) {
                    final Monster m = (Monster) e.getEntity(); //monster variable
                    Main.getPlugin().getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
                        public void run() {
                            m.setVelocity(new Vector()); //Sets the velocity a tick after getting damaged, else it won't work
                        }
                    }, 1L);
                    if (antiKb == 7) {
                        antiKb = 0;
                    }
                }
            }
        }

    }
    What would be wrong with this?
     
  5. Not sure what "boss" or "antiKb" are; usually for entity comparison you should get checking the EntityType of the entity.

    Also IIRC a default vector gives a size of 10
     
  6. You have to set velocity according to the damager like this:

    Code (Text):
       
        @EventHandler
        public void onHit(EntityDamageByEntityEvent e) {
            if (e.getEntity().equals(boss)) {  //Checks if the damaged entity is a monster (zombie, skeleton etc.)
                Entity damager = e.getDamager();
                if (antiKb >= 4 && antiKb <= 7) {
                    final Monster m = (Monster) e.getEntity(); //monster variable
                    Main.getPlugin().getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
                        public void run() {
                            m.setVelocity(damager.getLocation().getDirection().setY(0).normalize().multiply(5));
                        }
                    }, 1L);
                    if (antiKb == 7) {
                        antiKb = 0;
                    }
                }
            }
        }
     
     
  7. Got it, it wasn't running so I threw in some debug and noticed that my event wasn't firing. I was wondering how I would register an event inside of a bukkit runnable. I would also need to unregister it on the cancel(); .

    bossRunnable = new BossRunnable(this);
    bossRunnable.runTaskTimer(Main.getPlugin(), 0L, 20L);

    This is where I start it.
     
  8. You don't register events inside of runnables. As I said before I believe your error is if (e.getEntity().equals(boss)) as you said you want to compare the type of entity, use e.getEntity().getType().equals(EntityType.<whatever>)
     
    #8 darthteddy1, Feb 3, 2020
    Last edited: Feb 3, 2020
  9. I've done get type, but I've also done .getName() and it isn't working. I don't think my event is getting registered.
     
  10. can you post your code from your onEnable in your main class? Also make sure your plugin.yml is correct
     
  11. Got it working, however, it looks like the vector is making them go back really fast instead of giving anti kb.
     
  12. What are you setting the vector as?
     
  13. m.setVelocity(damager.getLocation().getDirection().setY(0).normalize().multiply(5));
     
  14. I personally suggested to set the velocity to 0. Not sure what that code does offhand, but setting it to 0 should stop all movement.
     
  15. So just: m.setVelocity(new Vector()) ?
     
  16. no. The Default for a vector i believe is 10. new Vector(0,0,0) is what you want
     
  17. Interesting, giving this a try. Also, do you know how I could give the mob a shield in his offhand? Are we able to do that?
     
  18. I believe something like this should work.
    Code (Text):

    if (e.getEntity() instanceof Zombie) {
        Zombie ee = (Zombie) e.getEntity();
        ee.getEquipment().setItemInOffHand(new ItemStack(Material.SHIELD));
    }
     
     
  19. Ah I see, and to make it so he uses it? Also, is it possible to make the boss AI target the nearest player? Is there a .target? At the moment I just sort in 30 blocks and then narrow down to 15 and target whomever the code finds.
     
  20. Again, off the top of my head, something like this would target the nearest player (untested)
    Code (Text):

    double distance = 30;
    Player p = null;
    for (Entity ent : zombie.getNearbyEntities(15, 30, 15)) {
        if (ent instanceof Player) {
            Player all = (Player) ent;
            if (zombie.getLocation().distance(all.getLocation()) <= distance) {
                distance = zombie.getLocation().distance(all.getLocation());
                p = all;
            }
        }
        if (p != null)
            zombie.setTarget(p);
    }
     
    Change numbers as you see fit
     
    • Winner Winner x 1