MiniBlock API and Multiple words for a command

Discussion in 'Spigot Plugin Development' started by DJJujuVI, Jun 17, 2016.

  1. Hey guys I am having trouble with this LIB: https://www.spigotmc.org/resources/mini-blocks-library.9155/
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Location p = (Location) sender;
            if (cmd.getName().equalsIgnoreCase("tini enchantmenttable 1")) {
                MiniBlock block = new MiniBlock(p, new ItemStack(Material.ENCHANTMENT_TABLE), 1);
                return true;
            }
            return true;
        }
    }

    This code doesn't seem to work for me. Also when I change my command to just tini and not with enchantment table or the 1 I get an error, when I do it ingame I get a unknown command.

    So when I change my code to this:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Location p = (Location) sender;
            if (cmd.getName().equalsIgnoreCase("tini")) {
                MiniBlock block = new MiniBlock(p, new ItemStack(Material.ENCHANTMENT_TABLE), 1);
                return true;
            }
            return true;
        }
    }

    I get an internal error, when I do tini enchantmenttable 1 I get an unknown command, any help??
    Do they count it as args or not? If so how would I do all of this?

    Thanks!!
     
  2. Because you're sender isn't a Location...
     
    • Funny Funny x 1
  3. Cast your sender to the Player object instead of Location.
    Then simply get the players location. Player#getLocation
     
  4. Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("tini enchantmenttable 1")) {
                MiniBlock block = new MiniBlock(p, new ItemStack(Material.ENCHANTMENT_TABLE), 1);
                return true;
            }
            return true;
        }
    }

    Is this correct? Because now this is in red:
    Code (Text):
    MiniBlock block = new MiniBlock(p, new ItemStack(Material.ENCHANTMENT_TABLE), 1);
    Someone please help me, I am very anxious to get this working!
     
  5. @JujuMot don't blindcast to Player, and command names are the first word after a slash (so in "/you are stupid" "you" is the command name). You can use the String[] (in your case named args) parameter to get arguments.
     
  6. Can you just remind me what it is? Wasn't it like (arg == length 2)
     
  7. @JujuMot length is a property/field of an array which is of type int. So to get the length of an array, you use <array>.length. To check if it has a length of 2, you use <array>.length == 2
     
  8. Ok thank you! Can you just help me with the MiniBlock API now? Thanks!
     
  9. It looks like MiniBlock is asking for a Location, ItemStack.
    get the location from the player or elsewhere, Player#getLocation
     
    • Winner Winner x 1
  10. Thanks so much @TeddyDev , if I can't believe I left that out but thanks for all the help!
     
  11. Hey guys my multi command words are still not working?

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "The console cannot place a MiniBlock silly!");
            }
           
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("tini enchantmenttable 1")) {
                if (args.length == 2) {
                MiniBlock block = new MiniBlock(p.getLocation(), new ItemStack(Material.ENCHANTMENT_TABLE), 1);
                return true;
            }
        }
            return true;
        }
    }

    Thanks!
     
  12. That's because cmd is only the base commands, anything you type after the command ("enchantmenttable 1") are called arguments.
    They are stored in the String[] args. You already have args.length in there to see how many there are.
    To see what the first argument is you get index 0 from the array (args[0]), since arrays start at 0 and go up from there. In your case index 0 is "enchantmenttable". Getting index 1 from the array will give you "1".
     
    • Optimistic Optimistic x 1
  13. So I changed it to 1 and it still gives me an unknown command.
    Any other ideas?

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "The console cannot place a TiniWeenie silly!");
            }
           
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("tini enchantmenttable 1")) {
                if (args.length == 1) {
                MiniBlock block = new MiniBlock(p.getLocation(), new ItemStack(Material.ENCHANTMENT_TABLE), 1);
                return true;
            }
        }
            return true;
        }
    }
     
  14. You aren't understanding what he's saying. The command name will never have spaces in it. The command will be "tini". You should also return false if the sender isn't a player since it will still execute all the code anyways.

    The args array contains all the arguments after the main command so in your case, args[0] would be enchantmenttable.
     
  15. Then do you have any idea how I would do this? Is there a special way or what?
     
  16. You should perhaps read the documentation. args is a list of all arguments in the command. Everything past the base command, split by a whitespace delimiter.

    Here is pseudo code for what you need to change.

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
        if(!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.RED + "The console cannot place a TiniWeenie silly!");
            return false;
        }
     
        Player p = (Player) sender;
        if (cmd.getName().equalsIgnoreCase("tini")) {
            if (args.length == 2) {
                if (args 0 is "enchantmenttable" and args 1 is "1") {
                    MiniBlock block = new MiniBlock(p.getLocation(), new ItemStack(Material.ENCHANTMENT_TABLE), 1);
                    return true;
                }
            }
        }
        return true;
    }
     
  17. I just get a big red line under:

    if (args 0 is "enchantmenttable" and args 1 is "1") {
     
  18. It's pseudocode. You have to turn that into real code yourself.
     
  19.  
  20. Sorry, but I don't know what to change in it, can you give me the exact code? Thanks!