/* (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#removeSpellList(java.lang.String) */ @Override public void removeSpellList(String spellList) { if (spellList.equalsIgnoreCase(Globals.getDefaultSpellBook())) { Logging.errorPrint( LanguageBundle.getString("InfoSpells.can.not.delete.default.spellbook")); // $NON-NLS-1$ return; } if (pc.delSpellBook(spellList)) { pc.setDirty(true); for (Iterator<SpellNode> iterator = preparedSpellLists.iterator(); iterator.hasNext(); ) { SpellNode listNode = iterator.next(); if (spellList.equals(listNode.getRootNode().getName())) { iterator.remove(); } } for (Iterator<SpellNode> iterator = preparedSpellNodes.iterator(); iterator.hasNext(); ) { SpellNode spell = iterator.next(); if (spellList.equals(spell.getRootNode().getName())) { iterator.remove(); } } } else { Logging.errorPrint("delBookButton:failed "); // $NON-NLS-1$ return; } }
/* (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(); } } } }
/** * Update the stored spellbook details in response to a spellbook item of equipment changing. * Changes to equipment other than spellbooks will be ignored. * * @param equip The equipment item that changed. */ private void updateSpellBooks(Equipment equip) { if (equip == null || equip.isType("SPELLBOOK")) { buildKnownPreparedNodes(); for (Iterator<SpellNode> iterator = bookSpellNodes.iterator(); iterator.hasNext(); ) { SpellNode spell = iterator.next(); if (!spellBookNames.containsElement(spell.getRootNode().getName())) { iterator.remove(); } } } }
/** * 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; }
/* (non-Javadoc) * @see pcgen.core.facade.SpellSupportFacade#removeFromSpellBook(pcgen.core.facade.SpellSupportFacade.SpellNode, java.lang.String) */ @Override public void removeFromSpellBook(SpellNode spell, String spellBook) { if (removeSpellFromCharacter(spell, spellBook)) { if (spell.getCount() > 1) { spell.addCount(-1); // Remove and readd to ensure the display is updated bookSpellNodes.removeElement(spell); bookSpellNodes.addElement(spell); } else { bookSpellNodes.removeElement(spell); } addDummyNodeIfSpellBookEmpty(spellBook); } }
/* (non-Javadoc) * @see pcgen.core.facade.SpellSupportFacade#removePreparedSpell(pcgen.core.facade.SpellSupportFacade.SpellNode, java.lang.String) */ @Override public void removePreparedSpell(SpellNode spell, String spellList) { if (removeSpellFromCharacter(spell, spellList)) { if (spell.getCount() > 1) { spell.addCount(-1); // Remove and readd to ensure the display is updated preparedSpellNodes.removeElement(spell); preparedSpellNodes.addElement(spell); } else { preparedSpellNodes.removeElement(spell); } addDummyNodeIfSpellListEmpty(spellList); } }
/** * If there are no spells in the spell book, add in the spell book placeholder node so that the * book shows in the UI. * * @param spellBook The book to be checked. */ private void addDummyNodeIfSpellBookEmpty(String spellBook) { boolean spellListEmpty = true; for (SpellNode node : bookSpellNodes) { if (spellBook.equals(node.getRootNode().getName())) { spellListEmpty = false; break; } } if (spellListEmpty) { for (SpellNode listNode : spellBooks) { if (spellBook.equals(listNode.getRootNode().getName())) { bookSpellNodes.addElement(listNode); } } } }
/** * 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; }
/** * 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; }