Skip to content

Commit

Permalink
Add getUpdates hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
arbron committed Jan 18, 2023
1 parent 2b639f8 commit 15718e7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.1.2]
- Add `arbron.preGetSummonsChanges` & `arbron.getSummonsChanges` hooks
- Update [French] translation

## [1.1.1]
- Fix bug when summoning from sheet [[#27]]

Expand Down Expand Up @@ -48,6 +52,7 @@
[1.0.6]: https://github.com/arbron/fvtt-summoner/compare/1.0.5...1.0.6
[1.1.0]: https://github.com/arbron/fvtt-summoner/compare/1.0.6...1.1.0
[1.1.1]: https://github.com/arbron/fvtt-summoner/compare/1.1.0...1.1.1
[1.1.2]: https://github.com/arbron/fvtt-summoner/compare/1.1.1...1.1.2

[#2]: https://github.com/arbron/fvtt-summoner/issues/2
[#3]: https://github.com/arbron/fvtt-summoner/issues/3
Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"readme": "https://raw.githubusercontent.com/arbron/fvtt-summoner/main/readme.md",
"changelog": "https://raw.githubusercontent.com/arbron/fvtt-summoner/main/changelog.md",
"bugs": "https://github.com/arbron/fvtt-summoner/issues",
"version": "1.1.1",
"version": "1.1.2",
"compatibility": {
"minimum": "10.274",
"verified": "10"
Expand Down
7 changes: 5 additions & 2 deletions module/summon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import { promptSummonsType } from "./summons-type-prompt.mjs";
* Perform the summons of the specified type.
* @param {Item5e} item The item performing the summoning.
* @param {SummonsData} [summonsData] Data of the actor to summon. If blank, then the type selection UI will be shown.
* @param {object} [usage={}]
* @param {ItemUseConfiguration} [usage.config]
* @param {ItemUseOptions} [usage.options]
*/
export default async function summon(item, summonsData) {
export default async function summon(item, summonsData, usage) {
// Ensure Warp Gate is installed and enabled, otherwise throw an error
if ( !globalThis.warpgate ) return ui.notifications.error(game.i18n.localize("ArbronSummoner.Error.NoWarpGate"));

Expand Down Expand Up @@ -44,7 +47,7 @@ export default async function summon(item, summonsData) {
let protoData = await actor.getTokenDocument();

// Prepare actor data changes
const updates = SummonsActor.getChanges.bind(protoData.actor)(item);
const updates = SummonsActor.getChanges.bind(protoData.actor)(item, usage);

// Figure out where to place the summons
item.parent?.sheet.minimize();
Expand Down
31 changes: 27 additions & 4 deletions module/summons-actor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,34 @@ export class SummonsActor {
}

/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ */

/**
* Get an updates for the changes needed when summoning.
* @param {Item5e} item The item performing the summoning.
* @param {Item5e} item The item performing the summoning.
* @param {object} [usage] Additional usage information provided by `Item5e#use`.
*/
static getChanges(item) {
static getChanges(item, usage) {
const clone = this.clone();
const config = this.getFlag("arbron-summoner", "config");
const updates = { actor: {}, embedded: {} };
const rollData = item.getRollData();

// Store roll data & summoner information in flags
foundry.utils.setProperty(updates.actor, "flags.arbron-summoner.summoner", { uuid: item.uuid, data: rollData });

if ( !config ) return updates;

/**
* A hook that is called before actor changes are prepared.
* @function arbron.preGetSummonsChanges
* @memberof hookEvents
* @param {Item5e} item The item that is performing the summoning.
* @param {object} updates Updates that will be done to the actor.
* @param {object} [usage] Additional usage information provided by `Item5e#use`.
* @returns {boolean} Explicitly return `false` to skip the rest of the automatic actor changes.
*/
if ( Hooks.call("arbron.preGetSummonsChanges", item, updates, usage) === false ) return updates;

const toHitTarget = config.matchToHit ? SummonsActor._determineToHit(item) : 0;

// Modify proficiency to match summoner using an active effect
Expand Down Expand Up @@ -175,6 +188,16 @@ export class SummonsActor {
}
}

/**
* A hook that is called after actor changes have been prepared.
* @function arbron.getSummonsChanges
* @memberof hookEvents
* @param {Item5e} item The item that is performing the summoning.
* @param {object} updates Updates that will be done to the actor.
* @param {object} [usage] Additional usage information provided by `Item5e#use`.
*/
Hooks.callAll("arbron.getSummonsChanges", item, updates, usage);

return updates;
}

Expand Down
14 changes: 7 additions & 7 deletions module/summons-item.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,26 @@ export class SummonsItem {

/**
* Summon the monster if `createSummons` is true, otherwise retain the summons type.
* @param {Item5e} item Item being rolled.
* @param {ItemRollConfiguration} config Configuration data for the roll.
* @param {ItemRollOptions} options Additional options used for configuring item rolls.
* @param {Item5e} item Item being rolled.
* @param {ItemUseConfiguration} config Configuration data for the roll.
* @param {ItemUseOptions} options Additional options used for configuring item rolls.
*/
static useItem(item, config, options) {
const summons = SummonsItem.getSummonsConfiguration(item);
if ( !summons?.length ) return;

// Trigger the summons
const summonsData = summons.find(s => s.uuid === config.summonsType);
if ( config.createSummons && summonsData ) summon(item, summonsData);
if ( config.createSummons && summonsData ) summon(item, summonsData, { config, options });
}

/* ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ */

/**
* Add the summons button to chat cards that require it.
* @param {Item5e} item Item for which the chat card is being displayed.
* @param {object} chatData Data used to create the chat message.
* @param {ItemRollOptions} options Options which configure the display of the item chat card.
* @param {Item5e} item Item for which the chat card is being displayed.
* @param {object} chatData Data used to create the chat message.
* @param {ItemUseOptions} options Options which configure the display of the item chat card.
*/
static preDisplayCard(item, chatData, options) {
if ( !SummonsItem.getSummonsConfiguration(item)?.length ) return;
Expand Down

0 comments on commit 15718e7

Please sign in to comment.