Pick random online player

Discussion in 'Spigot Plugin Development' started by MrJonton01, May 25, 2016.

Thread Status:
Not open for further replies.
  1. Hey!
    I have one Question: How can I pick a random player, that doesn't have the permission "premium.kick" and kick the player then? I know how I can kick a player, but I don't know how to pick a random player...

    It'll be cool if you can help me...

    How my plugin should work:
    A VIP/player with the permission "premium.kick" connects to the server. If the server is full, then the VIP should can connect and a random default ranked player should get kicked.
     
  2. You could have a for loop, loop 5 times (so you don't get stuck in an infinite loop), and in the loop select a random player via Collection#get( int ) and using Random#nextInt( Collection#size() ), if the player is matched do your desired thing and break from the loop.

    I needn't say you'll get the collection via Bukkit#getOnlinePlayers() and create the instance of Random yourself.
     
  3. <Inserts cool Java 8 one-liner using streams here>
     
  4. MiniDigger

    Supporter

    <random>.ints(5, 0, <num online player>).iterate(0, i -> i + 1).forEach(<kickplayer #i>);
    EDIT: f**k we need to filter on the perm...
     
  5. Code (Java):
    List<Player> list = Bukkit.getOnlinePlayers().stream().filter(p -> !p.hasPermission("premium.kick")).collect(Collectors.toList());
    if (!list.isEmpty()) {
      Collections.shuffle(list);
      list.get(0).kickPlayer("");
    }
     
  6. MiniDigger

    Supporter

    thats not very efficient ^^
    lemme try it. two lines, no shuffle ^^
    Code (Java):

    List<Player> list = Bukkit.getOnlinePlayers().stream().filter(p -> !p.hasPermission("premium.kick")).collect(Collectors.toList());
    ThreadLocalRandom.current().ints(5, 0, list.size()).iterate(0, i -> i + 1).forEach(list.get(i).kickPlayer(""));
     
     
    #6 MiniDigger, May 25, 2016
    Last edited: May 25, 2016
    • Agree Agree x 1
  7. Code (Text):
    int listSize = yourList.getSize() - 1;
    int random = new Random().nextInt(listSize);
    Player randomPlayer = yourList.get(random);
    Obviously you will need to convert this into a method and use a while statement to keep finding new players until it finds a player without the permission

    You could also populate a list of all the players online without that permission, then pull a random object from that list if the list size is above 1 - this would be your best option.
     
  8. MiniDigger

    Supporter

    this is what the code I posted does...
     
  9. Sooooo many ansers :O I'll try some, thank you all :)
     
  10. I was playing League while writing that, if you think that I was doing it intentionally to steal a few internet points from you that is not the case. I'm trying to help the guy get his problem solved, and more information never hurt anyone. If you're here to complain when someone writes a similar answer to yours, then this isn't the place for you.
     
  11. Spoonfeeding... That'll get op where he wants to be I'm sure, but he'll be back here again later with a not so different question. (OH LOOK, only 3 posts down... vvvvv)

    [​IMG]
     
    #11 _Cory_, May 25, 2016
    Last edited: May 25, 2016
    • Agree Agree x 1
    • Funny Funny x 1
    • Winner Winner x 1
  12. MiniDigger

    Supporter

    wow. it was not an attack ^^
    Stay calm dude. Just must be new here.
     
  13. MiniDigger

    Supporter

    sry, but I got challanged to use my crazy java 8 skillz....
    I will personal voluntier to help OP on his next question to compensate for that mistake ^^
     
    • Friendly Friendly x 1
    • Optimistic Optimistic x 1
  14. Eclipse underlines following red:
    In this line: new Random().ints(5, 0, list.size()).iterate(0, i -> i + 1).forEach(list.get(i).kickPlayer("")); it underlines me "iterate" (http://prntscr.com/b8937d) and the one "i" (http://prntscr.com/b893kf)

    It would be cool if you can help me ^^
     
  15. In my opinion, spoonfeeding is not a bad thing, as long as you explain what you're doing and why you're doing it.
     
  16. I see not a thing explaining the above code snippets. And giving op some code that they can copy and paste straight in without any motivation to read and piece together different snippets will get op nowhere in their knowledge.
     
  17. Yeah, the only issue with IntelliJ is that some of the functions it uses are only available in IntelliJ and don't work in some IDEs.

    Please refer to my answer if you're using Netbeans or Eclipse.

    Guess my answer was better after all ^.^
     
  18. MiniDigger

    Supporter

    you need java 8... (can't look at your screenshots, work proxy blocks them)
     
  19. I have Java 8... I can post the messages, wait.

    iterate - This static method of interface IntStream can only be accessed as IntStream.iterate (1 QuickFix: Change access to static using 'IntStream' (declaring type)
    i (in the (list.get(i).kickPlayer(""))) - i cannot be resolved to a variable (And then the four normal QuickFixes: Create local variable, Create field, Create parameter, Create constant...)
     
  20. It might not be Java 8, some functions you can use in IntelliJ but not Eclipse.

    Code (Text):
        public Player getRandomPlayer(){
            String ignorePermission = "test.permission.ignore";
            // get all the online players
            Collection<? extends Player> players = Bukkit.getOnlinePlayers();

            // make a new list, add all of the players who don't have your exempt permission to it
            List<Player> newList = new ArrayList<>();
            for (Player player : players){
                if (!player.hasPermission(ignorePermission)){
                    newList.add(player);
                }
            }

            // check if the new list actually has contents, there could be no players online or all the online players could have the exempt permission
            if (newList.size() > 0){

                // since we're getting from the list, you need to subtract 1 from your list size, because array contents start counting at 0
                int size = newList.size() - 1;

                // get a random number
                int random = new Random().nextInt(size);

                // take an object from the list and return it
                return newList.get(random);
            }

            // if the new list has a size of 0, it will return null
            return null;
        }
    Now when you call this method:

    Code (Text):
    public void a(){
            Player player = getRandomPlayer();
           
            // make sure to check for null as we set it to return null if the list was 0
            if (player != null){
                player.kickPlayer("lol get r3kt m8 lmaooooo");
            }
        }
     
Thread Status:
Not open for further replies.