Class ItemStack
java.lang.Object
net.minecraft.world.item.ItemStack
- All Implemented Interfaces:
DataComponentGetter, DataComponentHolder, TypedInstance<Item>, ItemInstance, IDataComponentHolderExtension, IItemStackExtension, MutableDataComponentHolder
public final class ItemStack
extends Object
implements DataComponentHolder, ItemInstance, MutableDataComponentHolder, IItemStackExtension
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.mojang.serialization.Codec<ItemStack> private final PatchedDataComponentMapprivate intprivate static final Componentstatic final ItemStackprivate @Nullable Entityprivate static final ComponentDeprecated.private static final org.slf4j.Loggerstatic final com.mojang.serialization.MapCodec<ItemStack> static final com.mojang.serialization.Codec<ItemStack> static final StreamCodec<RegistryFriendlyByteBuf, List<ItemStack>> static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> private intstatic final StreamCodec<RegistryFriendlyByteBuf, ItemStack> private static final ComponentFields inherited from interface ItemInstance
FIELD_COMPONENTS, FIELD_COUNT, FIELD_ID -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateItemStack(Holder<Item> item, int count, DataComponentPatch components) privateItemStack(Holder<Item> item, int count, PatchedDataComponentMap components) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddAttributeTooltips(Consumer<Component> consumer, TooltipDisplay display, @Nullable Player player) Deprecated.voidaddDetailsToTooltip(Item.TooltipContext context, TooltipDisplay display, @Nullable Player player, TooltipFlag tooltipFlag, Consumer<Component> builder) <T extends TooltipProvider>
voidaddToTooltip(DataComponentType<T> type, Item.TooltipContext context, TooltipDisplay display, Consumer<Component> consumer, TooltipFlag flag) private voidaddUnitComponentToTooltip(DataComponentType<?> dataComponentType, Component component, TooltipDisplay display, Consumer<Component> builder) private ItemStackapplyAfterUseComponentSideEffects(LivingEntity user, ItemStack stackBeforeUsing) voidapplyComponents(DataComponentMap components) Applies a set of component changes to this stack.voidApplies a set of component changes to this stack.voidprivate voidapplyDamage(int newDamage, @Nullable ServerPlayer player, Consumer<Item> onBreak) private voidapplyDamage(int newDamage, @Nullable LivingEntity player, Consumer<Item> onBreak) booleancanBeHurtBy(DamageSource source) booleancanBreakBlockInAdventureMode(BlockInWorld blockInWorld) booleancanDestroyBlock(BlockState state, Level level, BlockPos pos, Player player) booleancanPlaceOnBlockInAdventureMode(BlockInWorld blockInWorld) voidcauseUseVibration(Entity causer, Holder.Reference<GameEvent> event) voidconsume(int amount, @Nullable LivingEntity owner) consumeAndReturn(int amount, @Nullable LivingEntity owner) copy()<T> voidcopyFrom(DataComponentType<T> type, DataComponentGetter source) Copies a data component from a component getter.copyWithCount(int count) intcount()private static StreamCodec<RegistryFriendlyByteBuf, ItemStack> voidenchant(Holder<Enchantment> enchantment, int level) finishUsingItem(Level level, LivingEntity livingEntity) voidforEachModifier(EquipmentSlotGroup slot, org.apache.commons.lang3.function.TriConsumer<Holder<Attribute>, AttributeModifier, ItemAttributeModifiers.Display> consumer) voidforEachModifier(EquipmentSlot slot, BiConsumer<Holder<Attribute>, AttributeModifier> consumer) intintintgetCount()@Nullable ComponentgetDamageSource(LivingEntity attacker, Supplier<DamageSource> defaultSource) intfloatgetDestroySpeed(BlockState state) Deprecated.Neo: UsegetTagEnchantments()for NBT enchantments, orIItemStackExtension.getAllEnchantments(HolderLookup.RegistryLookup)for gameplay.@Nullable Entity@Nullable ItemFramegetFrame()getItem()intintintGets all enchantments from NBT.getTooltipLines(Item.TooltipContext context, @Nullable Player player, TooltipFlag tooltipFlag) intgetUseDuration(LivingEntity user) voidgrow(int amount) booleanhasFoil()static inthashItemAndComponents(@Nullable ItemStack item) static inthashStackList(List<ItemStack> items) Deprecated.booleanhasNonDefault(DataComponentType<?> type) voidhurtAndBreak(int amount, ServerLevel level, @Nullable ServerPlayer player, Consumer<Item> onBreak) voidhurtAndBreak(int amount, ServerLevel level, @Nullable LivingEntity player, Consumer<Item> onBreak) voidhurtAndBreak(int amount, LivingEntity owner, EquipmentSlot slot) voidhurtAndBreak(int amount, LivingEntity owner, InteractionHand hand) hurtAndConvertOnBreak(int amount, ItemLike newItem, LivingEntity owner, EquipmentSlot slot) booleanhurtEnemy(LivingEntity mob, LivingEntity attacker) voidhurtWithoutBreaking(int amount, Player player) interactLivingEntity(Player player, LivingEntity target, InteractionHand hand) voidinventoryTick(Level level, Entity owner, @Nullable EquipmentSlot slot) booleanbooleanbooleanisBroken()booleanbooleanisCorrectToolForDrops(BlockState state) booleanbooleanbooleanisEmpty()booleanbooleanbooleanisFramed()booleanisItemEnabled(FeatureFlagSet enabledFeatures) static booleanisSameItem(ItemStack a, @Nullable ItemStackTemplate b) Returns true if a and b refer to the same item, or if a is empty and b is null.static booleanisSameItem(ItemStack a, ItemStack b) static booleanisSameItemSameComponents(ItemStack a, @Nullable ItemStackTemplate b) Compares the item and components of this stack against anItemStackTemplate.static booleanbooleanbooleanisValidRepairItem(ItemStack repairItem) static com.mojang.serialization.MapCodec<ItemStack> lenientOptionalFieldOf(String name) voidlimitSize(int maxStackSize) static booleanlistMatches(List<ItemStack> left, List<ItemStack> right) Deprecated.static booleanmatches(ItemStack a, @Nullable ItemStackTemplate b) Compares an itemstack with anItemStackTemplateas permatches(ItemStack, ItemStack).static booleanstatic booleanmatchesIgnoringComponents(ItemStack a, ItemStack b, Predicate<DataComponentType<?>> ignoredPredicate) voidmineBlock(Level level, BlockState state, BlockPos pos, Player owner) booleanvoidonCraftedBy(Player player, int craftCount) voidonCraftedBySystem(Level level) voidonDestroyed(ItemEntity itemEntity) Deprecated.Forge: Use damage source sensitive versionprivate InteractionResultonItemUse(UseOnContext context, Function<UseOnContext, InteractionResult> callback) onItemUseFirst(UseOnContext context) voidonUseTick(Level level, LivingEntity livingEntity, int ticksRemaining) booleanoverrideOtherStackedOnMe(ItemStack other, Slot slot, ClickAction clickAction, Player player, SlotAccess carriedItem) booleanoverrideStackedOnOther(Slot slot, ClickAction clickAction, Player player) voidpostHurtEnemy(LivingEntity mob, LivingEntity attacker) private intprocessDurabilityChange(int amount, ServerLevel level, @Nullable ServerPlayer player) private intprocessDurabilityChange(int amount, ServerLevel level, @Nullable LivingEntity player) voidreleaseUsing(Level level, LivingEntity entity, int remainingTime) <T> @Nullable Tremove(DataComponentType<? extends T> type) Removes a data component.<T> @Nullable Tset(DataComponentType<T> type, @Nullable T value) Sets a data component.<T> @Nullable Tset(TypedDataComponent<T> value) voidsetCount(int count) voidsetDamageValue(int value) voidsetEntityRepresentation(@Nullable Entity entity) voidsetPopTime(int popTime) voidshrink(int amount) split(int amount) toString()transmuteCopy(ItemLike newItem) transmuteCopy(ItemLike newItem, int newCount) private ItemStacktransmuteCopyIgnoreEmpty(ItemLike newItem, int newCount) <T> @Nullable Tupdate(DataComponentType<T> type, T defaultValue, UnaryOperator<T> function) Updates a data component if it exists.<T,U> @Nullable T update(DataComponentType<T> type, T defaultValue, U value, BiFunction<T, U, T> combiner) Updates a data component if it exists, using an additionalupdateContext.use(Level level, Player player, InteractionHand hand) useOn(UseOnContext context) booleanstatic com.mojang.serialization.DataResult<?> validateComponents(DataComponentMap components) private static com.mojang.serialization.DataResult<?> validateContainedItemSizes(Iterable<? extends ItemInstance> items) static StreamCodec<RegistryFriendlyByteBuf, ItemStack> static com.mojang.serialization.DataResult<ItemStack> validateStrict(ItemStack itemStack) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface DataComponentGetter
get, getOrDefault, getTyped, hasMethods inherited from interface DataComponentHolder
get, getAllOfType, getOrDefault, hasMethods inherited from interface IDataComponentHolderExtension
addToTooltipMethods inherited from interface IItemStackExtension
canEquip, canFitInsideContainerItems, canGrindstoneRepair, canPerformAction, canWalkOnPowderedSnow, doesSneakBypassUse, getAllEnchantments, getAttributeModifiers, getBurnTime, getCapability, getCapability, getCraftingRemainder, getEnchantmentLevel, getEntityLifespan, getEquipmentSlot, getHighlightTip, getSweepHitBox, getXpRepairRatio, isCombineRepairable, isGazeDisguise, isNotReplaceableByPickAction, isPiglinCurrency, isPrimaryItemFor, makesPiglinsNeutral, onAnimalArmorTick, onDestroyed, onDroppedByPlayer, onEntityItemUpdate, onEntitySwing, onStopUsing, shouldCauseBlockBreakReset, supportsEnchantment
-
Field Details
-
OP_NBT_WARNING
-
UNBREAKABLE_TOOLTIP
-
INTANGIBLE_TOOLTIP
-
MAP_CODEC
-
CODEC
-
OPTIONAL_CODEC
-
OPTIONAL_STREAM_CODEC
-
OPTIONAL_UNTRUSTED_STREAM_CODEC
-
STREAM_CODEC
-
OPTIONAL_LIST_STREAM_CODEC
public static final StreamCodec<RegistryFriendlyByteBuf, List<ItemStack>> OPTIONAL_LIST_STREAM_CODEC -
LOGGER
private static final org.slf4j.Logger LOGGER -
EMPTY
-
DISABLED_ITEM_TOOLTIP
-
count
private int count -
popTime
private int popTime -
item
Deprecated. -
components
-
entityRepresentation
-
-
Constructor Details
-
ItemStack
-
ItemStack
-
ItemStack
-
ItemStack
-
ItemStack
-
ItemStack
-
ItemStack
-
-
Method Details
-
validateStrict
-
createOptionalStreamCodec
private static StreamCodec<RegistryFriendlyByteBuf, ItemStack> createOptionalStreamCodec(StreamCodec<RegistryFriendlyByteBuf, DataComponentPatch> patchCodec) -
validatedStreamCodec
public static StreamCodec<RegistryFriendlyByteBuf, ItemStack> validatedStreamCodec(StreamCodec<RegistryFriendlyByteBuf, ItemStack> codec) -
getTooltipImage
-
getComponents
- Specified by:
getComponentsin interfaceDataComponentHolder
-
getPrototype
-
getComponentsPatch
-
immutableComponents
-
hasNonDefault
-
isComponentsPatchEmpty
public boolean isComponentsPatchEmpty() -
validateComponents
public static com.mojang.serialization.DataResult<?> validateComponents(DataComponentMap components) -
validateContainedItemSizes
private static com.mojang.serialization.DataResult<?> validateContainedItemSizes(Iterable<? extends ItemInstance> items) -
isEmpty
public boolean isEmpty() -
isItemEnabled
-
split
-
copyAndClear
-
getItem
-
typeHolder
- Specified by:
typeHolderin interfaceTypedInstance<Item>
-
is
-
useOn
-
onItemUseFirst
- Specified by:
onItemUseFirstin interfaceIItemStackExtension
-
onItemUse
private InteractionResult onItemUse(UseOnContext context, Function<UseOnContext, InteractionResult> callback) -
getDestroySpeed
-
use
-
finishUsingItem
-
applyAfterUseComponentSideEffects
-
getMaxStackSize
public int getMaxStackSize()- Specified by:
getMaxStackSizein interfaceItemInstance
-
isStackable
public boolean isStackable() -
isDamageableItem
public boolean isDamageableItem() -
isDamaged
public boolean isDamaged() -
getDamageValue
public int getDamageValue() -
setDamageValue
public void setDamageValue(int value) -
getMaxDamage
public int getMaxDamage() -
isBroken
public boolean isBroken() -
nextDamageWillBreak
public boolean nextDamageWillBreak() -
hurtAndBreak
public void hurtAndBreak(int amount, ServerLevel level, @Nullable ServerPlayer player, Consumer<Item> onBreak) -
hurtAndBreak
public void hurtAndBreak(int amount, ServerLevel level, @Nullable LivingEntity player, Consumer<Item> onBreak) -
processDurabilityChange
-
processDurabilityChange
-
applyDamage
-
applyDamage
-
hurtWithoutBreaking
-
hurtAndBreak
-
hurtAndBreak
-
hurtAndConvertOnBreak
public ItemStack hurtAndConvertOnBreak(int amount, ItemLike newItem, LivingEntity owner, EquipmentSlot slot) -
isBarVisible
public boolean isBarVisible() -
getBarWidth
public int getBarWidth() -
getBarColor
public int getBarColor() -
overrideStackedOnOther
-
overrideOtherStackedOnMe
public boolean overrideOtherStackedOnMe(ItemStack other, Slot slot, ClickAction clickAction, Player player, SlotAccess carriedItem) -
hurtEnemy
-
postHurtEnemy
-
mineBlock
-
isCorrectToolForDrops
-
interactLivingEntity
public InteractionResult interactLivingEntity(Player player, LivingEntity target, InteractionHand hand) -
copy
-
copyWithCount
-
transmuteCopy
-
transmuteCopy
-
transmuteCopyIgnoreEmpty
-
matches
-
matches
Compares an itemstack with anItemStackTemplateas permatches(ItemStack, ItemStack). -
listMatches
Deprecated. -
isSameItem
-
isSameItemSameComponents
-
isSameItem
Returns true if a and b refer to the same item, or if a is empty and b is null.- Returns:
- true if a and b refer to the same item, or if a is empty and b is null
-
isSameItemSameComponents
Compares the item and components of this stack against anItemStackTemplate.- Returns:
- True if either this stack is empty and the template is null, or they reference the same item and have equivalent component patches.
-
matchesIgnoringComponents
public static boolean matchesIgnoringComponents(ItemStack a, ItemStack b, Predicate<DataComponentType<?>> ignoredPredicate) -
lenientOptionalFieldOf
-
hashItemAndComponents
-
hashStackList
Deprecated. -
toString
-
inventoryTick
-
onCraftedBy
-
onCraftedBySystem
-
getUseDuration
-
getUseAnimation
-
releaseUsing
-
causeUseVibration
-
useOnRelease
public boolean useOnRelease() -
set
Description copied from interface:MutableDataComponentHolderSets a data component.- Specified by:
setin interfaceMutableDataComponentHolder
-
set
-
copyFrom
Description copied from interface:MutableDataComponentHolderCopies a data component from a component getter.- Specified by:
copyFromin interfaceMutableDataComponentHolder
-
update
public <T,U> @Nullable T update(DataComponentType<T> type, T defaultValue, U value, BiFunction<T, U, T> combiner) Description copied from interface:MutableDataComponentHolderUpdates a data component if it exists, using an additionalupdateContext.- Specified by:
updatein interfaceMutableDataComponentHolder
-
update
Description copied from interface:MutableDataComponentHolderUpdates a data component if it exists.- Specified by:
updatein interfaceMutableDataComponentHolder
-
remove
Description copied from interface:MutableDataComponentHolderRemoves a data component.- Specified by:
removein interfaceMutableDataComponentHolder
-
applyComponentsAndValidate
-
applyComponents
Description copied from interface:MutableDataComponentHolderApplies a set of component changes to this stack.- Specified by:
applyComponentsin interfaceMutableDataComponentHolder
-
applyComponents
Description copied from interface:MutableDataComponentHolderApplies a set of component changes to this stack.- Specified by:
applyComponentsin interfaceMutableDataComponentHolder
-
getHoverName
-
getCustomName
-
getItemName
-
getStyledHoverName
-
addToTooltip
public <T extends TooltipProvider> void addToTooltip(DataComponentType<T> type, Item.TooltipContext context, TooltipDisplay display, Consumer<Component> consumer, TooltipFlag flag) - Specified by:
addToTooltipin interfaceIDataComponentHolderExtension
-
getTooltipLines
public List<Component> getTooltipLines(Item.TooltipContext context, @Nullable Player player, TooltipFlag tooltipFlag) -
addDetailsToTooltip
public void addDetailsToTooltip(Item.TooltipContext context, TooltipDisplay display, @Nullable Player player, TooltipFlag tooltipFlag, Consumer<Component> builder) -
addUnitComponentToTooltip
private void addUnitComponentToTooltip(DataComponentType<?> dataComponentType, Component component, TooltipDisplay display, Consumer<Component> builder) -
addAttributeTooltips
@Deprecated private void addAttributeTooltips(Consumer<Component> consumer, TooltipDisplay display, @Nullable Player player) -
hasFoil
public boolean hasFoil() -
getRarity
-
isEnchantable
public boolean isEnchantable() -
enchant
-
isEnchanted
public boolean isEnchanted() -
getTagEnchantments
Gets all enchantments from NBT. UseIItemStackExtension.getAllEnchantments(HolderLookup.RegistryLookup)for gameplay logic. -
getEnchantments
Deprecated.Neo: UsegetTagEnchantments()for NBT enchantments, orIItemStackExtension.getAllEnchantments(HolderLookup.RegistryLookup)for gameplay. -
isFramed
public boolean isFramed() -
setEntityRepresentation
-
getFrame
-
getEntityRepresentation
-
forEachModifier
public void forEachModifier(EquipmentSlotGroup slot, org.apache.commons.lang3.function.TriConsumer<Holder<Attribute>, AttributeModifier, ItemAttributeModifiers.Display> consumer) -
forEachModifier
public void forEachModifier(EquipmentSlot slot, BiConsumer<Holder<Attribute>, AttributeModifier> consumer) -
getDisplayName
-
getSwingAnimation
-
canPlaceOnBlockInAdventureMode
-
canBreakBlockInAdventureMode
-
getPopTime
public int getPopTime() -
setPopTime
public void setPopTime(int popTime) -
getCount
public int getCount() -
count
public int count()- Specified by:
countin interfaceItemInstance
-
setCount
public void setCount(int count) -
limitSize
public void limitSize(int maxStackSize) -
grow
public void grow(int amount) -
shrink
public void shrink(int amount) -
consume
-
consumeAndReturn
-
onUseTick
-
onDestroyed
Deprecated.Forge: Use damage source sensitive version -
canBeHurtBy
-
isValidRepairItem
-
canDestroyBlock
-
getDamageSource
-
AttributeUtil.addAttributeTooltips(ItemStack, Consumer, TooltipDisplay, AttributeTooltipContext)