Search code examples
javabukkit

Minecraft Spigot Command Not Enabling in Main Class


My Command (Specifically only my cmd2 command) doesn't register, and the console displays an error when I start the server. The other command, cmd1, works, but cmd2 doesn't. I'm really not sure why, so I came here for help.

Some of my Main Class:

package me.Vamp.Test;

import me.Vamp.Test.Events.EventsClass;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

    private Commands commands = new Commands();

    @Override
    public void onEnable() {
        /* Enabler */
        getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nTest Plugin has been enabled.\n\n");
        /* Events Register */
        getServer().getPluginManager().registerEvents(new EventsClass(), this);
        /* Commands Register */
        getCommand(commands.cmd1).setExecutor(commands);
        getCommand(commands.cmd2).setExecutor(commands);
    }
}

The following Class (Commands) only show for the errored command (cmd2). If the code for cmd1 is needed, I will show it. Some of My Command Class:

package me.Vamp.Test;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;

public class Commands implements Listener, CommandExecutor {

    public String cmd2 = "getpickaxe";

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {

            /*
            /getPickaxe Command
            */

            if (cmd.getName().equalsIgnoreCase(cmd2)) {
                Player player = (Player) sender;
                if (args.length == 0) {
                    commandGetPickaxe(player);
                    return true;
                } else {
                    player.sendMessage(Colors.chat("&c&lERROR &cToo many arguments&8."));
                    return true;
                }
            }

        } else {

            sender.sendMessage(Colors.chat("&c&lERROR &cOnly players can use this command&8."));

            return true;
        }
        return false;
    }

    public void commandGetPickaxe(Player player){
        Inventory inv = player.getInventory();
        ItemStack item = new ItemStack(Material.WOOD_PICKAXE, 1);
        ItemMeta meta = item.getItemMeta();
        ArrayList<String> lore = new ArrayList<String>();

        meta.setDisplayName(Colors.chat("&3Wooden Pickaxe"));
        lore.add(Colors.chat("&7&oThe Starter Pickaxe&8&o."));
        meta.setLore(lore);
        item.setItemMeta(meta);

        inv.addItem(new ItemStack(item));
        player.sendMessage(Colors.chat("&8&l» &3You have received a Wooden Pickaxe&8."));
    }
}

This is only the display error on my console.

My Console:

Console


Solution

  • Can I suggest you add a multitude of print statements to see what is null?

    /* Commands Register */
    System.out.println("cmd1 " + commands.cmd1);
    System.out.println("cmd2 " + commands.cmd2);
    System.out.println("cmdObj " + commands);
    getCommand(commands.cmd1).setExecutor(commands);
    getCommand(commands.cmd2).setExecutor(commands);
    

    EDIT 1: It seems as though you are missing the command in your plugin.yml? It's possible that it is a typo, look carefully. If you think everything is perfectly fine, and the error still occurs, please edit you original post and include the plugin.yml file. Thanks!