Choosing a player at random?

Discussion in 'Plugin Development' started by Trill, Nov 30, 2014.

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

    Skionz

    Trill Depends. I don't know what 'randomPlayer' is. Is it a Player an Integer, or a String?
     
  2. Offline

    Trill

    randomPlayer is meant to be a Player.
     
  3. Offline

    Skionz

    Trill Then you would have to get the player's name.
     
  4. Offline

    Trill

    So I'd use Bukkit.getPlayerExact()? Not sure how to code that yet. Not so sure how to get it to work. Any help?
     
  5. Offline

    Skionz

    Trill Use the Player#getName() method.
     
  6. Offline

    Trill

    How would I make it take that name and put it in the Broadcasted Message then?
     
  7. Offline

    Skionz

    Trill Use concatenation with the player's name and the message in the Bukkit#broadcastMessage(String) method.
     
  8. Offline

    ChipDev

    All of it.
     
  9. Offline

    Trill

    ChipDev
    Thanks... Really helps.
     
    ChipDev likes this.
  10. Offline

    Monkey_Swag

    Trill you clearly know nothing about Java. Please take some time to learn Java BEFORE Bukkit. Bukkit is an API used in Java (which I'm sure you don't know what that means, but owell).
     
    Skionz likes this.
  11. Offline

    Trill

    I will Monkey_Swag

    ChipDev
    Like this?
    Code:java
    1. Random random = new Random();
    2. int playernumber = random.nextInt(Bukkit.getOnlinePlayers.size());
    3. Player player = Bukkit.getOnlinePlayers().get(randomPlayer);
    4. player.getName();
    5. Bukkit.broadcastMessage("Win1" + player.getName() + "Win2");
    6. random.nextInt(4);

    Where Win1 & Win2 are both text lines editable in the config.yml

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

    ChipDev

    Good.
    •why the last line?
    randomPlayer does not exist, change it to playernumber.
     
  13. Offline

    Trill

    ChipDev
    Still Errors in Eclipse
    Scroll up to understand why the last line was put in.

    Changed to playernumber: Cannot invoke get(int) on the array type Player[]
    Hmm... that would make sense if it worked.

    ChipDev
    These are the errors in the Java Code:
    http://gyazo.com/5c505da3069d3687ee9603fa6406918c
    getOnlinePlayers cannot be resolved or is not a field
    Cannot invoke get(int) on the array type Player[]

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

    ChipDev

    Learn a little more java :3
    Bukkit.getOnlinePlayers --> Bukkit.getOnlinePlayers()
    get is not a Player[] method.
     
  15. Offline

    Trill

    ChipDev
    Ok I coded this is and I hope it works:
    Code:java
    1. public class Win extends BukkitRunnable {
    2. LuckyWinner plugin;
    3.  
    4. public Win(final LuckyWinner plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. @Override
    9. public void run()
    10. {
    11. Random random = new Random();
    12. int playernumber = random.nextInt(Bukkit.getOnlinePlayers.size());
    13. Player player = Bukkit.getOnlinePlayers().get(playernumber);
    14. player.getName();
    15. Bukkit.broadcastMessage("Win1" + player.getName() + "Win2");
    16. random.nextInt(4);
    17. e.getPlayer().getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK, 384));
    18. e.getPlayer().getInventory().addItem(new ItemStack(Material.EMERALD_BLOCK, 384));
    19. }
    20. }
    21.  

    Still a few errors, but I'll fix them on the mean time.
    This might look OP but it is for a prison server.
     
  16. Offline

    ChipDev

    No, you copy pasted.
    Code:java
    1. public class Win extends BukkitRunnable {
    2. LuckyWinner plugin;
    3.  
    4. public Win(final LuckyWinner plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. @Override
    9. public void run()
    10. {
    11. Random random = new Random();
    12. int playernumber = random.nextInt(Bukkit.getOnlinePlayers.size());
    13. Player player = Bukkit.getOnlinePlayers().get(playernumber);
    14. player.getName();
    15. Bukkit.broadcastMessage("Win1" + player.getName() + "Win2");
    16. random.nextInt(4);
    17. e.getPlayer().getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK, 384));
    18. e.getPlayer().getInventory().addItem(new ItemStack(Material.EMERALD_BLOCK, 384));
    19. }
    20. }
    21.  

    This makes it so it has 2 players who win.
    EDIT: Why extend bukkitrunnable?
     
  17. Offline

    Trill

    ChipDev
    I am extending bukkitrunnable because this class is running off my Main class which has a clock on it. And where did that first "quote" come from. I never typed that code. This is confusing.

    Win1 & win2 are just lines of text in the config so that the playername is in the middle. They aren't 2 different people that win. Hope that clarifies it. FYI look at previously said posts before typing things in.
     
  18. Offline

    ChipDev

    Merge those posts please, you used 2 bumps to give me 2 threads. There is an edit button.
    You typed it in:
    [​IMG]
     
  19. Offline

    Trill

    ChipDev
    Sorry about that. Wasn't paying attention...
    Ok so now I need to fix the two errors.
    -------------------------------------------------------------------------
    Code Errors:
    http://gyazo.com/5c505da3069d3687ee9603fa6406918c
    getOnlinePlayers cannot be resolved or is not a field
    Cannot invoke get(int) on the array type Player[]
     
  20. Offline

    teej107

  21. Offline

    Trill

    Thanks for the help on coding the main part of the plugin! I'll fix the syntax errors myself :)
     
  22. Offline

    ChipDev

    Look at my code above: I did not use get(int)..
    I used this:
    Code:java
    1. Player[] player = Bukkit.getOnlinePlayers();
    2. Player win = player[number];

    Oops, I messed up above and put my code in your quote :p
     
  23. Offline

    Trill

    ChipDev
    That's what I thought.
     
  24. Offline

    Dragonphase

    Why is teej107 the only one in this thread with a brain cell?




    Stop spoon feeding him when he does not understand it.

    The first thing to do is learn Java if you have not done so already. Make sure you make yourself accustomed to the principles of Object Oriented Programming. After that, check out the Bukkit Plugin Tutorial. It's a basic tutorial that covers about 90% of plugin-related programming.

    For information regarding the interface, refer to the Bukkit API.
     
Thread Status:
Not open for further replies.

Share This Page