Hello, I own a plugin called Legendary that is the remake of very popular server, and I've recently been working on a HUGE update, fixing the commands, cleaning up the coding format, ect. So everything works well, and I'm happy about that but, the one thing I was most eager to learn how to do, was to make a command to give a player items, I've been searching for ages and couldn't find anything related to this, only one post was able to 'help' me. I am a firm believer in looking at other peoples mistakes and using what happened to them, to help me. I seen this guy who said a way to target a player via, command. I was wondering if anyone can help me fix this line of code up. (*I will not release any other parts of it besides this, since the fact that all of the rest of the plugin works*). Here's the code: Code (Text): if (args.length == 3) { if (p.hasPermission("legendary.admin")) { if (args[0].equalsIgnoreCase("greencode")) { if (args[1].equalsIgnoreCase("give")) { String name = args[0]; Player pl = Bukkit.getPlayer(name); if (!pl.isOnline() && args[2].equalsIgnoreCase(name)) { p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " Sorry, " + ChatColor.DARK_RED + args[2] + ChatColor.GREEN + " isn't online"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); } if (pl.isOnline() && args[2].equalsIgnoreCase(name)) { Inventory inventory = p.getInventory(); if (args[1].equalsIgnoreCase("pyroaxe")) { ItemStack axe = new ItemStack(Material.DIAMOND_AXE, 1); axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 5); axe.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 2); ItemMeta meta = axe.getItemMeta(); ArrayList<String> lore = new ArrayList<String>(); meta.setDisplayName(ChatColor.DARK_RED + "Pyro Axe"); lore.add(ChatColor.DARK_RED + "I come from the Iron Hills"); lore.add(ChatColor.GREEN + randomString(10)); meta.setLore(lore); axe.setItemMeta(meta); pl.getInventory().addItem(axe); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " You have given" + name + "a Pyro Axe!"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(" "); } if (args[3].equalsIgnoreCase("lightning")) { ItemStack bow = new ItemStack(Material.BOW); bow.addEnchantment(Enchantment.ARROW_DAMAGE, 5); bow.addEnchantment(Enchantment.ARROW_FIRE, 1); ItemMeta meta = bow.getItemMeta(); ArrayList<String> lore = new ArrayList<String>(); lore.add(ChatColor.DARK_RED + "" + ChatColor.ITALIC + "Lightning"); lore.add(ChatColor.GREEN + randomString(10)); meta.setLore(lore); bow.setItemMeta(meta); pl.getInventory().addItem(bow); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " You have given " + name + " a Lightning Bow!"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(" "); } if (args[3].equalsIgnoreCase("deathbringer")) { ItemStack sword = new ItemStack(Material.DIAMOND_SWORD); sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 5); sword.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 2); ItemMeta meta = sword.getItemMeta(); ArrayList<String> lore = new ArrayList<String>(); meta.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.BOLD + "D" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "e" + ChatColor.DARK_RED + "" + ChatColor.BOLD + "a" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "t" + ChatColor.DARK_RED + "" + ChatColor.BOLD + "h" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "b" + ChatColor.DARK_RED + "" + ChatColor.BOLD + "r" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "i" + ChatColor.DARK_RED + "" + ChatColor.BOLD + "n" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "g" + ChatColor.DARK_RED + "" + ChatColor.BOLD + "e" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "r"); lore.add(ChatColor.DARK_RED + "" + ChatColor.ITALIC + "Deathbringer"); lore.add(ChatColor.DARK_RED + "" + ChatColor.ITALIC + "Lifesteal"); lore.add(ChatColor.GREEN + randomString(10)); meta.setLore(lore); sword.setItemMeta(meta); pl.getInventory().addItem(sword); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " You have given " + name + " a Deathbringer!"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(" "); } if (args[3].equalsIgnoreCase("Apollos")) { ItemStack apollos = new ItemStack(Material.DIAMOND_HELMET, 1); apollos.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 5); apollos.addUnsafeEnchantment(Enchantment.PROTECTION_FIRE, 4); apollos.addUnsafeEnchantment(Enchantment.DURABILITY, 3); ItemMeta meta = apollos.getItemMeta(); ArrayList<String> lore = new ArrayList<String>(); meta.setDisplayName(ChatColor.BLUE + "Apollos Crest"); lore.add(ChatColor.DARK_RED + "" + ChatColor.ITALIC + "Glowing"); lore.add(ChatColor.DARK_RED + "" + ChatColor.ITALIC + "Implants"); lore.add(ChatColor.GREEN + randomString(10)); meta.setLore(lore); apollos.setItemMeta(meta); pl.getInventory().addItem(apollos); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " You have given " + name + " Apollos Crest!"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(" "); } if (args[3].equalsIgnoreCase("Aegis")) { ItemStack aegis = new ItemStack(Material.DIAMOND_CHESTPLATE, 1); aegis.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 5); aegis.addUnsafeEnchantment(Enchantment.PROTECTION_FIRE, 4); aegis.addUnsafeEnchantment(Enchantment.DURABILITY, 3); ItemMeta meta = aegis.getItemMeta(); ArrayList<String> lore = new ArrayList<String>(); meta.setDisplayName(ChatColor.DARK_GRAY + "* " + ChatColor.DARK_RED + "Aegis" + ChatColor.DARK_GRAY + " *"); lore.add(ChatColor.DARK_GREEN + "Infused with Strength"); lore.add(ChatColor.GREEN + randomString(10)); meta.setLore(lore); aegis.setItemMeta(meta); pl.getInventory().addItem(aegis); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " You have given " + name + " a Aegis!"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(" "); } if (args[3].equalsIgnoreCase("Ethereals")) { ItemStack ethereals = new ItemStack(Material.DIAMOND_LEGGINGS, 1); ethereals.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 5); ethereals.addUnsafeEnchantment(Enchantment.PROTECTION_FIRE, 4); ethereals.addUnsafeEnchantment(Enchantment.DURABILITY, 3); ItemMeta meta = ethereals.getItemMeta(); ArrayList<String> lore = new ArrayList<String>(); meta.setDisplayName(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "Ethereals"); lore.add(ChatColor.DARK_GREEN + "Infused with Invisibility"); lore.add(ChatColor.GREEN + randomString(10)); meta.setLore(lore); ethereals.setItemMeta(meta); pl.getInventory().addItem(ethereals); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " You have given " + name + " Ethereals!"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(" "); } if (args[3].equalsIgnoreCase("Hermes")) { ItemStack hermes = new ItemStack(Material.DIAMOND_BOOTS, 1); hermes.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 5); hermes.addUnsafeEnchantment(Enchantment.PROTECTION_FIRE, 4); hermes.addUnsafeEnchantment(Enchantment.PROTECTION_FALL, 4); hermes.addUnsafeEnchantment(Enchantment.DURABILITY, 3); ItemMeta meta = hermes.getItemMeta(); ArrayList<String> lore = new ArrayList<String>(); meta.setDisplayName(ChatColor.GOLD + "Hermes Boots"); lore.add(ChatColor.DARK_RED + "Infused with Speed"); lore.add(ChatColor.GREEN + randomString(10)); meta.setLore(lore); hermes.setItemMeta(meta); pl.getInventory().addItem(hermes); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " You have given " + name + " Hermes Boots!"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); } } } } } }
It's probably because you have lost the working flow of the code You start with args[0].equalsIgnoreCase("greencode"), then you go args[1].equalsIgnoreCase("give"). Now you ask the player if args2 and you are checking if it's args[0] so greencode = greencode. Then you check if (args[1].equalsIgnoreCase("pyroaxe")) but you already looked if it was give. That might be a small mistake because later you go to args[3]. Now the biggest mistake is: You check if the length of the arguments is 3. Yet you are asking for the 4th argument. Arguments go 0, 1, 2, 3. Some extra tips: Code (Text): ItemStack bow = new ItemStack(Material.BOW); bow.addEnchantment(Enchantment.ARROW_DAMAGE, 5); bow.addEnchantment(Enchantment.ARROW_FIRE, 1); ItemMeta meta = bow.getItemMeta(); ArrayList<String> lore = new ArrayList<String>(); lore.add(ChatColor.DARK_RED + "" + ChatColor.ITALIC + "Lightning"); lore.add(ChatColor.GREEN + randomString(10)); meta.setLore(lore); bow.setItemMeta(meta); pl.getInventory().addItem(bow); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(ChatColor.DARK_RED + "Legendary " + ChatColor.GRAY + "➽" + ChatColor.GREEN + " You have given " + name + " a Lightning Bow!"); p.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-------------------------------------------------------"); p.sendMessage(" "); p.sendMessage(" "); p.sendMessage(" "); This code is basically the same for every item. To keep your code clean create a function called createItem Code (Text): public void createItem(Player target, Player sender, Material m, Map<Enchantment, Integer> ench, List<String> lore, String name) This will keep your code clean and better to read it for yourself and others
line 1 in your snippet: Code (Text): if (args.length == 3) { but here you ask for argument 4 Code (Text): if (args[3].equalsIgnoreCase("lightning")) { This is what happens when you don't use extra functions or classes until it's becoming unreadable arguments work like this /command arg0 arg1 arg2 arg3 arg4 arg0 is args.length == 1 arg1 is args.length == 2 arg2 is args.length == 3 arg3 is args.length == 4 so when you do /command greencode give greencode lightning it won't run because it's 4 arguments long
@DizMizzer is correct in his part (I didn't read the entirety of the code myself because it is.. Odd) Command arguments are presented as an array of strings. Arrays are a collection of objects that you reference by using the respective numbers associated with each object. Think of it like a crate full of boxes, and each box has a number on it. That's how you tell the movers which box you want. By number. Arrays are what's called zero-indexed which means that instead of starting at 1, they start at 0. ie. Your boxes have the numbers 0-3 instead of 1-4. Currently, you're telling the movers you expect to have three boxes, as per the line below. Code (Text): if (args.length == 3) { And later, you're requesting that they deliver box #4, which doesn't exist because you told them earlier you expect to have exactly three boxes. Doesn't make any sense, does it? Code (Text): if (args[3].equalsIgnoreCase("lightning")) { Remember that arrays are zero-indexed, so taking the box labeled "3" means they want box #4.