/* (non-Javadoc) * @see pcgen.core.facade.SpellSupportFacade#addToSpellBook(pcgen.core.facade.SpellSupportFacade.SpellNode, java.lang.String) */ @Override public void addToSpellBook(SpellNode spell, String spellBook) { String bookName = spellBook; if (bookName.endsWith("]") && bookName.contains(" [")) { bookName = bookName.substring(0, bookName.lastIndexOf(" [")); } SpellNode node = addSpellToCharacter(spell, bookName, new ArrayList<>()); if (node != null) { if (bookSpellNodes.containsElement(node)) { SpellNode spellNode = bookSpellNodes.getElementAt(bookSpellNodes.getIndexOfElement(node)); spellNode.addCount(1); // Remove and readd to ensure the display is updated bookSpellNodes.removeElement(spellNode); bookSpellNodes.addElement(spellNode); } else { bookSpellNodes.addElement(node); } // Remove dummy spellbook node from the list for (Iterator<SpellNode> iterator = bookSpellNodes.iterator(); iterator.hasNext(); ) { SpellNode sn = iterator.next(); if (sn.getSpell() == null && bookName.equals(sn.getRootNode().getName())) { iterator.remove(); } } } }
/* (non-Javadoc) * @see pcgen.core.facade.SpellSupportFacade#addPreparedSpell(pcgen.core.facade.SpellSupportFacade.SpellNode, java.lang.String) */ @Override public void addPreparedSpell(SpellNode spell, String spellList, boolean useMetamagic) { List<Ability> metamagicFeats = new ArrayList<>(); if (useMetamagic) { metamagicFeats = queryUserForMetamagic(spell); if (metamagicFeats == null) { return; } } SpellNode node = addSpellToCharacter(spell, spellList, metamagicFeats); if (node != null) { if (preparedSpellNodes.containsElement(node)) { SpellNode spellNode = preparedSpellNodes.getElementAt(preparedSpellNodes.getIndexOfElement(node)); spellNode.addCount(1); // Remove and readd to ensure the display is updated preparedSpellNodes.removeElement(spellNode); preparedSpellNodes.addElement(spellNode); } else { preparedSpellNodes.addElement(node); } // Remove dummy spelllist node from the list for (Iterator<SpellNode> iterator = preparedSpellNodes.iterator(); iterator.hasNext(); ) { SpellNode sn = iterator.next(); if (sn.getSpell() == null && spellList.equals(sn.getRootNode().getName())) { iterator.remove(); } } } }
/** * Add a spell to the named book for the character. The request will be validated and any errors * shown to the user by the UIDelegate. * * @param spell The spell to be added. * @param bookName The book to add the spell to. * @param metamagicFeats List of the metamagic feats that should be applied to this spell. * @return The new SpellNode, or null if the selection was invalid. */ private SpellNode addSpellToCharacter( SpellNode spell, String bookName, List<Ability> metamagicFeats) { if (!(spell.getSpell() instanceof SpellFacadeImplem)) { return null; } if (spell.getSpellcastingClass() == null) { return null; } CharacterSpell charSpell = ((SpellFacadeImplem) spell.getSpell()).getCharSpell(); if (charSpell == null) { return null; } int level = Integer.parseInt(spell.getSpellLevel()); for (Ability ability : metamagicFeats) { level += ability.getSafe(IntegerKey.ADD_SPELL_LEVEL); } String errorMsg = pc.addSpell( charSpell, metamagicFeats, spell.getSpellcastingClass().getKeyName(), bookName, level, level); if (!StringUtils.isEmpty(errorMsg)) { delegate.showErrorMessage(Constants.APPLICATION_NAME, errorMsg); return null; } SpellInfo spellInfo = charSpell.getSpellInfoFor(bookName, level, metamagicFeats); boolean isKnown = Globals.getDefaultSpellBook().equals(bookName); SpellFacadeImplem spellImplem = new SpellFacadeImplem(pc, charSpell.getSpell(), charSpell, spellInfo); SpellNodeImpl node = new SpellNodeImpl( spellImplem, spell.getSpellcastingClass(), String.valueOf(spellInfo.getActualLevel()), getRootNode(bookName)); return node; }
/** * Create a map of the spell nodes for a class in the supplied list. This is intended to allow * quick checking of the presence of a spell in the list. * * @param spellNodeList The list of spell nodes * @param pcClass The class to filter the map by * @return A double map to the class' spells from the list. */ private DoubleKeyMapToList<SpellFacade, String, SpellNode> buildExistingSpellMap( DefaultListFacade<SpellNode> spellNodeList, PCClass pcClass) { DoubleKeyMapToList<SpellFacade, String, SpellNode> spellMap = new DoubleKeyMapToList<>(); for (SpellNode spellNode : spellNodeList) { if (pcClass.equals(spellNode.getSpellcastingClass())) { spellMap.addToListFor(spellNode.getSpell(), spellNode.getSpellLevel(), spellNode); } } return spellMap; }
/** * Remove a spell from the named book for the character. The request will be validated and any * errors shown to the user by the UIDelegate. * * @param spell The spell to be removed. * @param bookName The book to remove the spell from. * @return True if the removal worked, false if the selection was invalid. */ private boolean removeSpellFromCharacter(SpellNode spell, String bookName) { if (!(spell.getSpell() instanceof SpellFacadeImplem)) { return false; } SpellFacadeImplem sfi = (SpellFacadeImplem) spell.getSpell(); CharacterSpell charSpell = sfi.getCharSpell(); SpellInfo spellInfo = sfi.getSpellInfo(); if (charSpell == null || spellInfo == null) { return false; } final String errorMsg = pc.delSpell(spellInfo, (PCClass) spell.getSpellcastingClass(), bookName); if (errorMsg.length() > 0) { delegate.showErrorMessage(Constants.APPLICATION_NAME, errorMsg); ShowMessageDelegate.showMessageDialog( errorMsg, Constants.APPLICATION_NAME, MessageType.ERROR); return false; } return true; }
/** * Get the list of metatmagic feats that the character can use. * * @param spellNode The spell the feats would be applied to. * @return The list of metamagic feats. */ private List<InfoFacade> buildAvailableMetamagicFeatList(SpellNode spellNode) { List<Ability> characterMetaMagicFeats = new ArrayList<>(); List<CNAbility> feats = pc.getCNAbilities(AbilityCategory.FEAT); for (CNAbility cna : feats) { Ability aFeat = cna.getAbility(); if (aFeat.isType("Metamagic") // $NON-NLS-1$ && !aFeat.getSafe(ObjectKey.VISIBILITY).isVisibleTo(View.HIDDEN_EXPORT)) { characterMetaMagicFeats.add(aFeat); } } Globals.sortPObjectListByName(characterMetaMagicFeats); if (!(spellNode.getSpell() instanceof SpellFacadeImplem)) { return Collections.emptyList(); } List<InfoFacade> availableList = new ArrayList<>(); availableList.addAll(characterMetaMagicFeats); return availableList; }