Make a command take to you to a random but pre-determined spawn point.

Discussion in 'Plugin Development' started by ForgedTrinity, Aug 27, 2012.

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

    ForgedTrinity

    I am trying to add something that on a command, it will teleport the player to a point that I specified. But I also want it to be a list it can choose from, so there are multiple points that the can spawn when using the command. I have no idea how to implement the randomness of it. Any help?
     
  2. Offline

    MucTweezer

    Someone else might have a better solution (especially if you want to weight the possible outcomes), but for flat distributions, I tend to just use Math.random().
     
  3. Offline

    Giant

    Well, you could do something like this.
    Code:
    List<Location> locations = new List<Location>();
    Location to;
    for(int i = 0; i < locations.length; i++) {
        Random rand = new Random();
        int x = rand.nextInt(10) > 5 ? 1 : 0;
        if(x == 1) {
            to = locations.get(i);
            break;
        }else if(null == to && i == (locations.length - 1)) {
            to = locations.get(i);
        }
    }
     
    player.teleport(to);
    
    Edit:
    Just realized, a shorter version of this is possible as well!

    Code:
    List<Location> locations = new List<Location>();
    Random rand = new Random();
    int x = rand.nextInt(locations.length);
     
    player.teleport(locations.get(x));
    
     
  4. Offline

    ForgedTrinity

    I realize that is a way to get a random location, but i want it to be a preset location, that picks from the list.

    So say i have (1, 80, 2) and (5, 80, 6) and on /rspawn it chooses one of those, and teleports you there
     
  5. Offline

    MucTweezer

    Your list would be populated with your preset locations.

    Giant's code above doesn't generate a random location, it randomly choses a location from a list of locations.
     
  6. Offline

    ForgedTrinity

    Thanks for the help. I have never used a list in java before so I gotta go play around to find out how it works :D
     
  7. Offline

    Giant

    ForgedTrinity you had no idea how to implement the randomness, which is what I showed you. You also said you wanted it to randomly pick from a list, hence why the List<Locations> is there. The code I gave you was pseudocode, or example code if you prefer.

    It was sort of meant for you to adjust it to your own code. :)
     
  8. Offline

    ForgedTrinity

    I know and i thank you for the help :) I just am kinda new to java so its a learning experience. One more question though. It says list can not be resolved as a type, but when i try to import things it gets errors.
     
  9. Offline

    Giant

    ForgedTrinity

    You'll need to import java.util.List for it to work, and it also uses java.util.Random. :)
    I'll also add a basic explanation of how Lists work as you stated in a previous post you don't really know.

    The way a List works is by first assigning it an object type that it should store. This is done between the <>. So the example I showed you, will only accept Location objects. After that you have set the type, you would need to initialize it. There are few types of Lists in existence, though you will mostly see ArrayList being used. And that is also why the error remains. In my example I did "new List<Location>();" However, as "List" can't be instantiated directly, that would error out. I should have instead done "new ArrayList<Location>();" my bad on that.

    After that you have pretty much "made" the List, you would then need to add values to it. To do this, the List object, has a nice little method called "add". So if we were to want to add a Location to the list in our example, we would do "locations.add(WhichEverVarHoldsTheLocation);". This would then be added onto our list, and assigned a position number in the List.

    Now of course, storing in a List is only half the job! We also want to get from that List, don't we?!
    To do so, we would have to get the position number for the element we want to get from the List. To do so can be done in a few ways, for example the for loop I used in the longer example, and the random number generator in the shorter one. But as soon as we have the position, we can then do "Location loc = locations.get(WhichEverVarHoldsThePosition);", and we would have our Location back! :)

    And that is pretty much a basic explanation of the List object! :)

    Edit:
    Forgot a little sentence there xD
     
  10. Offline

    ForgedTrinity

    Giant

    I see how to do this and what i need to do, but i keep getting errors when doing the "locations.add(Variable);"
     
  11. Offline

    Giant

    What would the errors be?
    It's a bit hard for me to help you without knowing the errors. Seeing how I can't sort of "know" what errors you have... :)
     
  12. Offline

    ForgedTrinity

    Wow, now it is not giving me an error. I dont know what i changed. But for the variable how do i set it to a specific cordinate? i can only figure out how to make it a players location.
     
  13. Offline

    Giant

    well, you would create a new Location object. So you would do:
    Code:
    World world = Bukkit.getServer().getWorld("worldName"); // The world where the location is in.
    Double x = 0.0; // X Coordinate
    Doubly y = 0.0; // Y Coordinate
    Doubly z = 0.0; // Z Coordinate
    locations.add(new Location(world, x, y, z));
    
     
  14. Offline

    ForgedTrinity

    Thank you so much! The only other error I have is on this line: "int a = rand.nextInt(locations.length);"
    It is saying that length cannot be resolved or is not a field.
     
  15. Offline

    Firefly

    .size() ;)
     
  16. Offline

    Giant

    Whoops, I keep getting confused with size() and length on those damned things >__>...
     
  17. Offline

    ForgedTrinity

    THANK YOU! It should be all working now :D Ill go test it and let you know
     
  18. Offline

    Siggy999

    Alternatively, couldn't you make a list of locations and use list.shuffle to randomize them, then pick the first?

    (I haven't tried it in java, but that's how I shuffled a deck of cards in another language that used similar list functions)
     
  19. Offline

    Giant

    Siggy999 I don't believe Java has a shuffle for it :(

    Edit:
    Turns out it DOES have a shuffle, though it's a bit hidden hehe...
    Collections.shuffle(List list)
     
  20. Offline

    Siggy999

    Yeah I looked it up before I posted - I just hadn't used it in Java - I've used something similar in another language based loosely on Java, but hadn't had need to try it yet :)
     
  21. Offline

    Giant

    Hehe, I decided to look it up just after I posted xD
     
Thread Status:
Not open for further replies.

Share This Page