Hello everyone Yeah, so i'm making a plugin that shows the player a message when they do a certain event. The thing is, the person im making it for wants it to be setup so that there are several messages avaialable and they are on loop so the same message isn't repeated. I got config to work along with colour codes. I use Bukkit Standard configuration .yml stuff. I prefer not to use .bml :S So if anyone knows how to set messages for it and make them do one after another when the player does the event, then thanks
Not exactly sure what you want but if I understand correctly: Make a string array of all the options, get a random generated number with the array length, send the string with the random array.
@Randy Schoutenthat's probably what i need, but ill explain more. So when a player kills a pig, it will get the message from config, do some cool colour stuff also from config, and then give it to player, but normally, that only means one message is possible, but to save boringness, he wants a range of messages he can configure. So i would want it so it randomly goes one after another, or maybe a looped sequence?
From what you have said I get this: 1. A file of different "sayings" 2. plugin loads the "sayings" 3. event occurs it randomly choses a "saying" 4. then message...?? Well then what I would do then is make the plug in read a file and have a message or "saying" on each line be put in an arraylist so you have to import an arraylist. then when the event calls for a message just calls a message() and that would return a random message from the array
They can get repeated twice if the random get the same number twice lol and the way i'd avoid that is to make a varable of last index chosen and if the random chose that then make it randomiz again
If all your messages are in a single array, you could try using an Iterator and if !hasNext(), just reinitialize it. Its not random, but it wont repeat anything either.
i would use a random number generator, with an array of strings and an infinite loop, here is some help with the random class in java.... http://www.cs.geneseo.edu/~baldwin/reference/random.html and math.random could work just as well
and if you look her you can also just iterate threw the array an then using the array.shuffle method itterate threw the array again but this it would be in a difffrent order http://leepoint.net/notes-java/data/arrays/arrays-library.html
So lets see. List<String> list = config.getStringList("yourDumbConfig", new ArrayList<String>()); Iterator<String> iter = list.iterator(); while(iter.hasNext()) { String output = iter.next(); } iter = list.iterator(); Like bush said, theres the Collections.shuffle(list) method, that does what it says.
You mean how is the YAML config for stringlists? Code: List<String> list = config.getStringList("messages"); messages: - I ate a cookie - It was good - You get the idea
Put them in a string[], then do Code: int x; Random ran = new Random(array.length()) Or something like that. Can't remember too well, but then pick the one from the array with the random number has pointed to.
thanx for the reply, but that part i know how to do, its getting the custom strings that the user changes in the config file to actually go in to the array
Code:java List<String> list = config.getStringList("messages");int x;Random generator = new Random(list.length);String randommessage = list.get(generator.nextInt());player.sendMessage(randommessage);
That is how you would do it in code. Code: List<String> list = config.getStringList("messages", new ArrayList<String>()); and the YAML would look like Code: messages: - I ate a cookie - It was good - You get the idea To iterate though, either use the random method, or a cyclic iterator. Code: private Iterator<String> iter = list.iterator(); public String getMessage() { if(!list.isEmpty()) { if(iter.hasNext()) { return iter.next(); } else { iter = list.iterator(); return getMessage(); } } }