fvtt-rolemaster-frp/module/documents/item.js

123 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

import { RMFRPUtility } from "../rmfrp-utility.js";
2024-08-03 16:41:04 +02:00
export class RMFRPItem extends Item {
2024-07-26 09:20:48 +02:00
/** @override */
prepareData() {
// Prepare data for the item. Calling the super version of this executes
// the following, in order: data reset (to clear active effects),
// prepareBaseData(), prepareEmbeddedDocuments() (including active effects),
// prepareDerivedData().
console.log(`rmfrp | item.js | prepareData for: ${this.name}`);
2024-07-26 09:20:48 +02:00
super.prepareData();
}
// Set the icon images for newly created images.
async _preCreate(data, options, userId) {
await super._preCreate(data, options, userId);
// Do not set on copied items if they have a custom Icon.
if (!data.name.includes("(Copy)"))
{
if (this.type === "armor") {
2024-07-26 12:51:32 +02:00
await this.updateSource({img: "systems/fvtt-rolemaster-frp/assets/default/armor.svg"});
2024-07-26 09:20:48 +02:00
}
else if (this.type === "weapon") {
2024-07-26 12:51:32 +02:00
await this.updateSource({img: "systems/fvtt-rolemaster-frp/assets/default/weapon.svg"});
2024-07-26 09:20:48 +02:00
}
else if (this.type === "skill") {
2024-07-26 12:51:32 +02:00
await this.updateSource({img: "systems/fvtt-rolemaster-frp/assets/default/skill.svg"});
2024-07-26 09:20:48 +02:00
}
else if (this.type === "skill_category") {
2024-07-26 12:51:32 +02:00
await this.updateSource({img: "systems/fvtt-rolemaster-frp/assets/default/skill_category.svg"});
2024-07-26 09:20:48 +02:00
}
else if (this.type === "spell") {
2024-07-26 12:51:32 +02:00
await this.updateSource({img: "systems/fvtt-rolemaster-frp/assets/default/spell.svg"});
2024-07-26 09:20:48 +02:00
}
else if (this.type === "herb_or_poison") {
2024-07-26 12:51:32 +02:00
await this.updateSource({img: "systems/fvtt-rolemaster-frp/assets/default/herb_or_poison.svg"});
2024-07-26 09:20:48 +02:00
}
else if (this.type === "transport") {
2024-07-26 12:51:32 +02:00
await this.updateSource({img: "systems/fvtt-rolemaster-frp/assets/default/transport.svg"});
2024-07-26 09:20:48 +02:00
}
}
}
prepareDerivedData() {
const itemData = this;
const systemData = itemData.system;
2024-08-10 09:48:43 +02:00
if (this.parent?.getFlag("world", "importing")) {
return; // Don't calculate skill bonuses if we are importing
}
2024-07-26 09:20:48 +02:00
// Make separate methods for each item type to keep things organized.
if (itemData.type === "skill") {
this._prepareSkillCategoryData(itemData);
}
if (itemData.type === "skill") {
this._prepareSkillData(itemData);
}
}
_prepareSkillCategoryData(itemData) {
if (itemData.type !== "skill_category") return;
console.log(`rmfrp | item.js | Preparing Skill Category Data for: ${itemData.name}`);
2024-07-26 09:20:48 +02:00
// Calculate Skill Category Total Bonus
this.calculateSkillCategoryTotalBonus(itemData);
}
_prepareSkillData(itemData) {
if (itemData.type !== "skill") return;
2024-08-10 09:48:43 +02:00
console.log(`rmfrp | item.js | Preparing Skill Data for: ${itemData.name}`);
2024-07-26 09:20:48 +02:00
// Make modifications to data here. For example:
// const systemData = itemData.system;
// Calculate Skill Category Bonus
this.calculateSelectedSkillCategoryBonus(itemData);
// Calculate Skill Total Bonus
this.calculateSkillTotalBonus(itemData);
}
calculateSkillCategoryTotalBonus(itemData) {
if (this.type === "skill_category") {
console.log(`rmfrp | item.js | Calculating Skill Category Total Bonus for: ${itemData.name}`);
2024-07-26 09:20:48 +02:00
const systemData = itemData.system;
itemData.system.total_bonus = Number(systemData.rank_bonus)
+ Number(systemData.stat_bonus)
+ Number(systemData.prof_bonus)
+ Number(systemData.special_bonus_1)
+ Number(systemData.special_bonus_2);
}
}
calculateSkillTotalBonus(itemData) {
if (this.type === "skill") {
const systemData = itemData.system;
console.log(`rmfrp | item.js | Calculating Skill Total Bonus for: ${itemData.name}`);
2024-07-26 09:20:48 +02:00
itemData.system.total_bonus = Number(systemData.rank_bonus)
+ Number(systemData.category_bonus)
+ Number(systemData.item_bonus)
+ Number(systemData.special_bonus_1)
+ Number(systemData.special_bonus_2);
}
}
calculateSelectedSkillCategoryBonus(itemData) {
2024-08-03 16:41:04 +02:00
// Find the relevant skill category
2024-08-10 09:48:43 +02:00
if (!this.parent) { return; } // Only if attached to an actor
let skillC = this.parent?.items || RMFRPUtility.getSkillCategories();
2024-08-03 16:41:04 +02:00
if (skillC) {
let item = skillC.find(it => it.type == "skill_category" && it.name.toLowerCase() == itemData.system.category.toLowerCase());
2024-08-10 09:48:43 +02:00
if (item) {
this.system.category_bonus = item.system.total_bonus;
} else {
ui.notifications.warn(`Skill Category ${itemData.system.category} not found for Skill ${itemData.name}`);
}
2024-08-03 16:41:04 +02:00
} else {
2024-08-10 09:48:43 +02:00
ui.notifications.info("No Skill Categories found. Please create a Skill Category.");
2024-07-26 09:20:48 +02:00
}
}
}