Skip to content

Commit

Permalink
feat(tabulanil-sanity.js): add setting for message
Browse files Browse the repository at this point in the history
The insanity tier change now will default to sending a whisper to the GM
and actor owner, but with a module settings will use public chat
messages instead. This can be controlled by a setting added in the
module settings

refactor(tabulanil-sanity.js): move settings registration to its own
function

refactor(tabulanil-sanity.js): extract into object

The message data will be mostly the same be it a whisper or public chat
message. By extracting its contents into an object, it becomes easier to
implement the desired behaviour
  • Loading branch information
deadpyxel committed Jun 3, 2024
1 parent e8e8760 commit 14e62b0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
4 changes: 4 additions & 0 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"Name": "Display Sanity Bar on Player Character Sheets?",
"Hint": "Controls whether or not the module's Sanity bar is present on the Player Character sheet."
},
"publicSanMsg": {
"Name": "Show Insanity Tier changes in public chat",
"Hint": "Controls wether the insanity tier notification on chat should be visible to all or only to the GMa nd the actor owner"
},
"tokenHUDEnable": {
"Name": "Display Sanity controls on Token HUD",
"Hint": "Controls whether or not sanity controls are available on the Token HUD"
Expand Down
48 changes: 36 additions & 12 deletions scripts/tabulanil-sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class TabulanilSanity {
*/
static SETTINGS = {
INJECT_BUTTON: "injectButton",
HUD_ENABLE: "tokenHUDEnable"
HUD_ENABLE: "tokenHUDEnable",
PUBLIC_NOTIFICATION: "publicSanMsg",
}

/**
Expand All @@ -61,31 +62,49 @@ class TabulanilSanity {
}

/**
* Initialize module Settings
* Register the settings for the module
*/
static initialize() {
// Setting to show Sanity Bar on Actor sheet
static registerSettings() {
// Setting to show Sanity Bar on Actor sheet, defaults to true
game.settings.register(
this.ID, this.SETTINGS.INJECT_BUTTON, {
name: `TABULANIL_SANITY.settings.${this.SETTINGS.INJECT_BUTTON}.Name`, // Setting name
default: true, // default value
type: Boolean, // Type of setting
scope: "world", // scope of setting, either world (only changed by GM) or client (changeable by player, client only)
config: true, // show on settings page
hint: `TABULANIL_SANITY.settings.${this.SETTINGS.INJECT_BUTTON}.Hint`, // estra information for setting
hint: `TABULANIL_SANITY.settings.${this.SETTINGS.INJECT_BUTTON}.Hint`, // extra information for setting
}
);
// Setting to show TokenHUD element to control sanity
// Setting to show TokenHUD element to control sanity, defaults to true
game.settings.register(
this.ID, this.SETTINGS.HUD_ENABLE, {
name: `TABULANIL_SANITY.settings.${this.SETTINGS.HUD_ENABLE}.Name`, // Setting name
default: true, // default value
type: Boolean, // Type of setting
scope: "world", // scope of setting, either world (only changed by GM) or client (changeable by player, client only)
config: true, // show on settings page
hint: `TABULANIL_SANITY.settings.${this.SETTINGS.HUD_ENABLE}.Hint`, // estra information for setting
hint: `TABULANIL_SANITY.settings.${this.SETTINGS.HUD_ENABLE}.Hint`, // extra information for setting
}
);
// Setting to show insanity Tier messages in public chat, defaults to whisper.
game.settings.register(
this.ID, this.SETTINGS.PUBLIC_NOTIFICATION, {
name: `TABULANIL_SANITY.settings.${this.SETTINGS.PUBLIC_NOTIFICATION}.Name`, // Setting name
default: false, // default value
type: Boolean, // Type of setting
scope: "world", // scope of setting, either world (only changed by GM) or client (changeable by player, client only)
config: true, // show on settings page
hint: `TABULANIL_SANITY.settings.${this.SETTINGS.PUBLIC_NOTIFICATION}.Hint`, // extra information for setting
}
);
}

/**
* Initialize module Settings
*/
static initialize() {
this.registerSettings();
}
}

Expand Down Expand Up @@ -431,18 +450,23 @@ Hooks.on(TabulanilSanity.HOOKS.INSANITY_CHANGE, (insanityChanges, actor) => {
* @param {object} data - The data to be posted in the chat message.
*/
const postMessage = async (data) => {
await getDocumentClass("ChatMessage").create({
const messageData = {
// render chat message using custom template
content: await renderTemplate(TabulanilSanity.TEMPLATES.CHAT_MESSAGE, data),
// whisper message to owners of the actor
// TODO: control this using a setting (allow for public posting)
whisper: game.users.filter(u => actor.testUserPermission(u, "OWNER")).map(u => u.id),
// Change speaker field
speaker: ChatMessage.implementation.getSpeaker({
actor: actor,
alias: game.i18n.localize("TABULANIL_SANITY.moduleSpeaker")
})
});
}
if (!game.settings.get(TabulanilSanity.ID, TabulanilSanity.SETTINGS.PUBLIC_NOTIFICATION)) {
TabulanilSanity.log(false, "Insanity Tier changes are set to public");
// whisper message to owners of the actor
// TODO: control this using a setting (allow for public posting)
messageData.whisper = game.users.filter(u => actor.testUserPermission(u, "OWNER")).map(u => u.id)
}

await getDocumentClass("ChatMessage").create(messageData);
};

const insanityTierName = game.i18n.localize(`TABULANIL_SANITY.insanityTiers.TIER_${newTier}.shortName`);
Expand Down

0 comments on commit 14e62b0

Please sign in to comment.