Args on commands help

Discussion in 'Plugin Development' started by nicoxdry, May 7, 2017.

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

    nicoxdry

    Im trying to make a command so when i write /buffs it prints a menu and when i type /buffs give InfusinElixir it will give a player a Potion with a name and give the effects.

    When i write /buffs give InfusionElixir it doesn't give anything and it sends out all of the messages.

    Code:
    package me.nicoxdry.com;
    
    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.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.PotionMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    
    public class Buffs extends JavaPlugin {
          
        @Override
        public void onEnable() {
            getLogger().info("Plugin Enabled.");
        }
      
        @Override
        public void onDisable() {
          
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
            if (cmd.getName().equalsIgnoreCase("buffs")) {
                Player p = (Player) sender;
              
                if(p.hasPermission("corruptbuffs.allow")) {
                  
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8&l&m--------------------"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "     &6[&cCorruptFactions&6]"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', ""));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8» &7/buffs give InfusionElixir"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', ""));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8» &7/buffs give CompactElixir"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', ""));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8&l&m--------------------"));
               
                    }
                if (args.length == 1)
                {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c(!) You might wanted to write /buffs give InfusionElixir"));
                }
                else if (args[0].equalsIgnoreCase("give")) 
                {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c(!) You might wanted to write /buffs give InfusionElixir"));
                }
               
                else if (args[1].equalsIgnoreCase("infusionelixir"))
                {   
                   
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c(!) &7You have used your &a&lInfusion&d&lElixir"));
                    ItemStack item = new ItemStack(Material.POTION, 1);
                    ItemMeta meta = item.getItemMeta();
                    meta.setDisplayName(ChatColor.YELLOW + "&a&lInfusion&d&lElixir");
                    item.setItemMeta(meta);
                    p.getInventory().addItem(item);
                   
                    ItemStack potion = new ItemStack(Material.POTION);
                    PotionMeta meta2 = (PotionMeta) potion.getItemMeta();
                    meta2.addCustomEffect(new PotionEffect(PotionEffectType.SPEED, 4400, 1), true);
                    meta2.addCustomEffect(new PotionEffect(PotionEffectType.SATURATION, 4400, 1), true);
                }
                   
                return true;
                  
                }
              
            return false;
          
        }
    }
    
     
  2. Offline

    Rixterz

    Code:
    else if (args[0].equalsIgnoreCase("give"))
    {
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c(!) You might wanted to write /buffs give InfusionElixir"));
    }
    else if (args[1].equalsIgnoreCase("infusionelixir"))
    {   
    This means it will never check for "infusionelixir" if args[0] is "give"

    You should also check if args.length != 2 instead of == 1, otherwise you're also allowing more than 2 args.

    Edit: the logic is more flawed than I first thought. Here's the logic you have to follow:

    Code:
    boolean success = false;
    
    if there are 2 arguments
    {
        if args[0] is "give" and args[1] is the potion name
        {
            give potion
            success = true
        }
    }
    
    if !success, show error message
     
    Last edited: May 8, 2017
Thread Status:
Not open for further replies.

Share This Page