-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
betternpcsheet.js
620 lines (540 loc) · 24.2 KB
/
betternpcsheet.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/**
* @author Felix Müller aka syl3r86
*/
import ActorSheet5eNPC from "../../systems/dnd5e/module/actor/sheets/npc.js";
//let Actor5eSheet = CONFIG.Actor.sheetClass;
export class BetterNPCActor5eSheet extends ActorSheet5eNPC {
// ActorSheet5eNPC
get template() {
// adding the #equals and #unequals handlebars helper
Handlebars.registerHelper('equals', function (arg1, arg2, options) {
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('unequals', function (arg1, arg2, options) {
return (arg1 != arg2) ? options.fn(this) : options.inverse(this);
});
const path = "systems/dnd5e/templates/actors/";
if (!game.user.isGM && this.actor.limited) return path + "limited-sheet.html";
return "modules/betternpcsheet5e/template/npc-sheet.html";
}
static get defaultOptions() {
const options = super.defaultOptions;
mergeObject(options, {
classes: ["sheet", "better-npc-sheet-container"],
width: 600,
height: 300,
blockFavTab: true
});
return options;
}
getData() {
const data = super.getData();
data.config = CONFIG.DND5E;
data['useIcons'] = game.settings.get("betternpcsheet5e", "useIcons");
console.log(data);
console.log(data.effects);
data.effectCount = data.effects.inactive.effects.length + data.effects.passive.effects.length + data.effects.temporary.effects.length
return data;
}
activateListeners(html) {
super.activateListeners(html);
// only do stuff if its for npcs
if (this.actor.data.type === "character") {
return;
}
// set dynamic input width
let inputs = html.find('.npc-textinput,.npc-textinput-small');
inputs.keyup(e => {
let input = e.target;
let inputText = e.target.value || e.target.placeholder;
let prop = ["font-style", "font-variant", "font-weight", "font-size", "font-family"];
let font = "";
for (let x in prop)
font += window.getComputedStyle(input, null).getPropertyValue(prop[x]) + " ";
let element = document.createElement("canvas").getContext("2d");
element.font = font;
let txtWidth = element.measureText(inputText).width * 1.1 + 5;
e.target.style.width = txtWidth + "px";
});
inputs.change(e => {
inputs.trigger('keyup');
});
inputs.trigger('keyup');
// adding toggle for item detail
html.find('.npc-item-name').click(event => {
this._onItemSummary(event)
});
// make categorys colapsable
html.find('.body-tile-name').click(e => {
let target = e.target.getAttribute('data-tile');
let collapsTarget = e.target.getAttribute('data-target') || 'item';
let targetTile = `.body-tile[data-tile=${target}] .${collapsTarget}`;
html.find(targetTile).toggle(100);
});
// remove window padding
$('.better-npc-sheet').parent().css('padding', '0');
if (this.isEditable) {
// rebind roll function
html.find('.item .rollable').click(event => this._onItemRoll(event));
// spellslot control buttons
html.find('.spellslot-mod').click(ev => {
let mod = event.target.getAttribute("data-mod");
let level = event.target.getAttribute("data-level");
let slotElement = $(html.find(`input[name="data.spells.spell${level}.value"]`));
let newValue = mod == '+' ? Number(slotElement.val()) + 1 : Number(slotElement.val()) - 1;
slotElement.val(newValue >= 0 ? newValue : 0);
slotElement.trigger('submit');
});
// list changing logic:
html.find('.item-change-list').click(ev => {
let target = $(ev.target).parents('.item').find('.type-list');
target.toggle(200);
});
html.find('.npc-item-name').contextmenu(ev => {
let target = $(ev.target).parents('.item').find('.type-list');
target.toggle(200);
});
html.find('.type-list a').click(ev => {
let targetList = ev.target.dataset.value
let itemId = $(ev.target).parents('.item').attr('data-item-id');
let item = this.actor.getOwnedItem(itemId);
item.update({ "flags.adnd5e.itemInfo.type": targetList });
});
// Rollable Health Formula
html.find(".npc-roll-hp").click(this._onRollHPFormula.bind(this));
// toggle edit mode button event
this.editMode = this.object.data.flags.betterNpcSheet?.editMode;
if(this.editMode === undefined) this.editMode = false;
this._applySettingsMode(this.editMode, html);
html.find('.editBtn').click(e => {
this.object.update({ 'flags.betterNpcSheet.editMode': !this.editMode });
});
// apply saved item detail display state
this.saveState = false;
for (let element of html.find('.npc-item-name')) {
let item = this.actor.getOwnedItem($(element).parents('.item').data("item-id"));
if (hasProperty(item, 'data.flags.betternpcsheet5e.showItemSummary') && item.data.flags.betternpcsheet5e.showItemSummary) {
$(element).trigger('click');
}
}
this.saveState = true;
} else {
this._applySettingsMode(false, html);
}
}
render(force = false, options = {}) {
if (this.supressRender) {
return;
}
if (force) {
let newWidth = getProperty(this.object.data.flags, 'betterNpcSheet.sheet.width');
let newHeight = getProperty(this.object.data.flags, 'betterNpcSheet.sheet.height');
if (newWidth === undefined || newHeight === undefined) {
let columnCount = 2;
if (this.object.data.items.length > 10) {
columnCount = 3;
}
newWidth = columnCount * 300;
newHeight = columnCount * 300;
}
this.position.width = newWidth;
this.position.height = newHeight;
}
return super.render(force, options);
}
async close() {
if (this.isEditable) {
this.object.update({ 'flags.betterNpcSheet.sheet.height': this.position.height, 'flags.betterNpcSheet.sheet.width': this.position.width });
}
super.close();
}
async _onSpellSlotOverride(event) {
let span = event.currentTarget.parentElement;
let level = span.dataset.level;
let input = $(span).children('.spellslot-input');
input.attr('name', `data.spells.${level}.override`);
input.attr('readonly', false);
$(span).children('.slot-max-override').remove();
}
async _onItemSummary(event) {
super._onItemSummary(event);
if (this.isEditable && this.saveState !== false) {
let li = $(event.currentTarget).parents(".item");
let item = this.actor.getOwnedItem(li.data("item-id"));
let showItemSummary = true;
if (hasProperty(item, 'data.flags.betternpcsheet5e.showItemSummary')) {
showItemSummary = !item.data.flags.betternpcsheet5e.showItemSummary;
}
this.supressRender = true;
await item.setFlag('betternpcsheet5e', 'showItemSummary', showItemSummary);
this.supressRender = false;
}
}
_applySettingsMode(editMode, html) {
let hidable = html.find('.hidable');
for (let obj of hidable) {
let data = obj.getAttribute('data-hidable-attr');
if (data == '' || data == 0) {
if (editMode == false) {
obj.style.display = 'none';
} else {
obj.style.display = '';
}
}
//let hidableAttr = obj.getElementsByClassName('.hidable-attr');
}
if (editMode) {
console.log('in edit mode');
html.find('.show-on-edit').show();
html.find('.hide-on-edit').hide();
html.find('input').addClass('white-input');//.css('background', 'white');
html.find('.saves-div').show();
html.find('.skills-div').show();
} else {
html.find('.show-on-edit:not(.hidable)').hide();
html.find('.hide-on-edit').show();
html.find('input').removeClass('white-input');//.css('background', 'none');
if (html.find('.saves-div .hidable[data-hidable-attr="1"]').length == 0) {
html.find('.saves-div').hide();
}
if (html.find('.skills-div .hidable[data-hidable-attr="1"], .skills-div .hidable[data-hidable-attr="0.5"], .skills-div .hidable[data-hidable-attr="2"]').length == 0) {
html.find('.skills-div').hide();
}
}
}
/**
* Organize and classify Items for NPC sheets
* @private
*/
_prepareItems(actorData) {
// Features
const features = [];
// Weapons
const weapons = [];
const legendarys = [];
const reactions = [];
const lair = [];
const loot = [];
// Spellbook
const spellbook = {};
// Iterate through items, allocating to containers
for (let i of actorData.items) {
i.img = i.img || DEFAULT_TOKEN;
i.hasUses = i.data.uses && (i.data.uses.max > 0);
i.isOnCooldown = i.data.recharge && !!i.data.recharge.value && (i.data.recharge.charged === false);
// Spells
if (i.type === "spell") {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = game.settings.get("betternpcsheet5e", "expandSpells");
}
let lvl = i.data.level || 0;
let section = lvl;
let sectionLabel = CONFIG.DND5E.spellLevels[lvl];
let isCantrip = lvl === 0 ? true : false;
switch (i.data.preparation.mode) {
case 'always':
section = 'always';
sectionLabel = 'At Will';
isCantrip = true; break;
case 'innate':
section = 'innate';
sectionLabel = 'Innate Spellcasting';
isCantrip = true; break;
case 'pact':
section = 'pact';
sectionLabel = 'Pact';
isCantrip = true; break;
}
let uses = {
value: 0,
max: 0
}
if (!isCantrip) {
uses.value = actorData.data.spells["spell" + lvl].value;
uses.max = actorData.data.spells["spell" + lvl].max;
if (actorData.data.spells["spell" + lvl].override) uses.override = actorData.data.spells["spell" + lvl].override;
}
spellbook[section] = spellbook[section] || {
isCantrip: isCantrip,
label: sectionLabel,
spells: [],
uses: uses
};
//i.data.school.str = CONFIG.DND5E.spellSchools[i.data.school.value];
spellbook[section].spells.push(i);
continue;
}
// Features
let flag = getProperty(i, 'flags.adnd5e.itemInfo.type');
switch (flag) {
case 'feat': features.push(i); break;
case 'action': weapons.push(i); break;
case 'legendary': legendarys.push(i); break;
case 'reaction': reactions.push(i); break;
case 'lair': lair.push(i); break;
case 'loot': loot.push(i); break;
default: {
let type = getProperty(i, 'data.activation.type');
switch (type) {
case "legendary": legendarys.push(i); continue;
case "lair": lair.push(i); continue;
case "action": weapons.push(i); continue;
default: {
if (i.type === "weapon") weapons.push(i);
else if (i.type === "feat") features.push(i);
else if (i.type === "loot") loot.push(i);
else if (["equipment", "consumable", "tool", "backpack"].includes(i.type)) features.push(i);
}
}
}
}
}
let expandItem = game.settings.get("betternpcsheet5e", "expandFeats");
for (let i of features) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandAttacks");
for (let i of weapons) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandReactions");
for (let i of reactions) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandLegendary");
for (let i of legendarys) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandLair");
for (let i of lair) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
expandItem = game.settings.get("betternpcsheet5e", "expandLoot");
for (let i of loot) {
if (!hasProperty(i, 'flags.betternpcsheet5e')) {
i.flags.betternpcsheet5e = {};
}
if (!hasProperty(i, 'flags.betternpcsheet5e.showItemSummary')) {
i.flags.betternpcsheet5e.showItemSummary = expandItem;
}
}
// Assign the items
let useIcons = game.settings.get("betternpcsheet5e", "useIcons");
let sections = [
{ label: game.i18n.localize('DND5E.Features'), name: 'feat', type: 'feat', isFeat: true, items: features, useIcons: useIcons },
{ label: game.i18n.localize('BNPCSheet.Actions'), name: 'action', type: 'weapon', isAction: true, items: weapons, useIcons: useIcons },
{ label: game.i18n.localize('DND5E.LegAct'), name: 'legendary', type: 'feat', isLegendary: true, items: legendarys, useIcons: useIcons },
{ label: game.i18n.localize('BNPCSheet.Reactions'), name: 'reaction', type: 'feat', isReaction: true, items: reactions, useIcons: useIcons },
{ label: game.i18n.localize('BNPCSheet.LairActs'), name: 'lair', type: 'feat', isLair: true, items: lair, useIcons: useIcons },
{ label: game.i18n.localize('BNPCSheet.Loot'), name: 'loot', type: 'loot', isLoot: true, items: loot, useIcons: useIcons }
];
if(Object.keys(spellbook).length === 0) {
actorData.actor.spellbook = 0;
} else {
actorData.actor.spellbook = spellbook;
}
actorData.actor.sections = sections;
}
async _onDropItem(event, data) {
if (data.actorId !== this.object.id || data.data === undefined) {
return super._onDropItem(event, data);
}
let typeFlag = data.data.flags?.adnd5e?.itemInfo?.type;
let targetTile = $(event.toElement).parents('.body-tile');
let targetType = targetTile.length > 0 ? targetTile[0].dataset.tile : '';
if (targetType && targetType.indexOf('spell') === -1 && targetType !== typeFlag) {
let item = this.actor.getOwnedItem(data.data._id);
item.update({ 'flags.adnd5e.itemInfo.type': targetType });
} else {
super._onDropItem(event, data);
}
}
_onItemCreate(event) {
event.preventDefault();
let itemTypeLabel = $(event.target).parents('.body-tile').attr('data-tile');
let header = event.currentTarget;
let itemType = $(event.currentTarget).parents('.body-tile').children('.npc-item-create').attr('data-type');
let data = {};
data = {
type: header.dataset.type,
data: {},
flags: {
'adnd5e': {
'itemInfo': {
'type': itemTypeLabel
}
}
}
}
data["name"] = `New ${itemTypeLabel.capitalize()}`;
if (itemTypeLabel === 'legendary' || itemTypeLabel === 'lair') {
data["name"] += ' Action';
}
this.actor.createOwnedItem(data); // , { renderSheet: true } adding that back in once the core functionality has been fixed
}
_onSortItem(event, itemData) {
// TODO - for now, don't allow sorting for Token Actor ovrrides
if (this.actor.isToken) return;
// Get the drag source and its siblings
const source = this.actor.getOwnedItem(itemData._id);
const siblings = this._getSortSiblings(source);
// Get the drop target
const dropTarget = event.target.closest(".item");
const targetId = dropTarget ? dropTarget.dataset.itemId : null;
const target = siblings.find(s => s.data._id === targetId);
// Perform the sort
const sortUpdates = SortingHelpers.performIntegerSort(source, { target: target, siblings });
const updateData = sortUpdates.map(u => {
const update = u.update;
update._id = u.target.data._id;
return update;
});
// Perform the update
return this.actor.updateEmbeddedEntity("OwnedItem", updateData);
}
_getSortSiblings(source) {
if (source.data.type === 'spell') {
return super._getSortSiblings(source);
}
let sourceType = this._getItemType(source);
return this.actor.items.filter(i => {
let type = this._getItemType(i);
return (sourceType === type) && (i.data._id !== source.data._id)
});
}
_getItemType(item) {
if (item.data.type === 'spell') {
return 'spell'
}
let sourceType = getProperty(item.data, 'flags.adnd5e.itemInfo.type');
if (sourceType === undefined) {
let activationType = getProperty(item.data, 'data.activation.type');
switch (activationType) {
case "legendary": sourceType = "legendary";
case "lair": sourceType = "lair";
case "action": sourceType = "action";
case "reaction": sourceType = "reaction";
default: {
if (item.data.type === "weapon") sourceType = "action";
else if (item.data.type === "feat") sourceType = "feat";
else if (item.data.type === "loot") sourceType = "loot";
else if (["equipment", "consumable", "tool", "backpack"].includes(item.data.type)) sourceType = "feat";
}
}
}
return sourceType;
}
}
Actors.registerSheet("dnd5e", BetterNPCActor5eSheet, {
types: ["npc"],
makeDefault: true
});
Hooks.on('init', () => {
loadTemplates(['modules/betternpcsheet5e/template/section.hbs']);
});
Hooks.on('ready', () => {
window.setTimeout(() => {
if (window.BetterRolls) {
console.log('BetterNPCSheet - Registering Better Rolls');
window.BetterRolls.hooks.addActorSheet("BetterNPCActor5eSheet");
// window.BetterRolls.hooks.registerActorSheet("BetterNPCActor5eSheet", ".item .npc-item-name", ".item-summary", {
// itemButton: '.item .rollable',
// abilityButton: ".ability h4.ability-name.rollable",
// checkButton: ".ability div span.ability-mod",
// saveButton: ".saves-div .save .rollable"
// });
}
}, 2000);
game.settings.register("betternpcsheet5e", "useIcons", {
name: game.i18n.localize("BNPCSheet.useIcons"),
hint: game.i18n.localize("BNPCSheet.useIconsHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandFeats", {
name: game.i18n.localize("BNPCSheet.featSetting"),
hint: game.i18n.localize("BNPCSheet.featSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandAttacks", {
name: game.i18n.localize("BNPCSheet.attackSetting"),
hint: game.i18n.localize("BNPCSheet.attackSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandReactions", {
name: game.i18n.localize("BNPCSheet.reactionSetting"),
hint: game.i18n.localize("BNPCSheet.reactionSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandLegendary", {
name: game.i18n.localize("BNPCSheet.legendarySetting"),
hint: game.i18n.localize("BNPCSheet.legendarySettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandLair", {
name: game.i18n.localize("BNPCSheet.lairSetting"),
hint: game.i18n.localize("BNPCSheet.lairSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandSpells", {
name: game.i18n.localize("BNPCSheet.spellsSetting"),
hint: game.i18n.localize("BNPCSheet.spellsSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("betternpcsheet5e", "expandLoot", {
name: game.i18n.localize("BNPCSheet.lootSetting"),
hint: game.i18n.localize("BNPCSheet.lootSettingHelp"),
scope: "world",
config: true,
default: false,
type: Boolean
});
});