Choosing a player at random?

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

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

    Trill

    How can I select an online player at random? It's part of my plugin I'm making.
     
  2. Online

    timtower Administrator Administrator Moderator

    Trill Moved to plugin development.
    And select them from where?
     
  3. Offline

    Trill

    Let me clarify this. How can I select a online player at random every 10 minutes or so and broadcast their name in a message showing they won?
     
  4. Offline

    adam753

    Code:
    Random random = new Random();
    int playernumber = random.nextInt(Bukkit.getOnlinePlayers.size());
    Player player = Bukkit.getOnlinePlayers().get(randomplayer);
     
  5. Offline

    Trill

    I am a bit of a beginner at coding with bukkit. Can you explain where I would put the code?
     
  6. Offline

    TheCodingCat

    Depends Trill , is this part of a command? Or why would you need to do this?
     
  7. Offline

    Trill

    Also the code seems to have quite a few errors
     
  8. Offline

    indyetoile

    Trill
    Perhaps wherever you want to call it?
     
  9. Offline

    Trill

    This is part of a clock that runs every ten minutes. No command needed and no permission.
     
  10. Offline

    adam753

    Trill
    The errors will probably be either things you need to import, or (possibly) small things I didn't think about because I didn't write this in an IDE. Obviously you should put the code wherever you want to use it.

    You should loop up schedulers and repeating tasks to do the "every ten minutes" thing.
     
  11. Offline

    TheCodingCat

    in your onEnable, schedule a Bukkit Runnable that will last until the server closes. In that runnable put the code
     
  12. Offline

    Trill

    I set this for the clock:
    win.runTaskTimer(this, 0, 600);
    And I placed this in the Win class:
    @Override
    public void run()
    {
    Random random = new Random();
    int playernumber = random.nextInt(Bukkit.getOnlinePlayers.size());
    Player player = Bukkit.getOnlinePlayers().get(randomplayer);

    @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true)
    public void onChat(AsyncPlayerChatEvent e){
    e.setMessage(e.getMessage().replaceAll("(?i)butter","gold"));}
    }

    TheCodingCat could you show me how to code that? I started Bukkit just last week.

    This is in the win class:
    Code:java
    1. @Override
    2. public void run()
    3. {
    4. Random random = new Random();
    5. int playernumber = random.nextInt(Bukkit.getOnlinePlayers.size());
    6. Player player = Bukkit.getOnlinePlayers().get(randomplayer);
    7.  
    8. @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true)
    9. public void onChat(AsyncPlayerChatEvent e){
    10. e.setMessage(e.getMessage().replaceAll("(?i)butter","gold"));}
    11. }


    Is the way I placed the code correct?

    Any help on how to schedule a BukkitRunnable till the sever closes?

    Any help on how to fix this?

    These are the errors I am getting: http://gyazo.com/5072ccefe113cdd5457d449ffdba1683

    I was wondering how to make the broadcasted message configurable in the config. That way there wouldn't be a super long line of code with ChatColor's.
     
  13. Offline

    uksspy

    The first error is because the .size() method has no parentheses in your code.
    Where are you creating the randomplayer variable? Is it not an int, or does it not exist at all?
     
  14. Offline

    Trill

    The randomplayer variable doesn't exist yet. Not sure where to put it.

    indyetoile
    Please only use this thread as help towards helping me make a plugin. No side comments please.

    Can someone help me? I want to set up a config file where I can set the broadcasted message and the items sent to the player if they win.

    TheCodingCat
    Not so sure how to get the code to work.
    Can anyone explain to me what this code does?

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

    TheCodingCat

    So the one i suggested Trill is that you use a BukkitRunnable If you dont know what that is, there are plenty of tutroials on the bukkit forums within one google search. Anyway so you create a bukkit runnable that repeats itself x number of ticks and in that task, you put your random code. (Btw in "x" if you want 10 minutes its about 12000)
     
  16. Offline

    Trill

    I already created the new class and made a clock for it to run. But I am not sure how to edit the code I was given because I am receiving errors.
     
  17. Offline

    TheCodingCat

    what kind of errors post console and code
     
  18. Offline

    Trill

    Code:java
    1. @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true)
    2. public void onChat(AsyncPlayerChatEvent e){
    3. e.setMessage(e.getMessage().replaceAll("(?i)butter","gold"));}

    What does this do?

    Code Errors: http://gyazo.com/41a50a45a5bb29688304211348726ffe
    Plugin doesn't seem to startup in the console
    The plugin has errors in it which is probably why it won't start up.

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

    uksspy

    Can you write down what the errors say not just where they appear.
     
  20. Offline

    Trill

    getOnlinePlayers cannot be resolved or is not a field
    Cannot invoke get(int) on the array type Player[]
    void is an invalid type for the variable onChat
    Syntax error on token ")", ; expected
    ^Those are the errors from the picture posted above^
     
  21. Offline

    TGRHavoc

    Trill
    It's because you've blindly copied and pasted wrong code into your plugin.

    You're trying to create a method inside another method.
    Edit: Just realized you copied his signature :p
     
  22. Offline

    Trill

    So how would I fix that?
     
  23. Offline

    Skionz

  24. Offline

    Trill

    Skionz
    Which one do I need to know?
     
  25. Offline

    ChipDev

    Omg. Biggest laugh ever.
    You foolishly copy pasted adam753 s code, and possibly his signature ;)
    And kids, that is why you cannot spoon feed.
    *I bet you could give him code that actually op's yourself, and he would be fooled ;)
     
    AdamQpzm and adam753 like this.
  26. Offline

    Trill

    ChipDev
    Now I'm laughing too! :p

    My guess at the code:
    Code:java
    1. public class Win extends BukkitRunnable {
    2. private static final int Win1 = 0;
    3. private static final int Win2 = 0;
    4. LuckyWinner plugin;
    5.  
    6. public Win(final LuckyWinner plugin) {
    7. this.plugin = plugin;
    8. }
    9.  
    10. @Override
    11. public void run()
    12. {
    13. Random random = new Random();
    14. int playernumber = random.nextInt(Bukkit.getOnlinePlayers.size());
    15. Player player = Bukkit.getOnlinePlayers().get(randomPlayer);
    16. Bukkit.broadcastMessage(Win1 + randomPlayer + Win2);
    17. }
    18. }
    19.  

    Config File:
    Code:java
    1. # Default Configuration File for LuckyWinner
    2. Win1: '&7&l<&4&l!&7&l>&r"
    3. Win2: '&7has&f won&7this drawing!'

    If this is completely wrong then please correct :)


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

    Skionz

    Trill Add one to your random integer.
     
  28. Offline

    Trill

    Skionz
    What would that do?
     
  29. Offline

    Skionz

    Trill It would add one to the current integer. r.nextInt(4) would return either 0, 1, 2, or 3 so you have to add one.
     
  30. Offline

    Trill

    Would adding randomPlayer to the broadcasted message display their name or would I need to use something like Bukkit.getPlayerExact()?
     
Thread Status:
Not open for further replies.

Share This Page