Skip to content

Commit

Permalink
Minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Jul 2, 2024
1 parent 9a38d3f commit a092f94
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 26 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Item Piles Changelog

## Version 3.0.4

- Fixed errors when duplicating item piles and trying to open their interfaces
- Fixed issue with trying to split an item pile among 0 players

## Version 3.0.3

- Minor fix to latest release
Expand Down
5 changes: 3 additions & 2 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"AddCurrency": "Add currency",
"SplitAll": "Split {num_players} ways",
"SplitItems": "Split items {num_players} ways",
"SplitCurrencies": "Split currency {num_players} ways"
"SplitCurrencies": "Split currency {num_players} ways",
"SplitNoPlayers": "No characters were found to split the contents with."
},
"Trade": {
"Title": "Item Piles: Trading",
Expand Down Expand Up @@ -536,7 +537,7 @@
"ItemPileConfig": {
"Title": "Item Pile Configuration: {actor_name}",
"Configure": "Item Pile",
"Update": "Update Item Pile",
"Update": "Update",
"Main": {
"Title": "Main Settings",
"EnabledPile": "Enabled",
Expand Down
8 changes: 6 additions & 2 deletions src/API/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import TradeAPI from "./trade-api.js";
import PrivateAPI from "./private-api.js";
import { SYSTEMS } from "../systems.js";
import ItemPileConfig from "../applications/item-pile-config/item-pile-config.js";
import { getCharactersForItemPile } from "../helpers/sharing-utilities.js";

class API {
/**
Expand Down Expand Up @@ -1202,10 +1203,13 @@ class API {
throw Helpers.custom_error("SplitItemPileContents | instigator must be of type TokenDocument or Actor")
}

const actorUuids = (targets || SharingUtilities.getPlayersForItemPile(itemPileActor)
.map(u => u.character))
const actorUuids = (targets || SharingUtilities.getCharactersForItemPile(itemPileActor))
.map(actor => Utilities.getUuid(actor));

if (!actorUuids.length) {
throw Helpers.custom_error("SplitItemPileContents | Could not find any characters to split item piles' content with")
}

return ItemPileSocket.executeAsGM(ItemPileSocket.HANDLERS.SPLIT_PILE, itemPileUuid, actorUuids, game.user.id, instigator);

}
Expand Down
4 changes: 2 additions & 2 deletions src/applications/item-editor/item-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class ItemEditor extends SvelteApplication {
let title = game.i18n.format("ITEM-PILES.Applications.ItemEditor.Title", { item_name: item.name })
if (options.extraTitle) title += options.extraTitle;
super({
id: `item-pile-item-editor-${item.uuid}-${foundry.utils.randomID()}`,
id: `item-pile-item-editor-${item.id}${item.parent ? "-" + item.parent.id : ""}-${foundry.utils.randomID()}`,
title,
svelte: {
class: ItemEditorShell,
Expand All @@ -31,7 +31,7 @@ export default class ItemEditor extends SvelteApplication {
}

static getActiveApp(item) {
return getActiveApps(`item-pile-item-editor-${item.uuid}`, true);
return getActiveApps(`item-pile-item-editor-${item.id}${item.parent ? "-" + item.parent.id : ""}`, true);
}

static async show(item = false, options = {}, dialogData = {}) {
Expand Down
4 changes: 2 additions & 2 deletions src/applications/item-pile-config/item-pile-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class ItemPileConfig extends SvelteApplication {
constructor(pileActor, options = {}) {

super({
id: `item-pile-config-${pileActor.uuid}-${foundry.utils.randomID()}`,
id: `item-pile-config-${pileActor.id}-${foundry.utils.randomID()}`,
title: game.i18n.format("ITEM-PILES.Applications.ItemPileConfig.Title", { actor_name: pileActor.name }),
svelte: {
class: ItemPileConfigShell,
Expand All @@ -32,7 +32,7 @@ export default class ItemPileConfig extends SvelteApplication {
}

static getActiveApp(source) {
return getActiveApps(`item-pile-config-${source.uuid}`, true)
return getActiveApps(`item-pile-config-${source.id}`, true)
}

static async show(target, options = {}, dialogData = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ export default class ItemPileInventoryApp extends SvelteApplication {
*
* @param actor
* @param recipient
* @param overrides
* @param options
* @param dialogData
*/
constructor(actor, recipient, options = {}, dialogData = {}) {
super({
id: `item-pile-inventory-${actor?.token?.uuid ?? actor.uuid}-${foundry.utils.randomID()}`,
id: `item-pile-inventory-${actor?.token?.id ?? actor.id}-${foundry.utils.randomID()}`,
title: actor.name,
svelte: {
class: ItemPileInventoryShell,
Expand Down Expand Up @@ -51,7 +50,7 @@ export default class ItemPileInventoryApp extends SvelteApplication {
}

static getActiveApps(source) {
return Helpers.getActiveApps(`item-pile-inventory-${source?.token?.uuid ?? source.uuid}`);
return Helpers.getActiveApps(`item-pile-inventory-${source?.token?.id ?? source.id}`);
}

static async show(source, recipient = false, options = {}, dialogData = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// Stores
let canBeSplit = false;
let num_players = 0;
let searchStore = store.search;
let editQuantities = store.editQuantities;
let pileData = store.pileData;
Expand All @@ -47,10 +48,9 @@
$items;
$currencies;
canBeSplit = SharingUtilities.canItemPileBeSplit(actor);
num_players = SharingUtilities.getPlayersForItemPile(actor).length;
}
let num_players = SharingUtilities.getPlayersForItemPile(actor).length;
async function dropData(event) {
event.preventDefault();
Expand Down Expand Up @@ -183,7 +183,8 @@
{/if}

{#if $pileData.splitAllEnabled}
<button type="button" on:click={() => { store.splitAll() }} disabled="{isPileEmpty || !canBeSplit}">
<button type="button" on:click={() => { store.splitAll() }} disabled="{isPileEmpty || !canBeSplit}"
data-tooltip={num_players === 0 && !isPileEmpty ? localize("ITEM-PILES.Inspect.SplitNoPlayers") : ""}>
<i class="fas fa-handshake"></i>
{#if $pileData.shareItemsEnabled}
{localize("ITEM-PILES.Inspect.SplitAll", { num_players })}
Expand Down
4 changes: 2 additions & 2 deletions src/applications/merchant-app/merchant-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class MerchantApp extends SvelteApplication {
constructor(merchant, recipient = false, options = {}, dialogData = {}) {
super({
title: `Merchant: ${merchant.name}`,
id: `item-pile-merchant-${merchant.uuid}-${foundry.utils.randomID()}`,
id: `item-pile-merchant-${merchant.id}-${foundry.utils.randomID()}`,
svelte: {
class: MerchantAppShell,
target: document.body,
Expand Down Expand Up @@ -41,7 +41,7 @@ export default class MerchantApp extends SvelteApplication {
}

static getActiveApp(source) {
return Helpers.getActiveApps(`item-pile-merchant-${source.uuid}`, true);
return Helpers.getActiveApps(`item-pile-merchant-${source.id}`, true);
}

static async show(merchant, recipient = false, options = {}, dialogData = {}) {
Expand Down
4 changes: 2 additions & 2 deletions src/applications/vault-app/vault-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class VaultApp extends SvelteApplication {
*/
constructor(actor, recipient, options = {}, dialogData = {}) {
super({
id: `item-pile-vault-${actor?.token?.uuid ?? actor.uuid}-${foundry.utils.randomID()}`,
id: `item-pile-vault-${actor?.token?.id ?? actor.id}-${foundry.utils.randomID()}`,
title: actor.name,
svelte: {
class: VaultShell,
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class VaultApp extends SvelteApplication {
}

static getActiveApps(source) {
return Helpers.getActiveApps(`item-pile-vault-${source?.token?.uuid ?? source.uuid}`);
return Helpers.getActiveApps(`item-pile-vault-${source?.token?.id ?? source.id}`);
}

static async show(source, recipient = false, options = {}, dialogData = {}) {
Expand Down
17 changes: 14 additions & 3 deletions src/helpers/sharing-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ export function getPlayersForItemPile(target) {
return getActivePlayers(pileData.activePlayers);
}

/**
* Gets the characters for each player
*
* @param {Actor|TokenDocument|String} target
* @returns {Array<User>}
*/
export function getCharactersForItemPile(target) {
return getPlayersForItemPile(target).map(player => Utilities.getUserCharacter(player));
}

/**
* Determines whether a pile can be split
*
Expand All @@ -28,7 +38,8 @@ export function getPlayersForItemPile(target) {
export function canItemPileBeSplit(target) {
const pileData = PileUtilities.getActorFlagData(target);
const shareData = getItemPileSharingData(target);
const playerActors = getPlayersForItemPile(target).map(player => Utilities.getUserCharacter(player));
const playerActors = getCharactersForItemPile(target);
if (!playerActors.length) return false;
const items = pileData.shareItemsEnabled ? PileUtilities.getActorItems(target) : [];
const currencies = pileData.shareCurrenciesEnabled || pileData.splitAllEnabled ? PileUtilities.getActorCurrencies(target) : [];
for (const item of items) {
Expand Down Expand Up @@ -300,7 +311,7 @@ export function getItemSharesLeftForActor(pile, item, recipient, {

players = players ?? getPlayersForItemPile(pile).length;
let totalActorShare = totalShares / players;
if (!Number.isInteger(totalActorShare) && !floor) {
if (totalActorShare && !Number.isInteger(totalActorShare) && !floor) {
totalActorShare += 1;
}

Expand Down Expand Up @@ -333,7 +344,7 @@ export function getAttributeSharesLeftForActor(pile, path, recipient, {

players = players ?? getPlayersForItemPile(pile).length;
let totalActorShare = totalShares / players;
if (!Number.isInteger(totalActorShare) && !floor) {
if (totalActorShare && !Number.isInteger(totalActorShare) && !floor) {
totalActorShare += 1;
}

Expand Down
15 changes: 10 additions & 5 deletions src/helpers/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ export async function runMacro(macroId, macroData) {

}

export function getOwnedCharacters(user = false) {
user = user || game.user;
export function getOwnedCharacters(user = game.user) {
return game.actors.filter(actor => {
return actor.ownership?.[user.id] === CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER && actor.prototypeToken.actorLink;
})
Expand All @@ -387,8 +386,7 @@ export function getOwnedCharacters(user = false) {
});
}

export function getUserCharacter(user = false) {
user = user || game.user;
export function getUserCharacter(user = game.user) {
return user.character || (user.isGM ? false : (getOwnedCharacters(user)?.[0] ?? false));
}

Expand All @@ -415,7 +413,14 @@ export async function createFoldersFromNames(folders, type = "Actor") {
export function getSourceActorFromDropData(dropData) {
if (dropData.uuid) {
const doc = fromUuidSync(dropData.uuid);
return doc instanceof Actor ? doc : doc.parent;
if (doc instanceof Actor) {
return doc;
} else if (doc instanceof TokenDocument) {
return doc.actor;
} else if (doc instanceof Item) {
return doc.parent
}
return false;
} else if (dropData.tokenId) {
if (dropData.sceneId) {
const uuid = `Scene.${dropData.sceneId}.Token.${dropData.tokenId}`;
Expand Down

0 comments on commit a092f94

Please sign in to comment.