-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbundle.js
87 lines (81 loc) · 2.97 KB
/
bundle.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
// This file has been modified from the original PF2e toolbox module.
const settingsKey = "pf2e-flatten";
function registerSettings() {
game.settings.register(settingsKey, "autoflatten", {
name: "pf2e-flatten.settings.autoflatten.name",
hint: "pf2e-flatten.settings.autoflatten.hint",
scope: "world",
config: true,
type: Boolean,
default: false
}),
game.settings.register(settingsKey, "halflevel", {
name: "pf2e-flatten.settings.halflevel.name",
hint: "pf2e-flatten.settings.halflevel.hint",
scope: "world",
config: true,
type: Boolean,
default: false
})
};
Hooks.once("init", () => {
registerSettings();
});
Hooks.on('createActor', AutoFlattenNPC );
Hooks.on('getActorDirectoryEntryContext', onFlattenProficiencyContextHook);
async function AutoFlattenNPC(li){
if(game.settings.get(settingsKey, "autoflatten") === true){
const modifierName = game.settings.get(settingsKey, "halflevel") ? '1/2 Level Proficiency' :'Proficiency Without Level';
const id = li._id;
const actor = game.actors.get(id);
if(actor.type === 'npc'){
const level = game.settings.get(settingsKey, "halflevel") ? Math.max(Math.floor(parseInt(actor?.system['details'].level.value)/2), 0) : Math.max(parseInt(actor?.system['details'].level.value),0);
await actor.addCustomModifier('all', modifierName, -level, 'untyped');
}
}
}
async function onFlattenProficiencyContextHook(html, buttons) {
const modifierName = 'Proficiency Without Level';
const modifierSlug = 'proficiency-without-level';
const hasModifier = (actor) => {
const data = actor.system;
if (data.customModifiers && data.customModifiers.all) {
const all = data.customModifiers.all;
for (const modifier of all) {
if (modifier.label === modifierName) {
return true;
}
}
}
return false;
};
buttons.unshift({
name: 'PF2e Flatten NPC',
icon: '<i class="fas fa-level-down-alt"></i>',
condition: (li) => {
const id = li.data('document-id');
const actor = game.actors.get(id);
return actor?.type === 'npc' && !hasModifier(actor);
},
callback: async (li) => {
const id = li.data('document-id');
const actor = game.actors.get(id);
const level = game.settings.get(settingsKey, "halflevel") ? Math.max(Math.floor(parseInt(actor?.system['details'].level.value)/2), 0) : Math.max(parseInt(actor?.system['details'].level.value),0);
await actor.addCustomModifier('all', modifierName, -level, 'untyped');
}
});
buttons.unshift({
name: 'PF2e Unflatten NPC',
icon: '<i class="fas fa-level-up-alt"></i>',
condition: (li) => {
const id = li.data('document-id');
const actor = game.actors.get(id);
return actor?.type === 'npc' && hasModifier(actor);
},
callback: async (li) => {
const id = li.data('document-id');
const actor = game.actors.get(id);
await actor.removeCustomModifier('all', modifierSlug);
},
});
}