Solved Randomly chooses between 1st, 2nd or 3rd void.

Discussion in 'Plugin Development' started by adde, Mar 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    adde

    Hello, I am making a motd plugin that will randomly choose between 3 messages.

    These are:
    Code:java
    1.  
    2. event.setMotd(replaceColors(plugin.config.getString("Main.MOTD").replaceAll("%name%", existing.getString("player"))));
    3. event.setMotd(replaceColors(plugin.config.getString("Main.MOTD2").replaceAll("%name%", existing.getString("player"))));
    4. event.setMotd(replaceColors(plugin.config.getString("Main.MOTD3").replaceAll("%name%", existing.getString("player"))));
    5.  


    How do I make the plugin choose between one of these?
    Randomly each time the user you know.. "Sees" the server tab.
    Like if he/she presses "Refresh" or press "Back" then on multiplayer again.. how do I make it change each time?

    Help would be appreciated.
     
  2. new Random().nextInt(3); gives you a random value between 0 and 2... I suggest you just add 1 to that value and use it directly after Main.MOTD string. (also add 1 to your first MOTD key, e.g. Main.MOTD1)
     
  3. Offline

    adde

    Okey, I understand that I should put 1 after Main.MOTD but how should I do with new Random().nextInt(3);?
    Give me a code please? :(
     
  4. I've already given you the code... you should learn basic Java :)

    Code:
    int num = new Random().nextInt(3) + 1; // random 1 to 3
    
    event.setMotd(replaceColors(plugin.config.getString("Main.MOTD" + num).replaceAll("%name%", existing.getString("player"))));
    number + number adds the numbers, but string + object calls object.toString() on it and joins them together.
     
  5. Offline

    adde

    AAAAH!
    I understand now! Thank you.

    But it didn't work.. :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    chasechocolate

    Does it give any errors?
     
  7. Offline

    adde

    Nope.

    Can someone please explain why it doesn't work?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Offline

    HeyShibby

    what event are you calling this in may I ask?
     
  9. Can you please give use the full code adde
     
  10. Did your initial code even work ? :) Because I only added the random number to your code.

    But yeah, post the full code otherwise we're just guessing.
     
  11. Offline

    adde

    Nvm guys, I had to do this on another place too!

    int num = new Random().nextInt(3) + 1; // random 1 to 3

    event.setMotd(replaceColors(plugin.config.getString("Main.MOTD" + num).replaceAll("%name%", existing.getString("player"))));
     
Thread Status:
Not open for further replies.

Share This Page