Commands are not executing??

Discussion in 'Plugin Development' started by FlareLine, Apr 27, 2013.

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

    FlareLine

    Hello all,
    first up, I would like to announce that I have some knowledge of Java/Plugin Development.
    I recently developed my first plugin, consisting of:
    • onEnable
    • onDisable
    And one command that sent "Hello!" to the player when they typed /hello
    The problem became when I tried to rewrite this code into a new project (on Eclipse).
    The plugin enables and disables just fine, but when commands are executed nothing happens...
    All that appears is the 'usage:' (/hello) tag in the plugin.yml file.
    I thought this may be a problem when I downgraded to compiling in Java6, but I have then since updated again and the problem still exists.
    If you need any of my Source Code I'll be happy to reply with it.
    Any help is appreciated. FlareLine :)
     
  2. Offline

    chasechocolate

    What is your onCommand method?
     
  3. Offline

    FlareLine

    Code:
        public boolean cmdcv(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("store")) {
                sender.sendMessage("Hello!");
                sender.sendMessage(Color.RED + "=====" + Color.YELLOW + "LogStore" + Color.RED + "=====");
                sender.sendMessage(Color.MAGENTA + "/store" + Color.DARK_GRAY + " - " + Color.PINK + "Display this message.");
                sender.sendMessage(Color.MAGENTA + "/store <name>" + Color.DARK_GRAY + " - " + Color.PINK + "Store an entry for 'name'.");
                sender.sendMessage(Color.MAGENTA + "/store <name> <#>" + Color.DARK_GRAY + " - " + Color.PINK + "Store # entries for 'name'.");
            }
            return false;
                }
    I had a thought that the color names may have been the root of the problem as they were first the updated ones like 'pink' and 'darkgray' etc.
     
  4. Offline

    chasechocolate

    FlareLine add "return true;" after you send the messages.
     
  5. Offline

    FlareLine

    Grrr, stupid invalidPlugin errors...
    Give me 5 minutes to test :p

    chasechocolate
    Thanks for the help, but I'm not quite sure if I took your information in the correct way.
    You mean to add "return true;" after the Messages within the if{} statement?
    To clarfiy, here's the code again:
    Code:
        public boolean cmdcv(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("store")) {
                sender.sendMessage("Hello!");
                sender.sendMessage(Color.RED + "=====" + Color.YELLOW + "LogStore" + Color.RED + "=====");
                sender.sendMessage(Color.MAGENTA + "/store" + Color.DARK_GRAY + " - " + Color.PINK + "Display this message.");
                sender.sendMessage(Color.MAGENTA + "/store <name>" + Color.DARK_GRAY + " - " + Color.PINK + "Store an entry for 'name'.");
                sender.sendMessage(Color.MAGENTA + "/store <name> <#>" + Color.DARK_GRAY + " - " + Color.PINK + "Store # entries for 'name'.");
                return true;
            }
            return false;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. Offline

    chasechocolate

    FlareLine yes, that should fix the problem.
     
  7. Offline

    FlareLine

    chasechocolate Still the same error... :confused:
    I'm sure I'm declaring my command correctly in the plugin.yml...
    Code:
    name: LogStore
    main: user.flareline.logstore.LogStore
    version: 1.12
    commands:
        store:
            usage: /store
            description: LogStore Help Menu
            permission: logstore.help
            permissions-message: You don't have Permission. 'logstore.help'
    Do I need to declare the onCommand method before onDisable?

    I created a new function in the onCommand method:
    Code:
    getLogger().info("Store Command Activiated");
    
    Which is called before sender.sendMessage.
    But this is not appearing on the Console, which means onCommand is not being called?

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

    chasechocolate

    FlareLine Where is the onCommand method?
     
  9. Offline

    FlareLine

    Now it's between onEnable and onDisable...
    Here's the full class file :p:
    Code:
    //
    //            DON'T FORGET TO EDIT THE PLUGIN.YML TO THE CURRENT BUILD!!!!!!!!!
    //
     
    package user.flareline.logstore;
     
    import java.awt.Color;
    import java.io.*;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class LogStore extends JavaPlugin {
       
        public void onEnable() {
           
            getLogger().info("onEnable has been invoked!");
            // Directory stuffs
            File dir = new File("plugins/LogStore");
            if (dir.exists()){
                getLogger().info("LogStore folder already exists!");
            }
            else {
                dir.mkdir();   
                getLogger().info("LogStore folder created!");
            }
        }
        public boolean cmdcv(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("store")) {
                getLogger().info("Store Command Activiated!");
                sender.sendMessage("Hello!");
                sender.sendMessage(Color.RED + "=====" + Color.YELLOW + "LogStore" + Color.RED + "=====");
                sender.sendMessage(Color.MAGENTA + "/store" + Color.DARK_GRAY + " - " + Color.PINK + "Display this message.");
                sender.sendMessage(Color.MAGENTA + "/store <name>" + Color.DARK_GRAY + " - " + Color.PINK + "Store an entry for 'name'.");
                sender.sendMessage(Color.MAGENTA + "/store <name> <#>" + Color.DARK_GRAY + " - " + Color.PINK + "Store # entries for 'name'.");
                return true;
            }
            return false;
        }
       
        public void onDisable() {
           
            getLogger().info("onDisable has been invoked!");
        }
    }
    
    I know, they may be some things I can clean up, but I'm not worried about that yet.
     
  10. Offline

    chasechocolate

    FlareLine Try changing the name of the method to onCommand.
     
    FlareLine likes this.
  11. Offline

    MadArkael

    nothing is happening because you probably havent set the executor class in your main class onEnable().
    Code:
    public void onEnable(){
     
        getCommand("store").setExecutor(this);
    //only set the executor to 'this' if you have the onCommand boolean in your main class that extends JavaPlugin
    //if you have the onCommand in another class use below.
     
        getCommand("store").setExecutor(new AnotherClass(this));
    }
     
    public class AnotherClass implements CommandExecutor{
     
        private MainClass plugin;
     
        public AnotherClass(MainClass plugin){
     
            this.plugin = plugin;
        }
     
        onCommand(...
    }
    
    Edit: You replied while i was typing that up, put the first line of code I wrote into your on enable. getCommand("store").setExecutor(this);

    Also do what chase is saying about changing it to onCommand. It's overriding a super method, so name it correctly
     
  12. Offline

    FlareLine

    The plugins don't need to implement CommandExecutor anymore?
    Small plugins with only a main classfile don't need to implement CommandExecutor?
    chasechocolate Do they?

    Btw Thanks to you Chase for fixing the problem :)
     
  13. Offline

    MadArkael

    Code:
    //
    //            DON'T FORGET TO EDIT THE PLUGIN.YML TO THE CURRENT BUILD!!!!!!!!!
    //
     
    package user.flareline.logstore;
     
    import java.awt.Color;
    import java.io.*;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class LogStore extends JavaPlugin implements CommandExecutor{
     
        public void onEnable() {
       
            getCommmand("store").setExecutor(this);
            getLogger().info("onEnable has been invoked!");
            // Directory stuffs
            File dir = new File("plugins/LogStore");
            if (dir.exists()){
                getLogger().info("LogStore folder already exists!");
            }
            else {
                dir.mkdir();
                getLogger().info("LogStore folder created!");
            }
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("store")) {
                getLogger().info("Store Command Activiated!");
                sender.sendMessage("Hello!");
                sender.sendMessage(Color.RED + "=====" + Color.YELLOW + "LogStore" + Color.RED + "=====");
                sender.sendMessage(Color.MAGENTA + "/store" + Color.DARK_GRAY + " - " + Color.PINK + "Display this message.");
                sender.sendMessage(Color.MAGENTA + "/store <name>" + Color.DARK_GRAY + " - " + Color.PINK + "Store an entry for 'name'.");
                sender.sendMessage(Color.MAGENTA + "/store <name> <#>" + Color.DARK_GRAY + " - " + Color.PINK + "Store # entries for 'name'.");
                return true;
            }
            return false;
        }
     
        public void onDisable() {
       
            getLogger().info("onDisable has been invoked!");
        }
    }
    You have to tell bukkit whats executing the command, in this case its your main class file.

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

    FlareLine

    Disreguard that complaint
    Colors are now working...
    Thanks to both of you! :)
     
Thread Status:
Not open for further replies.

Share This Page