Solved Command not registering.

Discussion in 'Plugin Development' started by N1ghthauq, Jul 4, 2018.

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

    N1ghthauq

    I'm trying to make a points system using commands, but my command is not registering.
    Here is my main class, which has the command.
    Code:
    package com.MagesFolly.testplugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class GMCommands extends JavaPlugin {
    
        @Override
        public void onEnable() {
            getLogger().info("~~~~~~~~~~~~~~~~~~~~~~~~~~");
            getLogger().info("I Have Been Born!");
            getLogger().info("~~~~~~~~~~~~~~~~~~~~~~~~~~");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args, int[] args1) {
            if (cmd.getName().equalsIgnoreCase("gmpointsup"+args[0]+args1[0])) {
                if (sender instanceof Player) {
                    Player target = (Bukkit.getServer().getPlayer(args[0]));
                    if (target == null) {
                        sender.sendMessage(args[0] + " is not online!");
                    }
                    if (target != null) {
                        sender.sendMessage("&4You have given"+target+args1+"points!");
                    }
                    return true;
            } else {
                sender.sendMessage("&4You must be a player to use that command!");
                return true;
                }
            }
        return false;
        }
        @Override
        public void onDisable() {
            getLogger().info("am dead now!:(");
        }
    }
    Here is my plugin.yml just in case the problem is in there as well.
    Code:
    name: GMCommands
    main: com.MagesFolly.testplugin.GMCommands
    version: 0.0.1-SNAPSHOT
    author: N1ghthauq
    commands:
    gmpointsup:
    usage: /gmpointsup [player] [increment]
    Sorry if my code is a little confusing without any comments, but I was the only dev on this plugin, even though that is not a good excuse it is at least a reason.
     
  2. Offline

    Zombie_Striker

    @N1ghthauq
    1. cmd.getName() returns the name of the command, not the full message sent. In this case, it will only return "gmpointsup"
    2. The paramaters for the onCommand must Only contain a CommandSender, Command, String, and String[]. Changing, adding, or removing objects will break the method, making sure it will never be called.
    3. The player does not need to provide any args. If they don't, the array will be empty, and as such, getting the first arg (arg[0]) will throw an error. Make sure there the arg exists by checking the args.length before getting the arg.
     
  3. Offline

    Tabuu_

    Don't feel to bad about not commenting your code. I only comment my code in group projects and APIs. If I cannot understand my own code within the first 30 seconds of looking at it it's probably no good.

    Anyways a class that contains a command executor method should implement the CommandExecutor interface. Also in the onEnable method you should register the command.

    If you read this post you will probably understand how it works.
     
  4. Online

    timtower Administrator Administrator Moderator

    JavaPlugin does implement it already.
    Registering in the onEnable is only needed when it isn't the main class.
     
  5. Offline

    Tabuu_

    I think I made this mistake before on a thread... Feels bad.
    In that case @Zombie_Striker has provided enough information for you to fix it.
     
    timtower likes this.
  6. Offline

    N1ghthauq

    Okay I think I have done what everyone has told me to, but it still says there are no commands when I do /help.
    Code:
    package com.MagesFolly.testplugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class GMCommands extends JavaPlugin implements CommandExecutor {
    
        @Override
        public void onEnable() {
            getLogger().info("~~~~~~~~~~~~~~~~~~~~~~~~~~");
            getLogger().info("I Have Been Born!");
            getLogger().info("~~~~~~~~~~~~~~~~~~~~~~~~~~");
            this.getCommand("gmpointsup").setExecutor(this);
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("gmpointsup")) {
                if (sender instanceof Player) {
                    int length = args.length;
                    if (length == 2) {
                    Player target = (Bukkit.getServer().getPlayer(args[0]));
                    if (target == null) {
                        sender.sendMessage(args[0] + " is not online!");
                    }
                    if (target != null) {
                        sender.sendMessage("You have given"+target+args[1]+"points!");
                    } else {
                        sender.sendMessage("You have an incorrect amount of arguments. /gmpointsup [player] [points]");
                    }
                    }
                    return true;
            } else {
                sender.sendMessage("You must be a player to use that command!");
                return true;
                }
            }
        return false;
        }
        @Override
        public void onDisable() {
            getLogger().info("am dead now!:(");
        }
    }
    Code:
    name: GMCommands
    main: com.MagesFolly.testplugin.GMCommands
    version: 0.0.1-SNAPSHOT
    author: N1ghthauq
    commands:
    gmpointsup:
    usage: /gmpointsup [player] [increment]
    
    I want to thank you all for trying to help.
     
  7. Online

    timtower Administrator Administrator Moderator

  8. Offline

    N1ghthauq

    https://pastebin.com/cjX2TGn2
    That is the server log... at least what I could get of it, it asks in the log at line 380, "Error occurred while enabling GMCommands v0.0.1-SNAPSHOT (Is it up to date?)."
    What is that supposed to mean. There were no indents before, but now there is a 2 space indent.
    Code:
    name: GMCommands
    main: com.MagesFolly.testplugin.GMCommands
    version: 0.0.1-SNAPSHOT
    author: N1ghthauq
    commands:
      gmpointsup:
        usage: /gmpointsup [player] [increment]
     
    Last edited: Jul 9, 2018
  9. Offline

    KarimAKL

  10. Offline

    N1ghthauq

    @KarimAKL Thanks for showing me that, I figured out that I had one of the variables wrong in the onCommand() method. Command command. it cant be Command cmd.
    Thank you all for trying to help.
     
  11. Online

    timtower Administrator Administrator Moderator

    @N1ghthauq Names are not important though, so that probably wasn't the issue.
     
  12. Offline

    KarimAKL

Thread Status:
Not open for further replies.

Share This Page