Hey, I made custom items and I wanted to spawn them in with a command so I decided to use args in my command to make my command smaller. But my command is not working, can someone help me? https://hastebin.com/tumocokemo.java Thank you for your time, - Creatos
Definitly use String#equalsIgnoreCase() if you're working on commands and other user input that requires text!
1.: At the moment you will always return false if the arguments length is 1, otherwise you will return true. Shouldn't this be the other way around? 2.: I recommend using a switch statement instead of those if statements (when checking the value of args[0]: https://www.tutorialspoint.com/java/switch_statement_in_java.htm 3.: Are you sure returning false if the sender is not a player a good idea? Why don't you send the CommandSender a message explaining that only players can use the command, then return true? 4.: You set the player's first inventory slot to the desired item, are you sure you don't want to add it to an empty slot in their inventory instead? 5.: Your class is named "Item". I don't think this is a good choice: it's very generic and it doesn't show that this class handles commands. I believe the other issues have already been explained.
Spoiler: Change proposals Code (Text): @Override public boolean onCommand (CommandSender sender, Command command, String label, String[] args) { //Variable name: arg -> args if (!(sender instanceof Player)) { sender.sendMessage (ChatColor.RED + "Only players can use this command!"); //Instead of returning false return true; } Player player = (Player)sender; if (args.length == 1) { switch (args[0].toLowerCase ()) { //Switch statement instead of multiple if-s case "beginnersword": player.getInventory ().setItem (0, swords.beginnerSword ()); //Instead of setting an inventory slot directly, you should put the item into an empty slot return true; case "ironsword": player.getInventory ().setItem (0, swords.ironSword ()); return true; case "longsword": player.getInventory ().setItem (0, swords.longSword ()); return true; } } return false; } Are you sure the "swords.beginnerSword ()" returns a valid item? Do a test where you replace it with " new ItemStack (Material.STONE_SWORD)".