-Item embed items generate new ids on drop in actor

-Ignore the drop of already owned item in same sheet
This commit is contained in:
Vlyan
2021-05-28 10:37:37 +02:00
parent 127417a554
commit 1ec3b5700d
5 changed files with 56 additions and 30 deletions

View File

@@ -169,29 +169,30 @@ export class BaseSheetL5r5e extends ActorSheet {
// Check item type and subtype
let item = await game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event);
if (
!item ||
item.documentName !== "Item" ||
![
"item",
"armor",
"weapon",
"technique",
"peculiarity",
"advancement",
"title",
"bond",
"signature_scroll",
"item_pattern",
].includes(item.data.type)
) {
if (!item || item.documentName !== "Item" || item.data.type === "property") {
return;
}
// Dropped a item with same "id" as one owned, add qte instead
if (item.data.data.quantity && this.actor.data.items) {
const tmpItem = this.actor.data.items.find((e) => e.name === item.data.name && e.type === item.data.type);
if (tmpItem && this._modifyQuantity(tmpItem.id, 1)) {
// Dropped a item with same "id" as one owned
if (this.actor.data.items) {
if (item.data.data.quantity) {
// Add quantity instead if they have (id is different so use type and name)
const tmpItem = this.actor.data.items.find(
(embedItem) => embedItem.name === item.data.name && embedItem.type === item.data.type
);
if (tmpItem && this._modifyQuantity(tmpItem.id, 1)) {
return;
}
} else if (
this.actor.data.items.some((embedItem) => {
// Search in children
if (embedItem.items?.has(item.data._id)) {
return true;
}
return embedItem.data._id === item.data._id;
})
) {
// Exit if we already owned exactly this id (drag a personal item on our own sheet)
return;
}
}
@@ -219,6 +220,9 @@ export class BaseSheetL5r5e extends ActorSheet {
break;
case "title":
// Generate new Ids for the embed items
await item.generateNewIdsForAllEmbedItems();
// Add embed advancements bonus
for (let [embedId, embedItem] of item.data.data.items) {
if (embedItem.data.type === "advancement") {
@@ -231,10 +235,11 @@ export class BaseSheetL5r5e extends ActorSheet {
// School_ability and mastery_ability, allow only 1 per type
if (CONFIG.l5r5e.techniques.get(item.data.data.technique_type)?.type === "school") {
if (
Array.from(this.actor.items).some(
(e) =>
Array.from(this.actor.items).some((e) => {
return (
e.type === "technique" && e.data.data.technique_type === item.data.data.technique_type
)
);
})
) {
ui.notifications.info(game.i18n.localize("l5r5e.techniques.only_one"));
return;

View File

@@ -207,6 +207,23 @@ export class ItemL5r5e extends Item {
}
}
/**
* Generate new Ids for the embed items
* @return {Promise<void>}
*/
async generateNewIdsForAllEmbedItems() {
// Clear olds ids
const oldItems = Array.from(this.data.data.items);
this.data.data.items = new Map();
// Re-add with new ids
oldItems.forEach(([id, item]) => {
this.addEmbedItem(item, { save: false, newId: true, addBonusToActor: false });
});
return this.saveEmbedItems();
}
/**
* Save all the Embed Items
* @return {Promise<void>}

View File

@@ -7,7 +7,7 @@
"download": "https://gitlab.com/teaml5r/l5r5e/-/jobs/artifacts/v1.3.0/raw/l5r5e.zip?job=build",
"version": "1.3.0",
"minimumCoreVersion": "0.8.5",
"compatibleCoreVersion": "0.8.5",
"compatibleCoreVersion": "0.8.6",
"manifestPlusVersion": "1.0.0",
"socket": true,
"author": "Team L5R",