Teleporting With Random Integers, Setting Locations

Discussion in 'Plugin Development' started by BajanAmerican, Aug 28, 2013.

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

    BajanAmerican

    Hi, I have seemed to have stumbled on a problem. I have a random map rotation on my server by generating a random integer.
    Code:java
    1. public static int getMapID(){
    2. int rand = new Random().nextInt(2) + 1;
    3. int index = 1;
    4. if (rand == index){
    5. return 1;
    6. } else {
    7. return 2;
    8. }
    9. }


    After that, I set the random numbers as a new integer so I have the number that it returned.

    Code:java
    1. public static int getMapID1(){
    2. if(getMapID() == 1){
    3. return 1;
    4. }
    5. return 1;
    6. }
    7.  
    8. public static int getMapID2(){
    9. if(getMapID() == 2){
    10. return 2;
    11. }
    12. return 2;
    13. }
    14.  


    So, on the respawn when they die, I check to see which number the getMapID() method has generated and set the respawn location to the appropriate locations on the said map.

    Code:java
    1. if(Game.blue.contains(player.getName())){
    2. if(Game.getMapID1() == 1){
    3. event.setRespawnLocation(new Location(Bukkit.getWorld("world"), -2, 69, 386));
    4. } else if(Game.getMapID2() == 2) {
    5. event.setRespawnLocation(new Location(Bukkit.getWorld("world"), -26, 57, -456));
    6. }
    7. }
    8. if(Game.red.contains(player.getName())){
    9. if(Game.getMapID1() == 1){
    10. event.setRespawnLocation(new Location(Bukkit.getWorld("world"), -2, 69, 254));
    11. } else if(Game.getMapID2() == 2) {
    12. event.setRespawnLocation(new Location(Bukkit.getWorld("world"), -160, 57, -456));
    13. }
    14. }


    Now, the problem is that if the map #2 is picked, the players respawn in the map #1, but if map #1 is selected, then they spawn in map #1. I do not know what is happening or what is causing it to do this. Any help is appreciated, thanks!

    chasechocolate I know you are online :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  2. Offline

    TomFromCollege

    BajanAmerican
    You're going about Object Oriented Programming completely wrong!
    Why don't you make a Map Object?
    Define a new class:
    Code:java
    1. public class Map {
    2.  
    3. public Map(Location respawnLocation) {
    4. this.respawnLocation = respawnLocation;
    5. }
    6.  
    7. private Location respawnLocation;
    8.  
    9. private Location getRespawnLocation() {
    10. return respawnLocation;
    11. }
    12. }

    Now in your main code you could make an Array of Maps:
    Code:java
    1. Map[] mapList = {new Map(#respawnLocation#),
    2. new Map(#respawnLocation#),
    3. new Map(#respawnLocation#)};
    4.  


    And then in your code you'd get a random integer and set the current map to the one chose above

    Code:java
    1. public static int getRandomNumberInRange(int Max, int Min) {
    2. return Min + (int)(Math.random() * ((Max - Min) + 1));
    3. }
    4. // [url]http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java[/url]
    5.  

    Code:
    Map currentMap;
     
    public void selectMap() {
    currentMap = mapList[getRandomNumberInRange(0,mapList.length);
    }
     
    public void respawnPlayer(Player player) {
    player.teleport(currentMap.getRespawnLocation());
    }
    Obviously, you would add WAY more information to each map, even make the Map get the data from the config when you give it a name.

    Why do it this way?
    Think about it,this code you could use ANYWHERE, You could make any plugin and use the Map class. (Depending on what else you implement)

    The reason I said all of this is mainly because your method is so incredibly hard to understand, You set one method, then you set another method to set the respawn location, dude I just lost myself.

    Simplicity is the answer! :)
     
  3. Offline

    BajanAmerican

    TomFromCollege
    This is great, but I do have to ask, what is wrong with my code? I do have another class in which I use different cases that the random number correlates with for locations. I would personally rather keep everything number-orientated if that's possible. I'd rather stay away from Objects as much as possible because my programming skills are not as good as yours or others. Thanks.
     
  4. Offline

    TomFromCollege

    BajanAmerican
    I completely understand what you're saying but experimentation leads to innovation and that'll deffinitely help you in the long run...
    I don't understand all of your methods... Like really, I have no idea why you're sending one method, to another, which in turn is returning the exact same number...

    Take the getRandomNumberInRange method from above

    Things to note:
    • You need to change the locations
    • You need to call setMap(); when you start the game. (If you don't it will error)
    Code:java
    1.  
    2. public Location respawnLocationBlue;
    3. public Location respawnLocationRed;
    4.  
    5. public void setMap() {
    6. switch(getRandomNumberInRange(1,'MAXAMOUNTOFMAPS') {
    7. case 1:
    8. respawnLocationBlue = new Location(Bukkit.getWorld("world"), -2, 69, 386);
    9. respawnLocationRed = new Location(Bukkit.getWorld("world"), -2, 69, 386);
    10. break;
    11. case 2:
    12. respawnLocationBlue = new Location(Bukkit.getWorld("world"), -2, 69, 386);
    13. respawnLocationRed = new Location(Bukkit.getWorld("world"), -2, 69, 386);
    14. break;
    15. }
    16. }
    17.  
    18. public Location getBlueRespawnLocation() {
    19. return respawnLocationBlue;
    20. }
    21.  
    22. public Location getRedRespawnLocation() {
    23. return respawnLocationRed;
    24. }


    Now; This is your code transformed:
    Code:java
    1. if(Game.blue.contains(player.getName())){
    2. event.setRespawnLocation(getBlueRespawnLocation());
    3. } else if(Game.red.contains(player.getName())){
    4. event.setRespawnLocation(getRedRespawnLocation());
    5. }
     
  5. Offline

    BajanAmerican

    TomFromCollege
    Yes! That's what I am looking for! Thank you! Hopefully this will be a definite debug to my issue.. Thanks once again!
     
  6. Offline

    BajanAmerican

    TomFromCollege
    That method that you gave me that selects and random number from a range does not work. If I put the max and min like this:
    Code:
    Locations.teleportPlayers(plugin, getRandomNumberInRange(1, 2));
    It will return the second map every time. But if I do this:

    Code:
    Locations.teleportPlayers(plugin, getRandomNumberInRange(1, 1));
    It returns the first map. I don't know if I am getting this wrong or the method doesn't work. You said the second number is for the number of maps, but It doesn't appear to be ending up that way. Again, any help is appreciated, thanks!
     
  7. Offline

    TomFromCollege

    I got the Max and Min mixed up :p My bad!
    BajanAmerican
    Code:
    public static int getRandomNumberInRange(int Min, int Max) {
    return Min + (int)(Math.random() * ((Max - Min) + 1));
    }
     
Thread Status:
Not open for further replies.

Share This Page