diff --git a/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/model/character/stats/PTAbilitySet.java b/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/model/character/stats/PTAbilitySet.java index 52fd886..4e6a5a8 100644 --- a/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/model/character/stats/PTAbilitySet.java +++ b/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/model/character/stats/PTAbilitySet.java @@ -86,13 +86,13 @@ public void writeToParcel(Parcel out, int flags) { * @param abilityKey * @return The ability in the set with abilityKey. null if no such ability exists. */ - public PTAbility getAbility(int abilityKey) { + public PTAbility getAbility(int abilityKey) throws IllegalArgumentException { for(int i = 0; i < m_abilities.length; i++) { - if(abilityKey == m_abilities[i].getID()) { + if(abilityKey == m_abilities[i].getAbilityKey()) { return m_abilities[i]; } } - return null; + throw new IllegalArgumentException("Invalid ability key: "+abilityKey); } /** @@ -136,7 +136,7 @@ public static SparseArray getAbilityShortNameMap() { SparseArray map = new SparseArray(constAbilityKeys.size()); String[] names = getShortAbilityNames(); for (int i = 0; i < names.length; i++) { - map.append(constAbilityKeys.get(i).intValue(), names[i]); + map.append(constAbilityKeys.get(i), names[i]); } return map; } @@ -166,4 +166,12 @@ public PTAbilitySet[] newArray(int size) { return new PTAbilitySet[size]; } }; + + public static boolean isValidAbility(int abilityKey) { + List constAbilityKeys = ABILITY_KEYS(); + for (int key : constAbilityKeys) { + if (key == abilityKey) return true; + } + return false; + } } diff --git a/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/model/character/stats/PTSkillSet.java b/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/model/character/stats/PTSkillSet.java index 08f8b5f..64b0716 100644 --- a/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/model/character/stats/PTSkillSet.java +++ b/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/model/character/stats/PTSkillSet.java @@ -115,9 +115,15 @@ public void writeToParcel(Parcel out, int flags) { /** * Ensures that all skills occur at least once in the list. * If a skill does not appear, add a default version. - * @param valid list of skills, sorted by skill key + * Also verifies that all skills have valid skill keys */ private void verifySortSkills(List skills) { + for (PTSkill skill : skills) { + if (!isValidSkill(skill.getSkillKey())) { + skills.remove(skill); + } + } + int[] defaultSkillAbilityIds = getDefaultAbilityIds(); boolean found; List constSkillKeys = SKILL_KEYS(); @@ -129,7 +135,7 @@ private void verifySortSkills(List skills) { break; } } - if (found == false) { + if (!found) { skills.add(new PTSkill(constSkillKeys.get(i), defaultSkillAbilityIds[i])); } } @@ -313,6 +319,10 @@ public PTSkillSet[] newArray(int size) { return new PTSkillSet[size]; } }; + + public static boolean isValidSkill(int skillKey) { + return (skillKey > 0 && skillKey <= SKILL_KEYS().size()); + } } diff --git a/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/views/character/PTCharacterSkillsFragment.java b/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/views/character/PTCharacterSkillsFragment.java index 64232c6..1029939 100644 --- a/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/views/character/PTCharacterSkillsFragment.java +++ b/pathfinder-toolkit/src/com/lateensoft/pathfinder/toolkit/views/character/PTCharacterSkillsFragment.java @@ -116,7 +116,12 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { PTSkill skill = PTParcelableEditorActivity.getParcelableFromIntent(data); if (m_skillSelectedForEdit != null && skill != null) { m_skillSelectedForEdit.setSubType(skill.getSubType()); - m_skillSelectedForEdit.setAbilityKey(skill.getAbilityKey()); + if (PTAbilitySet.isValidAbility(skill.getAbilityKey())) { + m_skillSelectedForEdit.setAbilityKey(skill.getAbilityKey()); + } else { + m_skillSelectedForEdit.setAbilityKey( + PTSkillSet.getDefaultAbilityKeyMap().get(m_skillSelectedForEdit.getSkillKey())); + } m_skillSelectedForEdit.setRank(skill.getRank()); m_skillSelectedForEdit.setMiscMod(skill.getMiscMod()); m_skillSelectedForEdit.setClassSkill(skill.isClassSkill()); @@ -163,7 +168,7 @@ private void updateSkillsList() { } private void setSkillsAdapter() { - PTSkillsAdapter adapter = null; + PTSkillsAdapter adapter; if (m_trainedFilterCheckBox.isChecked()) { adapter = new PTSkillsAdapter(getActivity(), R.layout.character_skill_row, m_skillSet.getTrainedSkills(), @@ -197,12 +202,23 @@ public void updateDatabase() { @Override public void loadFromDatabase() { - m_skillSet = new PTSkillSet(m_skillRepo.querySet(getCurrentCharacterID())); + m_skillSet = new PTSkillSet(validateSkills(m_skillRepo.querySet(getCurrentCharacterID()))); addNewSubSkills(); m_maxDex = m_armorRepo.getMaxDex(getCurrentCharacterID()); m_armorCheckPenalty = m_armorRepo.getArmorCheckPenalty(getCurrentCharacterID()); m_abilitySet = new PTAbilitySet(m_abilityRepo.querySet(getCurrentCharacterID())); } + + private PTSkill[] validateSkills(PTSkill[] skills) { + for (PTSkill skill : skills) { + if (!PTAbilitySet.isValidAbility(skill.getAbilityKey())) { + skill.setAbilityKey( + PTSkillSet.getDefaultAbilityKeyMap().get(skill.getSkillKey())); + m_skillRepo.update(skill); + } + } + return skills; + } private void addNewSubSkills() { PTSkill newSkill;