Solved this is possible!

Discussion in 'Plugin Development' started by MinecraftChicken, Oct 22, 2014.

Thread Status:
Not open for further replies.
  1. Hello everybody,

    I have a plugin that gives a list of the current moderators.
    It looks something like this:
    1. if (cmd.getName().equalsIgnoreCase("staff")) {
    2. //stuffs here.
    3. }

    But is it possible to insert something that shows who of the moderators are online (using permissions)?

    thx for ur help ;)
  2. Offline


    Yes, iterate through all the players checking if they have the permission and add them to a list or something so you can send them to the command sender.
  3. Offline


    if you want to see how it is done (if you dont know how) then look here:
    Show Spoiler

    for (Player p : Bukkit.getOnlinePlayers()) {
    if (p.hasPermission("example.staff.permission")) {
  4. Offline


    MinecraftChicken Whenever I see a thread asking whether something is possible my first instinct is to reply "Why don't you try for yourself and find out?"
  5. Offline


    Code is very versatile. Everything is possible with code as long as the hardware allows it.
    MasterDoctor likes this.
  6. Offline


    If you spoonfeed him then do it right! Your code has in 4 lines 1 line bullshit. Save the array of online players in a variable and dont call the getter every time you want to loop through, you don't exactly know what the method getOnlinePlayers() is doing in the background. This is ugly coding
    MasterDoctor likes this.
  7. Offline


    Wat... It returns a Collection of Players, that's all it does (in simple terms)
  8. Offline


    Shmobi it to a variable, and then what? You'd have to update the list every time a player joins or leaves. How is that less "ugly" than the code provided by FisheyLP ?
  9. Offline


    Shmobi Bukkit.getOnlinePlayers() is not called at each iteration... just saying.
    Rocoty likes this.
  10. Offline


    Rocoty as Tomtom3 already said, it is just called 1 time. So it won't update anyway
    Totom3 i don't meant it like it is called at each iteration. i mean't like don't call every time you want to iterate through the getter in the head of the loop. It's better and nicer to read if you create a variable called onlinePlayers or something like this to make it nicer to read, there is no error in the code or an uneffective thing in the code, i just want to say don't spoonfeed them ugly code because if you really want to teatch him by showing him code, then make sure that it is even for a noob readable and understandable
    fireblast709 read what i wrote to tomtom3. yes the code is right, no it does not return a collection, a collection is something other, it returns an array. but that's not what i wanted to criticize. i just want you to make sure that even noobs can understand what FisheyLP is spoonfeeding them, because if you spoonfeed it should be as simple as you can make it to keep the hope, that the spoonfeeded programmer will learn something from the code and not just copypast it. in this case it isn't such a big problem because there are only 4 lines, but in 4 lines 1 line like this makes me not believing that he will do it any better in 100 lines
  11. Offline


    I wish i had a quid for every time I have had to say this!
  12. Offline


    MasterDoctor if i and you would, it would be bad, because then we would be rich and wont spend our time in telling such muglis to stop that shit. then we would spend our time in alcohol and girls, wouldn't we? xD
    MasterDoctor likes this.
  13. Offline


  14. Offline


    Shmobi What's ugly about an inline method call in a for-loop declaration? The method has a very expressive name so I honestly don't see the problem. Sure you could save the returned list to a variable first, and then use that variable in the for loop, but at the end of the day - as long as you give the variable a decent name - it really doesn't matter at all...

    In newer versions of the API that method returns a Collection<? extends Player> and the one that returns Player[] is deprecated. (silly in my opinion, but what'chu gonna do)
  15. Offline


    Rocoty if you would've read my penultimate post, you would understand, but it seems like you didn't :eek: im bored of discussing with people about coding and/or java standards. sure it works just fine in both ways, no question. but it looks like the codes you're writing aren't readable at all. for exampe:
    Player[] onlinePlayers = Bukkit.getOnlinePlayers(); <- you know it is an Array of Player
    Bukkit.getOnlinePlayers(); <- you must look in the javadoc to know return type etc == NOT READABLE AT ALL

    Codestandards mean things like name variables corecly with some sens. if you would use entitiesNearBy instead of onlinePlayers this would be stupid. But codestandards also mean that you can read the code in one flow instead of keep watching again and again in javadocs or looking where does what come from.
  16. Offline


    1. public ArrayList<String> modsonline = new ArrayList<String>();
    3. //Update mod list
    4. public void onJoin(PlayerJoinEvent e) {
    5. //Player join event
    6. for(Player all:Bukkit.getOnlinePlayers()) {
    7. //Loop through online players
    8. if(all.hasPermission("List.mod")) {
    9. //If the current looped player has List.mod
    10. this.modsonline.add(all.getName());
    11. //Add the player name to the modsOnline array list
    12. }
    13. all.sendMessage(ChatColor.BLUE + "Players online: " + ChatColor.RED + modsonline);
    14. //Send staff message to all players
    15. }
    16. }
    17. public void onLeave(PlayerQuitEvent e) {
    18. if(this.modsonline.contains(e.getPlayer().getName()) {
    19. //If modsOnline contains the quitter.
    20. this.modsonline.remove(e.getPlayer().getName());
    21. //remove player
    22. }
    23. }

    If the player has List.mod permission, modsonline gets updated.
  17. Offline


    Shmobi you don't need to know the return type. All you need to know that you iterate over all the players, that's what is important when you want to know what code does. Everyone with basic Java knowledge would (read as: should) be able to understand that.

    Also it does return a Collection<? extends Player>, so you better start updating that in your plugins (yes I am aware that I'm repeating Rocoty , but you seem to have missed or ignored that)
  18. Offline


    Shmobi I read all your posts, and you do bring forth some valid points. But I am compelled to disagree with you when you say the code gets less readable when getting the Iterable inline. There is no need at all to know the return type of the method, because since we don't get a compiler error when trying to iterate over it, we KNOW it's an Iterable. And that's really all we need to know, because all we want to do it iterate over it.

    Furthermore, given the method name we also know we are iterating over all online players, so I don't know how exactly that is...less readable? We have all the information we need really. Oh, and should the API change (like it just recently did), we don't have to change our code, because, as stated, we didn't care about what kind of Iterable we got.
  19. Offline



    This is the spoonfeeding you should be complaining about. Seriously, I've never seen this guy post decent code, it always looks like a 5 year old wrote it. Please stop doing this, you're not helping anyone by posting stupid code like this.
  20. Offline


    Its not stupid, it compiles (No code is stupid, whats stupid is kids who don't code) ^-^

    - Also, I bet you have seen me post at LEAST one 'Decent code'. And I also bet I've seen me post some too.

    • Stupid code may be:
    1. if(!sender instanceof Player) {
    2. (Player) player = sender;
    3. }

    • I edited it to show processes of the code.

    Show Spoiler

  21. Offline


    ChipDev The code you have in your original post still won't compile. The code you posted just now also won't compile. The code in your original post is also misleading because it implies that one player represents all players.
  22. Offline


    What shall I call it?
    EDIT: you got me :p add is not all!
  23. Offline


    2. StringBuilder message = new StringBuilder();
    3. for (Player p : Bukkit.getOnlinePlayers()) {
    4. if (p.hasPermission("moderatorPermission")) message.append(p.getDisplayName()).append(", ");
    5. }
    6. message.delete(message.length()-3, message.length());
    7. String list = message.toString();
    8. sender.sendMessage("The following moderators are online: " + list);

    jesus christ ChipDev just stop
    Garris0n likes this.
  24. Offline


    You're assuming there's at least one moderator online.
    Edit: Also, -2, not 3.
    ChipDev likes this.
  25. Offline


    ChipDev sec, gona film myself jumping out of the window holding this code on a printed piece of paper.
  26. Offline


    What ChipDev is doing should be considered trolling, even if it's not intended; I honestly vote for him to be banned for said reason.
  27. Offline


    Heres my day:

    Go on bukkit fourms.

    Posts something TRYING TO HELP.

    Sees mazentheamazin replies

    doesn't want to read it knowing I posted something stupid and he is coming in with his 'amazin' powers to come and make me feel bad

    goes 4 minutes without reading post

    goes and reads post
    Then.... (open)

  28. Offline


  29. Offline


    crap forgot to upload to imgur :/
  30. Offline


    Uh, I see the image. Uh.
Thread Status:
Not open for further replies.

Share This Page