import { Container, ItemStack, Player, world } from"@minecraft/server";
// Custom function to add an item to the inventory functionaddItemToInventory(itemStack: ItemStack, container: Container) { returncontainer.addItem(itemStack); }
// Custom function to move an item within the inventory functionmoveItemWithinInventory(fromSlot: number, toSlot: number, container: Container) { container.moveItem(fromSlot, toSlot, container); }
// Custom function to transfer an item from inventory to another container functiontransferItemToContainer(fromSlot: number, fromContainer: Container, toContainer: Container) { returnfromContainer.transferItem(fromSlot, toContainer); }
// Example usage of the custom functions constnewItemStack = newItemStack("apple", 10); // Assuming "apple" is a valid item constaddedItem = addItemToInventory(newItemStack, container); if (addedItem) { console.log("Item added to inventory:", addedItem); }
consttoContainer = toInventory.container; // Assuming 'someOtherContainer' is an instance of another container consttransferredItem = transferItemToContainer(0, container, toContainer); if (transferredItem) { console.log("Item transferred to another container:", transferredItem); } }
This property can't be read in early-execution mode.
ReadonlyinventorySize
inventorySize:number
Remarks
Number of slots the container has.
Throws
This property can throw when used.
World Ready
This property can't be read in early-execution mode.
ReadonlyisValid
isValid:boolean
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.
World Ready
This property can't be read in early-execution mode.
Readonlyprivate
private:boolean
Remarks
If true, the entity will not drop it's inventory on death.
Throws
This property can throw when used.
World Ready
This property can't be read in early-execution mode.
ReadonlyrestrictToOwner
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.
World Ready
This property can't be read in early-execution mode.
ReadonlytypeId
typeId:string
Remarks
Identifier of the component.
World Ready
This property can't be read in early-execution mode.
Defines this entity's inventory properties.
Example: playerInventory.ts