WorkAtComposter .java

net.minecraft.world.entity.ai.behavior.WorkAtComposter (class): members: COMPOSTABLE_ITEMS, useWorkstation. imports: ComposterBlock, SimpleContainer, Item.

net.minecraft.world.entity.ai.behaviorclass95 linescommonextends WorkAtPoi3.7 KB

API Surface (1)

void useWorkstation(final ServerLevel level, final Villager body)

Fields & Constants (1)

COMPOSTABLE_ITEMS

Full Source

WorkAtComposter.java95 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.google.common.collect.ImmutableList;
4
import java.util.List;
5
import java.util.Optional;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.GlobalPos;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.world.SimpleContainer;
10
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
11
import net.minecraft.world.entity.npc.villager.Villager;
12
import net.minecraft.world.item.Item;
13
import net.minecraft.world.item.ItemStack;
14
import net.minecraft.world.item.Items;
15
import net.minecraft.world.level.block.Blocks;
16
import net.minecraft.world.level.block.ComposterBlock;
17
import net.minecraft.world.level.block.state.BlockState;
18
19
public class WorkAtComposter extends WorkAtPoi {
20
	private static final List<Item> COMPOSTABLE_ITEMS = ImmutableList.of(Items.WHEAT_SEEDS, Items.BEETROOT_SEEDS);
21
22
	@Override
23
	protected void useWorkstation(final ServerLevel level, final Villager body) {
24
		Optional<GlobalPos> jobSiteMemory = body.getBrain().getMemory(MemoryModuleType.JOB_SITE);
25
		if (!jobSiteMemory.isEmpty()) {
26
			GlobalPos jobSitePos = jobSiteMemory.get();
27
			BlockState blockState = level.getBlockState(jobSitePos.pos());
28
			if (blockState.is(Blocks.COMPOSTER)) {
29
				this.makeBread(level, body);
30
				this.compostItems(level, body, jobSitePos, blockState);
31
			}
32
		}
33
	}
34
35
	private void compostItems(final ServerLevel level, final Villager body, final GlobalPos jobSitePos, BlockState blockState) {
36
		BlockPos pos = jobSitePos.pos();
37
		if (blockState.getValue(ComposterBlock.LEVEL) == 8) {
38
			blockState = ComposterBlock.extractProduce(body, blockState, level, pos);
39
		}
40
41
		int totalItemsToUse = 20;
42
		int minStackSize = 10;
43
		int[] itemsSeenSoFar = new int[COMPOSTABLE_ITEMS.size()];
44
		SimpleContainer inventory = body.getInventory();
45
		int containerSize = inventory.getContainerSize();
46
		BlockState tempState = blockState;
47
48
		for (int i = containerSize - 1; i >= 0 && totalItemsToUse > 0; i--) {
49
			ItemStack itemStack = inventory.getItem(i);
50
			int itemIndex = COMPOSTABLE_ITEMS.indexOf(itemStack.getItem());
51
			if (itemIndex != -1) {
52
				int stackSize = itemStack.getCount();
53
				int totalItemCount = itemsSeenSoFar[itemIndex] + stackSize;
54
				itemsSeenSoFar[itemIndex] = totalItemCount;
55
				int itemsToUse = Math.min(Math.min(totalItemCount - 10, totalItemsToUse), stackSize);
56
				if (itemsToUse > 0) {
57
					totalItemsToUse -= itemsToUse;
58
59
					for (int j = 0; j < itemsToUse; j++) {
60
						tempState = ComposterBlock.insertItem(body, tempState, level, itemStack, pos);
61
						if (tempState.getValue(ComposterBlock.LEVEL) == 7) {
62
							this.spawnComposterFillEffects(level, blockState, pos, tempState);
63
							return;
64
						}
65
					}
66
				}
67
			}
68
		}
69
70
		this.spawnComposterFillEffects(level, blockState, pos, tempState);
71
	}
72
73
	private void spawnComposterFillEffects(final ServerLevel level, final BlockState blockState, final BlockPos pos, final BlockState newState) {
74
		level.levelEvent(1500, pos, newState != blockState ? 1 : 0);
75
	}
76
77
	private void makeBread(final ServerLevel level, final Villager body) {
78
		SimpleContainer inventory = body.getInventory();
79
		if (inventory.countItem(Items.BREAD) <= 36) {
80
			int howMuchWheatIHave = inventory.countItem(Items.WHEAT);
81
			int maxAmountOfBreadToMake = 3;
82
			int amountOfWheatNeededToCraftOneBread = 3;
83
			int howMuchBreadToMake = Math.min(3, howMuchWheatIHave / 3);
84
			if (howMuchBreadToMake != 0) {
85
				int howMuchWheatToUse = howMuchBreadToMake * 3;
86
				inventory.removeItemType(Items.WHEAT, howMuchWheatToUse);
87
				ItemStack breadICantCarry = inventory.addItem(new ItemStack(Items.BREAD, howMuchBreadToMake));
88
				if (!breadICantCarry.isEmpty()) {
89
					body.spawnAtLocation(level, breadICantCarry, 0.5F);
90
				}
91
			}
92
		}
93
	}
94
}
95

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