OK so I am making a plugin on bukkit, but I am trying to make it display random messages from a config.
My code is supposed to be random, but it isn't exactly amazing, it repeats itself.
SO I need to make it so that the message being sent cannot be the message that was JUST sent, I wouldn't know how to store it and use it in the randomiser.. here's my code so far:
And finally, here's the config:
Hope you can help.
Thanks
-XtremeGamer572
My code is supposed to be random, but it isn't exactly amazing, it repeats itself.
SO I need to make it so that the message being sent cannot be the message that was JUST sent, I wouldn't know how to store it and use it in the randomiser.. here's my code so far:
And finally, here's the config:
Hope you can help.
Thanks
-XtremeGamer572
Create an account or sign in to comment.
15
I'm not the best at Java, but you could make a variable, and that would be the last message that was displayed? Then, it picks a random string except the variable.
could you give an example please?
//Put this somewhere else so it doesnt get re-initialized
int lastChoice = -1;
public void run() {
List<String> Message_ = getConfig().getStringList("Messges");
Random rand = new Random();
int choice = rand.nextInt(Message_.size());
if(choice == lastChoice) {
if(choice == (Message_.size() - 1)) {
Bukkit.getServer().broadcastMessage(Message_.get(choice-1));
} else {
Bukkit.getServer().broadcastMessage(Message_.get(choice+1));
}
lastChoice = choice;
}
}
This was not tested, but I don't see why it wouldn't work.
MurasakiBanditpublic void run() {
List<String> Message_ = getConfig().getStringList("Messges");
Random rand = new Random();
int choice = rand.nextInt(Message_.size());
int lastChoice = -1;
if(choice == lastChoice) {
if(choice == (Message_.size() - 1)) {
Bukkit.getServer().broadcastMessage(Message_.get(choice--));
} else {
Bukkit.getServer().broadcastMessage(Message_.get(choice++));
}
lastChoice = choice;
}
}
This was not tested, but I don't see why it wouldn't work.
I had something similar, it didn't work. I'll try this though
Look at my code again, I keep editing it lol
XtremeGamer572MurasakiBanditpublic void run() {
List<String> Message_ = getConfig().getStringList("Messges");
Random rand = new Random();
int choice = rand.nextInt(Message_.size());
int lastChoice = -1;
if(choice == lastChoice) {
if(choice == (Message_.size() - 1)) {
Bukkit.getServer().broadcastMessage(Message_.get(choice--));
} else {
Bukkit.getServer().broadcastMessage(Message_.get(choice++));
}
lastChoice = choice;
}
}
This was not tested, but I don't see why it wouldn't work.
I tried it, but chat remains empty after this. Like, it broadcasts nothing, no errors either
//Put this somewhere else so it doesnt get re-initialized
int lastChoice = -1;
public void run() {
List<String> Message_ = getConfig().getStringList("Messges");
Random rand = new Random();
int choice = rand.nextInt(Message_.size());
if(choice == lastChoice) {
if(choice == (Message_.size() - 1)) {
Bukkit.getServer().broadcastMessage(Message_.get(choice-1));
} else {
Bukkit.getServer().broadcastMessage(Message_.get(choice+1));
}
lastChoice = choice;
} else Bukkit.getServer().broadcastMessage(Message_.get(choice));
//ooops, forgot that last line lol
}
Try that now lol
Oh, that would make sense.
Hmm,
Hmm,
Is that the whole result, or is the last 3 the result?
XtremeGamer572MurasakiBandit//Put this somewhere else so it doesnt get re-initialized
int lastChoice = -1;
public void run() {
List<String> Message_ = getConfig().getStringList("Messges");
Random rand = new Random();
int choice = rand.nextInt(Message_.size());
if(choice == lastChoice) {
if(choice == (Message_.size() - 1)) {
Bukkit.getServer().broadcastMessage(Message_.get(choice-1));
} else {
Bukkit.getServer().broadcastMessage(Message_.get(choice+1));
}
lastChoice = choice;
} else Bukkit.getServer().broadcastMessage(Message_.get(choice));
//ooops, forgot that last line lol
}
Try that now lol
=EDIT=
I forgot to remove int lastChoice = -1;
before I moved it, silly me.
Thanks so much!
Everything works perfect now, thank you MurasakiBandit.
Yayyy so glad I could help
I dont think you understand the definition of "random" when something is random it can come in any order you can get a message 5000 times or not even once.
Yeah, what I want though is for it not to repeat.
As others said, this is proper behavior.
Rather than doing that, you should have the entire list shuffled, then go through it sequentially.
Rather than doing that, you should have the entire list shuffled, then go through it sequentially.