user10017436
user10017436

Reputation:

onCommand() setup correctly but doesn't get executed at all

My problem is that I have a simple Minecraft plugin where I just want it to executed one command. I've done commands before so the steps where pretty clear to me. My plugin.yml is setup correctly (the server detects the command I've added and displays help pages for it etc.) and the onCommand() function is also setup the same way as I did in all my other plugins. The plugin itself works (mainly the onEnable() function I tested) however onCommand() just doesn't get called.

I've allready tried different plugin.yml formattings aswell as adding the @Override annotation to onCommand() which Eclipse didn't really want me to do. Also I know that my API I use (com.PluginBase) works from working with it in other projects. There are no exceptions produced when executing commands, in the chat it just shows what command I entered.

This is Main.java:

package org.Professions;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.material.Command;
import org.bukkit.plugin.java.JavaPlugin;

import com.PluginBase.Chat;

public class Main extends JavaPlugin {

    public void onEnable() {
        Chat.getInstance().sendErrorMessageToConsole("Professions enabled");
        Bukkit.getPluginCommand("profession").setExecutor((CommandExecutor) this);
    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        Chat.getInstance().sendErrorMessageToConsole("Got the command: " + label);

        /*
         * Check if command sender is a player
         * and if the command is for this plugin
         */
        if
        (
                sender instanceof Player == false
                || !label.toLowerCase().equals("profession")
        ) {
            return true;
        }

        // Get the player who send the command
        Player player = (Player) sender;

        // Check if the player has given the right amount of arguments
        if (args.length != 1) {

            // Notify the player of invalid argument use
            Chat.getInstance().sendMessageToPlayer(player, ChatColor.RED + "Invalid arguments. Usage: /profession <name>");

            // Stop executing code after we've determined an incorrect amount of arguments
            return true;
        }

        // Get the players new profession from the first argument he gave for the command
        String profession = args[1];

        // Set the players name in the playerlist to feature his professions
        player.setPlayerListName
        (
                ChatColor.GREEN + "[" + profession + "] "   // the players' profession
                + ChatColor.WHITE + player.getName()        // the actual player name
        );

        // Always return true since if the command wasn't for this plugin we return false earlier
        return true;
    }
}

This is my plugin.yml:

name: Professions
main: org.Professions.Main
version: 1.0
api-version: 1.13
depend: [PluginBase]
commands:
    profession:
        description: Change your profession
        usage: /<command>
        aliases: [p]

Upvotes: 0

Views: 1894

Answers (3)

montlikadani
montlikadani

Reputation: 41

Instead of this command registration:

Bukkit.getPluginCommand("profession").setExecutor((CommandExecutor) this);

use:

getCommand("profession").setExecutor(this);

You don't need to cast CommandExecutor with your main class, because it is already inherited in JavaPlugin.

getCommand() is included in the JavaPlugin abstract class, so it is unnecessary to start with Bukkit.getPluginCommand(). It is only used in other resources, if you want to get a command as Command object.

Also you can check in the onCommand method to what command performed by a player. This is only needed if you're working with multiple commands in one class.

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("profession")) {
        // code here
    }
}

Upvotes: 0

Skwead
Skwead

Reputation: 41

You forgot to implement CommandExecutor and to register the command.

Your code should look like this:

public class Main extends JavaPlugin implements CommandExecutor{

    @Override
    public void onEnable(){
    //...
    getCommand("profession").setExecutor(this);
    }
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

//...
    }
}

Also, I want to give you some advice regarding minecraft plugin coding. You should not make commands in the main class, they should belong in a class of their own. For instance, your profession command would go in a ProfessionCmd class with all it's arguments (for instance, /profession give would be there too).

Upvotes: 2

bsaverino
bsaverino

Reputation: 1285

Is this the cause of your issue?

// Get the players new profession from the first argument he gave for the command
String profession = args[1];    // <-- [0] ?

Upvotes: 0

Related Questions