Search in sources :

Example 1 with PathNavigation

use of net.minecraft.world.entity.ai.navigation.PathNavigation in project Denizen-For-Bukkit 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.world.entity.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
    if (!(nmsEntityEntity instanceof Mob)) {
        return;
    }
    final Mob nmsEntity = (Mob) nmsEntityEntity;
    final PathNavigation entityNavigation = nmsEntity.getNavigation();
    final Path 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.createPath(location.getX(), location.getY(), location.getZ(), 1);
    if (path != null) {
        nmsEntity.goalSelector.enableControlFlag(Goal.Flag.MOVE);
        entityNavigation.moveTo(path, 1D);
        entityNavigation.setSpeedModifier(2D);
        final double oldSpeed = nmsEntity.getAttribute(Attributes.MOVEMENT_SPEED).getBaseValue();
        if (speed != null) {
            nmsEntity.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(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.isDone() || path.isDone()) {
                    if (callback != null) {
                        callback.run();
                    }
                    if (speed != null) {
                        nmsEntity.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(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 : Path(net.minecraft.world.level.pathfinder.Path) Mob(net.minecraft.world.entity.Mob) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) PathNavigation(net.minecraft.world.entity.ai.navigation.PathNavigation) org.bukkit.craftbukkit.v1_17_R1.entity(org.bukkit.craftbukkit.v1_17_R1.entity) org.bukkit.entity(org.bukkit.entity) net.minecraft.world.entity(net.minecraft.world.entity)

Example 2 with PathNavigation

use of net.minecraft.world.entity.ai.navigation.PathNavigation in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method follow.

@Override
public void follow(final Entity target, final Entity follower, final double speed, final double lead, final double maxRange, final boolean allowWander, final boolean teleport) {
    if (target == null || follower == null) {
        return;
    }
    final net.minecraft.world.entity.Entity nmsEntityFollower = ((CraftEntity) follower).getHandle();
    if (!(nmsEntityFollower instanceof Mob)) {
        return;
    }
    final Mob nmsFollower = (Mob) nmsEntityFollower;
    final PathNavigation 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.setSpeedModifier(2D);
            Location targetLocation = target.getLocation();
            Path path;
            if (hasMax && !Utilities.checkLocation(targetLocation, follower.getLocation(), maxRange) && !target.isDead() && target.isOnGround()) {
                if (!inRadius) {
                    if (teleport) {
                        follower.teleport(Utilities.getWalkableLocationNear(targetLocation, locationNearInt));
                    } else {
                        cancel();
                    }
                } else {
                    inRadius = false;
                    path = followerNavigation.createPath(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ(), 0);
                    if (path != null) {
                        followerNavigation.moveTo(path, 1D);
                        followerNavigation.setSpeedModifier(2D);
                    }
                }
            } else if (!inRadius && !Utilities.checkLocation(targetLocation, follower.getLocation(), lead)) {
                path = followerNavigation.createPath(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ(), 0);
                if (path != null) {
                    followerNavigation.moveTo(path, 1D);
                    followerNavigation.setSpeedModifier(2D);
                }
            } else {
                inRadius = true;
            }
            if (inRadius && !allowWander) {
                followerNavigation.stop();
            }
            nmsFollower.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(speed);
        }
    }.runTaskTimer(NMSHandler.getJavaPlugin(), 0, 10));
}
Also used : Path(net.minecraft.world.level.pathfinder.Path) Mob(net.minecraft.world.entity.Mob) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) PathNavigation(net.minecraft.world.entity.ai.navigation.PathNavigation) org.bukkit.craftbukkit.v1_17_R1.entity(org.bukkit.craftbukkit.v1_17_R1.entity) org.bukkit.entity(org.bukkit.entity) net.minecraft.world.entity(net.minecraft.world.entity) ResourceLocation(net.minecraft.resources.ResourceLocation) Location(org.bukkit.Location)

Example 3 with PathNavigation

