Checking if player has permission to use a command of another plugin ?

Discussion in 'Plugin Development' started by Shereis, Oct 7, 2011.

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

    Shereis

    Hello !!

    I'm trying to make a plugin, to use the Books of the BookWorm plugin to send macro-commands, and optionnaly remove the book if the command result is true ! But I need to check if the player has permission to do this command before and i haven't find how to... !

    I know there is a boolean testPermission(CommandSender sender) in org.bukkit.command.Command class ... but I don't know how to get the command in relation to this method ! Anyone can help me with this ?
     
  2. Offline

    snikkers

    Can't you just use player.haspermission(string/permission)?
     
  3. Offline

    Shereis

    No I can't! When you use player.hasPermission(), you need to know the permission node, but I don't know it ! because the commands used are not from my plugin ! My plugin is just here to make macros with other plugin's command .
     
  4. Offline

    snikkers

    so you will be able to bind commands to books with your plugin ?
    just put in the player thats holding the book as commandsender / executor and permissions system will work

    but this sounds a lot like tiki toolkit plugin, with which you can bind commands to tools etc
     
  5. Offline

    Shereis

    Well, not really... First, if you don't know BookWorm, it allow you to write text in books, and to have an undefined number of different written book and to keep them, of course.
    With my plugin and with the help of the BookWorm Plugin, You will be able to Write and execute while using the sneak key(or maybe right click, i'll see) a set of differents commands or text messages from other plugins in an undefined number of books, while keeping each book conservating the commands set that has been attributate to...
    It will also have the feature of bypass permissions (or not ) and to remove the book from Inventory when this one has been used, and it's in this final feature that i'm actually stuck ! the book has to be removed only if the CommandSender have the permission to do it and only if the command result is true ! but actually the book is removed in every situation, even if the player don't have the permission to execute the command he has written in the book, even if the command fail ...
     
  6. Offline

    snikkers

    ok and because of this you want to get permission before command is executed / book is destroyed
    but i don´t think there will be a way to get permissions for a specific command
    perhaps someone else has an idea
     
  7. Offline

    vaiquero

    The permissions for the command should already be defined within the other plugin, your plugin does not have to do anything but call the command. However, if you mean making your own permission nodes for their plugins, thats a different story.
     
  8. Offline

    Shereis

    As I said in the first post, there is a method to test if the player has the permission to use a command... but I still haven't find how to use it correctly... I'm still searching a way to get it work ; )

    If a player use the book to use a command, and if he doesn't have the permission to do it, the command won't be executed and he will lost the book, and that's what I want to avoid... the book should only be lost if the command is a success !
     
  9. Offline

    vaiquero

    Well then, feel free to use the testPermission() statement.

    if (this.getServer().getPluginCommand("commandhere").testPermission() == true) {
    // remove book code here
    }
     
  10. Offline

    Shereis

    Thanks for this ; ) that's what I was looking for...

    But after many try, and many different configuration, I still got a NullPointerException at this boolean .
    I tried a lot of commands, with and without the "/" ... Maybe I have to put the name of the command instead of the Command itself... but I didn't found where I can get the name relative to the command that is executed by the player....
     
  11. Offline

    Celeixen

    Can you explain this a little better. Are you trying to shorten commands? because i know an easy way on how to do that, that wouldn't require checking the permissions but if your doing something else please explain.

    Ohh just thought of this.
    Code:
        boolean test = new PluginCommand().execute(sender, commandLabel, args);
        if (test){
            // do stuff
    
        }
    Just pass the Sender, CommandLabel & args. But so you know the command will execute if they have the permission. So you wont need to execute it for them.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  12. Offline

    Shereis

    No, I don't try to shorten commands ... I'm trying to get Player able to write anything they want in books, and on right-click everything they written in the book go to chat... if it's a list of commands, commands will be Executed... but where i'm stuck actually is that i would like to add an option to remove the book if the commands are successfull ! With that, Admins will be able, for example, to write books (single use only) with permissions promotion commands, or permissions based skills from other plugins...

    This line doesn't work for me because "PluginCommand()" needs arguments like that :

    boolean test = new PluginCommand (String name, Plugin owner).execute(sender, commandLabel, args);

    I think I know how to get the "Plugin owner" ! but I didn't found how to get the "String name" who represent the name of the command (and not the label)!





     
  13. Offline

    Celeixen

    Ohh i get it, i am not at my pc right now so i cant give you code (takes to long on iphone) but what you want todo is check if it contains a "/" then try create a new commandpreprocess event then call it. I will post code when i can.

    Btw sorry about the other code, i just wrote that off the top of my head so i forgot the parameters.
     
  14. Offline

    Shereis

    Thanks for your interest in my problem ; )

    If you talk about a code like that :
    Code:
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.PluginManager;
    
    public class CommandExecute {
    
        public CommandExecute(Player player,String str){
    
            boolean slashTest = str.startsWith("/") ;
             if (slashTest == true){
                  str = str.replaceFirst("/", "");
                  boolean performedCommand   = player.performCommand(str);
                  if (performedCommand == true){
    
                      PlayerCommandPreprocessEvent PCPEvent = new PlayerCommandPreprocessEvent(player, str);
    
                      PluginManager pm = player.getServer().getPluginManager();
                      pm.callEvent(PCPEvent);
                      boolean PCPECancelStatus = PCPEvent.isCancelled();
                     if (PCPECancelStatus == false)
                         player.setItemInHand(null) ;
                     if (PCPECancelStatus == true) player.sendMessage("Gratz"); // only for debug
                  }
              }
             else player.chat(str);
          }
    }
    This one doesn't work properly... the event is never cancelled even if player don't have permission
     
  15. Offline

    Celeixen

    Are you op? Because when the command is called it is sent to the plugin, so its up to the plugin to check if the player has the permission. As long as your passing the correct player it should work.
     
  16. Offline

    Shereis

    I've tested with and without op player... the boolean PCPECancelStatus stay on false... do I have to register this PlayerCommandPreprocessEvent in the Main class, like other listeners ? I think I don't have to in this situation...
    Anyway... it looks like that the other plugins don't use the .setCancelled() method for commands Permissions... That certainly explain why the boolean .isCancelled() remain on false...
     
  17. Offline

    Celeixen

    Yea, you dont want to check if its cancelled. When you call that event it should be exactly the same as when a player types the command. So if they dont have the permission it wont work.
     
  18. I get the feeling your doing that wrong. You want that code to be executed every time you do new CommandExecute?
     
Thread Status:
Not open for further replies.

Share This Page