Skip to content

Commit

Permalink
Utilize more of TypeDataModel with ability items (#16756)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam authored Oct 4, 2024
1 parent 4ea7650 commit 69cd2c5
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/module/chat-message/listeners/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class ChatCards {
if (buttons) {
const span = createHTMLElement("span", { classes: ["effect-applied"] });
const anchor = effect.toAnchor({ attrs: { draggable: "true" } });
const locKey = "PF2E.Item.Action.SelfAppliedEffect.Applied";
const locKey = "PF2E.Item.Ability.SelfAppliedEffect.Applied";
const statement = game.i18n.format(locKey, { effect: anchor.outerHTML });
span.innerHTML = statement;
buttons.replaceChildren(span);
Expand Down
35 changes: 30 additions & 5 deletions src/module/item/ability/data.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { ItemSystemModel, ItemSystemSchema } from "@item/base/data/schema.ts";
import { ActionType, BaseItemSourcePF2e, FrequencyInterval, ItemSystemSource } from "@item/base/data/system.ts";
import { OneToThree } from "@module/data.ts";
import type { ActionType, BaseItemSourcePF2e, FrequencyInterval, ItemSystemSource } from "@item/base/data/system.ts";
import type { OneToThree } from "@module/data.ts";
import { LaxArrayField, SlugField } from "@system/schema-data-fields.ts";
import { AbilityItemPF2e } from "./document.ts";
import type { AbilityTraitToggles } from "./trait-toggles.ts";
import type { ModelPropFromDataField } from "types/foundry/common/data/fields.d.ts";
import type { AbilityItemPF2e } from "./document.ts";
import { AbilityTraitToggles } from "./trait-toggles.ts";
import type { AbilityTrait, ActionCategory } from "./types.ts";
import fields = foundry.data.fields;

type AbilitySource = BaseItemSourcePF2e<"action", AbilitySystemSource>;

class AbilitySystemData extends ItemSystemModel<AbilityItemPF2e, AbilitySystemSchema> {
static override LOCALIZATION_PREFIXES = [...super.LOCALIZATION_PREFIXES, "PF2E.Item.Ability"];

declare traits: AbilityTraits;

declare frequency: FrequencyData | null;

declare selfEffect: SelfEffectReference | null;

declare deathNote: boolean;
Expand Down Expand Up @@ -99,6 +104,24 @@ class AbilitySystemData extends ItemSystemModel<AbilityItemPF2e, AbilitySystemSc
),
};
}

override prepareBaseData(): void {
super.prepareBaseData();
this.traits.toggles = new AbilityTraitToggles(this.parent);
this.deathNote ??= false;

// Initialize frequency uses if not set
this.frequency ??= null;
if (this.parent.actor && this.frequency) {
this.frequency.value ??= this.frequency.max;
}

// Self effects are only usable with actions
this.selfEffect ??= null;
if (this.actionType.value === "passive") {
this.selfEffect = null;
}
}
}

interface AbilitySystemData
Expand Down Expand Up @@ -174,6 +197,8 @@ type AbilitySystemSource = SourceFromSchema<AbilitySystemSchema> & {
schema?: ItemSystemSource["schema"];
};

type FrequencyData = NonNullable<ModelPropFromDataField<AbilitySystemSchema["frequency"]>>;

type AbilityTraitsSource = AbilitySystemSource["traits"];

