Skip to content
This repository has been archived by the owner on Jul 10, 2021. It is now read-only.

Commit

Permalink
Added initiative button to utility menu for sw5e (#295)
Browse files Browse the repository at this point in the history
* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Update module.json

* Added initiative button to utility menu for sw5e
  • Loading branch information
jtljac authored Jun 26, 2021
1 parent 6e6b351 commit 98afcfe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
55 changes: 55 additions & 0 deletions scripts/actions/sw5e/sw5e-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ export class ActionHandlerSw5e extends ActionHandler {
let rests = this.initializeEmptySubcategory()
let utility = this.initializeEmptySubcategory();

this._addIntiativeSubcategory(macroType, result, tokenId);

if (actor.data.type === 'character') {
let shortRestValue = [macroType, tokenId, 'shortRest'].join(this.delimiter);
rests.actions.push({id:'shortRest', encodedValue: shortRestValue, name: this.i18n('tokenactionhud.shortRest')})
Expand Down Expand Up @@ -498,10 +500,13 @@ export class ActionHandlerSw5e extends ActionHandler {
_addMultiUtilities(list, tokenId, actors) {
let category = this.initializeEmptyCategory('utility');
let macroType = 'utility';

this._addMultiIntiativeSubcategory(macroType, tokenId, category);

let rests = this.initializeEmptySubcategory();
let utility = this.initializeEmptySubcategory();


if (actors.every(a => a.data.type === 'character')) {
let shortRestValue = [macroType, tokenId, 'shortRest'].join(this.delimiter);
rests.actions.push({id:'shortRest', encodedValue: shortRestValue, name: this.i18n('tokenactionhud.shortRest')})
Expand Down Expand Up @@ -552,6 +557,56 @@ export class ActionHandlerSw5e extends ActionHandler {

this._combineSubcategoryWithCategory(category, this.i18n('tokenactionhud.conditions'), conditions);
}

/** @private */
_addIntiativeSubcategory(macroType, category, tokenId) {
const combat = game.combat;
let combatant, currentInitiative;
if (combat) {
combatant = combat.combatants.find(c => c.tokenId === tokenId);
currentInitiative = combatant?.initiative;
}

let initiative = this.initializeEmptySubcategory();

let initiativeValue = [macroType, tokenId, 'initiative'].join(this.delimiter);
let initiativeName = `${this.i18n('tokenactionhud.rollInitiative')}`;

let initiativeAction = {id:'rollInitiative', encodedValue: initiativeValue, name: initiativeName};

if (currentInitiative)
initiativeAction.info1 = currentInitiative;
initiativeAction.cssClass = currentInitiative ? 'active' : '';

initiative.actions.push(initiativeAction);

this._combineSubcategoryWithCategory(category, this.i18n('tokenactionhud.initiative'), initiative);
}

/** @private */
_addMultiIntiativeSubcategory(macroType, tokenId, category) {
const combat = game.combat;

let initiative = this.initializeEmptySubcategory();

let initiativeValue = [macroType, tokenId, 'initiative'].join(this.delimiter);
let initiativeName = `${this.i18n('tokenactionhud.rollInitiative')}`;

let initiativeAction = {id:'rollInitiative', encodedValue: initiativeValue, name: initiativeName};

let isActive;
if (combat) {
let tokenIds = canvas.tokens.controlled.map(t => t.id);
let tokenCombatants = tokenIds.map(id => combat.combatants.find(c => c.tokenId === id));
isActive = tokenCombatants.every(c => !!c?.initiative)
}

initiativeAction.cssClass = isActive ? 'active' : '';

initiative.actions.push(initiativeAction);

this._combineSubcategoryWithCategory(category, this.i18n('tokenactionhud.initiative'), initiative);
}

/** @private */
_addMultiConditions(list, tokenId) {
Expand Down
17 changes: 14 additions & 3 deletions scripts/rollHandlers/sw5e/sw5e-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class RollHandlerBaseSw5e extends RollHandler {
this.rollItemMacro(event, tokenId, actionId);
break;
case "utility":
this.performUtilityMacro(event, tokenId, actionId);
await this.performUtilityMacro(event, tokenId, actionId);
break;
case 'effect':
await this.toggleEffect(event, tokenId, actionId);
Expand Down Expand Up @@ -108,7 +108,7 @@ export class RollHandlerBaseSw5e extends RollHandler {
return (item.data.data.recharge && !item.data.data.recharge.charged && item.data.data.recharge.value);
}

performUtilityMacro(event, tokenId, actionId) {
async performUtilityMacro(event, tokenId, actionId) {
let actor = super.getActor(tokenId);
let token = super.getToken(tokenId);

Expand All @@ -133,10 +133,21 @@ export class RollHandlerBaseSw5e extends RollHandler {
case 'deathSave':
actor.rollDeathSave({event});
break;
case 'initiative':
await this.performInitiativeMacro(tokenId);
break;
}
}

async performInitiativeMacro(tokenId) {
let actor = super.getActor(tokenId);

await actor.rollInitiative({createCombatants: true});

Hooks.callAll('forceUpdateTokenActionHUD')
}

async toggleCondition(event, tokenId, effectId) {
async toggleCondition(event, tokenId, effectId) {
const token = super.getToken(tokenId);
const isRightClick = this.isRightClick(event);
if (effectId.includes('combat-utility-belt.') && game.cub && !isRightClick) {
Expand Down

0 comments on commit 98afcfe

Please sign in to comment.