Skip to content

Commit

Permalink
Keybind to make observable
Browse files Browse the repository at this point in the history
  • Loading branch information
Eligarf committed Dec 7, 2024
1 parent 6c20b52 commit 36deb0a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v1.16.0
* Add a keybinding to make selected tokens observerable by removing `hidden`, `undetected`, or `unnoticed` condition items. Will also clear out any `PF2E Perception` flags on those tokens.

# v1.15.1
* Mark module as not yet compatible with v13

Expand Down
45 changes: 35 additions & 10 deletions esmodules/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ function getPerceptiveApi() {

export { MODULE_ID, PF2E_PERCEPTION_ID, PERCEPTIVE_ID, log, getPerceptionApi, getPerceptiveApi };

async function clearPerceptionData(token) {
// Remove any ids that perception is tracking
const perceptionData = token.flags?.[PF2E_PERCEPTION_ID]?.data;
if (!perceptionData || !Object.keys(perceptionData).length) return;
let tokenUpdate = {};
for (let id in perceptionData) {
tokenUpdate[`flags.${PF2E_PERCEPTION_ID}.data.-=${id}`] = true;
}
const updates = [{ _id: token.id, ...tokenUpdate }];
await canvas.scene.updateEmbeddedDocuments("Token", updates);
}

Hooks.once('init', () => {
Hooks.on('createChatMessage', async (message, options, id) => {
if (game.userId != id) return;
Expand All @@ -50,6 +62,28 @@ Hooks.once('init', () => {
// Roll the damage!
origin?.rollDamage({ target: message.token });
});

game.keybindings.register(MODULE_ID, "observable", {
name: `${MODULE_ID}.observable.name`,
hint: `${MODULE_ID}.observable.hint`,
editable: [
{ key: "B" }
],
onDown: async () => {
const conditionHandler = game.settings.get(MODULE_ID, 'conditionHandler');
const perceptionApi = (conditionHandler === 'perception') ? getPerceptionApi() : null;
const selectedTokens = canvas.tokens.controlled;
for (const token of selectedTokens) {
if (perceptionApi) await clearPerceptionData(token.document);
const conditions = token.actor.items
.filter((i) => ['hidden', 'undetected', 'unnoticed'].includes(i.system.slug))
.map((i) => i.id);
if (conditions.length > 0) {
await token.actor.deleteEmbeddedDocuments("Item", conditions);
}
}
}
});
});

function migrate(moduleVersion, oldVersion) {
Expand Down Expand Up @@ -80,16 +114,7 @@ Hooks.once('ready', () => {
// Get the token on the current scene
const token = options.parent?.parent ?? canvas.scene.tokens.find((t) => t.actorId === options.parent.id);
if (!token) return;

// Remove any ids that perception is tracking
const perceptionData = token.flags?.[PF2E_PERCEPTION_ID]?.data;
if (!perceptionData || !Object.keys(perceptionData).length) return;
let tokenUpdate = {};
for (let id in perceptionData) {
tokenUpdate[`flags.${PF2E_PERCEPTION_ID}.data.-=${id}`] = true;
}
const updates = [{ _id: token.id, ...tokenUpdate }];
await canvas.scene.updateEmbeddedDocuments("Token", updates);
await clearPerceptionData(token);
}

if (game.modules.get(PF2E_PERCEPTION_ID)?.active) {
Expand Down
4 changes: 4 additions & 0 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"name": "Compute Cover at Combat Start",
"hint": "Use 'PF2e Perception' to automatically compute cover between tokens using stealth for initiative and non-allied Tokens. It is only used for this check, and will not utilize greater cover"
},
"observable": {
"name": "Make selected tokens observable",
"hint": "Remove hidden, undetected, and unnoticed statuses from selected tokens."
},
"schema": {
"name": "Schema Version",
"hint": "Set this to a previous version to force a data migration from the specified version to the current version"
Expand Down
4 changes: 4 additions & 0 deletions languages/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"name": "Oblicz osłony na początku walki",
"hint": "Użyj „PF2e Perception”, aby automatycznie obliczyć osłonę między tokenami używającymi Ukrycia dla inicjatywy i niesprzymierzonych tokenów. Jest on używany tylko do tego sprawdzenia i nie będzie wykorzystywał większej osłony"
},
"observable": {
"name": "Make selected tokens observable",
"hint": "Remove hidden, undetected, and unnoticed statuses from selected tokens."
},
"schema": {
"name": "Wersja schematu",
"hint": "Ustaw to na poprzednią wersję, aby wymusić migrację danych z określonej wersji do wersji bieżącej."
Expand Down

0 comments on commit 36deb0a

Please sign in to comment.