interface AbilityTraits extends AbilityTraitsSource {
Expand All @@ -187,4 +212,4 @@ interface SelfEffectReference extends SelfEffectReferenceSource {
}

export { AbilitySystemData };
export type { AbilitySource, AbilitySystemSource, SelfEffectReference, SelfEffectReferenceSource };
export type { AbilitySource, AbilitySystemSchema, AbilitySystemSource, SelfEffectReference, SelfEffectReferenceSource };
22 changes: 1 addition & 21 deletions src/module/item/ability/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { UserPF2e } from "@module/user/index.ts";
import { sluggify } from "@util";
import type { AbilitySource, AbilitySystemData } from "./data.ts";
import { getActionCostRollOptions, normalizeActionChangeData, processSanctification } from "./helpers.ts";
import { AbilityTraitToggles } from "./trait-toggles.ts";
import type { AbilityTrait } from "./types.ts";

class AbilityItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends ItemPF2e<TParent> {
Expand All @@ -33,26 +32,7 @@ class AbilityItemPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> exten
}

get frequency(): Frequency | null {
return this.system.frequency ?? null;
}

override prepareBaseData(): void {
super.prepareBaseData();

// Initialize frequency uses if not set
if (this.actor && this.system.frequency) {
this.system.frequency.value ??= this.system.frequency.max;
}

this.system.traits.toggles = new AbilityTraitToggles(this);

this.system.selfEffect ??= null;
// Self effects are only usable with actions
if (this.system.actionType.value === "passive") {
this.system.selfEffect = null;
}

this.system.deathNote ??= false;
return this.system.frequency;
}

override prepareActorData(): void {
Expand Down
16 changes: 8 additions & 8 deletions src/module/item/ability/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AbilityItemPF2e } from "@item/ability/document.ts";
import { ItemSheetDataPF2e, ItemSheetOptions, ItemSheetPF2e } from "@item/base/sheet/sheet.ts";
import { ancestryTraits } from "@scripts/config/traits.ts";
import * as R from "remeda";
import type { SelfEffectReference } from "./data.ts";
import type { AbilitySystemSchema, SelfEffectReference } from "./data.ts";
import { activateActionSheetListeners, createSelfEffectSheetData, handleSelfEffectDrop } from "./helpers.ts";

// Gather traits to restrict to avoid trait selection noise in the selection
Expand Down Expand Up @@ -37,7 +37,7 @@ class AbilitySheetPF2e extends ItemSheetPF2e<AbilityItemPF2e> {

return {
...sheetData,
categories: CONFIG.PF2E.actionCategories,
fields: this.item.system.schema.fields,
actionTypes: CONFIG.PF2E.actionTypes,
actionsNumber: CONFIG.PF2E.actionsNumber,
actionTraits: CONFIG.PF2E.actionTraits,
Expand Down Expand Up @@ -65,12 +65,12 @@ class AbilitySheetPF2e extends ItemSheetPF2e<AbilityItemPF2e> {
}

interface ActionSheetData extends ItemSheetDataPF2e<AbilityItemPF2e> {
categories: ConfigPF2e["PF2E"]["actionCategories"];
actionTypes: ConfigPF2e["PF2E"]["actionTypes"];
actionsNumber: ConfigPF2e["PF2E"]["actionsNumber"];
actionTraits: ConfigPF2e["PF2E"]["actionTraits"];
frequencies: ConfigPF2e["PF2E"]["frequencies"];
proficiencies: ConfigPF2e["PF2E"]["proficiencyLevels"];
fields: AbilitySystemSchema;
actionTypes: typeof CONFIG.PF2E.actionTypes;
actionsNumber: typeof CONFIG.PF2E.actionsNumber;
actionTraits: typeof CONFIG.PF2E.actionTraits;
frequencies: typeof CONFIG.PF2E.frequencies;
proficiencies: typeof CONFIG.PF2E.proficiencyLevels;
selfEffect: SelfEffectReference | null;
}

Expand Down
2 changes: 2 additions & 0 deletions src/module/item/base/data/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { ItemDescriptionData } from "./system.ts";

abstract class ItemSystemModel<TParent extends ItemPF2e, TSchema extends ItemSystemSchema> extends foundry.abstract
.TypeDataModel<TParent, TSchema> {
static override LOCALIZATION_PREFIXES = ["PF2E.Item"];

static override defineSchema(): ItemSystemSchema {
const fields = foundry.data.fields;

Expand Down
4 changes: 4 additions & 0 deletions src/module/item/base/sheet/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class ItemSheetPF2e<TItem extends ItemPF2e> extends ItemSheet<TItem, ItemSheetOp
item,
isPhysical: false,
data: item.system,
fieldRootId: this.item.collection.has(this.item.id) ? this.id : foundry.utils.randomID(),
fieldIdPrefix: `field-${this.appId}-`,
enrichedContent,
limited: this.item.limited,
Expand Down Expand Up @@ -650,6 +651,9 @@ interface ItemSheetDataPF2e<TItem extends ItemPF2e> extends ItemSheetData<TItem>
detailsTemplate: string;
item: TItem;
data: TItem["system"];
/** The leading part of IDs used for label-input/select matching */
fieldRootId: string;
/** Legacy value of the above */
fieldIdPrefix: string;
enrichedContent: Record<string, string>;
isPhysical: boolean;
Expand Down
8 changes: 4 additions & 4 deletions src/scripts/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,10 @@ export const PF2ECONFIG = {
},

actionCategories: {
interaction: "PF2E.Item.Action.Category.Interaction",
defensive: "PF2E.Item.Action.Category.Defensive",
offensive: "PF2E.Item.Action.Category.Offensive",
familiar: "PF2E.Item.Action.Category.Familiar",
interaction: "PF2E.Item.Ability.Category.Interaction",
defensive: "PF2E.Item.Ability.Category.Defensive",
offensive: "PF2E.Item.Ability.Category.Offensive",
familiar: "PF2E.Item.Ability.Category.Familiar",
},

frequencies: {
Expand Down
6 changes: 3 additions & 3 deletions src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ function getActionTypeLabel(
): string | null {
switch (type) {
case "action":
return cost === 1 ? "PF2E.Item.Action.Type.Single" : "PF2E.Item.Action.Type.Activity";
return cost === 1 ? "PF2E.Item.Ability.Type.Single" : "PF2E.Item.Ability.Type.Activity";
case "free":
return "PF2E.Item.Action.Type.Free";
return "PF2E.Item.Ability.Type.Free";
case "reaction":
return "PF2E.Item.Action.Type.Reaction";
return "PF2E.Item.Ability.Type.Reaction";
default:
return null;
}
Expand Down
21 changes: 16 additions & 5 deletions static/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
"ActionActionTypeLabel": "Action Type",
"ActionActionsLabel": "Actions",
"ActionBrowserSearchHint": "You can search for name or custom attributes. Possible searchable attributes are:<br> source, spellType, level, materials, target, range, time, duration, damage, damageType, save, concentration, ritual, ability and classes. <br>Example: 'fire, damage:d6' to show all spells that have fire in their name and a d6 in the damage",
"ActionDeathNoteLabel": "Death Note",
"ActionNumber1": "One",
"ActionNumber2": "Two",
"ActionNumber3": "Three",
Expand Down Expand Up @@ -1702,21 +1701,28 @@
"Features": "Features",
"InvalidDrop": "{badType} cannot be dropped here (must be {goodType})"
},
"Action": {
"Ability": {
"Category": {
"None": "None",
"Defensive": "Defensive",
"Interaction": "Interaction",
"Offensive": "Offensive",
"Familiar": "Familiar Ability"
},
"FIELDS": {
"deathNote": {
"label": "Death Note"
},
"selfEffect": {
"hint": "A single effect item to be applied to the actor that uses this action",
"label": "Self-Applied Effect"
}
},
"Plural": "Abilities",
"SelfAppliedEffect": {
"Applied": "{effect} was applied.",
"Delete": "Delete Reference",
"Drop": "Drop Effect",
"Label": "Self-Applied Effect",
"Hint": "A single effect item to be applied to the actor that uses this action"
"Drop": "Drop Effect"
},
"Type": {
"Activity": "Activity",
Expand Down Expand Up @@ -2274,6 +2280,11 @@
"ShowTokenIcon": "Show token icon?",
"Unidentified": "Unidentified?"
},
"FIELDS": {
"category": {
"label": "Category"
}
},
"Feat": {
"Info": {
"Added": "{item} was added to {category}."
Expand Down
2 changes: 1 addition & 1 deletion static/templates/actors/party/regions/exploration.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<div class="empty" data-action="open-sheet" data-tab="exploration">
<div class="icon"><i class="fa-solid fa-plus fa-fw"></i></div>
<div>
<div class="name">{{localize "PF2E.Item.Action.Type.Activity"}}</div>
<div class="name">{{localize "PF2E.Item.Ability.Type.Activity"}}</div>
<div class="hint">{{localize "PF2E.Actor.Party.SlotAvailable"}}</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion static/templates/compendium-browser/compendium-browser.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="content-box" data-tooltip-class="pf2e">
<nav data-group="primary">
<a data-tab="landing-page" style="display:none"></a>
<a data-tab="action">{{localize "PF2E.Item.Action.Plural"}}</a>
<a data-tab="action">{{localize "PF2E.Item.Ability.Plural"}}</a>
{{#if user.isGM}}
<a data-tab="bestiary">{{localize "PF2E.CompendiumBrowser.TabBestiary"}}</a>
{{/if}}
Expand Down
8 changes: 1 addition & 7 deletions static/templates/items/action-details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@
{{#unless (eq item.system.actionType.value "passive")}}
{{> "systems/pf2e/templates/items/partials/self-applied-effect.hbs"}}
{{/unless}}

<div class="form-group">
<label for="{{fieldIdPrefix}}death-note">{{localize "PF2E.ActionDeathNoteLabel"}}</label>
<div class="form-fields">
<input type="checkbox" name="system.deathNote" id="{{fieldIdPrefix}}death-note" {{checked data.deathNote}} />
</div>
</div>
{{formGroup fields.deathNote value=data.deathNote rootId=fieldRootId}}
7 changes: 1 addition & 6 deletions static/templates/items/action-sidebar.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<div class="inventory-details">
<div class="form-group">
<label>{{localize "PF2E.Category"}}</label>
<select name="system.category">
{{selectOptions categories selected=data.category blank="PF2E.Item.Action.Category.None" localize=true}}
</select>
</div>
{{formGroup fields.category value=data.category rootId=fieldRootId localize=true}}
</div>
1 change: 0 additions & 1 deletion static/templates/items/partials/ability-activation.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<div class="form-group">
<label for="{{fieldIdPrefix}}action-type">{{localize "PF2E.ActionActionsLabel"}}</label>
<div class="form-fields">
Expand Down
8 changes: 4 additions & 4 deletions static/templates/items/partials/self-applied-effect.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="form-group" data-drop-zone="self-applied-effect">
<label>{{localize "PF2E.Item.Action.SelfAppliedEffect.Label"}}</label>
<label>{{localize "PF2E.Item.Ability.FIELDS.selfEffect.label"}}</label>
<div class="drop-zone{{#unless selfEffect}} empty{{/unless}}">
{{#if selfEffect}}
<a class="content-link" draggable="true" data-tooltip="{{localize "CONTROLS.CommonOpenSheet"}}"
Expand All @@ -12,11 +12,11 @@
<img src="{{selfEffect.img}}" />
<span class="name">{{selfEffect.name}}</span>
</a>
<a class="delete" data-action="delete-effect" data-tooltip="{{localize "PF2E.Item.Action.SelfAppliedEffect.Delete"}}"><i class="fa-solid fa-trash"></i></a>
<a class="delete" data-action="delete-effect" data-tooltip="{{localize "PF2E.Item.Ability.SelfAppliedEffect.Delete"}}"><i class="fa-solid fa-trash"></i></a>
{{else}}
<picture class="icon-placeholder"></picture>
<span class="name">{{localize "PF2E.Item.Action.SelfAppliedEffect.Drop"}}</span>
<span class="name">{{localize "PF2E.Item.Ability.SelfAppliedEffect.Drop"}}</span>
{{/if}}
</div>
<p class="hint">{{localize "PF2E.Item.Action.SelfAppliedEffect.Hint"}}</p>
<p class="hint">{{localize "PF2E.Item.Ability.FIELDS.selfEffect.hint"}}</p>
</div>
3 changes: 3 additions & 0 deletions types/foundry/common/abstract/type-data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export default class TypeDataModel<
/** The package that is providing this DataModel for the given sub-type. */
readonly modelProvider: packages.BaseSystem | packages.BaseModule | null;

/** A set of localization prefix paths which are used by this data model. */
static LOCALIZATION_PREFIXES: string[];

constructor(data?: object, options?: DataModelConstructionOptions<abstract.Document | null>);

/** Prepare data related to this DataModel itself, before any derived data is computed. */
Expand Down

0 comments on commit 69cd2c5

Please sign in to comment.