Solved New to command args. Need help!

Discussion in 'Plugin Development' started by FireBreath14, Mar 1, 2013.

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

    FireBreath14

    As the title says, im new to needing to use command args. When i use this code, i always get an "out of bounds" errorish thing (lol) take a look and help me if u can :) THANKS!!!

    Code:
    if(cmd.getName().equalsIgnoreCase("store") && args.length <= 1){
                    String pack = args[0];
                    String name = sender.getName();
                    Player buyer = (Player)sender;
                   
                    if(args.length == 1){                   
                       
                        if(pack == "1"){
                            //jump
                            if(this.getConfig().contains(name)){
                                int points = this.getConfig().getInt(name);
                                this.getConfig().set(name, points-500);
                                this.saveConfig();
                                PotionEffect effect = new PotionEffect(PotionEffectType.JUMP, 1200, 1);
                                buyer.addPotionEffect(effect);
                                buyer.sendMessage(ChatColor.GREEN+"Enjoy your purchase!");
                            }else{
                                sender.sendMessage(ChatColor.RED+"You don't have enough points!");
                            }
                        }
                       
                        if(pack == "2"){      //CODE HERE
                            //speed
                            if(this.getConfig().contains(name)){
                                int points = this.getConfig().getInt(name);
                                this.getConfig().set(name, points-500);
                                this.saveConfig();
                                PotionEffect effect = new PotionEffect(PotionEffectType.SPEED, 1200, 1);
                                buyer.addPotionEffect(effect);
                                buyer.sendMessage(ChatColor.GREEN+"Enjoy your purchase!");
                            }else{
                                sender.sendMessage(ChatColor.RED+"You don't have enough points!");
                            }
                        }
                       
                        if(pack == "3"){
                            //invisibility
                            if(this.getConfig().contains(name)){
                                int points = this.getConfig().getInt(name);
                                this.getConfig().set(name, points-700);
                                this.saveConfig();
                                PotionEffect effect = new PotionEffect(PotionEffectType.INVISIBILITY, 1200, 1);
                                buyer.addPotionEffect(effect);
                                buyer.sendMessage(ChatColor.GREEN+"Enjoy your purchase!");
                            }else{
                                sender.sendMessage(ChatColor.RED+"You don't have enough points!");
                            }
                        }
                       
                    }else{                   
                        sender.sendMessage(ChatColor.YELLOW+"(1) Jump Boost"+ChatColor.DARK_GREEN+"[500]");
                        sender.sendMessage(ChatColor.YELLOW+"(2) Speed Boost"+ChatColor.DARK_GREEN+"[500]");
                        sender.sendMessage(ChatColor.YELLOW+"(3) Invisibility"+ChatColor.DARK_GREEN+"[700]");
                       
                        if(this.getConfig().contains(name)){
                            int points = this.getConfig().getInt(name);
                            sender.sendMessage(ChatColor.BOLD+"You have "+points+" points");
                        }else{
                            sender.sendMessage(ChatColor.BOLD+"You have 0 points"); //cuz theyre not in the config.
                            //they would be if they had points. but theyre not, so they don't
                        }
                    }
                    return true;
                }
     
  2. Offline

    Tirelessly

    Yeah, because you say "If there's one or zero elements in the array, use the first element"

    Therefore if there are zero elements you get an error.
     
  3. Offline

    FireBreath14

    OH GOODNESS DOY! Line 2! Lol i need to test to see if there's a first element first before i go grabbing it. Lol brain fart ;p (I've had a long day)
     
Thread Status:
Not open for further replies.

Share This Page