Explain this code?

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

Thread Status:
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
    1. public void shoot(Player player, int arrowCount) {
    2. for (int i = 0; i < arrowCount; i++) {
    3. Location location = player.getLocation();
    4.  
    5. location.setX((location.getX() - radius) + (radius * 2 + 1) * Math.random());
    6. location.setY(location.getY() + (4 + (3 * Math.random())));
    7. location.setZ((location.getZ() - radius) + (radius * 2 + 2) * Math.random());
    8.  
    9. Vector vector = location.getDirection();
    10.  
    11. player.getWorld().spawnArrow(location, vector, (float) 1.0, (float) 12);
    12. }
    13. }


    Thanks so much for your help! Here's a cookie to show my appreciation! :D

    Show Spoiler
    [​IMG]
     
  2. Offline

    Forseth11

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

    PHP:
    public void shoot(Player playerint arrowCount) { //This is a method
        
    for (int i 0arrowCounti++) {                                //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 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() + (+ (Math.random())));                            //This takes (3 * (0.0001 - 0.9999) + 4 + y coordinate.
            
    location.setZ((location.getZ() - radius) + (radius 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(locationvector, (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.

    Oh, the cookie made me HUNGRY!
     
    devilquak likes this.
  3. Offline

    MrSparkzz

    This is the "shoot" method
    Code:java
    1.  
    2. public void shoot(Player player, int arrowCount) {
    3.  

    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
    1.  
    2. for (int i = 0; i < arrowCount; i++) {
    3.  

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

    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
    1.  
    2. location.setX((location.getX() - radius) + (radius * 2 + 1) * Math.random());
    3. location.setY(location.getY() + (4 + (3 * Math.random())));
    4. location.setZ((location.getZ() - radius) + (radius * 2 + 2) * Math.random());
    5.  

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

    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
    1.  
    2. player.getWorld().spawnArrow(location, vector, (float) 1.0, (float) 12);
    3.  

    Hope this helped!
     
  4. Offline

    Forseth11

  5. Offline

    MrSparkzz

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

    without the spaces
     
  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
    1. location.setX((location.getX() - radius) + (radius * 2 + 1) * Math.random());
    2. location.setY(location.getY() + (4 + (3 * Math.random())));
    3. location.setZ((location.getZ() - radius) + (radius * 2 + 2) * Math.random());
     
  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
     
    McStormify likes this.
Thread Status:
Not open for further replies.

Share This Page