I am relatively new to Java. I am currently putting together a mod involving a Human NPC. Using these tutorials (POST 9) (http://www.minecraft...111-generation/), I was able to successfully make an NPC; I also put in the code from the tutorial to make my NPC speak when right-clicked. This is the code:
public boolean interact(EntityPlayer entityplayer) { ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Hello there!"); return true; } This works just fine; however, I would like to know how, if possible, to either randomise or say a different phrase each time the NPC is clicked. If I copy the ModLoader line and write a different message in, it will say both of the phrases instead of one individually.
As I said, I am new to Java. Could this be randomised by adding a 'random' class somewhere in the code?
I'm honestly not too terribly knowledgeable about Java myself, but instead of having the phrases randomized, could you somehow set it up so that your NPC would say the phrases in order, with a different one for each time you clicked him?
I honestly couldn't tell you HOW to do such a thing, I was just trying to help brainstorm, haha.
I've tried doing it so the phrases are said in order; however, it always comes out as a big mass of writing with everything together, instead of them separate.
You will need to use javas Random object to generate a random number and the determine the phrase based on that.
So use something like this:
public boolean interact(EntityPlayer entityplayer)
{
Random random = new Random();
int random_numer = random.nextInt(2); // this generates a random number between 0-2 so the possible outputs are 0,1,2
if(random_number == 0) {
ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Phrase 1");
}
if(random_number == 1) {
ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Phrase 2");
}
if(random_number == 2) {
ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Phrase 3");
}
return true;
}
I can't thank you enough, Miniature Tuxedo and Logicx! Both methods worked and I have successfully put random chat messages in!
You have my thanks, once again. I really appreciate it.
(And Miniture Tuxedo, what you wrote was very clear and easy to understand. I am relatively new to Java, having only watched tutorials and the like, so how you explained it was helpful.)
I'm actually beginning Java this year as part of one of my subjects, so I thought I'd get a head-start making something. Now I'll always know how to randomise selections!
public String[] phrases = new String[] {
"Random Message 1", "Random Message 2", "Random Message 3" }
public boolean interact(EntityPlayer entityplayer)
{
Random random = new Random();
int num = random.nextInt(phrases.length);
ModLoader.getMinecraftInstance().thePlayer.addChatMessage(phrases[num]);
return true;
}
public String[] phrases = new String[] {
"Random Message 1", "Random Message 2", "Random Message 3" }
public boolean interact(EntityPlayer entityplayer)
{
Random random = new Random();
int num = random.nextInt(phrases.length);
ModLoader.getMinecraftInstance().thePlayer.addChatMessage(phrases[num]);
return true;
}
Definitely, definitely, DEFINITELY do it this way. It's much more maintainable, and you'll thank yourself later. When programming, remember to copy/paste as little as you have to :wink.gif:
I am relatively new to Java. I am currently putting together a mod involving a Human NPC. Using these tutorials (POST 9) (http://www.minecraft...111-generation/), I was able to successfully make an NPC; I also put in the code from the tutorial to make my NPC speak when right-clicked. This is the code:
public boolean interact(EntityPlayer entityplayer)
{
ModLoader.getMinecraftInstance().thePlayer.addChatMessage("Hello there!");
return true;
}
This works just fine; however, I would like to know how, if possible, to either randomise or say a different phrase each time the NPC is clicked. If I copy the ModLoader line and write a different message in, it will say both of the phrases instead of one individually.
As I said, I am new to Java. Could this be randomised by adding a 'random' class somewhere in the code?
Thanks in advance.
I honestly couldn't tell you HOW to do such a thing, I was just trying to help brainstorm, haha.
I've tried doing it so the phrases are said in order; however, it always comes out as a big mass of writing with everything together, instead of them separate.
Thanks for the idea, though. I appreciate it.
So use something like this:
Want a place to advertise your Minecraft server? try MyMCStatus.net now!
You have my thanks, once again. I really appreciate it.
(And Miniture Tuxedo, what you wrote was very clear and easy to understand. I am relatively new to Java, having only watched tutorials and the like, so how you explained it was helpful.)
Cheers!
Definitely, definitely, DEFINITELY do it this way. It's much more maintainable, and you'll thank yourself later. When programming, remember to copy/paste as little as you have to :wink.gif:
Many thanks again.