Solved Method makes server crash

Discussion in 'Plugin Development' started by MatsExe, Feb 19, 2016.

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

    MatsExe

    Hello,

    I am trying to get arenas which are able to change map. Maps contain info about locations and whether it is available. I made a method which returns a random map, but it makes the server crashes whenever it enables or reloads.
    Code:
    public Arena(int id) {
            this.id = id;
            this.map = ArenaManager.getManager().getRandomMap();
        }
    Code:
    public GameMap getRandomMap() {
            Random r = new Random();
            GameMap map;
          
            do {
                map = getMaps().get(r.nextInt(getMaps().size()));
            }
            while (map.isAvailable());
          
            return map;
        }
    When I do:
    Code:
    public Arena(int id) {
            this.id = id;
            this.map = ArenaManager.getManager().getMap("Mapname");
        }
    it doesn't crash so the getRandomMap is responsible. How do I fix this?
     
  2. Offline

    teej107

  3. Offline

    MatsExe

    @teej107 Wouldn't that remove the ability to check if the map is available? Maps get unavailable when an arena is using it to prevent 2 games being played in the same map.
     
  4. Offline

    george132222222

    Don't use do-while loops when making plugins. They do crash the server, I have no idea why but I've had really bad experiences with them. Try using if(a parameter is true/false && another parameter is true/false){do this}
    Else{do this}
     
  5. Offline

    teej107

    @MatsExe perhaps you should add a check to see if all the maps are available. Because if they are, then that is why your endless loop happens. You could also just iterate through the Collection that you have rather than leaving everything up to chance.
     
  6. Offline

    mythbusterma

    @george132222222

    There's no reason not to use a do-while, while, or for loop. Disregarding a language feature because you don't understand how it works is not good advice.

    The reason he's having issues is because the "map" never becomes not available, hence he has created an infinite loop.
     
  7. Offline

    MatsExe

    Update: using a for loop worked but the first entries in the list will get used more frequently.

    I'd still like to have it getting chosen randomly though, help will be appreciated.
     
  8. Offline

    Zombie_Striker

    @MatsExe
    Code:
    Random r = new Random(); //create a new instance of random
    int value = r.nextInt(Max Size); // get a random number between 0 and the (max amount of object from the collection -1)
    Collection.get(value); // get a random object from the collection.
     
  9. Offline

    MatsExe

Thread Status:
Not open for further replies.

Share This Page