Search in sources :

Example 51 with PathEntity

use of net.minecraft.server.v1_16_R3.PathEntity in project Denizen by DenizenScript.

the class EntityHelperImpl method walkTo.

@Override
public void walkTo(final LivingEntity entity, Location location, Double speed, final Runnable callback) {
    if (entity == null || location == null) {
        return;
    }
    net.minecraft.server.v1_16_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 = !entity.hasAI();
    if (aiDisabled) {
        entity.setAI(true);
        try {
            ENTITY_ONGROUND_SETTER.invoke(nmsEntity, true);
        } catch (Throwable ex) {
            Debug.echoError(ex);
        }
    }
    path = entityNavigation.a(location.getX(), location.getY(), location.getZ(), 0);
    if (path != null) {
        entityNavigation.a(path, 1D);
        entityNavigation.a(2D);
        final double oldSpeed = nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).getBaseValue();
        if (speed != null) {
            nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
        }
        new BukkitRunnable() {

            @Override
            public void run() {
                if (!entity.isValid()) {
                    if (callback != null) {
                        callback.run();
                    }
                    cancel();
                    return;
                }
                if (aiDisabled && entity instanceof Wolf) {
                    ((Wolf) entity).setAngry(false);
                }
                if (entityNavigation.m() || path.c()) {
                    if (callback != null) {
                        callback.run();
                    }
                    if (speed != null) {
                        nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(oldSpeed);
                    }
                    if (aiDisabled) {
                        entity.setAI(false);
                    }
                    cancel();
                }
            }
        }.runTaskTimer(NMSHandler.getJavaPlugin(), 1, 1);
    } else // if (!Utilities.checkLocation(location, entity.getLocation(), 20)) {
    // TODO: generate waypoints to the target location?
    {
        entity.teleport(location);
    }
}
Also used : BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) net.minecraft.server.v1_16_R3(net.minecraft.server.v1_16_R3)

Aggregations

List (java.util.List)10 Optional (java.util.Optional)9 Location (org.bukkit.Location)9 Lists (com.google.common.collect.Lists)8 Sets (com.google.common.collect.Sets)8 Comparator (java.util.Comparator)8 Map (java.util.Map)8 Set (java.util.Set)8 Function (java.util.function.Function)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 Setting (net.citizensnpcs.Settings.Setting)8 PathEntity (net.minecraft.server.v1_16_R3.PathEntity)7 Iterator (java.util.Iterator)6 PathEntity (net.minecraft.server.v1_14_R1.PathEntity)6 PathEntity (net.minecraft.server.v1_15_R1.PathEntity)6 PathPoint (net.minecraft.server.v1_16_R3.PathPoint)6 PathEntity (net.minecraft.server.v1_8_R3.PathEntity)6 PathEntity (net.minecraft.server.v1_10_R1.PathEntity)5 PathPoint (net.minecraft.server.v1_10_R1.PathPoint)5