Solved How to include a command in the main file?

Discussion in 'Plugin Development' started by robertlit, Jul 14, 2019.

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

    robertlit

    Hello, I am just starting with coding with spiggot. I can not understand how to include the command that I have created in onEnable() (in the main file, of course). the only way I could find online that doesn't work anymore is attached, the errors are in Main: line 11 and in HelloCommand: line 15. I will be grateful for any help!
    Code:
    package me.robertlit.helloworld.commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import me.robertlit.helloworld.Main;
    
    public class HelloCommand implements CommandExecutor {
    
        private Main plugin;
    
        public HelloCommand(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("hello").setExecutor(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("You do not have the permission to execute this command!");
                return true;
            }
        
            Player p = (Player) sender;
        
            if (p.hasPermission("hello.use")) {
                p.sendMessage("Hey!");
            } else {
                p.sendMessage("You do not have permission to execute this command!");
            }
            return false;
        }
    
    
    }
    
    Code:
    package me.robertlit.helloworld;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.robertlit.helloworld.commands.HelloCommand;
    
    public class Main extends JavaPlugin {
    
        @Override
        public void onEnable() {
            new HelloCommand(this);
        }
    }
    
     
    Last edited: Jul 14, 2019
  2. Offline

    timtower Administrator Administrator Moderator

    @robertlit And what are the errors? I am just gonna assume and say that you also need to have the command in the plugin.yml
     
  3. Offline

    robertlit

    @timtower
    Errors are at Erroes.png
    Line 15 in HelloCommand is:
    Code:
    plugin.getCommand("hello").setExecutor(this);
    Line 11 in Main is
    Code:
    new HelloCommand(this);
    This is the plugin.yml
    Code:
    name: HelloWorld
    version: 1.8.8
    author: robertlit
    main: me.robertlit.helloworld.Main
    I don't have the command in the plugin.yml file since I thought that this is not necessary. (https://bukkit.gamepedia.com/Plugin_YAML says that this is unnecessary)

    EDIT: I just added the command into my plugin.yml file and it worked! Thank you.
     

    Attached Files:

    Last edited: Jul 14, 2019
  4. Offline

    timtower Administrator Administrator Moderator

    @robertlit That it is not necessary is only if you don't have any commands.
    You do have one, the command hello, so you need to put it in your plugin.yml
     
  5. Offline

    robertlit

    Thanks. solved.
     
Thread Status:
Not open for further replies.

Share This Page