Solved Having a player execute a command without permission

Discussion in 'Plugin Development' started by PandazNWafflez, Jul 20, 2014.

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

    PandazNWafflez

    For the plugin I'm currently writing (source / context) I need to be able to have a player execute a command they don't have permission to execute. I can't execute it from the console, because the command would have to be something specific to the player.

    For example, a command could be: '/i cobblestone 64'

    Clearly, this wouldn't work if executed from the console, as it wouldn't give the items to the player. I thought of setting the players permissions to *, then having them execute the command, then removing the permission, but it isn't a method which convinces me. What if, for example, somebody with a modified client has it set to spam '/stop' as they redeemed the code, so that for the split second they had permissions they would be able to stop the server.

    Also, I'm not entirely sure how to change the permissions of a player (there doesn't seem to be any method to set a permission in Player).

    Any ideas would be helpful. Here's the relevant code:

    Code:java
    1. if (!sender.hasPermission("commandcodes.redeem")) {
    2. sender.sendMessage(ChatColor.DARK_RED
    3. + "You don't have permission to do that!");
    4. } else {
    5. if (!(sender instanceof Player)) {
    6. sender.sendMessage(ChatColor.DARK_RED
    7. + "Only players can redeem command codes!");
    8. } else {
    9. if (args.length == 1) {
    10. sender.sendMessage(ChatColor.DARK_RED
    11. + "Invalid syntax, /ccode redeem <code>");
    12. } else {
    13. int code;
    14. try {
    15. code = Integer.parseInt(args[1]);
    16. } catch (NumberFormatException e) {
    17. code = 10000000;
    18. sender.sendMessage(ChatColor.DARK_RED
    19. + "Invalid code entered, /ccode redeem <code>");
    20. }
    21.  
    22. final Player player = (Player) sender;
    23.  
    24. if (code != 10000000) {
    25. final CommandCode cc = codeMgr.redeemed(
    26. player.getUniqueId(), code);
    27.  
    28. if (cc == null) {
    29. sender.sendMessage(ChatColor.DARK_RED
    30. + "Couldn't redeem command code!");
    31. } else {
    32. // TODO: HAVE THE PLAYER EXECUTE THE COMMAND EVEN IF THEY // DON'T HAVE PERMISSION
    33. }
    34. }
    35. }
    36. }
    37. }
     
  2. Offline

    xmarinusx

    PandazNWafflez
    You can op the player before executing the command and deop after. The player will only be op for a few milliseconds, so they shouldn't be able to abuse it.

    To add extra safety you can put the command executing code in a try and deop the player in finally. In this way you're 100% sure the deop method gets called.
     
  3. Offline

    mine-care

    Bukkit.getserver().dispatchCommand(sender, commandstring); sorry but ima on iOS :3
    Hope I helped ps it runs the command as if the player is op so u better check for permission before
     
  4. Offline

    PandazNWafflez

    mine-care Thanks, I'll use that - I wasn't aware it ran it as if the player was OP.

    xmarinusx Thanks for the suggestion, but I think I'll use Bukkit.getServer().dispatchCommand()
     
  5. Offline

    mine-care

    PandazNWafflez
    Try it, I haven't use the method for ages, if you face problems contact me back! Good luck
     
  6. Offline

    xmarinusx

    PandazNWafflez mine-care
    As far as I know does the dispatchCommand() method not execute it as if the player is OP.
    Try it first, if it doesn't work just do what I said.
     
  7. Offline

    mine-care

  8. Offline

    PandazNWafflez

    xmarinusx Shall do. I'll be testing soon anyway, as I'm now pretty much finished
     
Thread Status:
Not open for further replies.

Share This Page