Solved I need help with args and cant find whats wrong with this code

Discussion in 'Spigot Plugin Development' started by Rhysbv, Jun 15, 2016.

  1. Hi im learning plugin development, I know java. Im trying to make a simple toggle command with args ect but I cant figure out whats wrong with this code.
    Help would be much appreciated :)
    Code (Text):
    package com.internationalrp.admin.commands;

    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    public class Admin implements CommandExecutor{
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a player to use this command!");
                return true;
            }
         
            if (cmd.getName().equalsIgnoreCase("admin")){
             
            Player player = (Player) sender;
            if (args.length == 0) {
                sender.sendMessage(ChatColor.AQUA + "Command usage:/admin [on/off]");
                return true;
            }
            if (args[0].equalsIgnoreCase("true")) {
                sender.sendMessage(ChatColor.AQUA + "Entering admin mode.");
                sender.getServer().broadcastMessage(ChatColor.RED + sender.getName() + ChatColor.DARK_BLUE + "Is on duty");
                player.setGameMode(GameMode.CREATIVE);
                player.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, 200, 1), true);
                player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 10), true);
             
            }
            if (args[0].equalsIgnoreCase("false")) {
                sender.sendMessage(ChatColor.AQUA + "Exiting admin mode.");
                player.setGameMode(GameMode.SURVIVAL);
                player.removePotionEffect(PotionEffectType.GLOWING);
                player.removePotionEffect(PotionEffectType.JUMP);
             
            }
            }
         
         
            return true;
        }

    Thank you for everyone's help as you can see im quite new and am trying different things in spigot. im happy to be with such a friendly and helpful community :)

    Also thankyou for helping me improve my code. I will use these techniques in the future
     
    #1 Rhysbv, Jun 15, 2016
    Last edited: Jun 15, 2016
  2. You need to tell us specifically what isn't working about it otherwise we can't help you :p Is it throwing an error, or just not doing what you want?
     
    • Agree Agree x 1
  3. You need to register your CommandExecutor class with Bukkit.getCommand("admin").setExecutor(CommandExecutor).
    You need to add the "admin" command to your plugin.yml (or else Bukkit.getCommand("admin") will throw an error).
    Additionally, you don't need to check cmd.getName() if your CommandExecutor class is only registered to handle "admin."
     
  4. Yeah. You should at least tell us whats wrong, give us some errors in console, errors in game, etc.
    I actually posted like 2 threads on how to fix command arguments when I didn't understand them either (which you probably could've found) but i'd be happy to help you with them. Here's what I think's wrong with your code:

    1. I don't think this is how you do toggle commands. Think about it; If I type in
    /admin -- Wait, let me stop here. The command usage is apparently /admin [on/off] but your args are true/false?
    If I type /admin true, it might work. But what if I type it again?
    /admin true. It'll probably let me. I'm really interested in learning toggle commands, and I think they'll probably have something to do with HashMaps or ArrayLists. I'm not the person to ask though

    2. Above (cmd.getName().equalsIgnoreCase("admin")) {
    Ohh. I think I see what you did wrong. You have if(args.length == 0) {
    inside. Put that above when you get the command.

    3. Above if(args[0].equalsIgnoreCase("true")) {
    Now you put if(args.length == 1) { above that.

    4. Player player = (Player) sender;
    Why is this inside the command? Why did you use sender. for sending messages and not player? This code is really confusing.

    5. Learn Java/Bukkit/Spigot before attempting it. It helps.

    EDIT: Just saw the post above, he's right as well.
     
  5. MiniDigger

    Supporter

    while we wait for you to respond whats actually wrong a have a minor improvement for you:
    you should not do
    Code (Text):
    if(subcommand1){
    dostuff
    }
    if(subcommand2){
    dostuff
    }
    but
    if(subcommand1){
    dostuff
    }else if(subcommand2){
    dostuff
    }[/CODE]
    this is a optimization. no need to check for the second sub command if the first one already matched.
     
    • Agree Agree x 2
  6. It seems to be a problem with the args as i can do /admin, however if i add a arg such as /admin on, nothing happens.
    Code (Text):
    if (args[0].equalsIgnoreCase("true")) {
                sender.sendMessage(ChatColor.AQUA + "Entering admin mode.");
                sender.getServer().broadcastMessage(ChatColor.RED + sender.getName() + ChatColor.DARK_BLUE + "Is on duty");
                player.setGameMode(GameMode.CREATIVE);
                player.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, 200, 1), true);
                player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 10), true);
    }
     
  7. ive tried to find a error im the logs it says nothing
     
  8. MiniDigger

    Supporter

    because you need to enter /admin true.......
    if you also want to check for on, you need to add that to your condition ^^
     
    • Agree Agree x 1
    • Useful Useful x 1
  9. If you'd like an example, here's a class from my MagicalEnchants plugin (private) that i'm working on. Basically, if you execute /enchantinfo <enchant> it tells you info about the enchant. (Go figure)
    Code (Text):
    package me.athena222.magicenchants.enchants;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class EnchantInfo implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
     
            Player player = (Player) sender;
            String s = ChatColor.DARK_GRAY + "" + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "-+-----------------------+-";
            char c = ',';
         
            if(args.length == 0) {
            if(cmd.getName().equalsIgnoreCase("enchantinfo")) {
                 player.sendMessage(ChatColor.DARK_AQUA + "Enchants" + ChatColor.DARK_GRAY + " > ");
                 player.sendMessage(ChatColor.DARK_AQUA + "Key" + ChatColor.DARK_GRAY + " > " + ChatColor.GOLD + "Legendary" + ChatColor.WHITE + c + ChatColor.YELLOW + " Ultimate" + ChatColor.WHITE + c + ChatColor.AQUA + " Elite" + ChatColor.WHITE + c + ChatColor.GREEN + " Uncommon" + ChatColor.WHITE + c + ChatColor.GRAY + " Common");
                 player.sendMessage(ChatColor.DARK_AQUA + "Usage" + ChatColor.DARK_GRAY + " > " + ChatColor.AQUA + "/enchantinfo <enchant>");
                 }
            }
            if(args.length == 1) {
                /* Legendary */
                if(args[0].equalsIgnoreCase("inquisitive")) {
                    player.sendMessage(s);
                    player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "     Inquisitive Enchantment");
                    player.sendMessage(s);
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Tier: " + ChatColor.GOLD + "" + ChatColor.BOLD + "Legendary");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Enchant Type: " + ChatColor.DARK_AQUA + "Sword");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Usage: " + ChatColor.DARK_AQUA + "Gives you more EXP upon killing entities");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Max Level: " + ChatColor.DARK_AQUA + "4");
                    player.sendMessage(s);
                }
                else if(args[0].equalsIgnoreCase("gears")) {
                    player.sendMessage(s);
                    player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "     Gears Enchantment");
                    player.sendMessage(s);
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Tier: " + ChatColor.GOLD + "" + ChatColor.BOLD + "Legendary");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Enchant Type: " + ChatColor.DARK_AQUA + "Boots");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Usage: " + ChatColor.DARK_AQUA + "Gives you speed when equipped.");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Max Level: " + ChatColor.DARK_AQUA + "3");
                    player.sendMessage(s);
                }
                else if(args[0].equalsIgnoreCase("featherweight")) {
                    player.sendMessage(s);
                    player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "   Featherweight Enchantment ");
                    player.sendMessage(s);
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Tier: " + ChatColor.GREEN + "Uncommon");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Enchant Type: " + ChatColor.DARK_AQUA + "Sword");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Usage: " + ChatColor.DARK_AQUA + "Gives you haste when hitting entities.");
                    player.sendMessage(ChatColor.GRAY + "» " + ChatColor.AQUA + "Max Level: " + ChatColor.DARK_AQUA + "3");
                    player.sendMessage(s);
                }
                else
                {
                    player.sendMessage(ChatColor.RED + "Unknown enchantment. Check your spelling.");
                }
            }
            return true;  
        }
    }
     
    • Useful Useful x 1
  10. You should also add an additional else statement after checking "on" and "off" so that it tells you "improper usage" instead of saying nothing. Another alternative is to set a usage message in your plugin.yml and return false instead of adding that else statement.
     
    • Useful Useful x 1
  11. omg i feel so stupid lol i was typing in the wrong command and in the wrong plugin.
    This is what happens when u have multiple project open at once.


    Also thank you athena222 this should help me.
     
  12. Lol, it happens man. No problem, if you need help, feel free to keep asking
     
    • Friendly Friendly x 2
  13. thank you
     
    • Like Like x 1
  14. E]

    Don't do else if, just put return; at the end of the if,

    If you would have even more statements you should even use a switch() like this:


    typed in forum so might have a small mistake:

    if (args.length == 1)
    {
    switch(args[0].toLowerCase)
    {
    case "true":
    // do your stuff
    break;
    case "false""
    // do your stuff
    break;
    default:
    // if you use player p
    p.sendMessage(ChatColor.RED + "The command " + args[0] + " was not found.")
    }
    return true;
    }
     
  15. MiniDigger

    Supporter

    whats wrong if elseif? adding a return adds one extra line with no extra value ;)
    I agree with you that a switch-case should be used in case of multiple sub command, for only two args/subcommands its not worth it.
     
  16. I don't like it when else is used when it isn't needed.
    in my opinion return; looks much cleaner

    Also:

    Use ctrl + alt + F to auto format your code since the one at the top of this topic isn't aligned properly.