onCommand always return false

Discussion in 'Spigot Plugin Development' started by SickSkillz, May 27, 2016.

  1. I'm coding some commands for my LuckyBlock plugin but the commands always return false

    this is my code:
    Code (Text):
    package me.Ghostrider.SuperLuckyBlock;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.SkullMeta;

    public class SLBCommandExecutor implements CommandExecutor {
         
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("slb")) {
            sender.sendMessage(ChatColor.YELLOW + "[SLB]" + ChatColor.WHITE + "Use /slb help for the commands");
            return true;
            }
            if (args[0].equalsIgnoreCase("help"))
            if (sender.hasPermission("SuperLuckyBlock.help")){
                sender.sendMessage(ChatColor.YELLOW + "===================================");
                sender.sendMessage(ChatColor.YELLOW + "SuperLuckyBlock help");
                sender.sendMessage(ChatColor.YELLOW + "/slb give - gives a lucky block");
                sender.sendMessage(ChatColor.YELLOW + "===================================");
            return true;
            }
                else if (args[0].equalsIgnoreCase("give"))
                if (sender.hasPermission("SuperLuckyBlock.give"))
                {
                    Player player = (Player) sender;
                    PlayerInventory inventory = player.getInventory();
                    ItemStack lb = new ItemStack(Material.SKULL_ITEM, 1 , (byte)3);
                    SkullMeta  meta = (SkullMeta) Bukkit.getItemFactory().getItemMeta(Material.SKULL_ITEM);
                   
                    meta.setDisplayName(ChatColor.GOLD + "Lucky Block");
                    meta.setOwner("Luck");
                    lb.setItemMeta(meta);
                    inventory.addItem(lb);
                    sender.sendMessage(ChatColor.YELLOW + ("[SLB]" + ChatColor.WHITE + "Gave you a LuckyBlock!"));
                return true;
                }
            sender.sendMessage(ChatColor.YELLOW + "[SLB]" + ChatColor.RED + "Unknown command! use /slb help for more help!");
            return false;
            }
        }
     
  2. Just a tip: any code you put after a return statement (eg return true; ) will not be run. Your above code will run until the first check (eg, if the command is "slb") and since you have a return true statement inside that check, nothing else will happen.
     
  3. Okay thanks for your help. I'll check if it's working after I'm back and have enough time to edit it.
     
  4. You need to also have brackets on your else if statement
     
  5. I already noticed that
     
  6. Still doesn't work

    code:
    Code (Text):

    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("slb")) {
            sender.sendMessage(ChatColor.YELLOW + "[SLB]" + ChatColor.WHITE + "Use /slb help for the commands");
            }
            if (args[0].equalsIgnoreCase("help"))
            if (sender.hasPermission("SuperLuckyBlock.help")){
                sender.sendMessage(ChatColor.YELLOW + "===================================");
                sender.sendMessage(ChatColor.YELLOW + "SuperLuckyBlock help");
                sender.sendMessage(ChatColor.YELLOW + "/slb give - gives a lucky block");
                sender.sendMessage(ChatColor.YELLOW + "===================================");
            }
               else if (args[0].equalsIgnoreCase("give"))
               if (sender.hasPermission("SuperLuckyBlock.give")) {
                  Player player = (Player) sender;
                  PlayerInventory inventory = player.getInventory();
                  ItemStack lb = new ItemStack(Material.SKULL_ITEM, 1 , (byte)3);
                  SkullMeta  meta = (SkullMeta) Bukkit.getItemFactory().getItemMeta(Material.SKULL_ITEM);
                 
                  meta.setDisplayName(ChatColor.GOLD + "Lucky Block");
                  meta.setOwner("Luck");
                  lb.setItemMeta(meta);
                  inventory.addItem(lb);
                  sender.sendMessage(ChatColor.YELLOW + ("[SLB]" + ChatColor.WHITE + "Gave you a LuckyBlock!"));
               return true;
               }
            sender.sendMessage(ChatColor.YELLOW + "[SLB]" + ChatColor.RED + "Unknown command! use /slb help for more help!");
            return false;
            }
        }
     
     
  7. My advice is to put return true; instead of return false;, that's the easiest way.

    Otherwise you have to put a return true; befor every end of a if-condition, to prevent reaching the return false; in the last lines. For example you have to place a return true; on line 13 (in the code you have posted).
     
  8. That last one doesn't work :/
    I didn't try the first(doing soon)
     
  9. I would strongly advise against using if/else logic without braces. You can do this in Java, but it makes your code harder to read and harder to trust. I would suggest adding braces after if (args[0].equalsIgnoreCase("help")) and else if (args[0].equalsIgnoreCase("give")) and enclosing the appropriate code within the braces.

    If that does not fix your problem, I would suggest printing out each important step in the process (e.g. sender.hasPermission("SuperLuckyBlock.help")) to make sure they are what you expect them to be. You can isolate the source of the problem this way. It may have to do with the way you are assigning permissions.
     
  10. Thanks for this help! Trying soon!
     
  11. I've found an error on startup but I don't get why

    Code (Text):
    [11:29:44] [Server thread/INFO]: [SuperLuckyBlock] Enabling SuperLuckyBlock v1.3.1
    [11:29:44] [Server thread/ERROR]: Error occurred while enabling SuperLuckyBlock v1.3.1 (Is it up to date?)
    java.lang.NullPointerException
        at me.Ghostrider.SuperLuckyBlock.SuperLuckyBlock.onEnable(SuperLuckyBlock.java:45) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at org.bukkit.craftbukkit.v1_9_R2.CraftServer.loadPlugin(CraftServer.java:361) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at org.bukkit.craftbukkit.v1_9_R2.CraftServer.enablePlugins(CraftServer.java:321) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at net.minecraft.server.v1_9_R2.MinecraftServer.t(MinecraftServer.java:416) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at net.minecraft.server.v1_9_R2.MinecraftServer.l(MinecraftServer.java:381) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at net.minecraft.server.v1_9_R2.MinecraftServer.a(MinecraftServer.java:336) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at net.minecraft.server.v1_9_R2.DedicatedServer.init(DedicatedServer.java:268) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:532) [spigot_server.jar:git-Spigot-4af49dc-4e19de1]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
     
     
  12. NullPointerException on line 45 of SuperLuckyBlock class.

    Pretty easy right? Try to debug the objects to figure out what is null.