Solved How to play a list of sound

Discussion in 'Spigot Plugin Development' started by fecxica, Nov 22, 2020.

  1. So i create a plugin that play a list of sound but in delay
    I mean i have 3 sound and i want play it 1 per 1 in second
    My code:
    Code (Text):

        public static void openGUISound(Player p) {
            running = true;
              new Thread() {
                  @Override
                  public void run() {
                        Location l = p.getLocation();
                        p.playSound(l, Sound.BLOCK_NOTE_HARP, 1, 1);
                        Main.getPlugin().getServer().getScheduler().runTaskLaterAsynchronously(Main.getPlugin(), new Runnable() {
                            @Override
                            public void run() {

                                p.playSound(l, Sound.BLOCK_NOTE_HARP, 1, Float.valueOf("1.5"));
                            }
                        }, 3L);
                        Main.getPlugin().getServer().getScheduler().runTaskLaterAsynchronously(Main.getPlugin(), new Runnable() {
                            @Override
                            public void run() {

                                p.playSound(l, Sound.BLOCK_NOTE_HARP, 1, 2);
                            }
                        }, 6L);
                        Main.getPlugin().getServer().getScheduler().runTaskLaterAsynchronously(Main.getPlugin(), new Runnable() {
                            @Override
                            public void run() {

                                p.playSound(l, Sound.BLOCK_NOTE_HARP, 1, 2);
                                running = false;

                            }
                        }, 8L);
                  }
              }.start();
        }
     
    But use task is so laggi, can someone help me with it?

    EDIT: Although create new thread, it still laggi
     
    • Funny Funny x 1
  2. Why are you making a new thread and 3 tasks...
    That code makes absolutely no sense, and makes me think you have no knowledge of Java.
    Just make them alternate from a single task?
     
    • Agree Agree x 1
  3. I don't really know, when i use repeat task, the sound run like one time, not delayed like my espect
     
  4. I just recognize that i forgot to add return ._.
    Thanks @TheViperShow

    Here's the final code
    Code (Text):

            taskId =  Main.getPlugin().getServer().getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable() {
                  int i = 0;
                  @Override
                  public void run() {
                      if (i == 4) {
                          Main.getPlugin().getServer().getScheduler().cancelTask(taskId);
                          return;
                      }
                      if (i == 0) {
                          p.playSound(l, Sound.BLOCK_NOTE_HARP, 20, 1);
                          i++;
                          return;
                      }
                      if (i == 1) {
                          p.playSound(l, Sound.BLOCK_NOTE_HARP, 20, Float.valueOf("1.5"));
                          i++;
                          return;
                      }
                      if (i == 2) {
                          p.playSound(l, Sound.BLOCK_NOTE_HARP, 20, 2);
                          i++;
                          return;
                      }
                      if (i == 3) {
                          p.playSound(l, Sound.BLOCK_NOTE_HARP, 20, 2);
                          i++;
                          return;
                      }
                  }
              }, 0L, 100L);
     
     
  5. Just wondering why are you writing this:
    Code (Java):
    Float.valueOf("1.5")
    instead of just
    Code (Java):
    1.5F
    ?

    You can just write the first letter of the word and it'll become that primitive for things like double = 0.0D/d, float = 1.0F/f, long = 20L/l.
     
    • Useful Useful x 1
  6. Consider using an array rather than repeating the same thing 4 times with one value changed.
     
    • Useful Useful x 1