Solved Get EntityInsentient from bukkit entity

Discussion in 'Plugin Development' started by Niv-Mizzet, Aug 7, 2020.

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

    Niv-Mizzet

    Hi there everyone, I was wondering how to get an EntityInsentient from a bukkit entity(Assuming that the entity is an EntityInsentient, which pretty much all entities are except players).
    Here is my current code, which loops through all the creatures around the player and tries to set their navigation to go towards the player. The code is able to find the creatures.
    The e is an event which this code is inside of.
    Code:
         for(Entity et:e.getPlayer().getNearbyEntities(10, 10, 10)){
    
                    if(et instanceof EntityInsentient){
    
                        ((EntityInsentient)et).getNavigation().a(e.getPlayer().getLocation().getBlockX(), e.getPlayer().getLocation().getBlockY(), e.getPlayer().getLocation().getBlockZ(), 5F);
                    }
                }
     
  2. Offline

    KarimAKL

    @Niv-Mizzet EntityInsentient is from NMS, you have to get the NMS entity by using #getHandle() on the CraftBukkit entity.
    Code:Java
    1. Entity entity = ...; // Your entity
    2. Creature creature = (Creature) entity; // Make a check before casting, this is just an example
    3. CraftCreature craftCreature = (CraftCreature) creature; // Cast Creature to CraftCreature to access #getHandle()
    4. EntityCreature nmsCreature = craftCreature.getHandle(); // Get the NMS entity using #getHandle()
    5. EntityInsentient entityInsentient = nmsCreature; // This is not needed as EntityCreature extends EntityInsentient
     
  3. Offline

    Niv-Mizzet

    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page