All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

  1. import { FEATURE_CONTROL, TVA_CONFIG } from '../settings.js';
  2. import { registerHook, unregisterHook } from './hooks.js';
  3. const feature_id = 'UserMappings';
  4. export function registerUserMappingHooks() {
  5. if (!FEATURE_CONTROL[feature_id]) {
  6. ['updateToken', 'updateTile', 'sightRefresh'].forEach((id) => unregisterHook(feature_id, id));
  7. return;
  8. }
  9. registerHook(feature_id, 'updateToken', _updateToken);
  10. registerHook(feature_id, 'updateTile', _updateTile);
  11. registerHook(feature_id, 'sightRefresh', _sightRefresh);
  12. }
  13. async function _updateToken(token, change) {
  14. // Update User Specific Image
  15. if (change.flags?.['token-variants']) {
  16. if ('userMappings' in change.flags['token-variants'] || '-=userMappings' in change.flags['token-variants']) {
  17. const t = canvas.tokens.get(token.id);
  18. if (t) {
  19. await t.draw();
  20. canvas.effects.visibility.restrictVisibility();
  21. }
  22. }
  23. }
  24. }
  25. async function _updateTile(tile, change) {
  26. // Update User Specific Image
  27. if (change.flags?.['token-variants']) {
  28. if ('userMappings' in change.flags['token-variants'] || '-=userMappings' in change.flags['token-variants']) {
  29. const t = canvas.tiles.get(tile.id);
  30. if (t) {
  31. await t.draw();
  32. canvas.effects.visibility.restrictVisibility();
  33. }
  34. }
  35. }
  36. }
  37. function _sightRefresh() {
  38. if (!game.user.isGM) {
  39. for (let t of canvas.tokens.placeables) {
  40. if (_isInvisible(t)) t.visible = false;
  41. }
  42. for (let t of canvas.tiles.placeables) {
  43. if (_isInvisible(t)) t.visible = false;
  44. }
  45. }
  46. }
  47. function _isInvisible(obj) {
  48. const img = (obj.document.getFlag('token-variants', 'userMappings') || {})?.[game.userId];
  49. return img === TVA_CONFIG.invisibleImage;
  50. }