Fix issue #54: Adding a background to an actor bug
Some checks failed
Test / test (push) Failing after 2s
Some checks failed
Test / test (push) Failing after 2s
This commit is contained in:
@@ -1000,10 +1000,10 @@ function makeFoundryCharacterData(entry) {
|
||||
type: entry.type === "character" ? "character" : "npc",
|
||||
img: getDragonrollImageUrl(entry.image) || undefined,
|
||||
system: {
|
||||
// `details.race` and `details.background` are left out on purpose: they reference
|
||||
// embedded items and are linked once the actor stands with its items made.
|
||||
details: {
|
||||
race: entry.species || "",
|
||||
level: Number(entry.level) || 1,
|
||||
background: entry.background || "",
|
||||
biography: { value: entry.description || "" },
|
||||
cr: entry.challengeRating || "",
|
||||
},
|
||||
@@ -1036,9 +1036,7 @@ function characterUpdateData(entry) {
|
||||
const updates = {
|
||||
name: entry.name,
|
||||
img: getDragonrollImageUrl(entry.image) || "icons/svg/mystery-man.svg",
|
||||
"system.details.race": entry.species || "",
|
||||
"system.details.level": Number(entry.level) || 1,
|
||||
"system.details.background": entry.background || "",
|
||||
"system.details.biography.value": entry.description || "",
|
||||
"system.details.cr": entry.challengeRating || "",
|
||||
[`flags.${MODULE_ID}.traits`]: entry.traits || "",
|
||||
@@ -1052,6 +1050,40 @@ function characterUpdateData(entry) {
|
||||
return updates;
|
||||
}
|
||||
|
||||
// `system.details.background` and `system.details.race` are LocalDocumentFields in
|
||||
// D&D 5e: they hold the id of an item embedded in the actor, never its name. A name
|
||||
// written into one is a reference that resolves to nothing, and adding that item in
|
||||
// Foundry then fails, so each of these follows the actor's own item for it instead.
|
||||
const EMBEDDED_DETAILS = [
|
||||
// Dragonroll owns the background item it lists, so its reference follows it always and
|
||||
// a background taken off in the campaign leaves no reference behind.
|
||||
{ field: "background", path: "system.details.background", types: ["background"], clearWhenMissing: true },
|
||||
// The species item belongs to the world: point at it when it carries the name, and
|
||||
// leave the reference to the GM when it does not.
|
||||
{ field: "species", path: "system.details.race", types: ["race", "species"] },
|
||||
];
|
||||
|
||||
function localDocumentId(value) {
|
||||
// The field resolves to the referenced document when it can and to the stored id
|
||||
// when the document is gone (its `fallback` option).
|
||||
if (typeof value === "string") return value;
|
||||
return value?._id || "";
|
||||
}
|
||||
|
||||
function embeddedDetailReferenceData(actor, entry) {
|
||||
const updates = {};
|
||||
for (const detail of EMBEDDED_DETAILS) {
|
||||
const wanted = String(entry[detail.field] || "").trim();
|
||||
const item = wanted
|
||||
? actor.items?.contents?.find((candidate) => detail.types.includes(candidate.type) && candidate.name === wanted)
|
||||
: null;
|
||||
const reference = item?.id || "";
|
||||
if (!reference && !detail.clearWhenMissing) continue;
|
||||
if (localDocumentId(documentValue(actor, detail.path)) !== reference) updates[detail.path] = reference;
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
|
||||
async function reconcileEmbeddedItems(actor, entry) {
|
||||
const desired = [];
|
||||
if (entry.className) desired.push({ type: "class", name: entry.className, system: { levels: Number(entry.level) || 1 } });
|
||||
@@ -1110,6 +1142,9 @@ async function applyDragonrollCharacter(actor, entry) {
|
||||
await withActorSyncSuppressed(actor, async () => {
|
||||
if (changed) await actor.update(updates);
|
||||
await reconcileEmbeddedItems(actor, entry);
|
||||
// The item references are written after the reconcile that makes the items they point at.
|
||||
const references = embeddedDetailReferenceData(actor, entry);
|
||||
if (Object.keys(references).length) await actor.update(references);
|
||||
if (dragonrollActorId(actor) !== String(entry._id)
|
||||
|| actor.getFlag?.(MODULE_ID, "actorType") !== entry.type) {
|
||||
await actor.update({
|
||||
@@ -1137,7 +1172,12 @@ async function createFoundryCharacter(entry) {
|
||||
if (!actor) return;
|
||||
|
||||
await withActorSyncSuppressed(actor, async () => {
|
||||
await actor.update({ [`flags.${MODULE_ID}.syncMutation`]: false });
|
||||
await actor.update({
|
||||
// The actor was made with its items, so its background and species references
|
||||
// can point at them right away.
|
||||
...embeddedDetailReferenceData(actor, entry),
|
||||
[`flags.${MODULE_ID}.syncMutation`]: false,
|
||||
});
|
||||
});
|
||||
await mapDragonrollActor(entry._id, actor.id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user