Solved Having trouble with onCommand

Discussion in 'Plugin Development' started by Kulkinz, Mar 29, 2017.

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

    Kulkinz

    Hello. I have just started with making plugins and using eclipse, and I am having trouble with having more than one command in the main (the one with package, export, and public class) file. I am using the plugin tutorial at http://bukkit.gamepedia.com/Plugin_Tutorial#The_onCommand.28.29_Method and I can't find any help there. What do I do, since I want more than one command to work. Sorry if this is a basic question since I have never done coding like this before.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Kulkinz Do you check the cmd.getName() in the onCommand? If so: just add another if statement.
     
  3. Offline

    Kulkinz

    How would I do that? Also, is it possible to have the command's functions in a different file? I am completely new at this and I am having trouble finding tutorials that help with this stuff.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Kulkinz Yes, splitting is possible, but lets keep it simple.
    Post your code, then it will be easier to explain.
     
  5. Offline

    Kulkinz

    Main file (open)

    package kul.plugin.chatwarnings;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {


    @Override
    public void onEnable() {


    }

    @Override
    public void onDisable() {

    }

    @Override
    public boolean onCommand(CommandSender sender,
    Command command,
    String label,
    String[] args) {
    if (command.getName().equalsIgnoreCase("chatwarnstart")) {
    sender.sendMessage("Chat Warnings is active.");
    return true;
    }
    return false;
    }

    }


    I already have all of the plugin.yml details done. What I am trying to add:
    Second command (open)

    @Override
    public boolean onCommand(CommandSender sender,
    Command command,
    String label,
    String[] args) {
    if (command.getName().equalsIgnoreCase("chatwarnstop")) {
    sender.sendMessage("Chat Warnings has stopped.");
    return true;
    }
    return false;
    }


    No code in it yet, because I don't want to have all the code made, then realize that to get it to work, I need to completely redo it.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Kulkinz You have an if statement in the main.
    Copy and paste that and chamge the command.
     
  7. Offline

    Kulkinz

    So like this:
    Code:
    @Override
    public boolean onCommand(CommandSender sender,
    Command command,
    String label,
    String[] args) {
    if (command.getName().equalsIgnoreCase("chatwarnstart")) {
    sender.sendMessage("Chat Warnings is active.");
    return true;
    }
    return false;
    }
    
    if (command.getName().equalsIgnoreCase("chatwarnstop")) {
    sender.sendMessage("Chat Warnings has stopped.");
    return true;
    }
    return false;
    }
    
    }
    
    or is that wrong?
     
    Last edited by a moderator: Mar 30, 2017
  8. Offline

    timtower Administrator Administrator Moderator

    @Kulkinz Please use code blocks instead of spoilers.
    And that is wrong because the onCommand is closed before the second if statement.
     
  9. Offline

    Kulkinz

    Ok, I think I got it. Thanks!
    Code:
    @Override
        public boolean onCommand(CommandSender sender,
                Command command,
                String label,
                String[] args) {
            if (command.getName().equalsIgnoreCase("chatwarnstart")) {
                sender.sendMessage("Chat Warnings is active."
                        + "Plugin by Kulkinz.");
                return true;
            }
         
            if (command.getName().equalsIgnoreCase("chatwarnstop")) {
                sender.sendMessage("Chat Warnings has stopped.");
                return true;
                }
     
            return false;
        }
    Update: Just tested it myself, and it worked. Thanks for the help @timtower.
     
    Last edited: Mar 31, 2017
  10. Offline

    timtower Administrator Administrator Moderator

    @Kulkinz That is indeed how it should look like ;)
    Marked as solved.
     
Thread Status:
Not open for further replies.

Share This Page