Solved Help! How can i use args in Eclipse?

Discussion in 'Spigot Plugin Development' started by StijnSimons, May 16, 2016.

  1. Right now, my plugin looks like this:
    Code (Text):
    package omega.omegafun;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class main extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("Enabled OmegaCore!");
        }
        @Override
        public void onDisable() {
            getLogger().info("Thanks for using OmegaFun!");

        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("heal")) {
                Player player = (Player) sender;
                sender.sendMessage("Healed");
                int length = args.length;
                if (length == 1) {
                    boolean playerFound = false;
                    for (Player playerToHeal : Bukkit.getServer().getOnlinePlayers()) {
                        if(playerToHeal.getName().equalsIgnoreCase(args [0])) {
                            playerToHeal.setHealth(20.0);
                            player.sendMessage(ChatColor.GREEN + "Player healed!");
                            playerFound = true;
                            break;
                        }
                     
                    }
                    if (playerFound == false) {
                        player.sendMessage(ChatColor.RED + "Could not find player:" + args[0] + "!");
                 
                    }
                 
                } else player.sendMessage(ChatColor.RED + "Incorrect arguments!");
                return true;
            }
            return false;
        }
     
    }
     
    But my arguments don't work.
    Can someone give me an example of an argument that uses '/omega help'?
    Thanks,
    Stijn
     
  2. if args.length == 0 (command is /command)
    if args.length == 1 (command is /comman arg)

    once checking the args.length inside that statement
    if your args.length is 1
    then args[0] would return whatever is in /command <here>
     
  3. Wait can you help me out a litte?
    so, if i have:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("heal")) {
    What comes after it?
     
  4. this was part of my plugin

    Code (Text):
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args)
          {
            Player player = (Player)sender;
           
            if (args.length == 0) {
            if (player.hasPermission("PGPluginPack.Heal")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Healed-Message")));
                player.setFoodLevel(20);
                player.setHealth(20);
            }
           
            } else if (args.length == 1) {
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (player.hasPermission("PGPluginPack.Heal.Others")) {
                if (args[0].equalsIgnoreCase(target.getName())) {
                    sender.sendMessage("§7[§b§lUTILITIES§7] §bYou have healed " + target.getName() + ".");
                    target.setFoodLevel(20);
                    target.setHealth(20);
                    target.sendMessage("§7[§b§lUTILITIES§7] §bYour have been healed by " + sender.getName() + "." );
                }
                }
                }
           
           
           
            if (!player.hasPermission("PGPluginPack.Heal")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message")));
            }
            if (!player.hasPermission("PGPluginPack.Heal.Others")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message")));
            }
            return true;
          }
    }
     
     

  5. If you need to check for anything after /heal, you would need to do

    Code (Text):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("heal")) {
                    if (args.lenth == 1) {
                           if (args[0].equalsIgnoreCase("whatever you want to check here") {
                               }
                       }
                }
     
  6. Thank you so much!
    Only one more question:
    how do i add more?
    Current FULL code:
    Code (Text):
    package omega.omegafun;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("Enabled OmegaCore!");
        }
        @Override
        public void onDisable() {
            getLogger().info("Thanks for using OmegaFun!");

        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("omegafun")) {
                    if (args.length == 1) {
                           if (args[0].equalsIgnoreCase("help")) {
                               sender.sendMessage("Works");
                               }
                       }
                }
            return false;
        }
    }
     
     
  7. Return true


    Sent from my iPod touch using Tapatalk
     
  8. Thanks for the help!
     
  9. Np


    Sent from my iPod touch using Tapatalk
     
  10. The only problem is, when i now do '/omegafun heal', it doesn't work
    Code:
    Code (Text):
    package omega.omegafun;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("Enabled OmegaCore!");
        }
        @Override
        public void onDisable() {
            getLogger().info("Thanks for using OmegaFun!");

        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("omegafun")) {
                    if (args.length == 1) {
                           if (args[0].equalsIgnoreCase("help")) {
                               sender.sendMessage("Works");
                               }
                           
                           
                           
                       }
                    if (args.length == 2) {
                        if (args[0].equalsIgnoreCase("heal")) {
                            sender.sendMessage("Healed!");
                   
                   
                   
                    }
           
           
           
                }
            }
            return true;
        }
    }
     
     
  11. Look at your help section.

    if (args.length == 2) {
    }

    should be:

    if (args.length == 1) {
    }
     
  12. The reason being is as follows.

    Args == 1 Is for when there are 2 words involved

    Args == 2 Is for when there are 3 words involved
     
  13. Oh ok, sorry, i'm a new dev
     
  14. No worries. Everyone has to start somewhere
     
  15. xD, sorry, but one thing, how can i make an error message?
     
  16. For??
     
  17. Like, when you type '/omegafun qsdkjfmlk' or '/omegafun notarealcommand'?
    It has to say this: 'Error: please use '/omegafun help'.
    BTW: ur a god tysm!
     
  18. if .equalsIgnoreCase != "help" {
    player.sendMessage("usage: /command help")
    }
     
  19. Beat me to it xD
     
  20. Two things: 1) args.lenth == 1 is checking for the first arg. Even though Java starts at 0, the length starts at 1.

    As for the command, make sure to register it in your plugin.yml