Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Jan 6, 2022
1 parent 1c13376 commit ea79697
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 60 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Item Piles Changelog

## Version 1.0.2
- Fixed issue where if an item pile was updated, it would break the ability for players to open the pile
- Fixed localization issue when an item pile was too far away from a player

## Version 1.0.1
- Added Pathfinder 1 system support
- Added Japanese localization
Expand Down
43 changes: 5 additions & 38 deletions scripts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1574,21 +1574,6 @@ export default class API {
}));
}

/**
* @private
*/
static async _registerClickEvents(targetDocument){
const shouldBeDeleted = await API._checkItemPileShouldBeDeleted(targetDocument.uuid);
if (!targetDocument || targetDocument.destroyed || !(targetDocument instanceof TokenDocument) || !API.isValidItemPile(targetDocument) || shouldBeDeleted) return;
const method = () => {
return doubleClickHandler.clicked(targetDocument);
}
if (!(targetDocument.owner || game.user.isGM) && !lib.object_has_event(targetDocument.object, "pointerdown", method)) {
lib.debug(`Registered pointerdown method for target with uuid ${targetDocument.uuid}`)
targetDocument.object.on('pointerdown', method);
}
}

/**
* Causes all connected users to re-render a specific pile's inventory UI
*
Expand Down Expand Up @@ -1642,7 +1627,7 @@ export default class API {
});
})).then(() => {
if(game.user.isGM){
API._refreshItemPile(tokenDocument);
API._refreshItemPile(tokenDocument.uuid);
}
})
}
Expand Down Expand Up @@ -1904,6 +1889,8 @@ export default class API {
*/
static async _itemPileClicked(pileDocument) {

if(!pileDocument || pileDocument.destroyed || !API.isValidItemPile(pileDocument)) return;

lib.debug(`Clicked: ${pileDocument.uuid}`);

const data = lib.getItemPileData(pileDocument);
Expand All @@ -1921,7 +1908,7 @@ export default class API {
})

if (!closestTokens.length && !game.user.isGM) {
lib.custom_warning(game.i18n.localize("ITEM-PILES.Dialogs.TooFar.Content"), true);
lib.custom_warning(game.i18n.localize("ITEM-PILES.Errors.PileTooFar"), true);
return;
}

Expand Down Expand Up @@ -2039,24 +2026,4 @@ export default class API {

}

const preloadedFiles = new Set();

const doubleClickHandler = {
_clicked: false,
_target: false,
clicked(target) {
if (target !== doubleClickHandler._target) {
doubleClickHandler._clicked = false
}
if (doubleClickHandler._clicked) {
doubleClickHandler._target = false;
doubleClickHandler._clicked = false;
return API._itemPileClicked(target);
}
doubleClickHandler._target = target;
doubleClickHandler._clicked = true;
setTimeout(() => {
doubleClickHandler._clicked = false
}, 500);
}
}
const preloadedFiles = new Set();
34 changes: 34 additions & 0 deletions scripts/hotkeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getTokensAtLocation } from "./lib/lib.js";
import API from "./api.js";

export function registerHotkeys(){

if(!game.user.isGM) {
let clicked = false;
window.addEventListener("mousedown", (event) => {
if (!canvas.ready) return;
if (!(canvas.activeLayer instanceof TokenLayer)) return;
if (game.activeTool !== "select") return;
const hover = document.elementFromPoint(event.clientX, event.clientY);
if (!hover || (hover.id !== "board")) return;
if (event.button !== 0) return;

const pos = canvas.app.renderer.plugins.interaction.mouse.getLocalPosition(canvas.app.stage);
const tokens = getTokensAtLocation(pos);
if(!tokens.length) return;
tokens.sort((a, b) => b.zIndex - a.zIndex);
const token = tokens[0].document;

if (clicked === token) {
clicked = false;
return API._itemPileClicked(token);
}

clicked = token;
setTimeout(() => {
clicked = false;
}, 500);
});
}

}
18 changes: 2 additions & 16 deletions scripts/lib/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function custom_error(error, notify = true) {
}

export function getTokensAtLocation(position) {
return canvas.tokens.placeables.filter(token => {
const tokens = [...canvas.tokens.placeables];
return tokens.filter(token => {
return position.x >= token.x && position.x < (token.x + (token.data.width * canvas.grid.size))
&& position.y >= token.y && position.y < (token.y + (token.data.height * canvas.grid.size));
});
Expand Down Expand Up @@ -76,21 +77,6 @@ export function distance_between(a, b) {
return new Ray(a, b).distance;
}

export function object_has_event(object, eventName, func) {

if (!object?._events?.[eventName]) return false;

let events = object?._events?.[eventName];
if (!Array.isArray(events)) events = [events];
for (let event of events) {
if (event.context === object && event.fn.toString() === func.toString()) {
return true;
}
}

return false;
}

export function getSimilarItem(items, { itemId, itemName, itemType }={}) {
for (const item of items) {
if (item.id === itemId || (item.name === itemName && item.type === itemType)) {
Expand Down
8 changes: 4 additions & 4 deletions scripts/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as lib from "./lib/lib.js";
import { ItemPileConfig } from "./formapplications/itemPileConfig.js";
import { registerLibwrappers } from "./libwrapper.js";
import { HOOKS } from "./hooks.js";
import { registerHotkeys } from "./hotkeys.js";

Hooks.once("init", () => {

Expand Down Expand Up @@ -56,6 +57,7 @@ Hooks.once("ready", () => {
if(game.modules.get('socketlib')) word = "activate";
throw lib.custom_error(`Item Piles requires the 'socketlib' module. Please ${word} it.`)
}
registerHotkeys();
Hooks.callAll(HOOKS.READY);
})

Expand Down Expand Up @@ -90,19 +92,17 @@ const module = {
const tokens = [...canvas.tokens.placeables].map(token => token.document);
for (const doc of tokens) {
await API._initializeItemPile(doc);
await API._registerClickEvents(doc);
}
},

async _createPile(doc) {
if (!API.isValidItemPile(doc)) return;
Hooks.callAll(HOOKS.PILE.CREATE, doc, lib.getItemPileData(doc));
await API._initializeItemPile(doc);
return API._registerClickEvents(doc);
return API._initializeItemPile(doc);
},

async _updatePile(doc){
return API._registerClickEvents(doc);
return API._initializeItemPile(doc);
},

async _deletePile(doc) {
Expand Down
2 changes: 0 additions & 2 deletions scripts/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const SOCKET_HANDLERS = {
TURN_INTO_PILE: "turnIntoPile",
REVERT_FROM_PILE: "revertFromPile",
REFRESH_PILE: "refreshItemPile",
REGISTER_PILE_EVENTS: "registerPileClickEvents",

/**
* UI sockets
Expand Down Expand Up @@ -65,7 +64,6 @@ export function registerSocket() {
itemPileSocket.register(SOCKET_HANDLERS.TURN_INTO_PILE, (...args) => API._turnTokenIntoItemPile(...args))
itemPileSocket.register(SOCKET_HANDLERS.REVERT_FROM_PILE, (...args) => API._revertTokenFromItemPile(...args))
itemPileSocket.register(SOCKET_HANDLERS.REFRESH_PILE, (...args) => API._refreshItemPile(...args))
itemPileSocket.register(SOCKET_HANDLERS.REGISTER_PILE_EVENTS, (...args) => API._registerClickEvents(...args))

/**
* UI sockets
Expand Down

0 comments on commit ea79697

Please sign in to comment.