Help wih Args

Discussion in 'Plugin Development' started by Ape101, Feb 27, 2014.

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

    Ape101

    Getting an array out of bound exception but not sure why

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd,
    2. String CommandLabel, String[] args) {
    3. Player p = (Player) sender;
    4. Player target = Bukkit.getServer().getPlayer(args[1]);
    5.  
    6.  
    7. if(!p.hasPermission("perm.perm")){
    8. p.sendMessage(Strings.getPermError());
    9. }
    10.  
    11. if(args.length > 0){
    12. p.sendMessage(Strings.getPrefix() + ChatColor.DARK_GREEN + " Define a player and amount");
    13. }
    14.  
    15. if(target == null){
    16. p.sendMessage(Strings.getPrefix() + ChatColor.DARK_GREEN + " Define a player and amount");
    17. }
    18.  
    19. p.sendMessage("works");
    20.  


    It's an ArrayIndexOutofBounds: 1
     
  2. Offline

    Techy4198

    will someone please just make a tutorial on command arguments, i'm sick of seeing like 100 of these threads every day.
    Ape101
    you have the structure of that completely wrong. im not gonna explain how to fix it, just take a look at one of the other million threads like this to find how to fix it.
     
  3. Offline

    Ape101

    Thanks for that helpful post :p i've already tried rearranging it and such, but i can't get it to work, thus why i'm here, for someone to help me figure it out.
     
  4. Offline

    Rocoty

  5. Offline

    Ape101

    I'm just looking for someone to fix my code quickly, i'm not after lessons, i learn from looking ahah :p
     
  6. Offline

    MordorKing78

    1. if(args.length > 0){ With this you say if the argument is higher than zero? And why do you then send a message with define a player amount.. and where are you creating the base of the command?

    F

    PS: You need to select the area to read what i said :) Its invisible text <3

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  7. Offline

    Rocoty

    Well, then you're in the wrong place. You may well learn from looking, but believe me, you learn better by trying.
     
  8. Offline

    Ape101

    Can't seem to get it to work :( Tried setting it so it was equal to 0 instead, still nothing. If anyone could fix my code that would be great
     
  9. Offline

    Rocoty

    Just sit down and actually learn java, will you?
     
    nuno1212sss and Konkz like this.
  10. Offline

    Ape101

    Sit down and stop being a dick to people who learn differently from you? This isn't a forum to have a go at people, it's a forum for sharing and helping
     
  11. Offline

    Rocoty

    Ape101 No really. I'm not trying to be a dick. But you should really know and understand the basics of Java before making Bukkit plugins. And judging by your posts in this thread, you don't.

    Sorry for coming off like that, I am merely trying to be constructive.
     
    AoH_Ruthless likes this.
  12. Offline

    ThunderWaffeMC

    The reason is because args start at 0. Example: args[0] really equals the seconds argument. So if there was a command like so: '/command arguments are fun' and you were to use args[0], it would return "arguments", args[1] would return "are" and args[2] would return "fun".
     
  13. Offline

    MooshViolet

    Rocoty
    You need to chill. Not everyone needs to learn java before they have a go at bukkit. There is no define rules. Stop being an ass and maybe be helpful?

    And in your code above, where do you actually define a command? You are saying the boolean, but never stating a command. You have args for such command, but there is no command to have args to.
     
  14. Offline

    AoH_Ruthless

    MooshViolet Ape101
    Rocoty is being helpful. If it's being an "ass" to tell someone to go learn Java before learning Bukkit, then I'd be an ass everytime. While he could have been a bit nicer in his first post, he apologized. You are without a doubt going to be a much better coder and do a lot better with Bukkit if you have a solid understanding of the fundamentals. He isn't saying to master the whole language (Frankly, all of us are still learning!), but I think he is just saying to learn the basics.

    Now for the actual issue:
    String[] args means it's an array. An array starts at 0, where really it's the first argument. So, in the command /hello world, world is args[0]. It's like centuries. The first century started at 0 A.D. We live in the 21st Century, but the year is 20-- . That's how I explain it to people at least.

    EDIT: Also, your if statement logic is off. You should learn how to use if / else statements.

    Since you aren't using else, it will send the message works, no matter what. So this is how I would structure your code.

    Code:java
    1. if(args.length > 0 || target == null){ // If the args.length > 0 OR the target is null, send the message.
    2. p.sendMessage(Strings.getPrefix() + ChatColor.DARK_GREEN + " Define a player and amount");
    3. } else { // Else, if the args.length is not 0 AND the target is not null, send the message. It becomes an AND from an OR due to DeMorgan's Law.
    4. p.sendMessage("works");
    5. }


    Another issue is that you are getting the player of args[1]. This means in the command /a b c, c is the player. Going back to my array explanation, player b is args[0].
     
Thread Status:
Not open for further replies.

Share This Page