Could this be more efficient?

Discussion in 'Plugin Development' started by thepaperboy99, Feb 8, 2014.

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

    thepaperboy99

    Hello Bukkit, today I wanted to make a method that would split people into 3 teams. For example, I wanted to have 1 person be on the 2 teams (Murderer, Special) , then the rest be on the other team (Bystander). Basically the same teams that are in Garry's mod "Murder". Is this code good enough because I'm worried that it might fail or something. Here is my code:

    Code:java
    1. boolean allowed = false;
    2.  
    3. while(!allowed){
    4. System.out.println("Test");
    5. String s = getPlayersInMap().get(new Random().nextInt(getPlayersInMap().size()));
    6. if(getPlayersInMap().size() <= 1){
    7. stop(); // Have to stop the game or the loop will go on forever, and will eventually crash the server.
    8. return;
    9. }
    10.  
    11. if(s == this.murderer){
    12. continue;
    13. }else{
    14. allowed = true;
    15. }
    16.  
    17. if(allowed){
    18. this.special = s;
    19. //Player sp = Bukkit.getPlayer(s);
    20. Role.getInstance().setRole(s, PlayerRole.Special);
    21. System.out.println("Special: " + this.special);
    22. System.out.println("Reached 1");
    23.  
    24. }
     
  2. Offline

    tamajpm

    Test it and you know.
     
  3. Offline

    thepaperboy99

    It works fine, but I was just wondering if I could do it better.
     
  4. Offline

    Maurdekye

    thepaperboy99 I mean, you could remove the allowed boolean and put all the code in the check from line 18-23 into the else clause.
    Edit: And make it a while(true) loop. It seems dangerous, but it really isn't much worse than what you're doing now.
     
Thread Status:
Not open for further replies.

Share This Page