-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
foundryvtt-5eOGLCharacterSheet.ts
244 lines (207 loc) · 7.4 KB
/
foundryvtt-5eOGLCharacterSheet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { log } from './helpers';
import { registerSettings } from './module/settings.js';
import { preloadTemplates } from './module/preloadTemplates.js';
import { MODULE_ID, MySettings } from './constants.js';
//@ts-ignore
import ActorSheet5eCharacter from '../../systems/dnd5e/module/actor/sheets/character.js';
Handlebars.registerHelper('ogl5e-sheet-path', (relativePath: string) => {
return `modules/${MODULE_ID}/${relativePath}`;
});
Handlebars.registerHelper('ogl5e-sheet-safeVal', (value, fallback) => {
return new Handlebars.SafeString(value || fallback);
});
Handlebars.registerHelper('ogl5e-sheet-add', (value: number, toAdd: number) => {
return new Handlebars.SafeString(String(value + toAdd));
});
Handlebars.registerHelper('ogl5e-sheet-isEmpty', (input: Object | Array<any> | Set<any>) => {
if (!input) {
return true;
}
if (input instanceof Array) {
return input.length < 1;
}
if (input instanceof Set) {
return input.size < 1;
}
return isObjectEmpty(input);
});
export class OGL5eCharacterSheet extends ActorSheet5eCharacter {
get template() {
//@ts-ignore
if (!game.user.isGM && this.actor.limited && !game.settings.get(MODULE_ID, MySettings.expandedLimited)) {
return `modules/${MODULE_ID}/templates/character-sheet-ltd.hbs`;
}
return `modules/${MODULE_ID}/templates/character-sheet.hbs`;
}
static get defaultOptions(): FormApplicationOptions {
const options = super.defaultOptions;
mergeObject(options, {
classes: ['dnd5e', 'sheet', 'actor', 'character', 'ogl5e-sheet'],
height: 680,
width: 830,
});
return options;
}
/**
* Inject character actions list before listeners are activated
* @override
*/
async _renderInner(...args) {
const html = await super._renderInner(...args);
const actionsListApi = game.modules.get('character-actions-list-5e')?.api;
try {
const actionsTab = html.find('.actions');
//@ts-ignore
const actionsTabHtml = $(await actionsListApi?.renderActionsList(this.actor));
actionsTab.html(actionsTabHtml);
} catch (e) {
log(true, e);
}
return html;
}
/**
* Handle rolling an Ability check, either a test or a saving throw
* @param {Event} event The originating click event
* @private
*/
_onRollAbilitySave(event) {
event.preventDefault();
let ability = event.currentTarget.parentElement.dataset.ability;
//@ts-ignore
this.actor.rollAbilitySave(ability, { event: event }); // FIXME TS
}
/**
* Change the quantity of an Owned Item within the Actor
* @param {Event} event The triggering click event
* @private
*/
async _onQuantityChange(event) {
event.preventDefault();
event.stopPropagation();
const itemId = event.currentTarget.closest('.item').dataset.itemId;
// @ts-ignore
const item = this.actor.items.get(itemId);
const quantity = parseInt(event.target.value);
event.target.value = quantity;
return item.update({ 'data.quantity': quantity });
}
/**
* Activate event listeners using the prepared sheet HTML
* @param html {HTML} The prepared HTML object ready to be rendered into the DOM
*/
async activateListeners(html) {
super.activateListeners(html);
//@ts-ignore
if (!this.options.editable) return; // FIXME TS
// Saving Throws
html.find('.saving-throw-name').click(this._onRollAbilitySave.bind(this));
// Item Quantity
html
.find('.item-quantity input')
.click((ev) => ev.target.select())
.change(this._onQuantityChange.bind(this));
}
getData() {
const sheetData = super.getData();
// replace classLabels with Subclass + Class list
try {
let items = sheetData.items;
const classList = items
.filter((item) => item.type === 'class')
.map((item) => {
return `${item.data.subclass} ${item.name} ${item.data.levels}`;
});
sheetData.classLabels = classList.join(', ');
} catch (e) {
log(true, 'error trying to parse class list', e);
}
// add abbreviated spell activation labels
try {
// MUTATES sheetData
sheetData?.spellbook.forEach(({ spells }) => {
spells.forEach((spell) => {
const newActivationLabel = spell.labels.activation
.split(' ')
.map((string: string, index) => {
// ASSUMPTION: First "part" of the split string is the number
if (index === 0) {
return string;
}
// ASSUMPTION: Everything after that we can safely abbreviate to be just the first character
return string.substr(0, 1);
})
.join(' ');
spell.labels.activationAbbrev = newActivationLabel;
});
});
} catch (e) {
log(true, 'error trying to modify activation labels', e);
}
// add abbreviated feature activation labels
try {
let activeFeaturesIndex = sheetData.features.findIndex(({ label }) => label.includes('Active'));
// MUTATES sheetData
sheetData.features[activeFeaturesIndex].items.forEach((item) => {
const newActivationLabel = item.labels.activation
.split(' ')
.map((string: string, index) => {
// ASSUMPTION: First "part" of the split string is the number
if (index === 0) {
return string;
}
// ASSUMPTION: Everything after that we can safely abbreviate to be just the first character
return string.substr(0, 1);
})
.join(' ');
item.labels.activationAbbrev = newActivationLabel;
});
} catch (e) {
log(true, 'error trying to modify activation labels', e);
}
// if description is populated and appearance isn't use description as appearance
try {
log(false, sheetData);
if (!!sheetData.data.details.description?.value && !sheetData.data.details.appearance) {
sheetData.data.details.appearance = sheetData.data.details.description.value;
}
} catch (e) {
log(true, 'error trying to migrate description to appearance', e);
}
// Settings
sheetData.settingsShowInventoryIcons = game.settings.get(MODULE_ID, MySettings.showIconsOnInventoryList);
sheetData.settingsShowEquipInventory = game.settings.get(MODULE_ID, MySettings.showEquipOnInventoryList);
// system features
const systemVersion = game.system.data.version;
//@ts-ignore
sheetData.systemFeatures = {
//@ts-ignore
skillConfig: !foundry.utils.isNewerVersion('1.5.0', systemVersion),
//@ts-ignore
attributeConfig: !foundry.utils.isNewerVersion('1.5.0', systemVersion),
//@ts-ignore
profLabel: !foundry.utils.isNewerVersion('1.5.0', systemVersion),
//@ts-ignore
currencyLabel: !foundry.utils.isNewerVersion('1.5.0', systemVersion),
};
return sheetData;
}
}
/* ------------------------------------ */
/* Initialize module */
/* ------------------------------------ */
Hooks.once('init', async function () {
log(true, `Initializing ${MODULE_ID}`);
// Register custom module settings
registerSettings();
// Preload Handlebars templates
await preloadTemplates();
});
// Register OGL5eCharacterSheet Sheet
Actors.registerSheet('dnd5e', OGL5eCharacterSheet, {
label: 'OGL Character Sheet',
types: ['character'],
makeDefault: false,
});
Hooks.once('devModeReady', ({ registerPackageDebugFlag }) => {
registerPackageDebugFlag(MODULE_ID);
});