Items

Item entities represent objects or features that are owned by an Actor. While the term item tends to imply physical possessions, as a developer in Foundry Virtual Tabletop you should consider Items as the building block for anything that can be owned by an Actor. For example, in the D&D5e system, Item types are used for Classes, Spells, and Feats in addition to standard inventory and equipment.

The FVTT framework provides core APIs for manipulating and modifying Items including support to allow Items to belong to an Actor to modify the Actor’s owned version of that Item.

There are several important API layers which are useful when working with Items. The Items() collection has all of the Item() instances which are defined at the World level (as oppoosed to owned items which belong to a specific actor). This Items collection is available under game.items.

The Item() class represents a single item which may be unowned (and defined at the World level) or owned (and defined at the Actor level). Owned items which belong to an Actor() are defined in that actor’s data model under actor.data.items. The Item() class may be overridden by a system or module by defining the Config.Item.entityClass global configuration value.

When viewing or interacting with an item’s data, the item is rendered and edited using the ItemSheet() class which can be extended or overridden by systems or modules. To override the default implementation, a mod should define the CONFIG.Item.sheetClass global configuration value.



The Items Collection

class Items()

The Collection() of Item() entities The items collection is accessible within the game as game.items

Items.Collection.insert(entity)

Add a new Entity to the Collection, asserting that they are of the correct type

Arguments:
  • entity (Entity) – The entity instance to add to the collection
Items.Collection.remove(id)

Remove an Entity from the Collection by its ID.

Arguments:
  • id (String) – The entity ID which should be removed
Items.Collection.get(id)

Get an element from the collection by ID

Arguments:
  • id (String) – The entity ID to retrieve from the collection
Returns:

Entity – The retrieved Entity, if the ID was found, otherwise null;

Items.Collection.index(id)

Retrieve the index of an entity within the collection by its ID

Arguments:
  • id (String) – The entity ID to retrieve from the collection
Returns:

Number – The index of the Entity within the collection, if found, otherwise -1;

Items.Collection.render()

Re-render any currently visible applications associated with this Collection

Items.importFromCollection(collection, entryId)

Import an Item from a compendium collection, adding it to the current World

Arguments:
  • collection (String) – The name of the pack from which to import
  • entryId (String) – The ID of the compendium entry to import
Returns:

Promise – A Promise containing the imported Item

Items.object

Elements of the Items collection are instances of the Item class, or a subclass thereof


The Item Class

class raw.Item(data, actor)

The Item entity. This base Item refers primarily to items which are not currently owned.

Arguments:
  • data (Object) – The data with which to create the Item
  • actor (Actor) – If the item is owned by an Actor, provide a reference
raw.Item.Entity.create(data, options)

Create a new entity using provided input data The data for entity creation is typically provided from the server through the ‘create<Entity>’ socket Alternatively, the creation event may originate locally and the new entity can be pushed back to the server

Arguments:
  • data (Object) – The data with which to create the entity
  • options (Object) – Additional options which customize the creation workflow
  • options.temporary (Boolean) – Create a temporary entity which is not saved to the world database. Default is false.
  • options.displaySheet (Boolean) – Show the configuration sheet for the created entity once it is created. Default is true.
Returns:

Promise – A Promise which resolves to contain the created Entity

raw.Item.Entity.update(data)

Update the current entity using new data This new data is typically provided from the server through the ‘update<Entity>’ socket Alternatively, the update may originate locally, in which case it can be pushed back to the server

Arguments:
  • data (Object) – The data with which to update the entity
Returns:

Promise – A Promise which resolves to the updated Entity

raw.Item.Entity.delete()

Delete the entity, removing it from its collection and deleting its data record

Returns:Promise – A Promise which resolves to the ID of the deleted Entity once handled by the server
raw.Item._onUpdate(data)

Additional updating steps for the Item entity when new data is saved which trigger some related updates.

Re-render the parent collection if names, images, or permissions have changed

Arguments:
  • data (Object) – The new data object which was used to update the Actor
raw.Item.hasPerm(user, permission)

Override the standard permission test for Item entities as we need to apply a special check for owned items OwnedItems have permission that the player has for the parent Actor.

Arguments:
  • user (User) – The user to test for permission
  • permission (Number) – The permission level to test
Returns:

Boolean – Whether or not the user has the permission for this item

raw.Item.isOwned

A flag for whether the item is owned by an Actor entity


The Item Sheet

class ItemSheet(item, options)

The default Item Sheet

This Application is responsible for rendering an item’s attributes and allowing the item to be edited.

System modifications may elect to override this class to better suit their own game system by re-defining the value CONFIG.Item.sheetClass.

Arguments:
  • item (Item) – The Item instance being displayed within the sheet.
  • options (Object) – Additional options which modify the rendering of the item.
  • options.editable (Boolean) – Is the item editable? Default is true.
ItemSheet.activateListeners(html)

Activate listeners which provide interactivity for item sheet events

Arguments:
  • html (jQuery) – The HTML object returned by template rendering
ItemSheet.actor

type: Actor

The Actor instance which owns this item. This may be null if the item is unowned.

ItemSheet.getData()

Customize the data provided to the item sheet for rendering. By default we just duplicate the item data.

ItemSheet.item

type: Item

A convenience reference to the Item entity