Solved Random Location Spawning help!

Discussion in 'Plugin Development' started by mrgreen33gamer, Dec 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    mrgreen33gamer

    Hi there,

    I am having an issue. I get an error in my console, saying that: "n needs to be a positive". There is no variable named N in my class. It turns out, I cannot get a random location with negatives. Any idea how I can fix this? Another thing too is that I am trying to spawn a player inside of a map and it puts them in a random location within that map. Here is the code:

    Code:java
    1.  
    2. int xmin = -135;
    3. int xmax = -179;
    4. int zmin = -13;
    5. int zmax = -87;
    6.  
    7. Random random = new Random();
    8. int sX = random.nextInt(xmax - xmin + 1) + xmin;
    9. int sZ = random.nextInt(zmax - zmin + 1) + zmin;
    10. double FinalX = sX + .5D;
    11. double FinalZ = sZ + .5D;
    12. int FinalY = Bukkit.getWorld("world").getHighestBlockYAt(sX, sZ);
    13.  
    14. Location loc = new Location(Bukkit.getWorld("world"), FinalX, FinalY, FinalZ);
    15. player.teleport(loc);
    16.  


    I get the error right here:
    Code:
    int sX = random.nextInt(xmax - xmin + 1) + xmin;
    Any idea how I can fix this? Either I can get a location between to points, instance: Location1 Location2 and get the location within those 2 points, or I can figure out a way to get this to work.

    Thanks in advanced!

    ~mrgreen33gamer
     
  2. Offline

    stenlankreijer

    First of all, -179 is less than -135, same goes for the zmin and zmax, so you should swap these to make my code work.
    Code:java
    1.  
    2.  
    3. int xmin = -179;
    4. int xmax = -135;
    5.  
    6.  
    7. int zmin = -87;
    8. int zmax = -13;
    9.  
    10. Random random = new Random();
    11.  
    12.  
    13. int sX = random.nextInt(xmin - xmax);
    14. int sZ = random.nextInt(zmin - zmax) ;
    15. double FinalX = xmin + sX;
    16. double FinalZ = zmin + sZ;
    17. int FinalY = Bukkit.getWorld("world").getHighestBlockYAt(sX, sZ);
    18.  
    19.  
    20. Location loc = new Location(Bukkit.getWorld("world"), FinalX, FinalY, FinalZ);
    21. player.teleport(loc);
    22.  


    That should do it. Just make sure the "min" numbers are smaller than the "max" numbers.
     
    Last edited: Dec 20, 2014
  3. Offline

    mrgreen33gamer

    @stenlankreijer Thanks for the response! The code does work! No errors. Sadly, it doesn't teleport me to the correct location. Any idea?
     
  4. Offline

    mythbusterma

  5. Offline

    Rocoty

    @mrgreen33gamer You said the code is working...but then proceed to say it doesn't do what it is supposed to do. Define "working" please, because I'm confused.
     
  6. Offline

    stenlankreijer

  7. Offline

    mrgreen33gamer

    @Rocoty and @stenlankreijer I had to do a little research on the web! I solved it. Thanks to everyone here, you all led me to a conclusion! Thanks in advanced!
     
Thread Status:
Not open for further replies.

Share This Page