use of net.minecraft.world.entity.ai.navigation.PathNavigation in project Denizen-For-Bukkit 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.world.entity.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
    if (!(nmsEntityEntity instanceof Mob)) {
        return;
    }
    final Mob nmsEntity = (Mob) nmsEntityEntity;
    final PathNavigation entityNavigation = nmsEntity.getNavigation();
    final Path 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.createPath(location.getX(), location.getY(), location.getZ(), 1);
    if (path != null) {
        nmsEntity.goalSelector.enableControlFlag(Goal.Flag.MOVE);
        entityNavigation.moveTo(path, 1D);
        entityNavigation.setSpeedModifier(2D);
        final double oldSpeed = nmsEntity.getAttribute(Attributes.MOVEMENT_SPEED).getBaseValue();
        if (speed != null) {
            nmsEntity.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(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.isDone() || path.isDone()) {
                    if (callback != null) {
                        callback.run();
                    }
                    if (speed != null) {
                        nmsEntity.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(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 : Path(net.minecraft.world.level.pathfinder.Path) Mob(net.minecraft.world.entity.Mob) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) PathNavigation(net.minecraft.world.entity.ai.navigation.PathNavigation) org.bukkit.entity(org.bukkit.entity) org.bukkit.craftbukkit.v1_18_R1.entity(org.bukkit.craftbukkit.v1_18_R1.entity) net.minecraft.world.entity(net.minecraft.world.entity)

Example 4 with PathNavigation

use of net.minecraft.world.entity.ai.navigation.PathNavigation in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method follow.

@Override
public void follow(final Entity target, final Entity follower, final double speed, final double lead, final double maxRange, final boolean allowWander, final boolean teleport) {
    if (target == null || follower == null) {
        return;
    }
    final net.minecraft.world.entity.Entity nmsEntityFollower = ((CraftEntity) follower).getHandle();
    if (!(nmsEntityFollower instanceof Mob)) {
        return;
    }
    final Mob nmsFollower = (Mob) nmsEntityFollower;
    final PathNavigation 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.setSpeedModifier(2D);
            Location targetLocation = target.getLocation();
            Path path;
            if (hasMax && !Utilities.checkLocation(targetLocation, follower.getLocation(), maxRange) && !target.isDead() && target.isOnGround()) {
                if (!inRadius) {
                    if (teleport) {
                        follower.teleport(Utilities.getWalkableLocationNear(targetLocation, locationNearInt));
                    } else {
                        cancel();
                    }
                } else {
                    inRadius = false;
                    path = followerNavigation.createPath(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ(), 0);
                    if (path != null) {
                        followerNavigation.moveTo(path, 1D);
                        followerNavigation.setSpeedModifier(2D);
                    }
                }
            } else if (!inRadius && !Utilities.checkLocation(targetLocation, follower.getLocation(), lead)) {
                path = followerNavigation.createPath(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ(), 0);
                if (path != null) {
                    followerNavigation.moveTo(path, 1D);
                    followerNavigation.setSpeedModifier(2D);
                }
            } else {
                inRadius = true;
            }
            if (inRadius && !allowWander) {
                followerNavigation.stop();
            }
            nmsFollower.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(speed);
        }
    }.runTaskTimer(NMSHandler.getJavaPlugin(), 0, 10));
}
Also used : Path(net.minecraft.world.level.pathfinder.Path) Mob(net.minecraft.world.entity.Mob) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) PathNavigation(net.minecraft.world.entity.ai.navigation.PathNavigation) org.bukkit.entity(org.bukkit.entity) org.bukkit.craftbukkit.v1_18_R1.entity(org.bukkit.craftbukkit.v1_18_R1.entity) net.minecraft.world.entity(net.minecraft.world.entity) ResourceLocation(net.minecraft.resources.ResourceLocation) Location(org.bukkit.Location)

Aggregations

net.minecraft.world.entity (net.minecraft.world.entity)4 Mob (net.minecraft.world.entity.Mob)4 PathNavigation (net.minecraft.world.entity.ai.navigation.PathNavigation)4 Path (net.minecraft.world.level.pathfinder.Path)4 org.bukkit.entity (org.bukkit.entity)4 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)4 ResourceLocation (net.minecraft.resources.ResourceLocation)2 Location (org.bukkit.Location)2 org.bukkit.craftbukkit.v1_17_R1.entity (org.bukkit.craftbukkit.v1_17_R1.entity)2 org.bukkit.craftbukkit.v1_18_R1.entity (org.bukkit.craftbukkit.v1_18_R1.entity)2