Solved Get at least ONE player with specific permission

Discussion in 'Plugin Development' started by Stephanech, Sep 24, 2016.

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

    Stephanech

    Hi!
    I already know how to get online players with a permission and.. do something
    Code:java
    1. for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    2. if (!p.hasPermission("iam.admin")) { // Getting only ADMINS players
    3.  
    4. sender.sendMessage("There aren't admins online!");
    5. }
    6. else
    7. {
    8.  
    9. // 'Something'
    10.  
    11. }
    12. }



    BUT!, When there's an admin and a normal user online, the plugin does 'something' AND send the error message. (If there are only admins online it just do 'something')

    There is my problem!, I want to the plugin just check if there's at least ONE admin (There, the plugin will not send the error message even if there are normal players online too.. and do 'something')
     
    Last edited: Sep 25, 2016
  2. Offline

    Zombie_Striker

    @Stephanech
    Then do the following:
    1. Create a boolean that is automatically set to false. This represents if an admin is online.
    2. For loop through all the players online
    3. If the player is an admin, set the boolean to true and break out of the loop.
    4. Outside the for loop, check if the boolean is equal to true. If so, an admin is online. Else, there is no admin online.
     
  3. Offline

    Stephanech

    Ok, I do it but now the command only send the error message ("There aren't admins online!")
    I don't know what I do wrong, I checked all the command and change it multiple times but it keeps the same

    Code:java
    1. boolean adminonline; {
    2.  
    3. for(Player p : Bukkit.getServer().getOnlinePlayers()){
    4. if (p.hasPermission("adminhelper.admin")){
    5. adminonline = true;
    6. }
    7. }
    8.  
    9. if (adminonline = true){
    10. //Some Stuff
    11.  
    12. } else {
    13. sender.sendMessage(ChatColor.RED + "There aren't admins online!");
    14. }
    15. return true;
    16.  
    17. }
    18.  
    19. }


    Maybe is really easy the solution, but I'm trying my best in Java
     
    Last edited: Sep 25, 2016
  4. Offline

    JanTuck

    Which permission plugin are you using? Try with checking if p has this permissions.

    Permission perm = new Permission("adminhelper.admin");

    I use above cause the has permission string did not work for me.

    Sent from Tapatalk
     
  5. @Stephanech I think you need to learn the Java syntax and how to use if statements.

    Why do you have an opening bracket after setting the Boolean to false? After you set it t true break out the loop. Your if statement is changing the Boolean to true then checking if it's true. Remove the = true
     
  6. Offline

    Stephanech

    @bwfcwalshy I'm still learning Java, that's why i'm very newbie in all this

    So, I do some changes:
    *I deleted the brackets from the boolean
    *I created a label for the loop, and break the loop with that label
    *I replaced the permission string to "perm"
    *And, I deleted the "= true" in my If statement

    But, it keeps sending the error message when an admin and a user are online. I checked all the permissions but nothing

    @JanTuck I use PermissionsEX

    Code:java
    1. boolean adminonline;
    2.  
    3. Permission perm = new Permission("adminhelper.admin");
    4.  
    5. adminonlineloop:
    6. for(Player p : Bukkit.getServer().getOnlinePlayers()){
    7. if (p.hasPermission(perm)){
    8. adminonline = true;
    9. break adminonlineloop;
    10. }
    11. }
    12.  
    13.  
    14. if (adminonline){
    15. // Stuff
    16.  
    17. } else {
    18. sender.sendMessage(ChatColor.RED + "There aren't admins online!");
     
  7. Offline

    JanTuck

    @Stephanech

    Adminonline might not be iniatilized

    Code:java
    1.  
    2. boolean adminonline = false;
    3. Permission perm = New Permission("adminhelper.admin");
    4.  
    5. for(Player p : Bukkit.getServer().getOnlinePlayers()){
    6. if (p.hasPermission(perm)){
    7. adminonline = true;
    8. break;
    9. }
    10. }
    11. if (adminonline){
    12. // Stuff
    13.  
    14.  
    15. } else {
    16. sender.sendMessage(ChatColor.RED + "There are no admins online!");
    17. }
    18.  
     
  8. Offline

    Stephanech

    @JanTuck

    Keeps with the error message
     
  9. Offline

    JanTuck

    It shoudlnt. Are you sure you gave yourself the permission?
     
  10. Offline

    Stephanech

    Pretty sure
    http://imgur.com/a/jVbiD
    ('Stephanech' has admin permission, 'Pepa' has user permission)
    ( "¡No hay ayudantes conectados!" is "There aren't admins online!", cause I'm making the plugin in spanish )
    (The screenshot was taken in 'Pepa' session)

    Maybe I should show you the full code?
     
  11. Offline

    JanTuck

    Just post the command class.
     
  12. Offline

    Stephanech

    @JanTuck

    Copy and paste it in a notepad or something, I will deleted it soon if anyone want to steal it

    Code:java
    1.  
    2. package Nope
     
    Last edited: Sep 26, 2016
  13. Offline

    timtower Administrator Administrator Moderator

    @Stephanech Nobody will steal that, if you remove it then I will just put it back.
     
    WolfMage1 likes this.
  14. Offline

    JanTuck

    @Stephanech

    It is working fine for me

    Code:php
    1.  
    2. >staff
    3. [20:44:26 INFO]: Admin found
    4. >pex user JanTuck remove *
    5. [20:44:32 INFO]: Permission "*" removed!
    6. >staff
    7. [20:44:35 INFO]: Admin found
    8. >deop JanTuck
    9. [20:44:39 INFO]: De-opped JanTuck
    10. >staff
    11. [20:44:40 INFO]: There aren't admins online!
    12. >pex user JanTuck add adminhelper.admin
    13. [20:46:24 INFO]: Permission "adminhelper.admin" added!
    14. >staff
    15. [20:46:26 INFO]: Admin found
    16. >pex user JanTuck remove adminhelper.admin
    17. [20:46:31 INFO]: Permission "adminhelper.admin" removed!
    18. >staff
    19. [20:46:33 INFO]: There aren't admins online!
    20.  
    21.  


    Code used

    Code:java
    1.  
    2. boolean adminonline = false;
    3. Permission perm = new Permission("adminhelper.admin");
    4.  
    5. for(Player p : getServer().getOnlinePlayers()){
    6. if (p.hasPermission(perm)){
    7. adminonline = true;
    8. break;
    9. }
    10. }
    11.  
    12.  
    13. if (adminonline) {
    14. commandSender.sendMessage(ChatColor.RED + "Admin found");
    15. } else {
    16. commandSender.sendMessage(ChatColor.RED + "There aren't admins online!");
    17. }
    18.  
     
  15. Offline

    Zombie_Striker

    Don't blidnly cast the player. What if a plugin or console sends a command? Since they are not players, this would throw an error. More importantly, you never use this variable. You can delete it.

    The only way to get that error would be if "adminonline" is false. Check if the boolean is ever true.
     
  16. Offline

    JanTuck

    @Zombie_Striker
    I tested the code and the code i used of his returned true.
     
  17. Offline

    WolfMage1

    You can actually do that? xD

    @Stephanech

    1. Please format your code properly, it makes it easier to read.
    2. Never blindly cast to player, always check who the sender is before casting.
    3. In your "hasPermission" check, you never return true, so even though you don't want them to use it they still can
    4. Dont use string concatonation in a string builder, use another .append (you did ask.append(s+" ") instead of ask.append(s).append(" ")
    5. In your for loop, do the check for admins online first, otherwise it will send the message to the player ? times based on players online, so if there're 50 players online, it will send it 50 times
    6. You don't understand how to use static properly, please learn how to use it
    7. Most important of all, please learn Java before using the Bukkit API or any API's for that matter, they're all reliant on having known Java before hand. So if you don't know it you can't use it to its full capability.

    EDIT:

    8. Package names should always be lower case, so instead of me.Stephanech, it should be me.stephanech
     
    Last edited: Sep 25, 2016
    Zombie_Striker likes this.
  18. Offline

    Stephanech

    SOLVED

    You guys won't believe this, it was my fault for a stupid reason all the time!

    By default, I export the .jar in the plugins folder of my local server, but this time I was exporting it in another folder and using an old version of the plugin!

    Thanks to all for the help, and sorry
     
Thread Status:
Not open for further replies.

Share This Page