Quote from Johny on April 24, 2022, 2:20 pmJust posted this Mopar, going to post here also
Terms to know:
Integer: An integer is any whole number (1, -9, 1049, 0, etc). There are 4 types of integer's, we will focus on just one. An int!
Int: An int is a type of integer. Int's do have their limits, one that hopefully you never exceed . They have a minimum value of -2,147,483,648 and a maximum value of +2,147,483,647.
__________________________________________________ ____________
What misc.random is?
Well, looking at the actual code is always helpful.
Code:public static int random(int range) { return (int)(java.lang.Math.random() * (range+1)); }
So, by looking at the code you should have concluded that:
A) (int range) can be any number that you define: Used like
Code:misc.random(1)
B) You should see that an int is being multiplied by Math.random multiplied by the number you define (range) + 1
So if you were to use misc.random(3) the following numbers would be used and outputted.
1 x 0 = 0
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3This is because it takes the integer (1) and multiplies it by a random number, up to 3 in this case, and outputs the result.
__________________________________________________ _________________
So why use this? We would use this for random events for example! We would do this like the following:
Code:int event = Misc.random(3); //defines what the int is (0,1,2, or 3) and names it event if (event == 0) { //States that if the int (event) is 0 something will happen c.sendMessage("The event outputs 0"); //Sends a message :P } if (event == 1) { //States that if the int (event) is 1 something will happen c.sendMessage("The event outputs 1"); //Sends a message :P } if (event == 2) { //States that if the int (event) is 2 something will happen c.sendMessage("The event outputs 2"); //Sends a message :P } if (event == 3) { //States that if the int (event) is 3 something will happen c.sendMessage("The event outputs 3"); //Sends a message :P }
However, if we were using a huge number like misc.random(1000) we would not want to define each number, as there would be 1001 if statements! (YUCK!)
So to fix this we could use something similar to:
Code:if (event == 327) { //States that if the int is 357 something will happen c.sendMessage("The event outputs 357"); //Sends a message :P } else { c.sendMessage("The event was not 357"); //All numbers that come up other then 357 will now output this message! }
So, now that the basic's of this feature are understood we can put it to use!
Under your click object case (case 132), we could add the clicking of a crate and the crate could have a few different outcomes.
So try something like this:
Code:case 357: //Crate c.sendMessage("You search the crate.."); //Sends the player a message int prize = Misc.random(3); //defines the int (can be any number from 0 - 4) if(prize == 0) { //if the int is 0 c.sendMessage("You find a nasty old cat hiding in the crate!"); //sends a message c.addItem(1555, 1); //Adds a cat :confused: :| } if(prize == 1) { //if the int is 1 int coins = Misc.random(300); //defines a new int (any number from 0 to 301) c.sendMessage("You find a few dusty coins..");//sends message c.getItems().addItem(995, coins);//adds an item and a random amount up to 301 } if(prize == 2) { //if the int is 2 c.sendMessage("You find an odd looking garment.."); //sends message c.addItem(6857); //Adds a scarf } if(prize == 3) { //if the int is 3 c.sendMessage("You fail to find anything.."); //sends message } break;
Now you have basically figured out the misc.random feature! By following this tutorial you should now be able to add many different types of random features into your server! Things such as random events, a random amount of items, or you could even base a skill such as cooking off of it!
Just posted this Mopar, going to post here also
Terms to know:
Integer: An integer is any whole number (1, -9, 1049, 0, etc). There are 4 types of integer's, we will focus on just one. An int!
Int: An int is a type of integer. Int's do have their limits, one that hopefully you never exceed . They have a minimum value of -2,147,483,648 and a maximum value of +2,147,483,647.
__________________________________________________ ____________
What misc.random is?
Well, looking at the actual code is always helpful.
public static int random(int range) {
return (int)(java.lang.Math.random() * (range+1));
}
So, by looking at the code you should have concluded that:
A) (int range) can be any number that you define: Used like
misc.random(1)
B) You should see that an int is being multiplied by Math.random multiplied by the number you define (range) + 1
So if you were to use misc.random(3) the following numbers would be used and outputted.
1 x 0 = 0
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
This is because it takes the integer (1) and multiplies it by a random number, up to 3 in this case, and outputs the result.
__________________________________________________ _________________
So why use this? We would use this for random events for example! We would do this like the following:
int event = Misc.random(3); //defines what the int is (0,1,2, or 3) and names it event
if (event == 0) { //States that if the int (event) is 0 something will happen
c.sendMessage("The event outputs 0"); //Sends a message :P
}
if (event == 1) { //States that if the int (event) is 1 something will happen
c.sendMessage("The event outputs 1"); //Sends a message :P
}
if (event == 2) { //States that if the int (event) is 2 something will happen
c.sendMessage("The event outputs 2"); //Sends a message :P
}
if (event == 3) { //States that if the int (event) is 3 something will happen
c.sendMessage("The event outputs 3"); //Sends a message :P
}
However, if we were using a huge number like misc.random(1000) we would not want to define each number, as there would be 1001 if statements! (YUCK!)
So to fix this we could use something similar to:
if (event == 327) { //States that if the int is 357 something will happen
c.sendMessage("The event outputs 357"); //Sends a message :P
}
else {
c.sendMessage("The event was not 357"); //All numbers that come up other then 357 will now output this message!
}
So, now that the basic's of this feature are understood we can put it to use!
Under your click object case (case 132), we could add the clicking of a crate and the crate could have a few different outcomes.
So try something like this:
case 357: //Crate
c.sendMessage("You search the crate.."); //Sends the player a message
int prize = Misc.random(3); //defines the int (can be any number from 0 - 4)
if(prize == 0) { //if the int is 0
c.sendMessage("You find a nasty old cat hiding in the crate!"); //sends a message
c.addItem(1555, 1); //Adds a cat :confused: :|
}
if(prize == 1) { //if the int is 1
int coins = Misc.random(300); //defines a new int (any number from 0 to 301)
c.sendMessage("You find a few dusty coins..");//sends message
c.getItems().addItem(995, coins);//adds an item and a random amount up to 301
}
if(prize == 2) { //if the int is 2
c.sendMessage("You find an odd looking garment.."); //sends message
c.addItem(6857); //Adds a scarf
}
if(prize == 3) { //if the int is 3
c.sendMessage("You fail to find anything.."); //sends message
}
break;
Now you have basically figured out the misc.random feature! By following this tutorial you should now be able to add many different types of random features into your server! Things such as random events, a random amount of items, or you could even base a skill such as cooking off of it!