Giving Everyone A Random Name from a String[] with TagAPI

Discussion in 'Plugin Development' started by LordVakar, May 5, 2014.

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

    LordVakar

    So I'm making a Minecraft Gmod Murder plugin and I want everyone to have a random name using TagAPI

    public static String[] names = {
    "Name1 ", "Name2 "
    };

    How would I execute making everyone have a random name from my String[] once:
    if(GameState = inGame) {
    //Give Random Names here
    }

    I'm not sure how to assign random names with TagAPI from a String[]
     
  2. Offline

    Gater12

    LordVakar
    You select a Random number between 0 and the array size from the array.
     
  3. Offline

    Ytry

    LordVakar What do you mean by random name? Do you want them to have an eight digit jumble of letters, or an actual name that make sense I.E: Tim, or John?
     
  4. Offline

    Gater12

    Ytry
    Basicurry, he has an array of Strings and he wants to select a random value from that array to use.
     
  5. Offline

    LordVakar

    Gater12 Ytry
    So like I would have a String[]:
    public static String[] nameTags = {
    "Bob ",
    "Joe ",
    }
    And I want to get names from that String[] and assign them to random people with TagAPI

    So I would use a Random
    public static Random rd = new Random();

    So if there were like 25 names I would get a random number?
    int getRandomName = rd.nextInt(25);

    How would I set that random name?
     
  6. Offline

    Gater12

    LordVakar
    Ok you have your array of Strings
    Code:java
    1. String[] leArrayDeString = {"Bob", "Mary Jane", "Gater"};


    You want to create a Random number from 0 to the array size.
    Code:java
    1. Random randomGen = new Random();
    2. int randomNumber = randomGen.nextInt(leArrayDeString.length);


    So the number can be anywhere from 0 to 2 (3 is excluded as we Java counts 0s. That is why if we want to generate a number that excludes 0 we add one to it)

    Now we get the name.
    Code:java
    1. String randomName = leArrayDeString[randomNumber];
     
  7. Offline

    Ytry

    Gater12 curse you, you beat me to it :D
     
  8. Offline

    LordVakar

    Gater12 Ytry
    My question would be, how would I actually set the name on the person's head?
    I know there is a AsyncPlayerReceiveNameTagEvent in TagAPI, but I haven't worked with TagAPI often so I don't know when it is fired.

    And you can't have an event inside a method because I want the people to be assigned names when:
    if(GameState = inGame) { //Custom thing in my plugin xP
    //Give Random Names here
    }
     
Thread Status:
Not open for further replies.

Share This Page