Skip to content

Commit

Permalink
Recursive flags is bad
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Aug 23, 2024
1 parent f26305e commit ff80218
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
17 changes: 13 additions & 4 deletions src/API/private-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ export default class PrivateAPI {
if (!handler) continue;
handler({ item, items: newItems });
}
items = items.concat(newItems);
items = items.concat(newItems.map(item => ({
_id: item._id,
flags: undefined,
quantity: Utilities.getItemQuantity(item)
})));
}
await sourceTransaction.appendItemChanges(items, { remove: true });

Expand Down Expand Up @@ -1034,10 +1038,15 @@ export default class PrivateAPI {
let itemsDropped;

foundry.utils.setProperty(itemData.item, game.itempiles.API.ITEM_QUANTITY_ATTRIBUTE, itemData?.quantity ?? 1);
const items = [itemData.item];
const containerItems = [itemData.item];
const item = fromUuidSync(itemData.uuid);
const handler = Utilities.getItemTypeHandler(CONSTANTS.ITEM_TYPE_METHODS.TRANSFER, item.type);
if (handler) handler({ item, items });
if (handler) handler({ item, items: containerItems });
const items = containerItems.map(item => ({
item,
flags: undefined,
quantity: Utilities.getItemQuantity(item)
}));

// If there's a source of the item (it wasn't dropped from the item bar)
if (sourceUuid) {
Expand Down Expand Up @@ -1067,7 +1076,7 @@ export default class PrivateAPI {
itemsDropped = await this._addItems(targetUuid, items, userId);
} else {
targetUuid = await this._createItemPile({
sceneId, position, items, tokenOverrides: { elevation: elevation || 0 }
sceneId, position, items: items.map(data => data.item), tokenOverrides: { elevation: elevation || 0 }
});
}

Expand Down
15 changes: 11 additions & 4 deletions src/helpers/pile-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,18 @@ export function canItemStack(item, targetActor) {
/**
*
* @param item
* @param data
* @param options
* @param useDefaults.data
* @param useDefaults.useDefaults
* @returns {Object<CONSTANTS.ITEM_DEFAULTS>}
*/
export function getItemFlagData(item, data = false) {
return getFlagData(Utilities.getDocument(item), CONSTANTS.FLAGS.ITEM, { ...CONSTANTS.ITEM_DEFAULTS }, data);
export function getItemFlagData(item, { data = false, useDefaults = true } = {}) {
return getFlagData(
Utilities.getDocument(item),
CONSTANTS.FLAGS.ITEM,
{ ...(useDefaults ? CONSTANTS.ITEM_DEFAULTS : {}) },
data
);
}

/**
Expand Down Expand Up @@ -795,7 +802,7 @@ export function cleanItemFlagData(flagData, { addRemoveFlag = false } = {}) {
const toRemove = new Set(defaults.filter(key => !difference.has(key)));
for (const key of toRemove) {
delete flagData[key];
if (!addRemoveFlag) {
if (addRemoveFlag) {
flagData["-=" + key] = null;
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/helpers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export default class Transaction {
? Math.abs(data.quantity ?? Utilities.getItemQuantity(itemData))
: Math.abs(data.quantity ?? Utilities.getItemQuantity(itemData)) * (remove ? -1 : 1);

console.log(itemData.name, incomingQuantity);

let itemId = itemData._id ?? itemData.id;
let documentHasItem = false;
let documentExistingItem = false;
Expand Down Expand Up @@ -211,6 +209,11 @@ export default class Transaction {
}))
this.itemsToCreate = this.itemsToCreate.filter(item => {
return !PileUtilities.canItemStack(item, this.document) || Utilities.getItemQuantity(item) > 0 || this.itemTypeMap.get(item._id) === "currency"
}).map(item => {
const flagData = PileUtilities.getItemFlagData(item);
Utilities.deleteProperty(item, CONSTANTS.FLAGS.ITEM);
foundry.utils.setProperty(item, CONSTANTS.FLAGS.ITEM, PileUtilities.cleanItemFlagData(flagData));
return item;
});
this.itemsToDelete = this.itemsToUpdate.filter(item => {
return Utilities.getItemQuantity(item) <= 0 && this.itemTypeMap.get(item._id) !== "currency";
Expand All @@ -224,9 +227,10 @@ export default class Transaction {

this.itemDeltas = Array.from(this.itemDeltas).map(([id, quantity]) => {
const item = this.document.items.get(id).toObject();
const existingFlagData = PileUtilities.cleanItemFlagData(PileUtilities.getItemFlagData(item));
const newFlagData = PileUtilities.cleanItemFlagData(this.itemFlagMap.get(id) ?? {});
foundry.utils.setProperty(item, CONSTANTS.FLAGS.ITEM, foundry.utils.mergeObject(existingFlagData, newFlagData));
const existingFlagData = PileUtilities.getItemFlagData(item);
const newFlagData = this.itemFlagMap.get(id) ?? {};
Utilities.deleteProperty(item, CONSTANTS.FLAGS.ITEM);
foundry.utils.setProperty(item, CONSTANTS.FLAGS.ITEM, foundry.utils.mergeObject(existingFlagData, PileUtilities.cleanItemFlagData(newFlagData)));
const type = this.itemTypeMap.get(id);
Utilities.setItemQuantity(item, quantity, true);
return { item, quantity, type };
Expand Down

0 comments on commit ff80218

Please sign in to comment.