Skip to content

Commit

Permalink
[#2930]Added recharge logic to character-features.hbs (#3556)
Browse files Browse the repository at this point in the history
Co-authored-by: Kim Mantas <[email protected]>
  • Loading branch information
Hoppyhob and Fyorl authored Jul 1, 2024
1 parent 04c17cd commit 4566c8d
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@
"DND5E.ContextMenuActionEquip": "Equip",
"DND5E.ContextMenuActionUnequip": "Unequip",
"DND5E.ContextMenuActionPrepare": "Prepare",
"DND5E.ContextMenuActionExpendCharge": "Uncharge",
"DND5E.ContextMenuActionCharge": "Charge",
"DND5E.ContextMenuActionUnprepare": "Unprepare",
"DND5E.ContextMenuActionAttune": "Attune",
"DND5E.ContextMenuActionUnattune": "Unattune",
Expand Down
1 change: 1 addition & 0 deletions less/v2/inventory.less
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
text-align: start;
}
input { text-align: end; }
i.fa-bolt { color: var(--dnd5e-color-gold); }
}
.spell-uses { display: none; }

Expand Down
4 changes: 1 addition & 3 deletions module/applications/actor/character-sheet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class ActorSheet5eCharacter extends ActorSheet5e {

// Partition items by category
let {items, spells, feats, races, backgrounds, classes, subclasses} = context.items.reduce((obj, item) => {
const {quantity, uses, recharge} = item.system;
const {quantity, uses} = item.system;

// Item details
const ctx = context.itemContext[item.id] ??= {};
Expand All @@ -83,8 +83,6 @@ export default class ActorSheet5eCharacter extends ActorSheet5e {

// Item usage
ctx.hasUses = item.hasLimitedUses;
ctx.isOnCooldown = recharge && !!recharge.value && (recharge.charged === false);
ctx.isDepleted = ctx.isOnCooldown && ctx.hasUses && (uses.value > 0);
ctx.hasTarget = item.hasAreaTarget || item.hasIndividualTarget;

// Unidentified items
Expand Down
4 changes: 1 addition & 3 deletions module/applications/actor/npc-sheet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ export default class ActorSheet5eNPC extends ActorSheet5e {
// Start by classifying items into groups for rendering
const maxLevelDelta = CONFIG.DND5E.maxLevel - (this.actor.system.details.level ?? 0);
let [spells, other] = context.items.reduce((arr, item) => {
const {quantity, uses, recharge, target} = item.system;
const {quantity, uses, target} = item.system;
const ctx = context.itemContext[item.id] ??= {};
ctx.isStack = Number.isNumeric(quantity) && (quantity !== 1);
ctx.isExpanded = this._expanded.has(item.id);
ctx.hasUses = uses && (uses.max > 0);
ctx.isOnCooldown = recharge && !!recharge.value && (recharge.charged === false);
ctx.isDepleted = item.isOnCooldown && (uses.per && (uses.value > 0));
ctx.hasTarget = !!target && !(["none", ""].includes(target.type));
ctx.canToggle = false;
if ( item.type === "class" ) ctx.availableLevels = Array.fromRange(CONFIG.DND5E.maxLevel, 1).map(level => ({
Expand Down
4 changes: 1 addition & 3 deletions module/applications/actor/vehicle-sheet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,11 @@ export default class ActorSheet5eVehicle extends ActorSheet5e {
};

context.items.forEach(item => {
const {uses, recharge} = item.system;
const {uses} = item.system;
const ctx = context.itemContext[item.id] ??= {};
ctx.canToggle = false;
ctx.isExpanded = this._expanded.has(item.id);
ctx.hasUses = uses && (uses.max > 0);
ctx.isOnCooldown = recharge && !!recharge.value && (recharge.charged === false);
ctx.isDepleted = item.isOnCooldown && (uses.per && (uses.value > 0));
});

const cargo = {
Expand Down
11 changes: 11 additions & 0 deletions module/applications/components/inventory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ export default class InventoryElement extends HTMLElement {
group: "state"
});

// Toggle Charged State
if ( item.system.recharge?.value ) options.push({
name: item.system.recharge.charged ? "DND5E.ContextMenuActionExpendCharge" : "DND5E.ContextMenuActionCharge",
icon: '<i class="fa-solid fa-bolt"></i>',
condition: () => item.isOwner,
callback: li => this._onAction(li[0], "toggleCharge"),
group: "state"
});

// Toggle Prepared State
else if ( ("preparation" in item.system) && (item.system.preparation?.mode === "prepared") ) options.push({
name: item.system?.preparation?.prepared ? "DND5E.ContextMenuActionUnprepare" : "DND5E.ContextMenuActionPrepare",
Expand Down Expand Up @@ -384,6 +393,8 @@ export default class InventoryElement extends HTMLElement {
return item.update({"system.preparation.prepared": !item.system.preparation?.prepared});
case "recharge":
return item.rollRecharge();
case "toggleCharge":
return item.update({"system.recharge.charged": !item.system.recharge?.charged});
case "unfavorite":
return this.actor.system.removeFavorite(item.getRelativeUUID(this.actor));
case "use":
Expand Down
12 changes: 12 additions & 0 deletions module/documents/item.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ export default class Item5e extends SystemDocumentMixin(Item) {

/* --------------------------------------------- */

/**
* Is the item on recharge cooldown?
* @type {boolean}
* @see {@link ActionTemplate#isOnCooldown}
*/
get isOnCooldown() {
const { recharge } = this.system;
return (recharge?.value > 0) && (recharge?.charged === false);
}

/* --------------------------------------------- */

/**
* Does this item require concentration?
* @type {boolean}
Expand Down
10 changes: 5 additions & 5 deletions templates/actors/parts/actor-features.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<ol class="item-list">
{{#each section.items as |item iid|}}
{{#dnd5e-itemContext item as |ctx|}}
<li class="item flexrow {{#if ctx.isDepleted}}depleted{{/if}} {{#if ctx.isExpanded}}expanded{{/if}}"
<li class="item flexrow {{#if ctx.isExpanded}}expanded{{/if}}"
data-item-id="{{item.id}}">
<div class="item-name flexrow rollable">
<div class="item-image item-action" data-action="use" tabindex="0" role="button" aria-label="{{item.name}}" style="background-image: url('{{item.img}}')"></div>
Expand All @@ -54,12 +54,12 @@

{{#if section.hasActions}}
<div class="item-detail item-uses">
{{#if ctx.isOnCooldown}}
<a class="item-action" data-action="recharge">{{item.labels.recharge}}</a>
{{#if item.isOnCooldown}}
<a class="item-action" data-action="recharge">{{item.labels.recharge}}</a>
{{else if item.system.recharge.value}}
{{localize "DND5E.Charged"}}
{{localize "DND5E.Charged"}}
{{else if ctx.hasUses}}
<input type="text" value="{{item.system.uses.value}}" placeholder="0" data-dtype="Number" data-name="system.uses.value">/ {{item.system.uses.max}}
<input type="text" value="{{item.system.uses.value}}" placeholder="0" data-dtype="Number" data-name="system.uses.value">/ {{item.system.uses.max}}
{{/if}}
</div>
<div class="item-detail item-action">
Expand Down
14 changes: 11 additions & 3 deletions templates/actors/tabs/character-features.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,21 @@
</div>

{{!-- Item Uses --}}
<div class="item-detail item-uses {{#unless ctx.hasUses}}empty{{/unless}}">
{{#if ctx.hasUses}}
{{#if item.isOnCooldown}}
<div class="item-detail item-uses" data-tooltip-direction="LEFT" data-tooltip="{{item.labels.recharge}}" aria-label="{{item.labels.recharge}}">
<a class="item-action" data-action="recharge"><i class="fa-solid fa-arrows-rotate"></i></a>
{{else if item.system.recharge.value}}
<div class="item-detail item-uses" data-tooltip-direction="LEFT" data-tooltip="{{item.labels.recharge}}" aria-label="{{item.labels.recharge}}">
<i class="fa-solid fa-bolt"></i>
{{else if ctx.hasUses}}
<div class="item-detail item-uses">
<input type="text" value="{{ item.system.uses.value }}" placeholder="0" data-dtype="Number"
data-name="system.uses.value" inputmode="numeric" pattern="[+=\-]?\d*">
<span class="separator">&sol;</span>
<span class="max">{{ item.system.uses.max }}</span>
{{/if}}
{{else}}
<div class="item-detail item-uses empty">
{{/if}}
</div>

{{!-- Item Recovery --}}
Expand Down

0 comments on commit 4566c8d

Please sign in to comment.