Random String from config.

Discussion in 'Plugin Development' started by JasonW, Dec 18, 2011.

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

    iPhysX

    I would like to print a message to a player when they interact in certain circumstances. However, i would like the message to be randomly selected from a group of strings in the config file!

    Any suggestions on the best way to do this?
     
  2. Offline

    wwsean08

    you could get the list of that node and then just use a random number between 0 and the length of the list and display that string
     
    HotelManager24, halley and iPhysX like this.
  3. Offline

    iPhysX

    Sounds like this would be the simplest way to do it :) Thanks.
     
  4. Offline

    Chiller

    Or just save some strings in the config from numbers 1-10 and randomly choose a number and receive that string!
     
  5. Offline

    wwsean08

    no problem, that's at least the way I would do it, though @Chiller 's method should also work
     
  6. Have a string of words separated by a constant character say the comma. Then just read the whole string and split it by the comma putting it into an array. Then just get the size of the array and pick a random index :).
     
    HotelManager24 likes this.
  7. Offline

    iPhysX

    aah..
    So I could just split a massive string. Good.

    Off-topic
    @Adamki11s I'll come back on skype soon, just ordered Fifa12. Lol.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
    Adamki11s likes this.
  8. For xbox I hope :DD
     
  9. Offline

    iPhysX

    Oh I already have it on PC.
    So yes, xbox.
     
    Adamki11s likes this.
  10. Offline

    halley

    Chiller and Adamki11s both had reasonable suggestions, but I would say they both have downsides.

    First, Chiller's integers-in-the-config idea depends on the user/admin to correctly maintain all those integers. If they delete or comment out a string, they have to renumber all the others to avoid gaps. If they add a new one, they have to be sure to pick a new integer to go with it. wwsean08's plan avoids all that by just getting the array of strings and picking in that range; no gaps, no maintaining.

    Second, Adamki11s suggested putting all the strings in one value, and splitting that string on a separator like a comma. This works well sometimes, such as for very short strings like material identifiers. Again, it depends on an admin being careful to get this right. For longer strings like death messages, you have to get very creative to come up with a separator that can't appear in any meaningful message string. My input: why add more string parsing code that might have bugs or feature limitations, when the YAML parser is already debugged and well-known to all of the admins. Follow the YAML conventions for lists of things... and the parser does the work for you.
     
  11. Offline

    Father Of Time

    I apologize if I restate someone elses suggestion, I only had time to briefly brush over this thread, but I would try utilizing the built in configuration list storage.

    So, you could make a list of quotes:


    Code:
    List<String> Quotes = new HashMap<String>();
    Quotes.add("Quote1");
    Quotes.add("Quote2");
    Quotes.add("Quote3");
    then make a function that randomly returns a quote:


    Code:
        import java.util.Random
        ...
        public String GetQuote()
        {
            Random rand = new Random();
            int rnd = generator.nextInt( Quotes.size() );
            return Quotes.get( rnd );
        }
    Then just store and retrieve your collection of quotes from your config.yml file using Bukkits built in configuration tools.
     
  12. Offline

    Razorcane

    You know, in JDK7 switch statements now accept strings. So that's always an option.
     
  13. Offline

    iPhysX

    I don't use 7.
     
  14. Offline

    Sagacious_Zed Bukkit Docs

    CraftBukkit and Bukkit is compiled with java 5, so plugins should really try to match that, otherwise you can have an environment that runs CraftBukkit but not your plugin.

    But i digress fromt he topic,
    so to answer that personally it should just keep a list of strings in the config, over giving each string a key in the config. From a usability standpoint it works the best, and its actually quite simple to code.
     
  15. Offline

    Razorcane

    I've been using 7 since it came out, and it doesn't seem to cause any problems. JDK updates don't usually break previous things.

    You honestly should, it has some amazing features.
     
  16. What are some of these amazing features? I'm intrigued :)
     
  17. Offline

    iPhysX

    I tried it out.
    But i had some compilation issues.
    So i went back to 6.
     
  18. Offline

    Sagacious_Zed Bukkit Docs

    @Razorcane
    JVM are backwards compatible so if i compile for Java5 you can run it on a java7 JVM
    but if you compile for a Java7 JVM i can't run it on a Java6 JVM
    In the world of public servers, this is more likely to happen than on your own machines.
     
  19. Offline

    wwsean08

    i know i'm not helping the digression, but i have had problems with one of my plugins using java 7 because it uses some of the new built in stuff and some servers not being able to run it because they are still running java 6
     
  20. Offline

    Razorcane

    I only know of the ability to use strings in switch statements now, but I'm sure there are other minor improvements. Honestly everyone needs JDK7.
     
  21. Offline

    halley

    The whole "strings in switch statements" has nothing to do with picking a string at random from a set of strings. Of course it doesn't: you would need the string value already to use a string in a switch statement. Can we please stay on the original poster's topic?
     
  22. Offline

    iPhysX

    There has been a lot of helpful methods, and I'm sure I now know a load more ways to do these things!
    Thanks to everyone!
     
Thread Status:
Not open for further replies.

Share This Page