Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System version 4.0 compatibility #77

Merged
merged 11 commits into from
Sep 17, 2024
Prev Previous commit
Next Next commit
support new damage roll config dialog
  • Loading branch information
kaelad02 committed Sep 14, 2024
commit a6d5f879f25e219ba6e9c004679eab37d5c63914
40 changes: 40 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,48 @@ Hooks.on("renderDialog", async (dialog, html, data) => {
}
});

// New roll dialog hook, as of dnd5e v4.0
Hooks.on("renderRollConfigurationDialog", async (dialog, html) => {
debug("renderRollConfigurationDialog hook called");

const message = await prepareMessage(dialog.options);
if (message) {
// put messages inside their own fieldset
const messageFieldset = document.createElement("fieldset");
messageFieldset.innerHTML = message;
const legend = document.createElement("legend");
legend.innerText = "Messages";
messageFieldset.insertBefore(legend, messageFieldset.firstChild);
// add messages right after configuration
const configFieldset = html.querySelector('fieldset[data-application-part="configuration"]');
configFieldset.after(messageFieldset);
// swap "inline-roll" class for "dialog-roll"
const inlineRolls = html.querySelectorAll("a.inline-roll");
inlineRolls.forEach(ir => {
debug("found inline-roll", ir);
ir.classList.remove("inline-roll");
ir.classList.add("dialog-roll");
// add click listener
ir.addEventListener("click", (event) => {
// get the formula from the button
const button = event.currentTarget;
const formula = button.dataset.formula;
debug("adding to input:", formula);
// add the formula to the bonus input
const dialogContent = button.closest(".window-content");
const input = dialogContent.querySelector('.rolls input[name="roll.0.situational"]');
input.value = !!input.value ? `${input.value} + ${formula}` : formula;
// rebuild dialog (i.e. show new die icons)
dialog.rebuild();
});
});
}
});

async function prepareMessage(dialogOptions) {
const opt = dialogOptions["adv-reminder"];
if (!opt) return;
if (opt.rendered) return;

// merge the messages with the advantage/disadvantage from sources
const messages = [...(opt.messages ?? [])];
Expand Down Expand Up @@ -173,6 +212,7 @@ async function prepareMessage(dialogOptions) {
async: true,
});
debug("messages", messages, "enriched", enriched);
opt.rendered = true;
return enriched;
}
}
7 changes: 4 additions & 3 deletions src/rollers/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class CoreRollerHooks {
if (this.isFastForwarding(config, dialog)) return;
const target = getTarget();
const distanceFn = getDistanceToTargetFn(message.data.speaker);
const activity = config.origin;
const activity = config.subject;

new AttackMessageV2(activity.actor, target, activity).addMessage(dialog);
if (showSources) new AttackSourceV2(activity.actor, target, activity, distanceFn).updateOptions(dialog);
Expand Down Expand Up @@ -149,11 +149,12 @@ export default class CoreRollerHooks {
if (this.isFastForwarding(config, dialog)) return;
const target = getTarget();
const distanceFn = getDistanceToTargetFn(message.data.speaker);
const activity = config.origin;
const activity = config.subject;

new DamageMessageV2(activity.actor, target, activity).addMessage(dialog);
if (showSources) new CriticalSourceV2(activity.actor, target, activity, distanceFn).updateOptions(dialog);
new CriticalReminderV2(activity.actor, target, activity, distanceFn).updateOptions(config.rolls[0] ?? {}, "isCritical");
const reminder = new CriticalReminderV2(activity.actor, target, activity, distanceFn);
config.rolls.forEach(roll => reminder.updateOptions(roll.options, "isCritical"));
}

/**
Expand Down