foundryvtt-reve-de-dragon/module/rdd-sheet-utility.js

85 lines
2.9 KiB
JavaScript
Raw Normal View History

import { DialogSplitItem } from "./dialog-split-item.js";
import { RdDItem } from "./item.js";
export class RdDSheetUtility {
static mergeDocumentRights(options, document, editable) {
const userRightLevel = game.user.isGM
? CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER
: document.getUserLevel(game.user);
2023-03-16 23:10:17 +01:00
let newOptions = {
isGM: game.user.isGM,
2023-03-15 00:34:20 +01:00
isOwned: document.parent ? true : false,
editable: editable,
cssClass: editable ? "editable" : "locked",
isLimited: userRightLevel >= CONST.DOCUMENT_OWNERSHIP_LEVELS.LIMITED,
isObserver: userRightLevel >= CONST.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER,
isOwner: userRightLevel >= CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER
2023-03-16 23:10:17 +01:00
}
2024-05-01 09:13:21 +02:00
foundry.utils.mergeObject(options, newOptions);
return options;
}
static getItem(event, actor) {
2022-06-12 08:17:59 +02:00
return actor.items.get(RdDSheetUtility.getItemId(event))
}
static getItemId(event) {
return RdDSheetUtility.getEventItemData(event, "item-id");
}
static getEventItemData(event, property) {
const node = RdDSheetUtility.getEventElement(event);
return node?.data(property);
}
static getEventElement(event) {
return $(event.currentTarget)?.parents(".item");
}
static async prepareItemDropParameters(destItemId, actor, dragData, objetVersConteneur) {
let item = fromUuidSync(dragData.uuid);
if (item.pack && !item.system) {
item = await RdDItem.getCorrespondingItem(item);
}
if (actor.canReceive(item)) {
if (!actor.prototypeToken.actorLink && actor.token) {
ui.notifications.warn(`Impossible de donner ${item.name} à ${actor.name}, c'est un acteur temporaire
<br>La suppression de son token entraînera la perte définitive de ${item.name}.`)
return
}
return {
destId: destItemId,
targetActorId: actor.id,
itemId: item.id,
sourceActorId: item.actor?.id,
2023-11-26 22:51:33 +01:00
sourceTokenId: item.actor?.token?.id,
srcId: objetVersConteneur[item.id],
onEnleverConteneur: () => { delete objetVersConteneur[item.id]; },
onAjouterDansConteneur: (itemId, conteneurId) => { objetVersConteneur[itemId] = conteneurId; }
}
}
else {
ui.notifications.warn(`Impossible de donner ${item.name} à ${actor.name}: ${item.type} / ${actor.type}`);
}
return undefined;
}
2022-01-01 14:01:41 +01:00
static async splitItem(item, actor, onSplit = () => { }) {
const dialog = await DialogSplitItem.create(item, async (item, split) => {
await RdDSheetUtility._onSplitItem(item, split, actor);
onSplit();
});
dialog.render(true);
}
static async _onSplitItem(item, split, actor) {
2022-06-12 09:46:58 +02:00
if (split >= 1 && split < item.system.quantite) {
await item.diminuerQuantite(split);
2024-05-02 14:08:02 +02:00
const splitItem = foundry.utils.duplicate(item);
// todo: ajouter dans le même conteneur?
splitItem.system.quantite = split;
await actor.createEmbeddedDocuments('Item', [splitItem])
}
}
}