Defines this entity's inventory properties.

Example

playerInventory.ts

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

Constructors

Properties

additionalSlotsPerStrength: number

Remarks

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

Throws

This property can throw when used.

canBeSiphonedFrom: boolean

Remarks

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

Throws

This property can throw when used.

container?: Container

Remarks

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

Throws

This property can throw when used.

containerType: string

Remarks

Type of container this entity has.

Throws

This property can throw when used.

entity: Entity

Remarks

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

inventorySize: number

Remarks

Number of slots the container has.

Throws

This property can throw when used.

private: boolean

Remarks

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

Throws

This property can throw when used.

restrictToOwner: boolean

Remarks

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

Throws

This property can throw when used.

typeId: string

Remarks

Identifier of the component.

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

Methods

  • Returns boolean

    Whether the component is valid.

    Remarks

    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.