Arguments aren't working

Discussion in 'Plugin Development' started by Ragnarok_, Jan 17, 2017.

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

    Ragnarok_

    Sigh, another; probably simple question that I will feel pretty stupid for asking. Anyway, sorry for me being so dumb as Java, but I', asking for your guys' help once again! My arguments aren't working! So, I have separated my arguments with code blocks, and before I haven't did that, it worked, all except one. I did this because I got an unreachable code error, and this is what I could fall back to. So, how do I fix this? The command works, but the args I placed aren't working at all. If I do /kit help, it would just display the normal message for /kit. Please help, and if you could, tell me how to prevent this!

    Code:
    Code:
    package me.fluddershy;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MaoKits extends JavaPlugin {
       
        @Override
        public void onEnable() {
            getLogger().info("Mao Kits");
           
        }
       
        @Override
        public void onDisable() {
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(cmd.getName().equalsIgnoreCase("kit")) {
               
                Player player = (Player) sender;
               
                player.sendMessage(ChatColor.GREEN + "Invalid usage! Please specify an argument or type /kit help for more info");
                return true;
            }
                   
                if(args[0].equalsIgnoreCase("help")) {
                    Player player = (Player) sender;
                    player.sendMessage(ChatColor.GREEN + "    McArtOnline Kit Information");
                    player.sendMessage(ChatColor.AQUA + " To select a kit, type /kit <kit name>");
                    player.sendMessage(ChatColor.LIGHT_PURPLE + "          Kit Names");
                    player.sendMessage(ChatColor.GOLD + "- Warrior");
                    player.sendMessage(ChatColor.GOLD + "- Archer");
                    player.sendMessage(ChatColor.GOLD + "- Rogue");
                    player.sendMessage(ChatColor.GOLD + "- Paladin");
                    player.sendMessage(ChatColor.GOLD + "- Ranger");
                    player.sendMessage(ChatColor.GOLD + "- Tank");
                    player.sendMessage(ChatColor.GOLD + "- Cleric");
                    player.sendMessage(ChatColor.GOLD + "- Bard");
                    return true;
               
                }
                       
                   
                    if(args[0].equalsIgnoreCase("warrior")) {
                        Player player = (Player) sender;
                        ItemStack[] kitwarrior = {new ItemStack(Material.IRON_SWORD), new ItemStack(Material.BOW), new ItemStack(Material.ARROW, 16)};
                       
                        ItemStack[] warriorarmor = {};
                        warriorarmor[0] = new ItemStack(Material.IRON_HELMET, 1);;
                        warriorarmor[1] = new ItemStack(Material.IRON_CHESTPLATE, 1);
                        warriorarmor[2] = new ItemStack(Material.IRON_LEGGINGS, 1);
                        warriorarmor[3] = new ItemStack(Material.IRON_BOOTS, 1);
                        player.getInventory().setArmorContents(warriorarmor);
                        player.getInventory().addItem(kitwarrior);
                        return true;
                    }
                    return false;
                   
        }
       
    }
     
  2. Offline

    Zombie_Striker

    don't log your own plugins. Bukkit does this for you. remove this line.

    if your onDisabled does nothing, remove it.

    Don't blindly cast. How do you know sender will always be a Player? Do an instanceof check before casting.

    You also do this multiple times. Move the player variable out of the if statements.

    main porblem. You moved your args out of the command check and you forget to check the args length. Move it inside the command check, don't return true in the command check, and check the args length before getting the first arg.
     
  3. Offline

    Ragnarok_

    Okay, I did all of what you said but the last one, them main problem. Can you go into more depth of what I need to do? I removed the return trues, but what do you mean command check? Also, I don't want to check the length, because I just want to test for the name.
    What does work: The /kit command(But theres a internal error but it does work)
    The /kit help works.
    /kit warrior doesn't
     
  4. Offline

    Zombie_Striker

    @Ragnarok_
    This is the command check.

    Then expect a lot of errors and that your command will not always work.
     
Thread Status:
Not open for further replies.

Share This Page