Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Issue #425 - Add default "Link Actor Data" for Characters #445

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/module/actor/__tests__/entity-actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,52 @@ export default ({
});
});

describe("_preCreate(data, options, user)", () => {
it("New character's prototypeToken actorLink defaults to true", async () => {
const actor = (await createMockActor("character")) as OseActor;
expect(actor.prototypeToken.actorLink).equal(true);
});
it("New monster's prototypeToken actorLink defaults to false", async () => {
const actor = (await createMockActor("monster")) as OseActor;
expect(actor.prototypeToken.actorLink).equal(false);
});
it("Character from compendium does not have defaults applied on import", async () => {
// Create Dummy Compendium
const compendiumData = {
name: `test-actors-ose-actor-entity`,
label: "Test Actors - ose-actor-entity",
system: "ose",
package: "ose",
type: "Actor",
path: "systems/ose/packs/test-actors-ose-actor-entity",
private: false,
};
const dummyCompendium = await CompendiumCollection.createCompendium(compendiumData);

// Create Dummy Actor (which has the temporary context to reduce mess)
const actorData = { name: `Test Actor ose.actor.entity`, type: "character" };
const actorContext = { temporary: true };
const dummyActor = await OseActor.create(actorData, actorContext);

// Import Dummy Actor into Dummy Compendium
const dummyActorInCompendium = await dummyCompendium.importDocument(dummyActor);

// Force Actor's prototypeToken.actorLink to false. As we created it, it would have received system defaults.
const prototypeToken = {};
Object.assign(prototypeToken, { actorLink: false });
await dummyActorInCompendium.updateSource({ prototypeToken });

// Import Dummy Compendium's Dummy Actor into world
const importedDummyActor = await game.actors.importFromCompendium(dummyCompendium, dummyActorInCompendium.id)

// Perform test
expect(importedDummyActor.prototypeToken.actorLink).equal(false);

// Clean up created dummy compendium, as quench doesn't want to.
dummyCompendium.deleteCompendium();
});
});

describe("isNew()", () => {
it("character", async () => {
const actor = (await createMockActor("character")) as OseActor;
Expand Down
20 changes: 20 additions & 0 deletions src/module/actor/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ export default class OseActor extends Actor {
return this.system.isNew;
}

/**
* assign more sane defaults to Actor
*/
async _preCreate(data, options, user) {
await super._preCreate(data, options, user);
// If this Actor came from a Compendium, do not apply defaults.
const sourceId = this.getFlag("core", "sourceId");
if (sourceId?.startsWith("Compendium.")) return;

// Configure prototype token settings
const prototypeToken = {};
if (this.type === "character")
Object.assign(prototypeToken, {
// sight: { enabled: true }, // Vision -> Basic Configuration -> Vision Enable
actorLink: true, // Identity -> Link Actor Data
// disposition: 1, // Identity -> Token Disposition = "Friendly"
});
this.updateSource({ prototypeToken });
}

generateSave(hd) {
hd = hd.includes("+") ? parseInt(hd) + 1 : parseInt(hd);

Expand Down