实验性实验性Type of item to create. See the @minecraft/vanilla-data.MinecraftItemTypes enumeration for a list of standard item types in Minecraft experiences.
可选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.
实验性amountNumber of the items in the stack. Valid values range between 1-255. The provided value will be clamped to the item's maximum stack size.
无法在只读模式下修改此属性,详见 WorldBeforeEvents。
只读 实验性is实验性keep实验性lockGets or sets the item's lock mode. The default value is
ItemLockMode.none.
无法在只读模式下修改此属性,详见 WorldBeforeEvents。
只读 实验性max可选 实验性nameGiven name of this stack of items. The name tag is displayed
when hovering over the item. Setting the name tag to an
empty string or undefined will remove the name tag.
无法在只读模式下修改此属性,详见 WorldBeforeEvents。
只读 实验性type只读 实验性type实验性Get the list of block types this item can break in Adventure mode.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
实验性Get the list of block types this item can be placed on in Adventure mode.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
实验性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.
import { world, ItemStack, EntityInventoryComponent, EntityComponentTypes, ItemComponentTypes, ItemDurabilityComponent, DimensionLocation } from "@minecraft/server";
import { MinecraftItemTypes } from "@minecraft/vanilla-data";
function giveHurtDiamondSword(
targetLocation: DimensionLocation
) {
const hurtDiamondSword = new ItemStack(MinecraftItemTypes.DiamondSword);
const durabilityComponent = hurtDiamondSword.getComponent(ItemComponentTypes.Durability) as ItemDurabilityComponent;
if (durabilityComponent !== undefined) {
durabilityComponent.damage = durabilityComponent.maxDurability / 2;
}
for (const player of world.getAllPlayers()) {
const inventory = player.getComponent(EntityComponentTypes.Inventory) as EntityInventoryComponent;
if (inventory && inventory.container) {
inventory.container.addItem(hurtDiamondSword);
}
}
}
实验性实验性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.
实验性ItemStack to check stacking compatibility with.
True if the Item Stack is stackable with the itemStack passed in. False for non-stackable items.
Returns whether this item stack can be stacked with the
given itemStack. This is determined by comparing the item
type and any custom data and properties associated with the
item stacks. The amount of each item stack is not taken into
consideration, but for non-stackable items this will always
return false.
实验性Identifier of the item.
可选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.
实验性可选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.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
import { world, ItemStack, EntityInventoryComponent, DimensionLocation } from "@minecraft/server";
import { MinecraftItemTypes } from "@minecraft/vanilla-data";
function giveDestroyRestrictedPickaxe(
targetLocation: DimensionLocation
) {
for (const player of world.getAllPlayers()) {
const specialPickaxe = new ItemStack(MinecraftItemTypes.DiamondPickaxe);
specialPickaxe.setCanDestroy([MinecraftItemTypes.Cobblestone, MinecraftItemTypes.Obsidian]);
const inventory = player.getComponent("inventory") as EntityInventoryComponent;
if (inventory === undefined || inventory.container === undefined) {
return;
}
inventory.container.addItem(specialPickaxe);
}
}
实验性可选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.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
import { world, ItemStack, EntityInventoryComponent, EntityComponentTypes, DimensionLocation } from "@minecraft/server";
import { MinecraftItemTypes } from "@minecraft/vanilla-data";
function givePlaceRestrictedGoldBlock(
targetLocation: DimensionLocation
) {
for (const player of world.getAllPlayers()) {
const specialGoldBlock = new ItemStack(MinecraftItemTypes.GoldBlock);
specialGoldBlock.setCanPlaceOn([MinecraftItemTypes.GrassBlock, MinecraftItemTypes.Dirt]);
const inventory = player.getComponent(EntityComponentTypes.Inventory) as EntityInventoryComponent;
if (inventory === undefined || inventory.container === undefined) {
return;
}
inventory.container.addItem(specialGoldBlock);
}
}
实验性可选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.
无法在只读模式下调用此函数,详见 WorldBeforeEvents。
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);
}
静态create实验性
Defines a collection of items.
示例: itemStacks.ts
示例: givePlayerEquipment.ts
示例: spawnFeatherItem.ts