Solved /kill command not working

Discussion in 'Plugin Development' started by BlueNova, Mar 23, 2020.

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

    BlueNova

    I have a problem that i got stuck on since a few hours now.. I am pretty new and had been watching tutorials on YT and reading some of the docs on Bukkit. I was making a /kill (player name) command and i was set to go until i stumbled upon an error.

    ERROR:
    java.lang.NullPointerException: null
    at me.GAMERMANA.EnderPearl.Main.onEnable(Main.java:13) ~[?:?]

    main line 13:
    getCommand(commands.killcmd).setExecutor(commands);

    my "Commands" class:

    Code:
    package me.GAMERMANA.EnderPearl;
    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;
    public class Commands implements Listener,CommandExecutor{   
         public String killcmd = "Kill";
         @Override   
         public boolean onCommand(CommandSender sender,Command cmd,String plrKill,String[] args) {
              if (sender instanceof Player) {
                   if (cmd.getName().equalsIgnoreCase("kill")) {
                   Player plr = ((Player)sender).getServer().getPlayer(plrKill);
                   plr.damage(50);
                   return true;
                }
              }else {return true;}
             return false;
           }
    }

    .yml file:
    Code:
    name: Plugin
    author: GAMERMANA
    version: 0.1
    main: me.GAMERMANA.EnderPearl.Main
    Description: first useless plugin
    
    Commands:
          kill:
              usage:/<command>
              description: lol kill ppl
    i would really appreciate some help thanks alot in advance :)
     
    Last edited by a moderator: Mar 23, 2020
  2. Online

    timtower Administrator Administrator Moderator

    @BlueNova plrKill will be the command that is executed. Not the target player.
    And you can do Bukkit.getServer() as well instead of casting the sender to player.
     
  3. Offline

    BlueNova

    allright i have done the changes you told me and reexported but the error stays the same..
    java.lang.NullPointerException: null
    at me.GAMERMANA.EnderPearl.Main.onEnable(Main.java:13) ~[?:?]
     
  4. Online

    timtower Administrator Administrator Moderator

    The getPlayer call is returning null. It can't find the player.
     
  5. Offline

    BlueNova

    i have not written the command tho. this happens as soon as the plugin loads up

    so it is running the command without me even writing it?
     
  6. Online

    timtower Administrator Administrator Moderator

    Ahh, sorry.
    Capitalization is different in the plugin.yml and the code. Make that the same.
     
  7. Offline

    BlueNova

    at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
    Caused by: org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here
    in 'reader', line 10, column 20:
    description: lol kill ppl
    . ^
    i assume something is wrong with the description i set..
    although i have written in exactly the same way and im sure the tabs are correct

    im sorry if this is very obvious. its my first time working with commands and yml files..


    EDIT: nvm it was the spaces i fixed it.

    the original error stays though
     
    Last edited: Mar 23, 2020
  8. Online

    timtower Administrator Administrator Moderator

    @BlueNova Then please post your plugin.yml again.
     
  9. Offline

    BlueNova

    it was the spacing. i tried googling the error while waiting for a response. the original error stays tho..

    idk if i should just rewrite the whole thing again. problem is im pretty sure copied the code exactly. its probably one wrong letter or smth that i really cant find. Eclipse doesnt show any error lines tho. im very confused..

    Edit: i will post both classes right now and the yml just to clear everything up now..

    Main class:
    Code:
    package me.GAMERMANA.EnderPearl;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.GAMERMANA.Events.BlockInteractEvent;
    import net.md_5.bungee.api.ChatColor;
    
    public class Main extends JavaPlugin{
        private Commands commands = new Commands();
        @Override
        public void onEnable() {
            getCommand(commands.killcmd).setExecutor(commands);
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "The plugin has been activated!");
            getServer().getPluginManager().registerEvents(new BlockInteractEvent(),this);
        }
        public void onDisable(){
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "The plugin has been deactivated!");
        }
    }

    Commands class:
    Code:
    package me.GAMERMANA.EnderPearl;
    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.event.Listener;
    public class Commands implements Listener,CommandExecutor{ 
        public String killcmd = "kill";
        @Override 
        public boolean onCommand(CommandSender sender,Command cmd,String kill,String[] args) {
            if (sender instanceof Player) {
                if (cmd.getName().equalsIgnoreCase("kill")) {
                    Player plr = Bukkit.getServer().getPlayer(args[0]);
                    plr.damage(50);
                    return true;
                }
            }else {return true;}
            return false;
        }
    }
    yml :
    Code:
    name: Plugin
    author: GAMERMANA
    version: 0.1
    main: me.GAMERMANA.EnderPearl.Main
    Description: first useless plugin
    
    Commands:
        kill:
            usage:/<command>
            description:lol kill ppl
     
    Last edited by a moderator: Mar 23, 2020
  10. Online

    timtower Administrator Administrator Moderator

    @BlueNova You are missing spaces after the colons in the plugin.yml
     
  11. Offline

    BlueNova

    i fixed them.. the error is always the same tho.. i dont get it..
     
  12. Online

    timtower Administrator Administrator Moderator

    @BlueNova Then post the new plugin.yml
    Using code blocks please: [code]<code or yml here>[/code]
     
  13. Offline

    BlueNova

    Code:
    name: Plugin
    author: GAMERMANA
    version: 0.1
    main: me.GAMERMANA.EnderPearl.Main
    Description: first useless plugin
    
    Commands:
        kill:
            usage: /<command>
            description: lol kill ppl
    
     
  14. Online

    timtower Administrator Administrator Moderator

    @BlueNova Check the capitalization, it is very important.
    Assume that every node that Bukkit gets is lowercase.
    So "Commands" should be "commands"
     
  15. Offline

    BlueNova

    ...hmm everything is lowercase now.. i really dont know what i did wrong. maybe one reason is because i dont understand the whole thing fully..

    i think ill just leave this project since i might not be good enough yet.. :(

    thanks for the help tho.. really appreciate it :)
     
  16. Online

    timtower Administrator Administrator Moderator

    @BlueNova It is not the same error probably.
    Can't help without the logs.
     
  17. Offline

    BlueNova

    I fixed it with a little help from oracle java tutorials!

    turns out i thought the event method name had to be exactly as it is as i saw in the tutorial. which referred to some other variable i had and not acting as the name of the method. Thanks for the help :)
     
Thread Status:
Not open for further replies.

Share This Page