Random Function chooser.

Discussion in 'Spigot Plugin Development' started by Zarlen, May 3, 2017.

  1. Hello!
    I'm trying to make an event when the player joins the server it will say a random quote.

    I know, I can use random messages but the problem is: each message has it playsound that has to be played with the message. So for example:
    Code (Text):
    event.getPlayer().sendMessage("Test Message! You heard the sound MUSIC1!");
    event.getPlayer().playSound(loc, "MUSIC1", 1, 1);
    //
    event.getPlayer().sendMessage("Test Message! You heard the sound MUSIC2!");
    event.getPlayer().playSound(loc, "MUSIC2", 1, 1);
    //
    event.getPlayer().sendMessage("Test Message! You heard the sound MUSIC3!");
    event.getPlayer().playSound(loc, "MUSIC3", 1, 1);
    // I'm using a custom texurepack for the sounds tho
    So how do I make a random thing that chooses 'something' and then executes one combo of @Above.
     
  2. konsolas

    Supporter

    switch on a random integer.

    Example:
    Code (Java):
    switch(ThreadLocalRandom.current().nextInt(4)) {
        case 0:
            // Send one message
            break;
        case 1:
            // Send another message
            break;
        case 2:
            // Send other message
            break;
        case 3:
            // Send other message
            break;
    }
     
  3. And everytime when I execute this one of the cases will be executed?
     
  4. konsolas

    Supporter

    Yes, a random one. The first line generates a random number between 0 and 3 (bounded above by 4), and the case with the generated number is executed.
     
  5. Thanks for your help man, gonna implent this now.