-
Notifications
You must be signed in to change notification settings - Fork 1
/
marvel-multiverse.mjs
283 lines (247 loc) · 9.78 KB
/
marvel-multiverse.mjs
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
// Import document classes.
import { MarvelMultiverseActor } from './module/documents/actor.mjs';
import { MarvelMultiverseItem } from './module/documents/item.mjs';
import { ChatMessageMarvel } from './module/documents/chat-message.mjs';
// Import sheet classes.
import { MarvelMultiverseCharacterSheet } from './module/sheets/character-sheet.mjs';
import { MarvelMultiverseNPCSheet } from './module/sheets/npc-sheet.mjs';
import { MarvelMultiverseItemSheet } from './module/sheets/item-sheet.mjs';
// Import helper/utility classes and constants.
import { preloadHandlebarsTemplates } from './module/helpers/templates.mjs';
import { MARVEL_MULTIVERSE } from './module/helpers/config.mjs';
// Import DataModel classes
import * as models from './module/data/_module.mjs';
import * as dice from "./module/dice/_module.mjs";
/* -------------------------------------------- */
/* Init Hook */
/* -------------------------------------------- */
Hooks.once('init', function () {
// Add utility classes to the global game object so that they're more easily
// accessible in global contexts.
game.MarvelMultiverse = {
MarvelMultiverseActor,
MarvelMultiverseItem,
rollItemMacro,
config: MARVEL_MULTIVERSE,
dice,
models,
MarvelMultiverseCharacterSheet,
MarvelMultiverseNPCSheet,
MarvelMultiverseItemSheet,
ChatMessageMarvel
};
// Record Configuration Values
CONFIG.MARVEL_MULTIVERSE = MARVEL_MULTIVERSE;
console.log(`Marvel Multiverse RPG 1e | Initializing the Marvel Multiverse Role Playing Game System - Version ${game.system.version}\n${MARVEL_MULTIVERSE.ASCII}`);
/**
* Set an initiative formula for the system
* @type {String}
*/
CONFIG.Combat.initiative = {
formula: '{1d6,1dm,1d6} + @attributes.init.value',
decimals: 2,
};
// Define custom Document and DataModel classes
CONFIG.Actor.documentClass = MarvelMultiverseActor;
// Note that you don't need to declare a DataModel
// for the base actor/item classes - they are included
// with the Character/NPC as part of super.defineSchema()
CONFIG.Actor.dataModels = {
character: models.MarvelMultiverseCharacter,
npc: models.MarvelMultiverseNPC
}
CONFIG.ChatMessage.documentClass = ChatMessageMarvel;
CONFIG.Item.documentClass = MarvelMultiverseItem;
CONFIG.Item.dataModels = {
item: models.MarvelMultiverseItem,
weapon: models.MarvelMultiverseWeapon,
trait: models.MarvelMultiverseTrait,
origin: models.MarvelMultiverseOrigin,
occupation: models.MarvelMultiverseOccupation,
tag: models.MarvelMultiverseTag,
power: models.MarvelMultiversePower
}
// Active Effects are never copied to the Actor,
// but will still apply to the Actor from within the Item
// if the transfer property on the Active Effect is true.
CONFIG.ActiveEffect.legacyTransferral = false;
CONFIG.Dice.MarvelDie = dice.MarvelDie;
CONFIG.Dice.types.push(dice.MarvelDie);
Roll.TOOLTIP_TEMPLATE = "systems/marvel-multiverse/templates/chat/roll-breakdown.hbs";
Roll.CHAT_TEMPLATE = "systems/marvel-multiverse/templates/dice/roll.hbs"
CONFIG.Dice.MarvelMultiverseRoll = dice.MarvelMultiverseRoll;
// Register Roll Extensions
CONFIG.Dice.rolls.push(dice.MarvelMultiverseRoll);
CONFIG.Dice.terms.m = dice.MarvelDie;
// Add fonts
_configureFonts();
// Register sheet application classes
Actors.unregisterSheet('core', ActorSheet);
Actors.registerSheet('marvel-multiverse', MarvelMultiverseCharacterSheet, {
types: ["character"],
makeDefault: true,
label: 'MARVEL_MULTIVERSE.SheetLabels.Actor',
});
Actors.registerSheet('marvel-multiverse', MarvelMultiverseNPCSheet, {
types: ["npc"],
makeDefault: true,
label: 'MARVEL_MULTIVERSE.SheetLabels.NPC',
});
Items.unregisterSheet('core', ItemSheet);
Items.registerSheet('marvel-multiverse', MarvelMultiverseItemSheet, {
makeDefault: true,
label: 'MARVEL_MULTIVERSE.SheetLabels.Item',
});
// Preload Handlebars templates.
return preloadHandlebarsTemplates();
});
/* -------------------------------------------- */
/* Handlebars Helpers */
/* -------------------------------------------- */
// If you need to add Handlebars helpers, here is a useful example:
Handlebars.registerHelper('toLowerCase', function (mle) {
return mle.toLowerCase();
});
/* -------------------------------------------- */
/**
* Configure additional system fonts.
*/
function _configureFonts() {
Object.assign(CONFIG.fontDefinitions, {
Roboto: {
editor: true,
fonts: [
{ urls: ["systems/marvel-multiverse/fonts/roboto/Roboto-Regular.woff2"] },
{ urls: ["systems/marvel-multiverse/fonts/roboto/Roboto-Bold.woff2"], weight: "bold" },
{ urls: ["systems/marvel-multiverse/fonts/roboto/Roboto-Italic.woff2"], style: "italic" },
{ urls: ["systems/marvel-multiverse/fonts/roboto/Roboto-BoldItalic.woff2"], weight: "bold", style: "italic" }
]
},
"Roboto Condensed": {
editor: true,
fonts: [
{ urls: ["systems/marvel-multiverse/fonts/roboto-condensed/RobotoCondensed-Regular.woff2"] },
{ urls: ["systems/marvel-multiverse/fonts/roboto-condensed/RobotoCondensed-Bold.woff2"], weight: "bold" },
{ urls: ["systems/marvel-multiverse/fonts/roboto-condensed/RobotoCondensed-Italic.woff2"], style: "italic" },
{
urls: ["systems/marvel-multiverse/fonts/roboto-condensed/RobotoCondensed-BoldItalic.woff2"], weight: "bold",
style: "italic"
}
]
},
"Roboto Slab": {
editor: true,
fonts: [
{ urls: ["systems/marvel-multiverse/fonts/roboto-slab/RobotoSlab-Regular.ttf"] },
{ urls: ["systems/marvel-multiverse/fonts/roboto-slab/RobotoSlab-Bold.ttf"], weight: "bold" }
]
}
});
}
/* -------------------------------------------- */
/* Ready Hook */
/* -------------------------------------------- */
Hooks.once('ready', function () {
// Wait to register hotbar drop hook on ready so that modules could register earlier if they want to
Hooks.on('hotbarDrop', (bar, data, slot) => createItemMacro(data, slot));
});
/* -------------------------------------------- */
/* Render Settings Hook */
/* -------------------------------------------- */
Hooks.on("renderSettings", (app, [html]) => {
const details = html.querySelector("#game-details");
const pip = details.querySelector(".system-info .update");
details.querySelector(".system").remove();
const heading = document.createElement("div");
heading.classList.add("mmrpg", "sidebar-heading");
heading.innerHTML = `
<h2 class='mmrpg-game-title'>${game.system.title}
<ul class="links mmrpg-ul">
<li>
<a href="https://github.com/mjording/marvel-multiverse/releases/latest" target="_blank">
Marvel Multiverse RPG
</a>
</li>
<li>
<a href="https://github.com/mjording/marvel-multiverse/issues" target="_blank">${game.i18n.localize("MARVEL_MULTIVERSE.Issues")}</a>
</li>
<li>
<a href="https://github.com/mjording/marvel-multiverse/wiki" target="_blank">${game.i18n.localize("MARVEL_MULTIVERSE.Wiki")}</a>
</li>
</ul>
</h2>
`;
details.insertAdjacentElement("afterend", heading);
const badge = document.createElement("div");
badge.classList.add("mmrpg", "system-badge");
badge.innerHTML = `
<img src="systems/marvel-multiverse/ui/official/mmrpg-badge-32.webp" data-tooltip="${game.system.title}" alt="${game.system.title}">
<span class="system-info">${game.system.version}</span>
`;
if ( pip ) badge.querySelector(".system-info").insertAdjacentElement("beforeend", pip);
heading.insertAdjacentElement("afterend", badge);
});
Hooks.on("renderChatLog", (app, html, data) => {
ChatMessageMarvel.onRenderChatLog(html);
});
/* -------------------------------------------- */
/* Hotbar Macros */
/* -------------------------------------------- */
/**
* Create a Macro from an Item drop.
* Get an existing item macro if one exists, otherwise create a new one.
* @param {Object} data The dropped data
* @param {number} slot The hotbar slot to use
* @returns {Promise}
*/
async function createItemMacro(data, slot) {
// First, determine if this is a valid owned item.
if (data.type !== 'Item' || data.type !== 'Weapon') return;
if (!data.uuid.includes('Actor.') && !data.uuid.includes('Token.')) {
return ui.notifications.warn(
'You can only create macro buttons for owned Items'
);
}
// If it is, retrieve it based on the uuid.
const item = await Item.fromDropData(data);
// Create the macro command using the uuid.
const command = `game.MarvelMultiverse.rollItemMacro("${data.uuid}");`;
let macro = game.macros.find(
(m) => m.name === item.name && m.command === command
);
if (!macro) {
macro = await Macro.create({
name: item.name,
type: 'script',
img: item.img,
command: command,
flags: { 'marvel-multiverse.itemMacro': true },
});
}
game.user.assignHotbarMacro(macro, slot);
return false;
}
/**
* Create a Macro from an Item drop.
* Get an existing item macro if one exists, otherwise create a new one.
* @param {string} itemUuid
*/
function rollItemMacro(itemUuid) {
// Reconstruct the drop data so that we can load the item.
const dropData = {
type: 'Item',
uuid: itemUuid,
};
// Load the item from the uuid.
Item.fromDropData(dropData).then((item) => {
// Determine if the item loaded and if it's an owned item.
if (!item || !item.parent) {
const itemName = item?.name ?? itemUuid;
return ui.notifications.warn(
`Could not find item ${itemName}. You may need to delete and recreate this macro.`
);
}
// Trigger the item roll
item.roll();
});
}