Selecting Item from Array List?

Discussion in 'Plugin Development' started by chennault, Feb 2, 2014.

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

    chennault

    Hi!

    For the life of me I can't figure this out...I'd greatly appreciate some help!

    So, I've created an array list of spells, and now I'm trying to implement a "select" command that will set the current spell to the one the player typed "/spells select <name>"

    If I type a random name/number it will throw an OutOfBounds exception. However, if I type a name that is included in the array list it will throw 10-15 "....<name> is not a valid spell!"

    Code:
    else if (args[0].equalsIgnoreCase("select"))
                {
                    Spell spell = parseSpell(args[1]);
                    for (int i = 0; i < spells.size(); i++)
                    {
                      if (((Spell)spells.get(i)).equals(spell.getNiceName()))
                      {
                        SpellsPlayer caster = players.get(sender.getName());
                        caster.setCurrentSpell(i);
                        sender.sendMessage(ChatColor.DARK_PURPLE + "[DA Staff]" + ChatColor.GREEN +  " Set current spell to " + spells.get(caster.getCurrentSpell()).getNiceName());
                        break;
                      }
                      else
                        sender.sendMessage(ChatColor.DARK_PURPLE + "[DA Staff]" + ChatColor.RED + "'" + args[1] + "' is not a valid spell.");//typed wrong name
                    }
                  return true;
                }
    Code:
        private Spell parseSpell(String spellName)
        {
          spellName = spellName.replaceAll(" ", "");
          for (Spell spell : Spells.spells) if (spell.getNiceName().replaceAll(" ", "").equalsIgnoreCase(spellName)) return spell;
          return null;
        }

    Any help, please? Thanks!
     
  2. Offline

    Felixx61

    Any help, please?


    The array list is being initialized in the link given.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 11, 2022
  3. Offline

    chennault

    I've done all I can...still can't figure it out!
     
Thread Status:
Not open for further replies.

Share This Page