Outputting random word out of a list?

Discussion in 'Plugin Development' started by Segatakai, Aug 24, 2013.

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

    Segatakai

    Hi, i'm making a plugin that when a player types in a command, the plugin will send the player a word out of a list of 4 and the server op will be sent that word and the players name.
    E.g.

    Player types "/word"

    Player gets sent word out of these 4 words : 'One', 'Two', 'Three' and 'Four'
    Player gets sent 'Two'
    Op gets sent player name and 'Two'

    If someone could help that would be just great. Thanks in advance!
     
  2. Offline

    metalhedd

    // setup
    Random rand = new Random();
    List<String> words = new ArrayList<String>()
    words.add("One");
    words.add("Two");
    words.add("Three");
    words.add("Four");

    // choose a random word
    String word = words.get(rand.nextInt(words.size()));
     
  3. Offline

    Segatakai

    Thanks very much, works perfectly, do you know if I can make the command only able to be used once?
     
  4. Offline

    metalhedd

    Yes you can.

    Edit: Yes I'm intentionally avoiding writing it for you. The answer is pretty obvious. you just store whether or not the command has been used, and then check it. ie. programming.
     
  5. Offline

    Segatakai

    Can you tell me how please?
     
  6. Offline

    metalhedd

    no. see my edit.
     
  7. Offline

    Segatakai

    well, i've only really been learning java for a few weeks, but if that's all your going to tell me then thanks for the help you have given.
     
  8. Offline

    metalhedd


    "you just store whether or not the command has been used, and then check it."

    unfortunately it's the only answer I can give that's not just writing the code for you (again). and that doesn't help anyone. If you're trying to learn, then you should attempt to translate what I said above into java... hint: it involves a boolean and an 'if' statement.
     
  9. Offline

    Axe2760

    Not going to give code really, but I'll try to help you a bit with how to do it.

    You want to store a Boolean value for each player. When the player runs whatever command, check to see if the Boolean is false. If it is, then run the command and set the Boolean to true. Therefore it will only run once.
    If you only want the command run once on the server, then make a Boolean variable in your plugin. If you want it once per player, it's slightly more complicated (not really), look into HashMaps. (Google, it has been explained a billion trillion times.) You would store a variable of type HashMap<String,Boolean>

    Edit: last post about trying to learn it for yourself was posted while i was posting. Sorry! :p
     
  10. Offline

    Connal

    Help please this is what i have

    } else if (commandLabel.equalsIgnoreCase("gmbword")) {
    player.sendMessage(ChatColor.AQUA + "Choosing a random word to build...");
    // setup
    Random rand = new Random();
    List<String> words = new ArrayList<String>();
    words.add("Hill");
    words.add("Tree");
    words.add("Apple");
    words.add("Bannana");

    // choose a random word
    String word = words.get(rand.nextInt(words.size()));

    And all it does is Choosing a random word to build... and it doesn't select any of the words! Please help!
     
  11. Offline

    metalhedd

    String word = words.get(rand.nextInt(words.size()));
    That line selects one of the words. if you want to do something with it... then you have to do something with it. right now it just selects one and stores it as 'word'
     
  12. Offline

    Connal


    Then how do i make it send the player who sent the command the word that was picked? (im still only learning these basics)
     
  13. Connal
    Code:
    player.sendMessage("Chosen word: " + words.get(rand.nextInt(words.size());
     
  14. Offline

    Connal

    omg whoever you are assist ur awesome :D

    Assist ummm at the end of the player.sendMessage("Chosen word: " + words.get(rand.nextInt(words.size());
    it has a red underline on the bracket, why? does it matter?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  15. Connal
    Sorry, use this:
    Code:
    player.sendMessage("Chosen word: " + words.get(rand.nextInt(words.size())));
     
    Connal likes this.
  16. Offline

    Connal


    ur just so epic assist :) im following u
     
Thread Status:
Not open for further replies.

Share This Page