Solved Force Player to run a command

Discussion in 'Plugin Development' started by Codisimus, Apr 3, 2013.

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

    Codisimus

    I need to force a Player to run a command even if the don't have permission.

    Code:
    player.setOp(true);
    server.dispatchCommand(player, cmd);
    player.setOp(false);
    Is there a better way? I wasn't sure if that would spam the Player/console either.
     
    G4meM0ment likes this.
  2. Offline

    the_merciless

    Last edited by a moderator: May 31, 2016
  3. Offline

    Codisimus

    the_merciless That sends the command from the console, not the Player. A lot of commands are ignored if they are not sent from the Player.
     
  4. Offline

    ZeusAllMighty11

    Codisimus

    I don't know how to do it, but as a response to your idea...


    Why would you make a player use a command they don't have permission for?
     
  5. Offline

    ase34

    AFAIK commands aren't executed async, so nobody can run a comand while the other has not fininshed (the reason why you should not set the current execution asleep), thus there is no backdoor! ;)
     
  6. Offline

    the_merciless

    That's true. I guess it depends what command you want to execute. A lot of commands will allow you to specify a player too
     
  7. Offline

    Codisimus

    Let's use my ButtonWarp plugin as an example. It allows you to assign specific commands to button. Suppose I wan't players to be able to use a certain command (for simplicity let's say '/spawn') but I don't want the player to be able to use it at any time. They should only be allowed to use it through pressing this button. I know that there is proly a console command that will send a player to spawn but there are certain commands that will not function as intended when sending them from the console. For example, If I want to run an informational command for the player (such as '/help'), running it from the console will send the response back to the console and not to the Player.
     
  8. Offline

    ZeusAllMighty11

    Codisimus

    Oooh I understand. Why not temporarily override permissions without op?
     
  9. Offline

    the_merciless

    If your using essentials you could always run a console command doing /sudo (player name) (command). Unless of course they dont have permission. In which case your original post (or temporarily setting permission) will be your best option.
     
  10. Offline

    Codisimus

    I do not know the permissions that would have to be set bc this should work with any command.

    Why would I depend on Essentials to do that? From my understanding it doesn't do anything different than dispatchCommand(player, cmd);
     
  11. Even if you got the permission the command was using, there are a few plugins that don't specify the permission in plugin.yml and directly use sender.hasPermission() in the code.

    So temp-op is the best way so far... however, just to be safe that nothing goes wrong, I suggest:

    Code:
    try
    {
        player.setOp(true);
        server.dispatchCommand(player, cmd);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        player.setOp(false);
    }
    And of course you should check if player is already OP before you execute that code.
     
    gabrielhowat and Codisimus like this.
  12. Offline

    Codisimus

    Digi
    Great idea, even though I doubt there are plugins out there that will cause exceptions when executing a command ;)

    I am gonna go ahead and mark this as solved. Thanks everyone for the help.
     
Thread Status:
Not open for further replies.

Share This Page