How to add an if op statement in my plugin.

Discussion in 'Plugin Development' started by xMwpgamer7ooox, Jul 12, 2014.

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

    xMwpgamer7ooox

    Code:java
    1. package me.xMwpgamer7ooox.PluginHider;
    2.  
    3.  
    4.  
    5. import java.util.logging.Logger;
    6.  
    7.  
    8.  
    9. import org.bukkit.ChatColor;
    10.  
    11. import org.bukkit.command.Command;
    12.  
    13. import org.bukkit.command.CommandSender;
    14.  
    15. import org.bukkit.entity.Player;
    16.  
    17. import org.bukkit.plugin.PluginDescriptionFile;
    18.  
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21.  
    22.  
    23. public class main
    24.  
    25. extends JavaPlugin
    26.  
    27. {
    28.  
    29. public final Logger logger = Logger.getLogger("Minecraft");
    30.  
    31.  
    32.  
    33. public void onDisable()
    34.  
    35. {
    36.  
    37. PluginDescriptionFile p = getDescription();
    38.  
    39. this.logger.info(p.getName() + "V" + p.getVersion() + "Has Been Enabled!");
    40.  
    41. }
    42.  
    43.  
    44.  
    45. public void onEnable()
    46.  
    47. {
    48.  
    49. PluginDescriptionFile p = getDescription();
    50.  
    51. this.logger.info(p.getName() + "V" + p.getVersion() + "Has Been Enabled!");
    52.  
    53. getConfig().options().copyDefaults(true);
    54.  
    55. saveConfig();
    56.  
    57. }
    58.  
    59.  
    60.  
    61. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    62.  
    63. {
    64.  
    65. Player player = (Player)sender;
    66.  
    67. if ((commandLabel.equalsIgnoreCase("plugin")) || (commandLabel.equalsIgnoreCase("plugins")) || (commandLabel.equalsIgnoreCase("pl")) || (commandLabel.equalsIgnoreCase("?")) || (sender.isOp() == false)) {
    68.  
    69. player.sendMessage(ChatColor.AQUA + "You are not allowed to view HyruleCraft's plugins!");
    70.  
    71. }
    72.  
    73.  
    74.  
    75. else if ((commandLabel.equalsIgnoreCase("help")) || (sender.isOp() == false)) {
    76.  
    77. player.sendMessage(ChatColor.AQUA + "You are not allowed to view HyruleCraft's plugins, help or commands!");
    78.  
    79. }
    80.  
    81.  
    82.  
    83. else if (sender.isOp() == true) {
    84.  
    85. }
    86.  
    87. return false;
    88.  
    89. }
    90.  
    91. }
    92.  
    93.  

    I am still getting the message error for ops, this is my current code. Please help!

    how would I make it so only ops could bypass the error when typing in /pl

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    Beeperdp

    Use
    Code:java
    1. if(player.isOp() == false){
    2. player.sendMessage("You are not op!");
    3. }


    Something like this to make it like the way you want I think:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. Player player = (Player) sender;
    3. if ((commandLabel.equalsIgnoreCase("plugin")) || (commandLabel.equalsIgnoreCase("plugins")) || (commandLabel.equalsIgnoreCase("pl")) || (commandLabel.equalsIgnoreCase("?")) && sender.isOp() == false) {
    4. player.sendMessage(ChatColor.AQUA + "You are not allowed to view HyruleCraft's plugins!");
    5. return true;
    6. }else{
    7. return false;
    8. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  3. Offline

    xMwpgamer7ooox

    Thanks, so would I put return true; at the end of my second if statement?

    Code:java
    1. package me.xMwpgamer7ooox.PluginHider;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class main
    13. extends JavaPlugin
    14. {
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16.  
    17. public void onDisable()
    18. {
    19. PluginDescriptionFile p = getDescription();
    20. this.logger.info(p.getName() + "V" + p.getVersion() + "Has Been Enabled!");
    21. }
    22.  
    23. public void onEnable()
    24. {
    25. PluginDescriptionFile p = getDescription();
    26. this.logger.info(p.getName() + "V" + p.getVersion() + "Has Been Enabled!");
    27. getConfig().options().copyDefaults(true);
    28. saveConfig();
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    32. {
    33. Player player = (Player)sender;
    34. if ((commandLabel.equalsIgnoreCase("plugin")) || (commandLabel.equalsIgnoreCase("plugins")) || (commandLabel.equalsIgnoreCase("pl")) || (commandLabel.equalsIgnoreCase("?")) && sender.isOp() == false) {
    35. player.sendMessage(ChatColor.AQUA + "You are not allowed to view HyruleCraft's plugins!");
    36. return true;
    37. }
    38.  
    39. else if ((commandLabel.equalsIgnoreCase("help")) && sender.isOp() == false) {
    40. player.sendMessage(ChatColor.AQUA + "You are not allowed to view HyruleCraft's plugins, help or commands!");
    41. }
    42.  
    43. return false;
    44. }

    Alright so when op, I'm still getting the error I have for the non ops using this code

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    Beeperdp

    Hmm.. It shouldn't be running if you are op.
     
  5. Offline

    xMwpgamer7ooox

    W
    Would my rank effect it?
     
  6. xMwpgamer7ooox Beeperdp
    Just a Java tip for both of you:

    Code:java
    1. if (player.isOp() == false)
    2.  
    3. //and
    4.  
    5. if (player.isOP() == true)


    is equivalent to

    Code:java
    1. if (!player.isOp())
    2.  
    3. //and, of course
    4.  
    5. if (player.isOp())


    Just looks better and more professional
     
    ZodiacTheories and Gnat008 like this.
  7. Offline

    iiHeroo

    The Gaming Grunts

    Not to mention the unneeded return statements, if boolean is true, then obviously it'll return true.
     
    ZodiacTheories likes this.
  8. Offline

    teej107

    And also he should be using command.getName() instead of checking the command label. It'll make your code a lot cleaner.
     
  9. Offline

    Kazzababe


    How does that make your code cleaner? In his case, they stand for the same thing, so why waste time with that when it's already defined for you?
     
  10. Offline

    teej107

    Ah mabye I forgot to explain why. Using command.getName() accounts for all aliases of the command which the OP is trying to do with commandLabel.
     
  11. Offline

    Kazzababe

    That doesn't mean cleaner code and as I stated, for him they stand for the same thing as I'm assuming he has no other aliases for that command.
     
  12. Offline

    teej107

    What do you mean? This is cleaner code in my opinion:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("plugin")) {
    2. player.sendMessage(ChatColor.AQUA + "You are not allowed to view HyruleCraft's plugins!");
    3. }

    Than doing this:
     
    ZodiacTheories likes this.
  13. Offline

    Kazzababe


    Not cleaner code, just more efficient.
    On a side note, I hadn't noticed he was using multiple aliases for his command, so in that case, it would be preferable to use command.getName().
     
  14. Offline

    Beeperdp

    I know - thanks though.
     
  15. Offline

    xMwpgamer7ooox

    hello, I am still having the issue do you have any ideas how to solve the problem?

    The Gaming Grunts ^^^ sorry forgot to tag you

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  16. Offline

    teej107

    What is your definition of clean code? I say one definition of clean code means it is easy to read. One boolean in an if statement is a lot easier to read than many.
    xMwpgamer7ooox You aren't returning true in your if statement where you are checking if the player is an op. And also please look at my earlier post:
    Unless you want something to be different for each alias, do the above.
     
    ZodiacTheories likes this.
  17. Offline

    Rocoty

    Kazzababe I have yet to come by a case where it would be preferable not to.
     
    ZodiacTheories and AdamQpzm like this.
  18. Rocoty I'm guessing it's preferable to check the label if you're making a bukkit plugin video tutorial, since that seems to happen... not mentioning any names...
     
    ZodiacTheories and Rocoty like this.
  19. Offline

    Kazzababe

    In the majority of my plugins that involve commands, I never have any commands with any extra aliases, so for me, using command.getName() is not needed. When using aliases, it would obviously be preferable to use command.getName().
     
    teej107 likes this.
  20. Offline

    Pokechu212

    Can't you also just create a permission for whatever you are trying to make? Then just configure the permission in the plugin.yml and set the default to "op".
     
  21. Offline

    xAstraah

    xMwpgamer7ooox, Wow you use to much spacing. Kinda makes it hard to read.
     
  22. Kazzababe You do realise that server administrators can, if they know how, edit plugin.yml files too? I learned how to do that long before learning how to make plugins. It was a simple way to make an alias for a command that the original developer didn't include, without need of an additional plugin specifically for aliases. This of course assumes that developers have checked the command name rather than a label :)

    But Rocoty said he hadn't seen a case where it was preferable to check the label - even if you can guarantee that there will be no aliases, that doesn't make checking the label preferable. At best it's the same, so why not go with the method that has additional benefits?
     
Thread Status:
Not open for further replies.

Share This Page