Solved Loop through again if the name is null

Discussion in 'Plugin Development' started by PimpDuck, May 11, 2014.

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

    PimpDuck

    Okay, so I'm making a lottery plugin and I need to check if one of the winners it picks isn't online I need it to repick, but I'm not sure how I would do this.. Any help?

    Here's my code:
    Code:java
    1. }else if(seconds == 0){
    2.  
    3.  
    4. int moneypot = getConfig().getInt("DCLottery.MoneyPot");
    5. double winnersmoney = moneypot * .5;
    6.  
    7.  
    8. List<String> list = Main.ticketbuyers.getStringList("Buyers");
    9. Random r = new Random();
    10.  
    11. // Getting the random player name from our list
    12. String p1 = list.get(r.nextInt(list.size()));
    13. String p2 = list.get(r.nextInt(list.size()));
    14. String p3 = list.get(r.nextInt(list.size()));
    15. String p4 = list.get(r.nextInt(list.size()));
    16. String p5 = list.get(r.nextInt(list.size()));
    17.  
    18.  
    19. Player winner1 = Bukkit.getPlayer(p1);
    20. Player winner2 = Bukkit.getPlayer(p2);
    21. Player winner3 = Bukkit.getPlayer(p3);
    22. Player winner4 = Bukkit.getPlayer(p4);
    23. Player winner5 = Bukkit.getPlayer(p5);
    24.  
    25. EconomyResponse r1 = econ.depositPlayer(winner1.getName(), winnersmoney);
    26. if(r1.transactionSuccess()){
    27. winner1.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    28. }
    29.  
    30. EconomyResponse r2 = econ.depositPlayer(winner2.getName(), winnersmoney);
    31. if(r2.transactionSuccess()){
    32. winner2.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    33. }
    34.  
    35. EconomyResponse r3 = econ.depositPlayer(winner3.getName(), winnersmoney);
    36. if(r3.transactionSuccess()){
    37. winner3.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    38. }
    39.  
    40. EconomyResponse r4 = econ.depositPlayer(winner4.getName(), winnersmoney);
    41. if(r4.transactionSuccess()){
    42. winner4.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    43. }
    44.  
    45. EconomyResponse r5 = econ.depositPlayer(winner5.getName(), winnersmoney);
    46. if(r5.transactionSuccess()){
    47. winner5.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    48. }
    49.  
    50. Bukkit.broadcastMessage(prefix + ChatColor.BLUE + "Congratulations, " + ChatColor.RED + p1.toString() + ChatColor.BLUE + ", " + ChatColor.RED + p2.toString() + ChatColor.BLUE + ", " + ChatColor.RED + p3.toString() + ChatColor.BLUE + ", " + ChatColor.RED + p4.toString() + ChatColor.BLUE + ", and " + ChatColor.RED + p5.toString()
    51. + ChatColor.BLUE + "! You have won the lottery containing $" + ChatColor.RED + moneypot + ChatColor.BLUE + "!");
    52. seconds = 241;
    53. seconds--;
    54. }


    Thanks :p
     
  2. Offline

    Gater12

    PimpDuck
    I guess maybe a while loop will do.
     
  3. Offline

    IvanTheForth

    PimpDuck
    You would use a while loop and have it run as long as the player selected is null.
     
  4. Offline

    PimpDuck

    Gater12 @IvanTheFourth I'm not that experienced with while loops. Would it look like

    Code:java
    1. while(winner1 == null){
    2. String p1 = list.get(r.nextInt(list.size()));
    3. }


    or something? xD If I'm wrong, could I get an example with my code?
    Thanks, again!

    IvanTheForth *

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

    IvanTheForth

    PimpDuck
    You would want to use the same variable. So, something like:
    Code:
    Player winner = list.get(r.nextInt(list.size()));
    while(winner == null){
      winner = Bukkit.getPlayer(list.get(r.nextInt(list.size())));
    }
     
  6. Offline

    PimpDuck

    Last edited by a moderator: Jun 8, 2016
  7. Offline

    IvanTheForth

    PimpDuck
    Oh sorry, my bad. Do
    Player winner = Bukkit.getPlayer(list.get(r.nextInt(list.size())));
     
  8. Offline

    PimpDuck

    That just adds a duplicate error on winner..

    IvanTheForth

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

    Gater12

    PimpDuck
    Change type winner to Player. Assign winner Bukkit.getPlayer(String a) and do while loop.
     
  10. Offline

    PimpDuck

    Got it like this
    Code:java
    1. }else if(seconds == 0){
    2.  
    3.  
    4. int moneypot = getConfig().getInt("DCLottery.MoneyPot");
    5. double winnersmoney = moneypot * .5;
    6.  
    7.  
    8. List<String> list = Main.ticketbuyers.getStringList("Buyers");
    9. Random r = new Random();
    10.  
    11. String player1 = list.get(r.nextInt(list.size()));
    12. while(player1 == null){
    13. Player winner1 = Bukkit.getPlayer(player1);
    14.  
    15. if(winner1 != null){
    16. EconomyResponse r1 = econ.depositPlayer(winner1.getName(), winnersmoney);
    17. if(r1.transactionSuccess()){
    18. winner1.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + (int) winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    19. }
    20. }
    21.  
    22. }
    23.  
    24. String player2 = list.get(r.nextInt(list.size()));
    25. while(player2 == null){
    26. Player winner2 = Bukkit.getPlayer(player2);
    27.  
    28. if(winner2 != null){
    29. EconomyResponse r2 = econ.depositPlayer(winner2.getName(), winnersmoney);
    30. if(r2.transactionSuccess()){
    31. winner2.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + (int) winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    32. }
    33. }
    34.  
    35. }
    36.  
    37. String player3 = list.get(r.nextInt(list.size()));
    38. while(player3 == null){
    39. Player winner3 = Bukkit.getPlayer(player3);
    40.  
    41. if(winner3 != null){
    42. EconomyResponse r2 = econ.depositPlayer(winner3.getName(), winnersmoney);
    43. if(r2.transactionSuccess()){
    44. winner3.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + (int) winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    45. }
    46. }
    47.  
    48. }
    49.  
    50. String player4 = list.get(r.nextInt(list.size()));
    51. while(player4 == null){
    52. Player winner4 = Bukkit.getPlayer(player4);
    53.  
    54. if(winner4 != null){
    55. EconomyResponse r2 = econ.depositPlayer(winner4.getName(), winnersmoney);
    56. if(r2.transactionSuccess()){
    57. winner4.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + (int) winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    58. }
    59. }
    60.  
    61. }
    62.  
    63. String player5 = list.get(r.nextInt(list.size()));
    64. while(player5 == null){
    65. Player winner5 = Bukkit.getPlayer(player5);
    66.  
    67. if(winner5 != null){
    68. EconomyResponse r2 = econ.depositPlayer(winner5.getName(), winnersmoney);
    69. if(r2.transactionSuccess()){
    70. winner5.sendMessage(prefix + ChatColor.BLUE + "$" + ChatColor.RED + (int) winnersmoney + ChatColor.BLUE + " has been deposited in to your account for winning the lottery!");
    71. }
    72. }
    73.  
    74. }
    75.  
    76.  
    77.  
    78. Bukkit.broadcastMessage(prefix + ChatColor.BLUE + "Congratulations, " + ChatColor.RED + player1.toString() + ChatColor.BLUE + ", " + ChatColor.RED + player2.toString() + ChatColor.BLUE + ", " + ChatColor.RED + player3.toString() + ChatColor.BLUE + ", " + ChatColor.RED + player4.toString() + ChatColor.BLUE + ", and " + ChatColor.RED + player5.toString()
    79. + ChatColor.BLUE + "! You have won the lottery containing $" + ChatColor.RED + moneypot + ChatColor.BLUE + "!");
    80. getConfig().set("DCLottery.MoneyPot", 0);
    81. seconds = 241;
    82. seconds--;
    83. }
    84.  
    85. seconds--;
    86. }
    87. }
    88. },0,20);
    89. }


    Solved.
     
  11. Offline

    IvanTheForth

    PimpDuck
    This should work:
    Code:
            Player winner = Bukkit.getPlayer(list.get(new Random().nextInt(list.size())));
            while(winner == null){
                winner = Bukkit.getPlayer(list.get(new Random().nextInt(list.size())));
            }
    I forgot that Bukkit switched over to UUID's.

    Oh and your list should not be Strings, but of UUID's

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page