Many noobs in java try to code minecraft mods but they don't even have a single idea on how java work and what are its utility such as random. From what i've noticed, Random is rarely used yet can make a mod really interesting depending on how you're gonna use it.
In this example, i'm gonna add a 10% chance of something happening when i walk on a certain block.
So lets get right into it.
First you wanna chuck this at the top of your block code or wherever you're using this
(Tips: You place import under package)
import java.util.*;
This will automatically import all java utility INCLUDING Random.
next i'm just gonna add the method that will detect when an entity walk on this block
public void onEntityWalking(World var1, int var2, int var3, int var4, Entity var5) {
}
Now, for the random.
Random generator = new Random();
int r = generator.nextInt(100);
inside of my onEntityWalking. This will generate a random number between 0 and 100 every time an entity walk on this block
under this random generator, i'll add this
if (r<=10) {
myBadassMethodAndStuff();
}
let me explain, if (r<=10) { will check if the generated value is 10 or lower than 10 and if it is, myBadassMethodAndStuff() method will run. In this case, the chance of it running is 10%. Really simple system. you might wanna generate between 0 and 1000 for more preciseness.
Hopefully this has been helpful for new modders. I'd do more original tutorial like this but i'm busy on my very own mod at the moment.
you can check it out by clicking the banner in my signature.
If you are either too stupid to understand this or simply started coding java yesterday, this is the final method:
public void onEntityWalking(World var1, int var2, int var3, int var4, Entity var5) {
Random generator = new Random();
int r = generator.nextInt(100);
if (r<=10) {
myBadassMethodAndStuff();
}
}
next tutorial if i have time will be basic loop (For and While)
if you have any question about Random, just leave a post below and i'll do my best to answer you.
And for heavens sake - please practice on simple java programs first. Don't even attempt it unless you know about program flow (if, while, do-while, for, foreach etc), recursion, exception statements etc etc. Oh and also how to read error messages.
Sadly, alot of n00bs think modding is just copy pasting the code from mr tutorial and they don't even know what it mean except for the part mr tutorial explained. They don't even have the basic of java such as knowing your integer like boolean, byte, short, int, long etc.
The result: Mod with Dirt, slime and obsidian tool and 20 new ore where the hardest work was copy pasting everything. This sadden me
Sadly, alot of n00bs think modding is just copy pasting the code from mr tutorial and they don't even know what it mean except for the part mr tutorial explained. They don't even have the basic of java such as knowing your integer like boolean, byte, short, int, long etc.
The result: Mod with Dirt, slime and obsidian tool and 20 new ore where the hardest work was copy pasting everything. This sadden me
Completely agree. And then they whine when they get simple errors - even syntax errors! I mean, come on! It tells you what the error is if you just read!
I would put it down to laziness though. People want to mod, and mod now, without putting in the effort. And programming takes effort! Been studying java for over a year now as part o my university course, but also done a fair bit with c (and even vba in high school (ages 11 to 16 <= british).). Even now I don't know it all, no where near, but if you cant do the basics at a hundred miles an hour, how are you supposed to do the harder stuff!
Also going to add to my list of things you should be able to do before you mod: generic classes (and normal classes too lol), the math class, event handling and casting (which is a piece of **** if you know the types)!
i started getting interested in java when i was like 8 i was making maplestory private server but i never really got serious in java until now. my first language is french too (from quebec) now i'm 14 and i get the best grade in english at school. Programming language has alway intrigued me and i think their awesome
For a single call it might be less code.
But what if you're having 10+ calls?
It will be random.nextInt(val) vs (int) (Math.random() * 100)
It doesn't matter so much. It only uses about 2 less lines (the import and the object). Its just how I learned to do it. It might be more lightweight to use the actual random object.
In this example, i'm gonna add a 10% chance of something happening when i walk on a certain block.
So lets get right into it.
First you wanna chuck this at the top of your block code or wherever you're using this
(Tips: You place import under package)
This will automatically import all java utility INCLUDING Random.
next i'm just gonna add the method that will detect when an entity walk on this block
Now, for the random.
inside of my onEntityWalking. This will generate a random number between 0 and 100 every time an entity walk on this block
under this random generator, i'll add this
let me explain, if (r<=10) { will check if the generated value is 10 or lower than 10 and if it is, myBadassMethodAndStuff() method will run. In this case, the chance of it running is 10%. Really simple system. you might wanna generate between 0 and 1000 for more preciseness.
Hopefully this has been helpful for new modders. I'd do more original tutorial like this but i'm busy on my very own mod at the moment.
you can check it out by clicking the banner in my signature.
If you are either too stupid to understand this or simply started coding java yesterday, this is the final method:
next tutorial if i have time will be basic loop (For and While)
if you have any question about Random, just leave a post below and i'll do my best to answer you.
Sadly, alot of n00bs think modding is just copy pasting the code from mr tutorial and they don't even know what it mean except for the part mr tutorial explained. They don't even have the basic of java such as knowing your integer like boolean, byte, short, int, long etc.
The result: Mod with Dirt, slime and obsidian tool and 20 new ore where the hardest work was copy pasting everything. This sadden me
100% agreed
Double post -.-
i started getting interested in java when i was like 8 i was making maplestory private server but i never really got serious in java until now. my first language is french too (from quebec) now i'm 14 and i get the best grade in english at school. Programming language has alway intrigued me and i think their awesome
Both work, but this is what I learned.
Thinking about coming a mod to simply not moderate.
I just shown you in this tutorial. HAZ COMON SENSE? U HAZ?
Edit:
It doesn't matter so much. It only uses about 2 less lines (the import and the object). Its just how I learned to do it. It might be more lightweight to use the actual random object.
Thinking about coming a mod to simply not moderate.
Thanks :tongue.gif: