Solved How Do I Properly Add Permission Nodes To My Bukkit Plugin?

Discussion in 'Plugin Development' started by imyussy, Oct 11, 2014.

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

    imyussy

    I started working on a plugin thats supposed to say a message when they type /plugins or /pl. I have the whole thing basically done, accept I don't know how to make it so /plugins and /pl sends the player the same message. I only know how to have 1 command. Also, I don't know how to add a permission node that will allow the player to get the message when they type /plugins and /pl. Also I know you need to add some other stuff to the plugin.yml if you wanna add permission nodes. Here is my code so far:
    Code:java
    1. package com.mcacraft.plugin;
    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.plugin.java.JavaPlugin;
    9.  
    10. public class Plugin extends JavaPlugin
    11. {
    12. public void onEnable()
    13. {
    14. Logger log = Logger.getLogger("Minecraft");
    15. log.info("Website version " + getDescription().getVersion() + " is now enabled!");
    16. }
    17. public void onDisable() {
    18. Logger log = Logger.getLogger("Minecraft");
    19. log.info("Website version " + getDescription().getVersion() + " is now disabled!");
    20. }
    21. public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
    22. if(cmd.getName().equalsIgnoreCase("plugins")) {
    23. //use sender instead of player. This will allow your console to execute the command as well.
    24. }
    25. sender.sendMessage( "" + ChatColor.WHITE + "Plugins (2): " + ChatColor.GREEN + "You" + ChatColor.WHITE + ", " + ChatColor.GREEN + "Wish" );
    26. return true;
    27. }
    28. }
     
  2. Offline

    teej107

  3. Offline

    Code0

    Well, to just create commands, use onCommand. But if you want to listen for command executions (pre-) use what teej107 told you.

    A lot of tutorials about (fully and correctly) blocking commands. A google will help.
     
    AdamQpzm likes this.
  4. Offline

    timbragg12

    imyussy Yes, do something like this:
    Code:java
    1. @EventHandler
    2. public void onCmd(PlayerCommandPreprocessEvent e){
    3. if(e.getMessage().startsWith("/pl")||e.getMessage().startsWith("/plugins")) {
    4. e.setCancelled(true);
    5. e.getPlayer().sendMessage(ChatColor.WHITE + "Plugins (2): " + ChatColor.GREEN + "You" + ChatColor.WHITE + ", " + ChatColor.GREEN + "Wish" );
     
  5. Offline

    Watto

    imyussy If you want the same result with different commands just created a method for example

    Code:java
    1. public void displayPlugins(CommandSender sender){
    2. //Send the sender information
    3. }


    and then call it in the onCommand method under both /plugins and /pl

    Or you can do what @timbragg did :p
     
  6. timbragg12 This solution would block literally any command that started with pl - I recommend that you, too, look for the tutorials that Code0 mentioned :)
     
  7. Offline

    timbragg12

    AdamQpzm Oh yeah it would, wouldn't it. :)
    The only other command that I can think of that I use that begins with /pl would be /playsound so I guess he could do:
    Code:java
    1. if(e.getMessage().startsWith("/pl") && !e.getMessage().equals("/playsound")) {

    But there is probably a better way that I don't know about.
     
  8. Offline

    CaptainUniverse

    Seriously Just do COMMAND ALIASES in plugin.yml
    Plugin.yml
    //skip to commands
    commands:
    plugins:
    aliases: [pl]
    And then in command just leave /plugins
    Signature
    Code:java
    1. public boolean isTheCaptainRight(boolean YourGuess) {
    2. if (YourGuess == true) {
    3. return true;
    4. Server.command("give @a cookie");
    5. }
    6. else if (YourGuess == false) {
    7. Server.command("summon Creeper 1000000");
    8. }
    9. return true;
    10. }

     
  9. Offline

    indyetoile

    timbragg12
    Convert the message to lowercase as well; players are still able to do /PL with your code and it'll work.
     
  10. Offline

    CaptainUniverse

    And as for your permissions you do not have to register them in the plugin yaml unless the plugin has its own permission system (pex, PB) if you are just checking do player.hasPermission()
     
  11. timbragg12 That's not the best system either - it would prevent any other plugins from creating commands that start with "pl", like "/players", "/playerlist", "playgame", etc. One possibility is to split the String by space, and that will give the a way to access the whole command entered.

    CaptainUniverse Bukkit has a lovely little system that allows you to run commands even if there are two plugins with the same command name, by prefixing them with the plugin's name. For example, the following command will run bukkit's default plugin command and will ignore any plugin's onCommand()

    Code:
    /bukkit:plugins
     
  12. Offline

    CaptainUniverse

    if there are two commands with the same name, disable Bukkit commands in /help in help.yml
     
  13. CaptainUniverse If one were to fiddle around with such things, I'd rather recommend simply negating the bukkit.commands.plugins permission node. Well, I'd recommend that way anyway, but still. :p
     
  14. Offline

    CaptainUniverse

    Thats like the only way I can think of doing AdamQpzm (And denying the permission)
    But removing Bukkit in help will literally remove the command so that there is no interfierence
     
  15. Offline

    imyussy

    @CaptainUniverse AdamQpzm indyetoile timbragg12 Watto code0 teej107 Hello everyone! This message tags everyone that responded to my thread. Since there were 7 different people that tried to help me, I couldn't decide 1 person to seek help from. From when I first created this thread I made a lot of improvement so I decided to change the title and ask for something else that I needed help on. I posted my java code and my plugin.yml below so you guys can see where my problems are being caused. Please let me know why I am entountering my problems and what I can do to fix them. Thank you!
    Code:java
    1. package com.imyussy.plugincommand;
    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.plugin.java.JavaPlugin;
    16.  
    17.  
    18.  
    19. public class PluginCommand extends JavaPlugin
    20.  
    21. {
    22.  
    23. public void onEnable()
    24.  
    25. {
    26.  
    27. Logger log = Logger.getLogger("Minecraft");
    28.  
    29. log.info("PluginCommand version " + getDescription().getVersion() + " is now enabled!");
    30.  
    31. }
    32.  
    33. public void onDisable() {
    34.  
    35. Logger log = Logger.getLogger("Minecraft");
    36.  
    37. log.info("PluginCommand version " + getDescription().getVersion() + " is now disabled!");
    38.  
    39. }
    40.  
    41. public boolean onCommand1(CommandSender sender, Command cmd, String lbl, String[] args) {
    42.  
    43. if(cmd.getName().equalsIgnoreCase("plugins") && sender.hasPermission("plugincommand.message.plugins")){
    44.  
    45. //use sender instead of player. This will allow your console to execute the command as well.
    46.  
    47. }
    48.  
    49. sender.sendMessage( "" + ChatColor.WHITE + "Plugins (3): " + ChatColor.GREEN + "You" + ChatColor.WHITE + ", " + ChatColor.GREEN + "Mad" + ChatColor.WHITE + ", "+ ChatColor.GREEN + "Bro?!?");
    50.  
    51. return true;}
    52.  
    53. public boolean onCommand2(CommandSender sender, Command cmd, String lbl, String[] args) {
    54.  
    55. if(cmd.getName().equalsIgnoreCase("pl") && sender.hasPermission("plugincommand.message.pl")){
    56.  
    57. //use sender instead of player. This will allow your console to execute the command as well.
    58.  
    59. }
    60.  
    61. sender.sendMessage( "" + ChatColor.WHITE + "Plugins (3): " + ChatColor.GREEN + "You" + ChatColor.WHITE + ", " + ChatColor.GREEN + "Mad" + ChatColor.WHITE + ", "+ ChatColor.GREEN + "Bro?!?");
    62.  
    63. return true;
    64.  
    65. }
    66.  
    67. }

    Code:
    name: PluginCommand
    main: com.imyussy.plugincommand.PluginCommand
    version: 1.0
    author: imyussy
    commands:
        plugins:
          description: Sends player the funny plugin message.
          usage: /plugins
          permission: plugincommand.message.plugins
        pl:
          description: Sends player the funny plugin message.
          permission: plugincommand.message.pl
          usage: /pl
    permissions:
      plugincommand.message.use:
        description: Gives access to the funny plugin message.
        children:
          plugincommand.message.pl: true
          plugincommand.message.plugins: true
      plugincommand.message.plugins:
        description: Gives access to the funny plugin message.
        default: not op
      plugincommand.message.pl:
        description: Gives access to the funny plugin message.
        default: not op
    Problems I am encountering:

    1. Everything I coded seems like it didn't do anything because when a deop player tries to type '/plugins' it just tells them the regular message saying they don't have permission even though I setup permission nodes in the java code and in the plugin.yml. And I added them to the groups in GroupManager too.

    2. When a player that is OP or not OP and tries to type '/pl' it just shows that command in the chat. I attached a picture of it below.

    Thank you so much for your cooperation and if you have any questions at all please leave a comment below.

    Thanks again,
    imyussy

    2014-10-14_19.15.09.png
     
  16. Offline

    teej107

    imyussy You need to learn about overridden methods. onCommand1, onCommand2, etc aren't methods recognized by Bukkit. The only reason why onCommand is recognized is because you are actually overriding that method when you create it in your class that implements CommandExecutor (JavaPlugin implements that class). In short, the reason why your code isn't working is because those methods aren't being called.
     
  17. Offline

    imyussy

    teej107 Sorry I only started making bukkit plugins about 3 weeks ago. So if the onCommand 1 and 2 don't work, then what should I change them to
     
  18. Offline

    teej107

  19. Offline

    Watto

    imyussy
    You do all of your command checks in the one onCommand method, basically when you're extending JavaPlugin you're using it's methods, onCommand is one of those methods so when it's called by bukkit, your onCommand method is called instead (Pretty much explained by the @Override) however onCommand1 isn't a method therefore it isn't being called.

    Hope that cleared it up a little better even though teej107 already explained it :)
     
  20. Offline

    timbragg12

    imyussy If I'm right I think you should be using an PreCommandEvent or AsyncPlayerChatEvent or something like that. I will be able to help you with this later but I'm currently at work. :)
     
  21. Offline

    Monkey_Swag

  22. Offline

    timbragg12

    imyussy
    Code:java
    1. public void onEnable() {
    2. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    3. //No log needed, it auto does it.
    4. }
    5.  
    6. public void onDisable() {
    7.  
    8. }
    9. @EventHandler
    10.  
    11. public void onCmd(PlayerCommandPreprocessEvent e) {
    12.  
    13. if(!e.getPlayer().hasPermission("noplugins.bypass")) {
    14. if(e.getMessage().equalsIgnoreCase("/pl") || e.getMessage().equalsIgnoreCase("/pl ") || e.getMessage().equalsIgnoreCase("/plugins") || e.getMessage().equalsIgnoreCase("/plugins ")) {
    15. e.setCancelled(true);
    16. e.getPlayer().sendMessage( "" + ChatColor.WHITE + "Plugins (3): " + ChatColor.GREEN + "You" + ChatColor.WHITE + ", " + ChatColor.GREEN + "Mad" + ChatColor.WHITE + ", "+ ChatColor.GREEN + "Bro?!?");
    17. }
    18. } else {
    19. }
    20. }
    21. }


    Sorry for delayed response. Forget doing it as a command. Use that ^.
     
  23. Offline

    imyussy

    timbragg12 Thank you so much! But after I replaced the code I has with yours, import the events and it puts a red line under the word "Bukkit" in this line:
    Code:java
    1. Bukkit.getServer().getPluginManager().registerEvents(this, this);

    Im sure which one I should click after i hover over it to see what my options are to fix it. Also, would this still allow the permission nodes to work that I set up in my plugin.yml? Here is what my plugin.yml looks like:
    Code:
    name: PluginCommand
    main: com.imyussy.plugincommand.PluginCommand
    version: 1.0
    author: imyussy
    commands:
        plugins:
          description: Sends player the funny plugin message.
          usage: /plugins
          permission: plugincommand.message.plugins
        pl:
          description: Sends player the funny plugin message.
          permission: plugincommand.message.pl
          usage: /pl
    permissions:
      plugincommand.message.use:
        description: Gives access to the funny plugin message.
        children:
          plugincommand.message.pl: true
          plugincommand.message.plugins: true
      plugincommand.message.plugins:
        description: Gives access to the funny plugin message.
        default: not op
      plugincommand.message.pl:
        description: Gives access to the funny plugin message.
        default: not op
    And if there is anything wrong with my plugin .yml please let me know so I can change that.

    Thank you for your help,
    imyussy
     
  24. Offline

    timbragg12

    imyussy Sorry I didn't reply. I have been away. Do you still need help?

    EDIT: You don't need to add the commands to the plugin.yml.
    EDIT 2: And you don't need the permissions in the plugin.yml either.
     
  25. Offline

    imyussy

    Ok. I took out the commands and permission in the plugin.yml and I also used the java code you told me to use. (Thank you for that again). When I use the java code you told me to use, I get an error for one of the lines. I think this may be a problem on why I get an error in console. The line that gets the error is at the bottom of this message. I also pasted what my plugin.yml looks like right now and what my error from console looks like below too. Thank you again for helping me, imyussy.

    Error from console:
    Code:
    [PluginCommand] Enabling PluginCommand v1.0
    [11:33:36 ERROR]: Error occurred while enabling PluginCommand v1.0 (Is it up to date?)
    java.lang.Error: Unresolved compilation problem:
        The method registerEvents(Listener, Plugin) in the type PluginManager is not applicable for the arguments (CustomPluginCommand, CustomPluginCommand)
     
        at com.imyussy.customplugincommand.CustomPluginCommand.onEnable(CustomPluginCommand.java:18) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[spigot.jar:git-Spigot-1649]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [spigot.jar:git-Spigot-1649]
        at us.Myles.PWP.TransparentListeners.PerWorldPluginLoader.enablePlugin(PerWorldPluginLoader.java:145) [PerWorldPlugins.jar:?]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [spigot.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.n(MinecraftServer.java:360) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.g(MinecraftServer.java:334) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.a(MinecraftServer.java:290) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:210) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:458) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1649]
    Plugin.yml:
    Code:
    name: PluginCommand
    main: com.imyussy.customplugincommand.CustomPluginCommand
    version: 1.0
    author: imyussy
    
    The line that gives me in error in Eclipse:
    Code:java
    1. Bukkit.getServer().getPluginManager().registerEvents(this, this);
     
  26. Offline

    mythbusterma

    imyussy

    That's a very simple fix, and it's quite obvious why that's an error, do you even understand what you're doing when you say "registerEvents?" Try looking at the Bukkit plugin tutorial.
     
  27. Offline

    imyussy

    mythbusterma I actually don't know that much at all about how to write java. Would you be able to tell me why this error is happening? Thank you, imyussy
     
  28. Offline

    mythbusterma

    imyussy

    Because your Main class doesn't implement Listener.
     
  29. Import org.bukkit.Bukkit if this error is still happening.
     
  30. Offline

    es359

    imyussy Please learn java first before attempting to write plugins.
     
    teej107 likes this.
Thread Status:
Not open for further replies.

Share This Page