Solved getOnlinePlayers with permissson

Discussion in 'Spigot Plugin Development' started by Chronuak, Feb 17, 2020.

Thread Status:
Not open for further replies.
  1. Hello everyone,
    I wanted to create an item which only shows players with a certain permission.
    However I don't know how to do this.
    I started like this, but there is an error:
    Code (Text):
    if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§5VIPs anzeigen")) {
                            p.closeInventory();
                            for(Player all : Bukkit.getOnlinePlayers() && p.hasPermission("lobby.vip")) {
                                p.hidePlayer(all);  
                                }
                            }
    Can somebody please help me?
     
  2. Can you elaborate on what exactly you are wanting? Right now it sounds like you want to be able to hide players from the player holding this item. Is that right?
     
  3. Probably e.getCurrentItem or getItemMeta() return null.
     
  4. What he said

    If you click on an empty inventory slot it's going to return a null ItemStack object

    I would suggest posting the stacktrace so it can be a little more clear what is causing the error
     
  5. At the very least, there are a few things I am seeing that I would change. Instead of using:
    Code (Java):
    .equalsIgnoreCase("§5VIPs anzeigen")
    I would use:
    Code (Java):
    .contains("anzeigen")
    Just to make sure there is no issue with formatting.

    Another thing, definitely check to make sure the itemmeta is not null before checking further into the meta.
     
  6. Is nobody going to comment on that foreach loop that is 100% causing a compiler error?

    You need to move the condition check that you put in the foreach loop somewhere else. It looks like you might just want to && it with the if statement above.
     
    • Agree Agree x 4
    • Funny Funny x 1
  7. was genau hast du vor? Nur das anzeigen aller Spieler mit der Permission "xyz" ?
     
  8. This is very true, you will want to change the for loop. One way that I would do it is instead of:
    Code (Java):
    if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§5VIPs anzeigen")) {
                            p.closeInventory();
                            for(Player all : Bukkit.getOnlinePlayers() && p.hasPermission("lobby.vip")) {
                                p.hidePlayer(all);
                                }
                            }
    where you have:
    Code (Java):
    #pseudo-code

    if(item name = custom item name){
        close player inventory;
        for(players online with custom permission){
            hide that player.
        }
    }
     
    do:
    Code (Text):
    #pseudo-code

    if(item name = custom item's name){
        close player inventory;
        for(all players online){
            if(selected player has permission){
                 hide that player specifically;
            }
        }
    }


    **** I am keeping the above in here in case it helps anyone in the future, however I have just re-read the question and I think what the OP is actually wanting to do is hide everyone from VIP's, in which case you just need an additional for loop in my opinion (have not tested, see below) *****


    If the note above is right, you will probably want something like:

    Code (Text):
    if(item name = custom item name){
        close player's inventory;
        for(players currently online with variable name "1"){
             if(selected player "1" does not have custom permission){
                 for(all players currently online with variable name "2"){
                      hide player 1 from player 2;
                }
            }
        }
    }
    The reason I would not combine the item name check and the permissions check is simply my personal opinion. I think it helps keep everything organized and also helps with troubleshooting and debugging.
     
    #8 FyrenGaming, Feb 17, 2020
    Last edited: Feb 17, 2020
    • Agree Agree x 2
  9. I'm afraid that's not what OP wants. The item is called "VIP's anzeigen", which is German and means "Show VIP's" in English. He doesn't want to hide everyone from VIP's, but he wants to hide everyone *except* VIP's. They should be shown.

    He wants an item that, when clicked, hides all others that do not have a certain permission.

    So this is what I would do:
    Code (Java):

    if (e.getCurrentItem() != null) {
     if (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§5VIPs anzeigen")) {
      e.setCancelled(true);
      p.closeInventory();

      for (Player ps: Bukkit.getOnlinePlayers()) {
       if (!ps.hasPermission("lobby.vip")) { // if does not have permission, therefore no VIP
        p.hidePlayer(ps); // hide that non-VIP player from the player
       }
      }
      p.sendMessage("You only see VIP's now.");
     }
    }
     
    So that only the VIPs (those with the specific permission) are shown to the player. All others are hidden.
     
    #9 Rawora, Feb 18, 2020
    Last edited: Feb 18, 2020
    • Agree Agree x 1
  10. Yes, I want to hide all players who doesn't have the permission. The people with the permission are shown.
     
  11. However I believe that if you do that what message #9 does ( I don't know how to mark a certain text), everybody will be hidden if ALL have the permission and if not everybody will be shown.
     
  12. I could try if this would work
     
  13. You could use a function like this:
    Code (Java):

    public static ArrayList<Player> getPlayersWithPermission(String perm) {
           ArrayList<Player> players = new ArrayList<Player>();
           
           for(Player plr : Bukkit.getOnlinePlayers()) {
               if(plr.hasPermission(perm))
                   players.add(plr);
           }
           
           return players;
       }
     
    And then loop through them where-ever you need:
    Code (Java):

    if(e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§5VIPs anzeigen")) {
                p.closeInventory();
                ArrayList<Player> players = getPlayersWithPermission("lobby.vip");
                for(Player all : players) {
                    p.hidePlayer(all);
                }
           }
     
     
    • Agree Agree x 1


  14. The last code in my last reply should hide all players except VIP's. If you look through, I loop through all online players twice and hide every player from everyone. The only players left not hidden are the players with the VIP permission. Granted it's not true code, but pseudo-code. The actual coding is up to however you want/can do it.
     
  15. No it won’t, since I negated the if statement. If everybody had that permission then obv everyone would be shown. I tested it and it works.
    So I don’t exactly get what is wrong with my code? I mean it does exactly what you want.


    I see where you coming from, but that’s not exactly what he wanted. As you said, you loop through all of the players twice and hide them from each other. But the non-vip players should only be hidden from the one player using the item. That’s why I did it like that with only one loop in #9.
     
    #15 Rawora, Feb 18, 2020
    Last edited: Feb 18, 2020
  16. Just going to leave that here:
    Bukkit.getOnlinePlayers().stream().filter(player -> player.hasPermission("perm.name")).collect(Collectors.toList())
    Or (probably even better)

    Bukkit.getOnlinePlayers().removeIf(p -> !p.hasPermission("name"))
     
    #16 robertlit, Feb 18, 2020
    Last edited: Feb 19, 2020
    • Agree Agree x 3
Thread Status:
Not open for further replies.