Exemple #1
0
 public static void updateNavigationWorld(
     org.bukkit.entity.Entity entity, org.bukkit.World world) {
   if (NAVIGATION_WORLD_FIELD == null) return;
   Entity en = NMS.getHandle(entity);
   if (en == null || !(en instanceof EntityInsentient)) return;
   EntityInsentient handle = (EntityInsentient) en;
   World worldHandle = ((CraftWorld) world).getHandle();
   try {
     NAVIGATION_WORLD_FIELD.set(handle.getNavigation(), worldHandle);
   } catch (Exception e) {
     Messaging.logTr(Messages.ERROR_UPDATING_NAVIGATION_WORLD, e.getMessage());
   }
 }
Exemple #2
0
 public static void look(Entity handle, Entity target) {
   if (handle instanceof EntityInsentient) {
     ((EntityInsentient) handle)
         .getControllerLook()
         .a(target, 10.0F, ((EntityInsentient) handle).bQ());
   } else if (handle instanceof EntityHumanNPC) {
     ((EntityHumanNPC) handle).setTargetLook(target, 10F, 40F);
   }
 }
Exemple #3
0
 public static void setDestination(
     org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
   Entity handle = NMS.getHandle(entity);
   if (handle == null) return;
   if (handle instanceof EntityInsentient) {
     ((EntityInsentient) handle).getControllerMove().a(x, y, z, speed);
   } else if (handle instanceof EntityHumanNPC) {
     ((EntityHumanNPC) handle).setMoveDestination(x, y, z, speed);
   }
 }
Exemple #4
0
 public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
   if (!npc.isSpawned() || !npc.getEntity().getType().isAlive()) return;
   EntityLiving en = NMS.getHandle((LivingEntity) npc.getEntity());
   if (!(en instanceof EntityInsentient)) {
     if (en instanceof EntityHumanNPC) {
       ((EntityHumanNPC) en).updatePathfindingRange(pathfindingRange);
     }
     return;
   }
   if (PATHFINDING_RANGE == null) return;
   EntityInsentient handle = (EntityInsentient) en;
   NavigationAbstract navigation = handle.getNavigation();
   try {
     AttributeInstance inst = (AttributeInstance) PATHFINDING_RANGE.get(navigation);
     inst.setValue(pathfindingRange);
   } catch (IllegalArgumentException e) {
     e.printStackTrace();
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   }
 }
Exemple #5
0
  public static void walkTo(
      final Entity entity, Location location, double speed, final Runnable callback) {
    if (entity == null || location == null) return;

    net.minecraft.server.v1_8_R3.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
    if (!(nmsEntityEntity instanceof EntityInsentient)) return;
    final EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
    final NavigationAbstract entityNavigation = nmsEntity.getNavigation();

    final PathEntity path;
    final boolean aiDisabled = isAIDisabled(entity);
    if (aiDisabled) {
      toggleAI(entity, true);
      nmsEntity.onGround = true;
    }
    path = entityNavigation.a(location.getX(), location.getY(), location.getZ());
    if (path != null) {
      entityNavigation.a(path, 1D);
      entityNavigation.a(2D);
      final double oldSpeed = nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).b();
      nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
      new BukkitRunnable() {
        @Override
        public void run() {
          if (entityNavigation.m() || path.b()) {
            if (callback != null) callback.run();
            nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(oldSpeed);
            if (aiDisabled) toggleAI(entity, false);
            cancel();
          }
        }
      }.runTaskTimer(DenizenAPI.getCurrentInstance(), 1, 1);
    }
    // if (!Utilities.checkLocation(location, entity.getLocation(), 20)) {
    // TODO: generate waypoints to the target location?
    else {
      entity.teleport(location);
    }
  }
Exemple #6
0
 public static void updateAI(EntityLiving entity) {
   if (entity instanceof EntityInsentient) {
     EntityInsentient handle = (EntityInsentient) entity;
     handle.getEntitySenses().a();
     NMS.updateNavigation(handle.getNavigation());
     handle.getControllerMove().c();
     handle.getControllerLook().a();
     handle.getControllerJump().b();
   } else if (entity instanceof EntityHumanNPC) {
     ((EntityHumanNPC) entity).updateAI();
   }
 }
Exemple #7
0
  public static void follow(
      final Entity target,
      final Entity follower,
      final double speed,
      final double lead,
      final double maxRange,
      final boolean allowWander) {
    if (target == null || follower == null) return;

    final net.minecraft.server.v1_8_R3.Entity nmsEntityFollower =
        ((CraftEntity) follower).getHandle();
    if (!(nmsEntityFollower instanceof EntityInsentient)) return;
    final EntityInsentient nmsFollower = (EntityInsentient) nmsEntityFollower;
    final NavigationAbstract followerNavigation = nmsFollower.getNavigation();

    UUID uuid = follower.getUniqueId();

    if (followTasks.containsKey(uuid)) followTasks.get(uuid).cancel();

    final int locationNearInt = (int) Math.floor(lead);
    final boolean hasMax = maxRange > lead;

    followTasks.put(
        follower.getUniqueId(),
        new BukkitRunnable() {

          private boolean inRadius = false;

          public void run() {
            if (!target.isValid() || !follower.isValid()) {
              this.cancel();
            }
            followerNavigation.a(2F);
            Location targetLocation = target.getLocation();
            PathEntity path;

            if (hasMax
                && !Utilities.checkLocation(targetLocation, follower.getLocation(), maxRange)
                && !target.isDead()
                && target.isOnGround()) {
              if (!inRadius) {
                follower.teleport(
                    Utilities.getWalkableLocationNear(targetLocation, locationNearInt));
              } else {
                inRadius = false;
                path =
                    followerNavigation.a(
                        targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
                if (path != null) {
                  followerNavigation.a(path, 1D);
                  followerNavigation.a(2D);
                }
              }
            } else if (!inRadius
                && !Utilities.checkLocation(targetLocation, follower.getLocation(), lead)) {
              path =
                  followerNavigation.a(
                      targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
              if (path != null) {
                followerNavigation.a(path, 1D);
                followerNavigation.a(2D);
              }
            } else {
              inRadius = true;
            }
            if (inRadius && !allowWander) {
              followerNavigation.n();
            }
            nmsFollower.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
          }
        }.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 20));
  }
Exemple #8
0
 public static void setSpeed(Entity entity, double speed) {
   net.minecraft.server.v1_8_R3.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
   if (!(nmsEntityEntity instanceof EntityInsentient)) return;
   EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
   nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
 }
Exemple #9
0
 public static double getSpeed(Entity entity) {
   net.minecraft.server.v1_8_R3.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
   if (!(nmsEntityEntity instanceof EntityInsentient)) return 0.0;
   EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
   return nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).b();
 }
Exemple #10
0
 public static void stopWalking(Entity entity) {
   net.minecraft.server.v1_8_R3.Entity nmsEntity = ((CraftEntity) entity).getHandle();
   if (!(nmsEntity instanceof EntityInsentient)) return;
   ((EntityInsentient) nmsEntity).getNavigation().n();
 }