Solved Getting All Online Operators

Discussion in 'Plugin Development' started by CodePlaysMinecraft, Jan 2, 2015.

Thread Status:
Not open for further replies.
  1. So, recently I was trying to figure out how to get all online operators in one of my plugins. (Kind of like this: http://gyazo.com/0a74572bcb8900912d9610886cb616f6 ) The way I wanted it to go is that when the player joins the server, the server will get the number of all online ops. I know that there are plugins that are able to do this, but I want the plugin to be customized.
     
  2. Offline

    adam753

    @CodePlaysMinecraft
    Loop through all online players and check if they're opped using Player#isOp().
     
    VG.Developments likes this.
  3. Offline

    teej107

    Last edited by a moderator: Oct 31, 2016
    Konato_K likes this.
  4. @teej107 Sorry, but I'm still having some trouble figuring it out. Here's my code:
    Code:
    public static Set<OfflinePlayer> getOperators() {
            for(Player onlineOperators : Bukkit.getOnlinePlayers()) {
                if(onlineOperators.isOp()) {
                    onlineOperators.getName();
                }
            }
            return Bukkit.getServer().getOperators();
        }
     
  5. @CodePlaysMinecraft Think about what your code is doing :)

    Currently, you loop over the online players, check if they're an op, get their name[1] and then return the offline operators[2]

    Here are the problems I notice, try and figure them out by yourself before looking though :)

    Show Spoiler

    1 = You don't actually do anything with this information, and are you sure that's the information you were trying to get anyway?
    2 = What's the point of looping over the online players, checking if each is an op, and then just returning all operators (online and offline)
     
    leon3001 likes this.
  6. Offline

    tomudding

    I don't if you want this inside a command or something like that, but I would do something like this:
    Code:
        public void checkOnlineOP(Player player) {
            for(Player players : Bukkit.getServer().getOnlinePlayers()) {
                if(players.isOp()) {
                    player.sendMessage(players.getName() + " is in " + players.getWhereTheyAre());
                }
            }
        }
     
  7. @tomudding I'm trying to make it so when the player joins it will send them a message like:
    ===================
    Welcome back, playerName!
    Online Players: 10
    Online Staff: op1, op2,
    ===================
    But I want it to be easy enough that the line "op1, op2," would be a variable, so I can use it like:
    player.sendMessage(ChatColor.WHITE + "Online Staff: " + onlineOps);

    How would I go about doing this?
     
  8. Offline

    teej107

    Close but no cigar! Here is how to achieve this:


    1. Loop through the Set returned by the method.
    2. Check to see if they are online: http://jd.bukkit.org/rb/apidocs/org/bukkit/OfflinePlayer.html#isOnline()
    You may alternatively keep track yourself by adding the Players to a Collection representing the online operators when they join.
     
  9. Offline

    1Rogue

    Just make a method that returns a collection of online operators. For example in Java 8:

    Code:java
    1. public Collection<? extends Player> getOnlineOperators() {
    2. return Bukkit.getOnlinePlayers().stream().filter(p -> return p.isOp()).collect(Collectors.toList());
    3. }


    From there you can simply list them:

    Code:java
    1. somePlayer.sendMessage("Online Staff: " + StringUtils.join(this.getOnlineOperators().stream().map(p -> return p.getName()).collect(Collectors.toList()), ", "));
     
  10. @1Rogue Hm... I'm getting some errors with your code.
    .stream() gives me the error, "Cannot invoke stream() on the array type Player[]", so I replaced it with .length().
    Then I got errors with p, "p cannot be resolved to a variable."

    If you needed to know, I have Java version 8 update 25.
     
  11. Offline

    1Rogue

    You need to use the newer revisions of bukkit (1.7.10-R0.2), add the online player array into a list, or not use streams then.
     
  12. @1Rogue Hmm.... I'm looking at the Bukkit downloads and all I see is 1.7.10-Ro.1, would it make a difference if I used this version?
     
  13. Offline

    1Rogue

    You would need to compile the newest version yourself from the github repo: https://github.com/bukkit/bukkit
     
  14. How would I compile this new version exactly...? XD Sorry, I have never heard of this until now and I'm very confused... @1Rogue

    Okay, I used a different method of choice, and it worked (sorta).
    Code:
    Code:
    List<Player> ops = new ArrayList<Player>();
            for(Player p : Bukkit.getOnlinePlayers()) {
                if(p.isOp()) {
                    ops.add(p);
                }
            }
    But, the outcome is, "Online Staff: [CraftPlayer{name=CodePlaysMC}]"
    How would I make it so it's just the player's name? If I use "ops.add(p.getName() + ", ");" I get an error saying, "The method add(Player) in the type List<Player> is not applicable for the arguments (String)"

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

    teej107

    @CodePlaysMinecraft Add the Player's name (which is a String) rather than the Player itself (which is not a String)
     
  16. @teej107 Still getting the same error when doing that.
    Code:
    List<Player> ops = new ArrayList<Player>();
            for(Player p : Bukkit.getOnlinePlayers()) {
                if(p.isOp()) {
                    String pName = p.getName();
                    ops.add(pName);
                }
            }
    Bump :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  17. Offline

    HeadGam3z

    Change your List from a Player to a String.
     
  18. Offline

    API_Tutorials

  19. Offline

    teej107

    @CodePlaysMinecraft
    And learn how Collections work. Players are not Strings and Strings are not Players.
     
  20. @HeadGam3z Thank you!
    @API_Tutorials Thank you!
    @teej107 I will take that into consideration, after all you do make a good point. Thanks for the help and advice.
     
  21. Offline

    CheesyFreezy

Thread Status:
Not open for further replies.

Share This Page