How do i add permissions to a plugin? ( PermissionsEx Support)

Discussion in 'Plugin Development' started by javoris767, Oct 18, 2011.

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

    javoris767

    I need to know how to add permission support to this code.

    *This is a plugin just for my server*

    Code:java
    1. package me.javoris767.Pr3datorCraft;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.GameMode;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Pr3datorCraft extends JavaPlugin {
    14. public static Pr3datorCraft plugin;
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16.  
    17. public void onEnable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled.");
    20. }
    21. public void onDisable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is disabled.");
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    27. readCommand((Player) sender, commandLabel);
    28. return false;
    29. }
    30.  
    31. public void readCommand(Player player, String command) {
    32. if(command.equalsIgnoreCase("JavC")) {
    33. player.setGameMode(GameMode.CREATIVE);
    34. player.sendMessage(ChatColor.DARK_RED + "[Pr3dator Craft] " + ChatColor.DARK_GREEN + "Creative Mode");
    35. }
    36. else if(command.equalsIgnoreCase("JavS")) {
    37. player.setGameMode(GameMode.SURVIVAL);
    38. player.sendMessage(ChatColor.DARK_RED + "[Pr3dator Craft] " + ChatColor.DARK_GREEN + "Survival Mode");
    39. }
    40. }
    41. }


    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  2. player.hasPermission("Do.My.Mom")
     
  3. Offline

    javoris767

    i dont think im doing it right lol
     
  4. Offline

    AinSophAur

    Show us what you have attempted to do along with the permission file itself.
     
  5. Offline

    javoris767

    Code:java
    1. package me.javoris767.ChangeGameMode;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.GameMode;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class ChangeGameMode extends JavaPlugin {
    14. public static ChangeGameMode plugin;
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16.  
    17. public void onEnable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled.");
    20. }
    21. public void onDisable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is disabled.");
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    27. readCommand((Player) sender, commandLabel);
    28. return false;
    29. }
    30.  
    31. public void readCommand(Player player, String command) {
    32. if(command.equalsIgnoreCase("JavC")) {
    33. player.setGameMode(GameMode.CREATIVE);
    34. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "Set To Creative Mode");
    35. if(player.hasPermission("pr3d.creative")) {
    36. //Do something
    37. }else{
    38. //Do something else
    39. }
    40. }
    41. else if(command.equalsIgnoreCase("JavS")) {
    42. player.setGameMode(GameMode.SURVIVAL);
    43. player.sendMessage("[Pr3dator Craft] " + ChatColor.GREEN + "Set To Survival Mode");
    44. if(player.hasPermission("pr3d.survival")) {
    45. //Do something
    46. }else{
    47. //Do something else
    48. }
    49. }
    50. }
    51. }
    52.  



    Plugin.yml
    Code:
    name: Pr3datorCraft
    main: me.javoris767.ChangeGameMode.ChangeGameMode
    version: 1.5
    description: >
                 Changes player's game mode.
    commands:
      JavC:
        description: Toggles game mode to creative.
      JavS:
        description: Toggles game mode to survival.
    permissions:
        pr3d.creative:
            description: Allows you to switch to creative mode.
            default: op
        pr3d.survival:
            description: Allows you to switch to survival mode.
            default: op
     
  6. You need to check if the player has permissions BEFORE the command does its stuff....
     
  7. Offline

    thehutch

    It should be this:
    Code:
    if ((command.equalsIgnoreCase("JavC")) && (player.hasPermission("nice.to.meet.you"))) {
    you could also add
    Code:
    || player.isOp()
    but that isnt necessary because you can add the default in the plugin.yml
     
  8. Offline

    javoris767

    O forgot the && part <,<"
     
  9. Well, no. Because then, it will say unknown command when they don't have permission.
     
  10. Offline

    javoris767

    the permissions doesnt work only ops can do it
     
  11. Code:java
    1. public void readCommand(Player player, String command) {
    2. if (command.equalsIgnoreCase("JavC")) {
    3. if (player.hasPermission("pr3d.creative")) {
    4. player.setGameMode(GameMode.CREATIVE);
    5. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "Set to creative mode");
    6. } else {
    7. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "You dont have permissions for that command.");
    8. }
    9. } else if (command.equalsIgnoreCase("JavS")) {
    10. if (player.hasPermission("pr3d.survival")) {
    11. player.setGameMode(GameMode.SURVIVAL);
    12. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "Set to survival mode");
    13. } else {
    14. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "You dont have permissions for that command.");
    15. }
    16. }
    17. }

    Try that, should work.
     
  12. Offline

    Stantastic

    doesnt he need to import permissions before he can use it?
     
  13. Are you saying permissions as in a permissions plugin ?
     
  14. Offline

    Stantastic

    add external jar...

    Code:
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    import org.bukkit.plugin.Plugin;
    sorry im new to java... not sure if the stuff im talking is right but thats why i said "doesnt he need?"

    :rolleyes:
     
  15. Offline

    javoris767

    Code:java
    1.  
    2. if (player.hasPermission("pr3d.survival")) {
    3. player.setGameMode(GameMode.SURVIVAL);
    4. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "Set to survival mode");
    5. } else {
    6. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "You dont have permissions for that command.");
    7. }
    8. }
    9. }
    10.  

    player variable has already been used
     
  16. Offline

    thehutch

    yh I didnt type what he should write below just the beginning part

    Hmm ok I retyped your command for you and I think it should be this, I added "instanceof" because you cant change the gamemode of a console :p Also I typed this in notepad so I might have missed a "{}" one of these should be at the end otherwise
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. if ((cmd.getName.equalsignoreCase("JavC")) && (cs.hasPermission("insert.permission") || cs.isOp())) {
    3.  
    4. if (!(cs instanceof Player)) {
    5. cs.sendMessage("Invalid player. Cannot setGameMode for non-player");
    6. return false;
    7. }
    8.  
    9. cs.setGameMode(GameMode.CREATIVE);
    10. cs.sendMessage(ChatColor.DARK_RED + "[Pr3dator Craft] " + ChatColor.DARK_GREEN + "Creative Mode");
    11.  
    12. }
    13.  
    14. else if (cmd.equalsIgnoreCase("JavS")) {
    15.  
    16. if (!(cs instanceof Player)) {
    17. cs.sendMessage("Invalid player. Cannot setGameMode for non-player");
    18. return false;
    19. }
    20.  
    21. player.setGameMode(GameMode.SURVIVAL);
    22. player.sendMessage(ChatColor.DARK_RED + "[Pr3dator Craft] " + ChatColor.DARK_GREEN + "Survival Mode");
    23. }
    24. }


    And this I re-wrote your entire class for you hope it helps the fixes were so you know I added the getCommand("commandname").setExecutor(this); so you register your commands in your plugin.yml. here it is:
    Code:java
    1. public class Pr3datorCraft extends JavaPlugin {
    2. Logger log = Logger.getLogger("Minecraft");
    3. PluginDescriptionFile pdfFile;
    4. PluginManager pm;
    5.  
    6.  
    7. public void onEnable() {
    8. pm = getServer().getPluginManager();
    9. pdfFile = getDescription();
    10. getCommand("JavC").setExecutor(this);
    11. getCommand("JavS").setExecutor(this);
    12. log.info((pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled.");
    13. }
    14. public void onDisable() {
    15. pdfFile = getDescription();
    16. log.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is disabled.");
    17. }
    18.  
    19. INSERT ON COMMAND HERE FROM POST ABOVE
    20. ||
    21. ||
    22. ||
    23. \/
    24. XD
    25. }


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

    Daniel Heppner

    No! That's for the old Permissions.

    FFS, what plugin are you using for Permissions, @javoris767?
     
  18. Offline

    thehutch

    Calm down and I already wrote it out for him
     
    Daniel Heppner likes this.
  19. No, im saying, your code is incorrect.
    If you do that and the player doesn't have permission, it will say "Unknown command"
    IMO You want to tell them they don't have permission, because that is a correct command
     
  20. Offline

    thehutch

    I use this method all the time no errors for me. this checks if the command is JavC and they have permission
     
  21. Okay. Heres what I'm saying
    OP player types JavC: Preforms command
    Player types someRandomCommand: Tells him unknown command
    Player without permission types JavC: Unknown Command
    The case with the bold is incorrect, it should say No permissions
     
  22. Offline

    thehutch

    Meh fair enough ok just add an if statement saying if they dont have it return false and send player a message
     
  23. Yep
     
    thehutch likes this.
  24. Offline

    javoris767

    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. if ((cmd.getName.equalsignoreCase("JavC")) && (cs.hasPermission("insert.permission") || cs.isOp())) {
    4. }
    5.  
    6. if (!(cs instanceof Player)) {
    7. cs.sendMessage("Invalid player. Cannot setGameMode for non-player");
    8. return false;
    9. }
    10.  
    11. cs.setGameMode(GameMode.CREATIVE);
    12. cs.sendMessage(ChatColor.DARK_RED + "[Pr3dator Craft] " + ChatColor.DARK_GREEN + "Creative Mode");
    13.  
    14. }
    15.  
    16. else if (cmd.equalsIgnoreCase("JavS")) {
    17. }
    18.  
    19. if (!(cs instanceof Player)) {
    20. cs.sendMessage("Invalid player. Cannot setGameMode for non-player");
    21. return false;
    22.  

    cs
    returnfalse
    else
    getName
    has errors
     
  25. Offline

    thehutch

    Oh sorry change cs to sender I usually use cs instead of sender for my CommandSender variable :p
     
  26. Offline

    cyberdude

    @thehutch , tips48 is right. However, you might not have noticed because AFAIK the "unknown command" message only appears for players that is OP. So if your player is not OP you might not see the unknown command message.
     
    tips48 likes this.
  27. Offline

    thehutch

    Yes I do understand what he was trying to say, I didn't get what he was saying at the beginning, anyway @javoris767 did you get it working yet?
     
  28. Offline

    javoris767

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. if ((cmd.getName.equalsignoreCase("JavC")) && (sender.hasPermission("pr3d.gamemode") || sender.isOp())) {
    3. }
    4.  
    5. if (!(sender instanceof Player)) {
    6. sender.sendMessage("Invalid player. Cannot setGameMode for non-player");
    7. return false;
    8. sender.setGameMode(GameMode.CREATIVE);
    9. sender.sendMessage(ChatColor.DARK_RED + "[Pr3dator Craft] " + ChatColor.DARK_GREEN + "Creative Mode");
    10. }
    11. else if (cmd.equalsIgnoreCase("JavS")) {
    12. }
    13. if (!(sender instanceof Player)) {
    14. sender.sendMessage("Invalid player. Cannot setGameMode for non-player");
    15. return false;
    16. }
    17. sender.setGameMode(GameMode.SURVIVAL);
    18. sender.sendMessage(ChatColor.DARK_RED + "[Pr3dator Craft] " + ChatColor.DARK_GREEN + "Survival Mode");
    19. }
    20. }
    21.  

    if ((cmd.getName.equalsignoreCase("JavC")) && (sender.hasPermission("pr3d.gamemode") || sender.isOp())) {

    sender.setGameMode(GameMode.CREATIVE);

    else if (cmd.equalsIgnoreCase("JavS")) {

    sender.setGameMode(GameMode.SURVIVAL);
     
  29. Code:java
    1. package me.javoris767.Pr3datorCraft;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.GameMode;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Pr3datorCraft extends JavaPlugin {
    14. public static Pr3datorCraft plugin;
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16.  
    17. public void onEnable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled.");
    20. }
    21.  
    22. public void onDisable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is disabled.");
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    28. String cmd = command.getName();
    29. if (cmd.equalsIgnoreCase("JavC")) {
    30. if (!(sender instanceof Player)) {
    31. logger.info("You cant use that command from the console");
    32. return true;
    33. }
    34. Player player = (Player) sender;
    35. if (player.hasPermission("pr3d.creative") || player.isOp()) {
    36. player.setGameMode(GameMode.CREATIVE);
    37. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "Set to creative mode");
    38. } else {
    39. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "You dont have permissions for that command.");
    40. }
    41. } else if (cmd.equalsIgnoreCase("JavS")) {
    42. if (!(sender instanceof Player)) {
    43. logger.info("You cant use that command from the console");
    44. return true;
    45. }
    46. Player player = (Player) sender;
    47. if (player.hasPermission("pr3d.survival") || player.isOp()) {
    48. player.setGameMode(GameMode.SURVIVAL);
    49. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "Set to survival mode");
    50. } else {
    51. player.sendMessage("[Pr3dator Craft] " + ChatColor.RED + "You dont have permissions for that command.");
    52. }
    53. }
    54. }
    55. }

    Replace your ENTIRE class with that. If it doesn't work then it's you doing something wrong.
     
Thread Status:
Not open for further replies.

Share This Page