2021-12-05 01:48:26 +01:00
|
|
|
import { DialogSplitItem } from "./dialog-split-item.js";
|
2023-01-01 22:18:18 +01:00
|
|
|
import { RdDItem } from "./item.js";
|
2021-12-05 01:48:26 +01:00
|
|
|
|
|
|
|
export class RdDSheetUtility {
|
|
|
|
|
2023-01-28 16:34:09 +01:00
|
|
|
static mergeDocumentRights(options, document, editable) {
|
|
|
|
const userRightLevel = game.user.isGM
|
|
|
|
? CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER
|
|
|
|
: document.getUserLevel(game.user);
|
|
|
|
mergeObject(options, {
|
|
|
|
isGM: game.user.isGM,
|
2023-03-15 00:34:20 +01:00
|
|
|
isOwned: document.parent ? true : false,
|
2023-01-28 16:34:09 +01:00
|
|
|
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
|
|
|
|
});
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2021-12-05 01:48:26 +01:00
|
|
|
static getItem(event, actor) {
|
2022-06-12 08:17:59 +02:00
|
|
|
return actor.items.get(RdDSheetUtility.getItemId(event))
|
2021-12-05 01:48:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2023-01-01 22:18:18 +01:00
|
|
|
static async prepareItemDropParameters(destItemId, actor, dragData, objetVersConteneur) {
|
|
|
|
let item = fromUuidSync(dragData.uuid);
|
2023-01-28 16:34:09 +01:00
|
|
|
if (item.pack && !item.system) {
|
2023-01-01 22:18:18 +01:00
|
|
|
item = await RdDItem.getCorrespondingItem(item);
|
|
|
|
}
|
2022-12-03 15:33:16 +01:00
|
|
|
if (actor.canReceive(item)) {
|
|
|
|
return {
|
|
|
|
destId: destItemId,
|
|
|
|
targetActorId: actor.id,
|
|
|
|
itemId: item.id,
|
|
|
|
sourceActorId: item.actor?.id,
|
|
|
|
srcId: objetVersConteneur[item.id],
|
|
|
|
onEnleverConteneur: () => { delete objetVersConteneur[item.id]; },
|
|
|
|
onAjouterDansConteneur: (itemId, conteneurId) => { objetVersConteneur[itemId] = conteneurId; }
|
|
|
|
}
|
2021-12-05 01:48:26 +01:00
|
|
|
}
|
2022-12-03 15:33:16 +01:00
|
|
|
else {
|
|
|
|
ui.notifications.warn(`Impossible de donner ${item.name} à ${actor.name}: ${item.type} / ${actor.type}`);
|
|
|
|
}
|
|
|
|
return undefined;
|
2021-12-05 01:48:26 +01:00
|
|
|
}
|
|
|
|
|
2022-01-01 14:01:41 +01:00
|
|
|
static async splitItem(item, actor, onSplit = () => { }) {
|
2021-12-05 01:48:26 +01:00
|
|
|
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) {
|
2021-12-05 01:48:26 +01:00
|
|
|
await item.diminuerQuantite(split);
|
2022-09-07 18:47:56 +02:00
|
|
|
const splitItem = duplicate(item);
|
2021-12-05 01:48:26 +01:00
|
|
|
// todo: ajouter dans le même conteneur?
|
2022-09-07 18:47:56 +02:00
|
|
|
splitItem.system.quantite = split;
|
|
|
|
await actor.createEmbeddedDocuments('Item', [splitItem])
|
2021-12-05 01:48:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|