Solved Args.length not working..

Discussion in 'Plugin Development' started by Lefay, Dec 19, 2020.

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

    Lefay

    Hello, I made a manhunt plugin which is all working except for args.length != 1 as you can see in the code. Please help me, because I have tried everything I could.

    Code:
    package me.Lefay.manhuntcommands;
    
    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.meta.ItemMeta;
    
    import me.Lefay.Manhunt.Main;
    
    import org.bukkit.ChatColor;
    
    
    
    
    public class ManHuntcmds implements CommandExecutor {
       
        private Main plugin;
       
        public ManHuntcmds(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("manhunt").setExecutor(this);
        }
       
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            ItemStack item = new ItemStack(Material.COMPASS, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.GREEN + "Player Tracker");
            item.setItemMeta(meta);
           
           
           
            Player p = (Player) sender;
            Player target = p.getServer().getPlayer(args[0]);
           
            if(args.length != 1) {
                sender.sendMessage(ChatColor.RED + "Usage: /manhunt <player>");
           
            } if(target == null) {
                sender.sendMessage(ChatColor.RED + "No player online named " + args[0] + "!");
           
            } else {
               
               
            sender.sendMessage(ChatColor.GREEN + "" + args[0] + " is getting hunted...");
           
           
            p.getInventory().addItem(item);
            p.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "You have received a hunting compass!");
           
           
            return false;
    }
            return false;
    
           
        }
    }
    
    It says "An error occured while attempting to perform this command" when ever I type /manhunt

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 20, 2020
  2. Offline

    timtower Administrator Administrator Moderator

    @Lefay Probably because you are using the argument before you check if it is there.
     
  3. Offline

    Strahan

    Additionally, you should check if sender is a Player before casting it. Also the default quantity of a new ItemStack is one, so doing new ItemStack(Material.COMPASS, 1); isn't necessary; you can just do new ItemStack(Material.COMPASS);

    Also don't forget to return in an error condition; even if you didn't crash at the first use of args[0] it would display the usage message, then it would continue and crash on the getting hunted message because you never returned.

    You also do not need to add that empty string between green and args[0].
     
    Lefay likes this.
  4. Offline

    Lefay

    This helped me. My code is now working, thanks a lot!
     
Thread Status:
Not open for further replies.

Share This Page