Script API - v1.21.120.21
    Preparing search index...

    Class EntityInventoryComponent

    Defines this entity's inventory properties.

    import { Container, ItemStack, Player, world } from "@minecraft/server";

    // Custom function to add an item to the inventory
    function addItemToInventory(itemStack: ItemStack, container: Container) {
    return container.addItem(itemStack);
    }

    // Custom function to move an item within the inventory
    function moveItemWithinInventory(fromSlot: number, toSlot: number, container: Container) {
    container.moveItem(fromSlot, toSlot, container);
    }

    // Custom function to transfer an item from inventory to another container
    function transferItemToContainer(fromSlot: number, fromContainer: Container, toContainer: Container) {
    return fromContainer.transferItem(fromSlot, toContainer);
    }

    function moveItemBetweenPlayers(fromPlayer: Player, toPlayer: Player) {
    const inventory = fromPlayer.getComponent("inventory");
    const toInventory = toPlayer.getComponent("inventory");
    fromPlayer.sendMessage(`additionalSlotsPerStrength: ${inventory.additionalSlotsPerStrength}`);
    fromPlayer.sendMessage(`canBeSiphonedFrom: ${inventory.canBeSiphonedFrom}`);
    fromPlayer.sendMessage(`containerType: ${inventory.containerType}`);
    fromPlayer.sendMessage(`inventorySize: ${inventory.inventorySize}`);
    fromPlayer.sendMessage(`private: ${inventory.private}`);
    fromPlayer.sendMessage(`restrictToOwner: ${inventory.restrictToOwner}`);
    fromPlayer.sendMessage(`isValid: ${inventory.isValid}`);
    const container = inventory.container;

    // Example usage of the custom functions
    const newItemStack = new ItemStack("apple", 10); // Assuming "apple" is a valid item
    const addedItem = addItemToInventory(newItemStack, container);
    if (addedItem) {
    console.log("Item added to inventory:", addedItem);
    }

    const sourceSlot = 2;
    const destinationSlot = 5;
    moveItemWithinInventory(sourceSlot, destinationSlot, container);

    const toContainer = toInventory.container; // Assuming 'someOtherContainer' is an instance of another container
    const transferredItem = transferItemToContainer(0, container, toContainer);
    if (transferredItem) {
    console.log("Item transferred to another container:", transferredItem);
    }
    }

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    additionalSlotsPerStrength: number

    Number of slots that this entity can gain per extra strength.

    This property can throw when used.

    This property can't be read in early-execution mode.

    canBeSiphonedFrom: boolean

    If true, the contents of this inventory can be removed by a hopper.

    This property can throw when used.

    This property can't be read in early-execution mode.

    container: Container

    Defines the container for this entity. The container will be undefined if the entity has been removed.

    This property can throw when used.

    InvalidEntityError

    This property can't be read in early-execution mode.

    containerType: string

    Type of container this entity has.

    This property can throw when used.

    This property can't be read in early-execution mode.

    entity: Entity

    The entity that owns this component. The entity will be undefined if it has been removed.

    This property can throw when used.

    InvalidEntityError

    This property can't be read in early-execution mode.

    inventorySize: number

    Number of slots the container has.

    This property can throw when used.

    This property can't be read in early-execution mode.

    isValid: boolean

    Returns whether the component is valid. A component is considered valid if its owner is valid, in addition to any addition to any additional validation required by the component.

    This property can't be read in early-execution mode.

    private: boolean

    If true, the entity will not drop it's inventory on death.

    This property can throw when used.

    This property can't be read in early-execution mode.

    restrictToOwner: boolean

    If true, the entity's inventory can only be accessed by its owner or itself.

    This property can throw when used.

    This property can't be read in early-execution mode.

    typeId: string

    Identifier of the component.

    This property can't be read in early-execution mode.

    componentId: "minecraft:inventory" = 'minecraft:inventory'