Files
vermine2047/module/sheets/creature-sheet.mjs
T
uberwald 1b66c24258 fix(sheets): use foundry.utils.duplicate instead of global duplicate
Foundry V11+ no longer exposes 'duplicate' as a global function.
It must be accessed via foundry.utils.duplicate.

Fixed in:
- actor-sheet.mjs:140
- creature-sheet.mjs:148

This resolves the ReferenceError: duplicate is not defined error
when creating items on actor sheets.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-04 23:25:11 +02:00

168 lines
5.0 KiB
JavaScript

import { onManageActiveEffect, prepareActiveEffectCategories } from "../system/effects.mjs";
import { VermineActorSheet } from "./actor-sheet.mjs";
/**
* Extend the basic ActorSheet with some very simple modifications
* @extends {ActorSheet}
*/
export class VermineCreatureSheet extends VermineActorSheet {
/** @override */
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
classes: ["vermine2047", "sheet", "actor", "creature"],
template: "systems/vermine2047/templates/actor/actor-sheet.hbs",
width: 650,
height: 600,
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }]
});
}
/** @override */
get template() {
return `systems/vermine2047/templates/actor/actor-${this.actor.type}-sheet.hbs`;
}
/* -------------------------------------------- */
/** @override */
getData() {
// Retrieve the data structure from the base sheet. You can inspect or log
// the context variable to see the structure, but some key properties for
// sheets are the actor object, the data object, whether or not it's
// editable, the items array, and the effects array.
const context = super.getData();
// Use a safe clone of the actor data for further operations.
const actorData = this.actor.toObject(false);
// Add the actor's data to context.data for easier access, as well as flags.
context.system = actorData.system;
context.flags = actorData.flags;
context.config = CONFIG.VERMINE;
// Prepare character data and items.
if (actorData.type == 'character') {
this._prepareItems(context);
this._prepareCharacterData(context);
}
// Prepare NPC data and items.
if (actorData.type == 'npc') {
this._prepareItems(context);
}
// Prepare Creature data and items.
if (actorData.type == 'creature') {
this._prepareItems(context);
this._prepareCreatureData(context);
}
// Add roll data for TinyMCE editors.
context.rollData = context.actor.getRollData();
// Prepare active effects
context.effects = prepareActiveEffectCategories(this.actor.effects);
return context;
}
/**
* Organize and classify Items for Character sheets.
*
* @param {Object} actorData The actor to prepare.
*
* @return {undefined}
*/
_prepareItems(context) {
context.gear = this.actor.itemTypes['item'];
context.traits = this.actor.itemTypes['trait'];
}
/**
* Prepare Character type specific data.
*
* @param {Object} actorData The actor to prepare.
*
* @return {undefined}
*/
_prepareCharacterData(context) {
// Handle ability scores.
for (let [k, v] of Object.entries(context.system.abilities)) {
v.label = game.i18n.localize(context.system.abilities[k].label) ?? k;
}
}
/**
* Prepare Creature type specific data for the sheet.
*
* @param {Object} context The context data to prepare.
* @return {undefined}
*/
_prepareCreatureData(context) {
if (this.actor.type !== 'creature') return;
// Add computed values to context
context.computed = context.system.computed || {};
// Get labels for pattern, size, role
const patternLevel = context.system.pattern?.value || 1;
const sizeLevel = context.system.size?.value || 1;
const roleLevel = context.system.role?.value || 1;
const packLevel = context.system.pack?.value || 0;
// Add pattern label
const patternConfig = CONFIG.VERMINE.creaturePatternLevels[patternLevel];
if (patternConfig) {
context.patternLabel = game.i18n.localize(patternConfig.label);
}
// Add size label (using numeric for now)
context.sizeLabel = sizeLevel;
// Add role label
const roleConfig = CONFIG.VERMINE.creatureRoleLevels[roleLevel];
if (roleConfig) {
context.roleLabel = game.i18n.localize(roleConfig.label);
}
// Add pack label
context.packLabel = packLevel > 0 ? packLevel : game.i18n.localize('VERMINE.none');
}
/* -------------------------------------------- */
/** @override */
activateListeners(html) {
super.activateListeners(html);
html.find('.item-create').click(this._onItemCreate.bind(this));
}
async _onItemCreate(event) {
event.preventDefault();
const header = event.currentTarget;
// Get the type of item to create.
const type = header.dataset.type;
// Grab any data associated with this control.
const data = foundry.utils.duplicate(header.dataset);
// Initialize a default name.
// const name = `New ${type.capitalize()}`;
const name = game.i18n.localize('ITEMS.new_' + type);
console.log('onItemCreate child', data.type, this.actor.type);
// Prepare the item object.
const itemData = {
name: name,
type: type,
system: data
};
// Remove the type from the dataset since it's in the itemData.type prop.
delete itemData.system["type"];
// Finally, create the item!
return await Item.create(itemData, { parent: this.actor });
}
}