#Add items in container

1 messages · Page 1 of 1 (latest)

turbid ember
#

is it possible to put items in chest container using script?

opaque agate
#

yes

#

i’ll get a example for you in a sec

#

this is from the docs so i’m not sure on it working properly but it’s the rough idea ```js

import { ItemStack, BlockInventoryComponent, DimensionLocation } from "@minecraft/server";
import { MinecraftBlockTypes, MinecraftItemTypes } from "@minecraft/vanilla-data";

function placeItemsInChest(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
// Fetch block
const block = targetLocation.dimension.getBlock(targetLocation);

if (!block) {
log("Could not find block. Maybe it is not loaded?", -1);
return;
}

// Make it a chest
block.setType(MinecraftBlockTypes.Chest);

// Get the inventory
const inventoryComponent = block.getComponent("inventory") as BlockInventoryComponent;

if (!inventoryComponent || !inventoryComponent.container) {
log("Could not find inventory component.", -1);
return;
}

const inventoryContainer = inventoryComponent.container;

// Set slot 0 to a stack of 10 apples
inventoryContainer.setItem(0, new ItemStack(MinecraftItemTypes.Apple, 10));
}``` from https://jaylydev.github.io/scriptapi-docs/latest/classes/_minecraft_server.BlockInventoryComponent.html