ItemUtils .java

net.minecraft.world.item.ItemUtils (class): members: onContainerDestroyed, startUsingInstantly, createFilledResult. imports: Prediction, ItemEntity.

net.minecraft.world.itemclass50 linescommon1.7 KB

API Surface (4)

InteractionResult startUsingInstantly(final Level level, final Player player, final InteractionHand hand)
ItemStack createFilledResult(final ItemStack itemStack, final Player player, final ItemStack newItemStack, final boolean limitCreativeStackSize)
ItemStack createFilledResult(final ItemStack itemStack, final Player player, final ItemStack newItemStack)
void onContainerDestroyed(final ItemEntity container, final Stream<ItemStack> contents)

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ItemUtils.java50 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import java.util.stream.Stream;
4
import net.minecraft.util.Prediction;
5
import net.minecraft.world.InteractionHand;
6
import net.minecraft.world.InteractionResult;
7
import net.minecraft.world.entity.item.ItemEntity;
8
import net.minecraft.world.entity.player.Player;
9
import net.minecraft.world.level.Level;
10
11
public class ItemUtils {
12
	public static InteractionResult startUsingInstantly(final Level level, final Player player, final InteractionHand hand) {
13
		player.startUsingItem(hand);
14
		return InteractionResult.CONSUME;
15
	}
16
17
	public static ItemStack createFilledResult(final ItemStack itemStack, final Player player, final ItemStack newItemStack, final boolean limitCreativeStackSize) {
18
		boolean isCreative = player.hasInfiniteMaterials();
19
		if (limitCreativeStackSize && isCreative) {
20
			if (!player.getInventory().contains(newItemStack)) {
21
				player.getInventory().add(newItemStack);
22
			}
23
24
			return itemStack;
25
		} else {
26
			itemStack.consume(1, player);
27
			if (itemStack.isEmpty()) {
28
				return newItemStack;
29
			}
30
31
			if (!player.getInventory().add(newItemStack)) {
32
				player.drop(newItemStack, false, Prediction.PREDICTED);
33
			}
34
35
			return itemStack;
36
		}
37
	}
38
39
	public static ItemStack createFilledResult(final ItemStack itemStack, final Player player, final ItemStack newItemStack) {
40
		return createFilledResult(itemStack, player, newItemStack, true);
41
	}
42
43
	public static void onContainerDestroyed(final ItemEntity container, final Stream<ItemStack> contents) {
44
		Level level = container.level();
45
		if (!level.isClientSide()) {
46
			contents.forEach(stack -> level.addFreshEntity(new ItemEntity(level, container.getX(), container.getY(), container.getZ(), stack)));
47
		}
48
	}
49
}
50

Decompiled Minecraft 26.3 source of ItemUtils (net/minecraft/world/item/ItemUtils.java) with Mojang official mappings, stored verbatim on the site.