Not able to execute the command in my custom plugin

Discussion in 'Plugin Development' started by MightyAkash, Mar 3, 2021.

  1. Offline

    MightyAkash

    I am creating a custom plugin, and I want to create a command which will give the player some items. But I am not able to execute the command, it is showing me --- "Unknown command. Type "/help" for help."

    Here are the codes \/

    This is the GiveCommand class file\/
    Code:
    package tk.havenlands.survival.havenlandssurvival.commands;
    
    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.inventory.ItemStack;
    
    public class GiveCommand implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player){
                Player plr = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("kit")){
                    ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                    ItemStack soup = new ItemStack(Material.MUSHROOM_STEM,17);
                    plr.getInventory().addItem(sword,soup);
                    plr.sendMessage("Kit has been added to your inventory!");
                    return true;
                }
            }
            return false;
        }
    }
    
    This is the main file \/
    Code:
    package tk.havenlands.survival.havenlandssurvival;
    
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.plugin.java.JavaPlugin;
    import tk.havenlands.survival.havenlandssurvival.commands.GiveCommand;
    import tk.havenlands.survival.havenlandssurvival.commands.HelloCommand;
    
    public class HavenLandsSurvival extends JavaPlugin {
    
        @Override
        public void onEnable() {
            // Plugin startup logic
            System.out.println("Plugin started!");
            getServer().getPluginManager().registerEvents(new Events(), this);
    
            this.getCommand("hello").setExecutor(new HelloCommand());
            this.getCommand("kit").setExecutor(new GiveCommand());
        }
    
        @Override
        public void onDisable() {
            // Plugin shutdown logic
            System.out.println("Plugin disabled :(");
        }
    }
    
    This is the plugin.yml \/
    Code:
    name: HavenLandsSurvival
    version: 1.0.0
    description: Survival plugin for HavenLands
    main: tk.havenlands.survival.havenlandssurvival.HavenLandsSurvival
    
    commands:
      hello:
        usage: /<command>
        description: Hello command !
    
      kit:
        usage: /<command>
        description: Get the kit to figh
    Please help me out with these.
     
  2. Offline

    Wick

    Any errors in the console?
     
  3. Offline

    MightyAkash

    Yes, it is giving me this error.
    Code:
    java.lang.NullPointerException: Cannot invoke "org.bukkit.command.PluginCommand.setExecutor(org.bukkit.command.CommandExecutor)" because the return value of "tk.havenlands.survival.havenlandssurvival.HavenLandsSurvival.getCommand(String)" is null
            at tk.havenlands.survival.havenlandssurvival.HavenLandsSurvival.onEnable(HavenLandsSurvival.java:14) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:494) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:408) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.reload(CraftServer.java:876) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.Bukkit.reload(Bukkit.java:642) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:758) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:743) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:387) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:356) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1007) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:846) ~[server.jar:git-Spigot-37d799b-3eb7236]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[server.jar:git-Spigot-37d799b-3eb7236]
     
  4. Offline

    KarimAKL

    @MightyAkash The stack trace provided does not seem to match the code provided. Did you change the code before posting this stack trace?
     
  5. Offline

    MightyAkash

    No, I didn't change, I just posted this as it was.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @MightyAkash Did you export to the correct location? Because it seems that your plugin.yml isn't updated.
     
  7. Offline

    MightyAkash

    I have correctly put the path to the main file in plugin.yml. Do I have to put the path to the Commands file also in the plugin.yml?
     
  8. Offline

    timtower Administrator Administrator Moderator

    No, check the exported jar, check if it is in the right place, check if the content is the same as your code.
     
  9. Offline

    MightyAkash

    Actually, finally I found out that I had created one more plugin.yml file except that which is already created by the spigot plugin template. I fixed that and now it is working all good. :)

    Thanks a lot for all of your help and support.
     

Share This Page