1.16.5 How to make armor stand follow you

Discussion in 'Spigot Plugin Development' started by AlexNomad, Jun 9, 2021.

  1. I have this code and I want armor stand to follow player for 2 seconds

    Code (Java):
    package dev.alexnomad.cringe.bubles;

    import dev.alexnomad.cringe.Cringe;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;


    public class Buble {

        public Buble(Location location, String message, Player player){


            Bukkit.getScheduler().runTask(Cringe.getPlugin(Cringe.class),
                    () -> {
                        ArmorStand armor = (ArmorStand) location.getWorld().spawn(location, EntityType.ARMOR_STAND.getEntityClass());
                        armor.setCustomName(message);
                        armor.setCustomNameVisible(true);
                        armor.setGravity(false);
                        armor.setInvulnerable(true);
                        System.out.println(message);
                        armor.setVisible(false);
                       

                        Bukkit.getScheduler().runTaskLater(Cringe.getPlugin(Cringe.class), armor::remove, 40L);
                        });


        }

        private void movement(ArmorStand armorStand, Location location){
            armorStand.teleport(location);
        }
    }
     
     
  2. spawn an armor stand to the player location and do all the settings outside the runnable, then use a run task timer that will run every half second or something, then inside the runnable, update the players location, then teleport your ArmorStand to that location. Don't forget to add a counter or something so you can stop the task after 2 seconds and remove the armorstand,
     
  3. how stop runTaskTimer?
     
  4. You can get the task id and call
    Code (Java):
    Bukkit.getScheduler().cancelTask(taskId);
     
  5. How to get it?
     
  6. You can get it from your BukkitTask.
    Code (Java):
    int taskid = Bukkit.getScheduler().runTask(plugin, () -> System.out.println("Useless task")).getTaskId();