1

Any Java experts, help?

Rawstrus's Avatar Rawstrus10/30/13 4:21 pm
10/30/2013 5:18 pm
Paril's Avatar Paril
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
Posted by Rawstrus's Avatar
Rawstrus
Level 40 : Master Nerd
28

Create an account or sign in to comment.

15

1
10/30/2013 5:18 pm
Level 88 : Elite Scapegoat
Paril
Paril's Avatar
As others said, this is proper behavior.
Rather than doing that, you should have the entire list shuffled, then go through it sequentially.
1
10/30/2013 5:07 pm
Level 54 : Grandmaster Programmer
nickfromgreece
nickfromgreece's Avatar
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.
1
10/30/2013 5:09 pm
Level 40 : Master Nerd
Rawstrus
Rawstrus's Avatar
Yeah, what I want though is for it not to repeat.
1
10/30/2013 5:07 pm
Level 40 : Master Nerd
Rawstrus
Rawstrus's Avatar
Everything works perfect now, thank you MurasakiBandit.
1
10/30/2013 5:08 pm
Level 4 : Apprentice Toast
MurasakiBandit
MurasakiBandit's Avatar
Yayyy so glad I could help
1
10/30/2013 5:05 pm
Level 40 : Master Nerd
Rawstrus
Rawstrus's Avatar
XtremeGamer572
MurasakiBandit//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!
1
10/30/2013 4:55 pm
Level 4 : Apprentice Toast
MurasakiBandit
MurasakiBandit's Avatar
//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
1
10/30/2013 5:00 pm
Level 40 : Master Nerd
Rawstrus
Rawstrus's Avatar
Oh, that would make sense.

Hmm,

1
10/30/2013 5:02 pm
Level 4 : Apprentice Toast
MurasakiBandit
MurasakiBandit's Avatar
Is that the whole result, or is the last 3 the result?
1
10/30/2013 4:52 pm
Level 40 : Master Nerd
Rawstrus
Rawstrus's Avatar
XtremeGamer572
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 tried it, but chat remains empty after this. Like, it broadcasts nothing, no errors either
1
10/30/2013 4:45 pm
Level 4 : Apprentice Toast
MurasakiBandit
MurasakiBandit's Avatar
Look at my code again, I keep editing it lol
1
10/30/2013 4:41 pm
Level 40 : Master Nerd
Rawstrus
Rawstrus's Avatar
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
1
10/30/2013 4:37 pm
Level 4 : Apprentice Toast
MurasakiBandit
MurasakiBandit's Avatar

//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.
1
10/30/2013 4:27 pm
Level 42 : Master Modder
BitBox
BitBox's Avatar
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.
1
10/30/2013 4:30 pm
Level 40 : Master Nerd
Rawstrus
Rawstrus's Avatar
could you give an example please?
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome