 # Explain this code?

Discussion in 'Plugin Development' started by McStormify, Apr 5, 2013.

Not open for further replies.
1. Offline

### McStormify

I found this code on Github to create an arrow wall, but after almost 30 minutes of trying to understand it, I've gotten nowhere. Perhaps someone here can help?

Code:java
`public void shoot(Player player, int arrowCount) {    for (int i = 0; i < arrowCount; i++) {        Location location = player.getLocation();         location.setX((location.getX() - radius) + (radius * 2 + 1) * Math.random());        location.setY(location.getY() + (4 + (3 * Math.random())));        location.setZ((location.getZ() - radius) + (radius * 2 + 2) * Math.random());         Vector vector = location.getDirection();         player.getWorld().spawnArrow(location, vector, (float) 1.0, (float) 12);    }}`

Thanks so much for your help! Here's a cookie to show my appreciation!  #1
2. Offline

### Forseth11

McStormify does this explain it: (read the // comments.)

PHP:
``` public void shoot(Player player, int arrowCount) { //This is a method    for (int i = 0; i < arrowCount; i++) {                                //for loop repeats the amount in arrowCount        Location location = player.getLocation();                    //this gets the Player's location (world, x, y, z)         location.setX((location.getX() - radius) + (radius * 2 + 1) * Math.random());  //This takes the  radius times by two + 1 times by (0.0001 - 0.9999) + x coordinate - by the radius.        location.setY(location.getY() + (4 + (3 * Math.random())));                            //This takes (3 * (0.0001 - 0.9999) + 4 + y coordinate.        location.setZ((location.getZ() - radius) + (radius * 2 + 2) * Math.random());  // this is the same as the x coordinate, but it adds one to radius times by two.         Vector vector = location.getDirection();                          //This puts the direction the player is facing and puts it into a vector         player.getWorld().spawnArrow(location, vector, (float) 1.0, (float) 12);  //This places the arrows in the location of the player and faces them in the direction the player is facing. I think the floats has something to do with how far and fast it goes.    }} ```
I am just explaining the code how I see it. I have never used spawnArrow before so I'm not sure. Basically what this does is spawn arrowCound amount of arrows in a random spot around the player facing the same direction.

I hope this helped.

#2
devilquak likes this.
3. Offline

### MrSparkzz

This is the "shoot" method
Code:java
` public void shoot(Player player, int arrowCount) { `

This is a for loop. This will keep running until it returns false. What this is doing is reading how many arrows there are and comparing it to int i, which gets added +1 every time this loops.
Code:java
` for (int i = 0; i < arrowCount; i++) { `

This is a variable for the player location, the lowercase "location" can be changed, but is used in place of player.getLocation();
Code:java
` Location location = player.getLocation(); `

This is setting the players location, with some math added onto the end. There's also a variable "radius", which is undefined by the code given.
Code:java
` location.setX((location.getX() - radius) + (radius * 2 + 1) * Math.random());location.setY(location.getY() + (4 + (3 * Math.random())));location.setZ((location.getZ() - radius) + (radius * 2 + 2) * Math.random()); `

This is another variable "vector", used in place of location.getDirection();
Code:java
` Vector vector = location.getDirection(); `

This is basically just spawning an arrow, at the players location in the direction they're looking. I've never used this before, but the (float) is a cast to the number. I'm guessing the first number is the location on the players body and the second number is the velocity.
Code:java
` player.getWorld().spawnArrow(location, vector, (float) 1.0, (float) 12); `

Hope this helped!

#3
4. Offline

#4
5. Offline

### MrSparkzz

Type [syntax = java]//code [/syntax]

without the spaces

#5
6. Offline

### McStormify

Thanks for the replies guys! But I already knew basic Java, the part that's confusing me is this. My maths is awful haha!

Code:java
`location.setX((location.getX() - radius) + (radius * 2 + 1) * Math.random());location.setY(location.getY() + (4 + (3 * Math.random())));location.setZ((location.getZ() - radius) + (radius * 2 + 2) * Math.random());`

#6
7. Offline

### Forseth11

McStormify
I explained the math in my post.
Random = (0.0001 - 0.9999)
Then the rest is order of operations I think. (if it is not order of operations then it is () then left to right.)

Oh and next time please quote me or tag me.

Thank you MrSparkzz

#7
McStormify likes this.