Beta
Beta
Type of item to create. See the @minecraft/vanilla-data.MinecraftItemTypes enumeration for a list of standard item types in Minecraft experiences.
Optional
amount: numberNumber of items to place in the stack, between 1-255. The provided value will be clamped to the item's maximum stack size. Note that certain items can only have one item in the stack.
Beta
amountReadonly
Beta
isBeta
keepBeta
lockReadonly
Beta
maxOptional
Beta
nameReadonly
Beta
typeReadonly
Beta
typeBeta
The identifier of the component (e.g., 'minecraft:food'). If no namespace prefix is specified, 'minecraft:' is assumed. Available component IDs can be found as part of the ItemComponentTypes enum.
Returns the component if it exists on the item stack, otherwise undefined.
// Gives a player a half-damaged diamond sword
import { ItemStack, Player, ItemComponentTypes, EntityComponentTypes } from '@minecraft/server';
import { MinecraftItemTypes } from '@minecraft/vanilla-data';
function giveHurtDiamondSword(player: Player) {
const hurtDiamondSword = new ItemStack(MinecraftItemTypes.DiamondSword);
const durabilityComponent = hurtDiamondSword.getComponent(ItemComponentTypes.Durability);
if (durabilityComponent !== undefined) {
durabilityComponent.damage = durabilityComponent.maxDurability / 2;
}
const inventory = player.getComponent(EntityComponentTypes.Inventory);
if (inventory === undefined || inventory.container === undefined) {
return;
}
inventory.container.addItem(hurtDiamondSword);
}
import { world } from "@minecraft/server";
const player = world.getPlayers()[0];
const inventory = player.getComponent("inventory");
const slot = inventory.container.getSlot(player.selectedSlotIndex);
let durabilityComp = slot.getItem().getComponent("durability");
player.sendMessage(
"Item Durability: " +
(durabilityComp.maxDurability - durabilityComp.damage) +
"/" +
durabilityComp.maxDurability
);
Beta
Beta
Returns the total size, in bytes, of all the dynamic properties that are currently stored for this entity. This includes the size of both the key and the value. This can be useful for diagnosing performance warning signs - if, for example, an entity has many megabytes of associated dynamic properties, it may be slow to load on various devices.
Beta
Identifier of the item.
Optional
states: Record<string, string | number | boolean>Applicable only for blocks. An optional set of states to compare against. If states is not specified, matches checks against the set of types more broadly.
Returns a boolean whether the specified item matches.
Beta
Optional
blockIdentifiers: string[]String list of block types that the item can destroy.
The list of block types this item can break in Adventure mode. The block names are displayed in the item's tooltip. Setting the value to undefined will clear the list.
This function can't be called in read-only mode.
const specialPickaxe = new ItemStack('minecraft:diamond_pickaxe');
specialPickaxe.setCanDestroy(['minecraft:cobblestone', 'minecraft:obsidian']);
// Creates a diamond pickaxe that can destroy cobblestone and obsidian
import { ItemStack, Player } from '@minecraft/server';
import { MinecraftItemTypes } from '@minecraft/vanilla-data';
function giveRestrictedPickaxe(player: Player) {
const specialPickaxe = new ItemStack(MinecraftItemTypes.DiamondPickaxe);
specialPickaxe.setCanPlaceOn([MinecraftItemTypes.Cobblestone, MinecraftItemTypes.Obsidian]);
const inventory = player.getComponent('inventory');
if (inventory === undefined || inventory.container === undefined) {
return;
}
inventory.container.addItem(specialPickaxe);
}
Beta
Optional
blockIdentifiers: string[]String list of block types that the item can be placed on.
The list of block types this item can be placed on in Adventure mode. This is only applicable to block items. The block names are displayed in the item's tooltip. Setting the value to undefined will clear the list.
This function can't be called in read-only mode.
// Creates a gold block that can be placed on grass and dirt
import { ItemStack, Player, EntityComponentTypes } from '@minecraft/server';
import { MinecraftItemTypes } from '@minecraft/vanilla-data';
function giveRestrictedGoldBlock(player: Player) {
const specialGoldBlock = new ItemStack(MinecraftItemTypes.GoldBlock);
specialGoldBlock.setCanPlaceOn([MinecraftItemTypes.Grass, MinecraftItemTypes.Dirt]);
const inventory = player.getComponent(EntityComponentTypes.Inventory);
if (inventory === undefined || inventory.container === undefined) {
return;
}
inventory.container.addItem(specialGoldBlock);
}
Beta
Optional
loreList: string[]List of lore lines. Each element in the list represents a new line. The maximum lore line count is 20. The maximum lore line length is 50 characters.
Sets the lore value - a secondary display string - for an ItemStack. The lore list is cleared if set to an empty string or undefined.
This function can't be called in read-only mode.
import { EntityComponentTypes, ItemStack, Player } from '@minecraft/server';
import { MinecraftItemTypes } from '@minecraft/vanilla-data';
function giveAwesomeSword(player: Player) {
const diamondAwesomeSword = new ItemStack(MinecraftItemTypes.DiamondSword, 1);
diamondAwesomeSword.setLore([
'§c§lDiamond Sword of Awesome§r',
'+10 coolness', '§p+4 shiny§r'
]);
// hover over/select the item in your inventory to see the lore.
const inventory = player.getComponent(EntityComponentTypes.Inventory);
if (inventory === undefined || inventory.container === undefined) {
return;
}
inventory.container.setItem(0, diamondAwesomeSword);
}
Static
createBeta
import { ItemStack, world } from "@minecraft/server";
import {
MinecraftPotionEffectTypes,
MinecraftPotionLiquidTypes,
MinecraftPotionModifierTypes,
} from "@minecraft/vanilla-data";
const item = ItemStack.createPotion({
effect: MinecraftPotionEffectTypes.Harming,
liquid: MinecraftPotionLiquidTypes.Splash,
modifier: MinecraftPotionModifierTypes.Long,
});
for (const player of world.getAllPlayers()) {
player.getComponent("inventory").container.addItem(item);
}
import { ItemStack, world } from "@minecraft/server";
import {
MinecraftPotionEffectTypes,
MinecraftPotionLiquidTypes,
MinecraftPotionModifierTypes,
} from "@minecraft/vanilla-data";
const item = ItemStack.createPotion({
effect: MinecraftPotionEffectTypes.FireResistance,
liquid: MinecraftPotionLiquidTypes.Regular,
modifier: MinecraftPotionModifierTypes.Strong,
});
for (const player of world.getAllPlayers()) {
player.getComponent("inventory").container.addItem(item);
}
Defines a collection of items.
Example: givePlayerIronFireSword.ts
Example: givePlayerEquipment.ts
Example: spawnFeatherItem.ts