InventoryCarrier .java

net.minecraft.world.entity.npc.InventoryCarrier (interface): members: readInventoryFromTag, writeInventoryToTag, TAG_INVENTORY, getInventory, pickUpItem.

net.minecraft.world.entity.npcinterface45 linescommon1.5 KB

API Surface (0)

No public/protected methods detected (fields or constants only).

Fields & Constants (1)

TAG_INVENTORY

String literals (1)

"Inventory"

Full Source

InventoryCarrier.java45 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.npc;
2
3
import net.minecraft.server.level.ServerLevel;
4
import net.minecraft.world.SimpleContainer;
5
import net.minecraft.world.entity.Mob;
6
import net.minecraft.world.entity.item.ItemEntity;
7
import net.minecraft.world.item.ItemStack;
8
import net.minecraft.world.level.storage.ValueInput;
9
import net.minecraft.world.level.storage.ValueOutput;
10
11
public interface InventoryCarrier {
12
	String TAG_INVENTORY = "Inventory";
13
14
	SimpleContainer getInventory();
15
16
	static void pickUpItem(final ServerLevel level, final Mob mob, final InventoryCarrier inventoryCarrier, final ItemEntity itemEntity) {
17
		ItemStack itemStack = itemEntity.getItem();
18
		if (mob.wantsToPickUp(level, itemStack)) {
19
			SimpleContainer inventory = inventoryCarrier.getInventory();
20
			boolean hasSpace = inventory.canAddItem(itemStack);
21
			if (!hasSpace) {
22
				return;
23
			}
24
25
			mob.onItemPickup(itemEntity);
26
			int count = itemStack.getCount();
27
			ItemStack remainder = inventory.addItem(itemStack);
28
			mob.take(itemEntity, count - remainder.getCount());
29
			if (remainder.isEmpty()) {
30
				itemEntity.discard();
31
			} else {
32
				itemStack.setCount(remainder.getCount());
33
			}
34
		}
35
	}
36
37
	default void readInventoryFromTag(final ValueInput input) {
38
		input.list("Inventory", ItemStack.CODEC).ifPresent(list -> this.getInventory().fromItemList((ValueInput.TypedInputList<ItemStack>)list));
39
	}
40
41
	default void writeInventoryToTag(final ValueOutput output) {
42
		this.getInventory().storeAsItemList(output.list("Inventory", ItemStack.CODEC));
43
	}
44
}
45

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