1.16.5 How do I make a code repeat?

Discussion in 'Spigot Plugin Development' started by 2Hex, Jul 16, 2021.

  1. im making an armor stand dancing plugin, where u do /armorstand then it spawns and starting dancing
    and i want the code to get repeated instead of me spamming it over and over. so it doesnt become a spaghetti code (it already is but im hoping to make it better through conventions)

    Code (Java):
    package me.hex.armorstanddancing.commands;

    import me.hex.armorstanddancing.Armorstanddancing;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.util.EulerAngle;

    public class SpawnAndDance implements CommandExecutor {


        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if (command.getName().equalsIgnoreCase("armorstand")) {
                Player player = (Player) sender;
                if (sender instanceof Player) {
                    ArmorStand armorStand = (ArmorStand) player.getWorld().spawnEntity(player.getLocation(), EntityType.ARMOR_STAND);
                    armorStand.setArms(true);

                    player.sendMessage("FLOSSIN STARTING IN 1 SECOND");
    // the following code i what i want to repeat. please help!
                    Bukkit.getScheduler().runTaskLater(Armorstanddancing.INSTANCE, () -> armorStand.setLeftArmPose(new EulerAngle(0, 0, Math.toRadians(34))), 20); //20 ticks equal 1 second
                    Bukkit.getScheduler().runTaskLater(Armorstanddancing.INSTANCE, () -> armorStand.setRightArmPose(new EulerAngle(0, 0, Math.toRadians(330))), 1); //20 ticks equal 1 second
                 
                    return false;
                }
                return true;
            }
        return true;
        }
    }
     

    Your help is appreciated, thanks in advance
     
  2. But im doing 2 lines that i want to repeat
     
  3. Then the run() method of your Runnable with have two lines in its body.
     
  4. Try making a bukkit runnable with seconds to countdown
     
  5. I tried
    Bukkit.getScheduler().scheduleSyncRepeatingTask(Armorstanddancing.INSTANCE, Bukkit.getScheduler().runTaskLater(new Armorstanddancing(), () -> armorStand.setLeftArmPose(new EulerAngle(0, 0, Math.toRadians(316))), 7), Bukkit.getScheduler().runTaskLater(new Armorstanddancing(), () -> armorStand.setRightArmPose(new EulerAngle(0, 0, Math.toRadians(330))), 1));
    but obviously it doesnt work
     
  6. You could try .runTaskTimer and make a countdown so it stops after certain time.
     
  7. How? Can you give me an example?
     
  8. Cannot resolve method 'runTaskTimer(me.hex.armorstanddancing.Armorstanddancing, org.bukkit.scheduler.BukkitTask, org.bukkit.scheduler.BukkitTask)'


    CODE:
    Code (Java):
    Bukkit.getScheduler().runTaskTimer(Armorstanddancing.INSTANCE, Bukkit.getScheduler().runTaskLater(new Armorstanddancing(), () -> armorStand.setLeftArmPose(new EulerAngle(0, 0, Math.toRadians(316))), 7), Bukkit.getScheduler().runTaskLater(new Armorstanddancing(), () -> armorStand.setRightArmPose(new EulerAngle(0, 0, Math.toRadians(330))), 1));
    EDIT: idk how to use it honestly lmao
     
  9. new BukkitRunnable() {
    int seconds = 3;
    public void run() {

    seconds--;

    if(seconds > 0) {
    //method
    } else
    if(seconds == 0) {
    //method
    this.cancel();
    }

    }

    }.runTaskTimer(sm, 0, 1 * 20L);

    something like this could work.
     
  10. Code (Java):
    Bukkit.getScheduler().scheduleSyncRepeatingTask(Armorstanddancing.INSTANCE, () -> {
                        armorStand.setLeftArmPose(new EulerAngle(0, 0, Math.toRadians(34)));
                        armorStand.setRightArmPose(new EulerAngle(0, 0, Math.toRadians(330)));
                    }, 20L, 20L);
    do u think this will work too?
     
  11. It probably will but you have to test it. Correct me if I am wrong, but I would personally use Async so for example if many players execute that command, the event wont be cancelled all the time and restarted but I am not too sure.
     
  12. I figured it out! Thank you all for the help