Getting an array out of bound exception but not sure why Code:java public boolean onCommand(CommandSender sender, Command cmd,String CommandLabel, String[] args) {Player p = (Player) sender;Player target = Bukkit.getServer().getPlayer(args[1]); if(!p.hasPermission("perm.perm")){p.sendMessage(Strings.getPermError());} if(args.length > 0){p.sendMessage(Strings.getPrefix() + ChatColor.DARK_GREEN + " Define a player and amount");} if(target == null){p.sendMessage(Strings.getPrefix() + ChatColor.DARK_GREEN + " Define a player and amount");} p.sendMessage("works"); It's an ArrayIndexOutofBounds: 1
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.
Thanks for that helpful post 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.
I'm just looking for someone to fix my code quickly, i'm not after lessons, i learn from looking ahah
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.
Well, then you're in the wrong place. You may well learn from looking, but believe me, you learn better by trying.
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
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
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.
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".
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.
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 if(args.length > 0 || target == null){ // If the args.length > 0 OR the target is null, send the message. p.sendMessage(Strings.getPrefix() + ChatColor.DARK_GREEN + " Define a player and amount");} 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. p.sendMessage("works");} 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].