TradeWithVillager .java

net.minecraft.world.entity.ai.behavior.TradeWithVillager (class, 111 lines): members: trades, checkExtraStartConditions, canStillUse, stop, start, tick.

net.minecraft.world.entity.ai.behaviorclass111 linescommonextends Behavior<Villager>4.2 KB

API Surface (5)

boolean checkExtraStartConditions(final ServerLevel level, final Villager body)
boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp)
void start(final ServerLevel level, final Villager myBody, final long timestamp)
void tick(final ServerLevel level, final Villager body, final long timestamp)
void stop(final ServerLevel level, final Villager body, final long timestamp)

Fields & Constants (1)

trades

Full Source

TradeWithVillager.java111 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.google.common.collect.ImmutableMap;
4
import com.google.common.collect.ImmutableSet;
5
import java.util.Set;
6
import java.util.function.Predicate;
7
import java.util.stream.Collectors;
8
import net.minecraft.core.component.DataComponents;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.world.SimpleContainer;
11
import net.minecraft.world.entity.EntityTypes;
12
import net.minecraft.world.entity.LivingEntity;
13
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
14
import net.minecraft.world.entity.ai.memory.MemoryStatus;
15
import net.minecraft.world.entity.npc.villager.Villager;
16
import net.minecraft.world.entity.npc.villager.VillagerProfession;
17
import net.minecraft.world.item.Item;
18
import net.minecraft.world.item.ItemStack;
19
import net.minecraft.world.item.Items;
20
21
public class TradeWithVillager extends Behavior<Villager> {
22
	private Set<Item> trades = ImmutableSet.of();
23
24
	public TradeWithVillager() {
25
		super(
26
			ImmutableMap.of(
27
				MemoryModuleType.INTERACTION_TARGET, MemoryStatus.VALUE_PRESENT, MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, MemoryStatus.VALUE_PRESENT
28
			)
29
		);
30
	}
31
32
	protected boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
33
		return BehaviorUtils.targetIsValid(body.getBrain(), MemoryModuleType.INTERACTION_TARGET, EntityTypes.VILLAGER);
34
	}
35
36
	protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
37
		return this.checkExtraStartConditions(level, body);
38
	}
39
40
	protected void start(final ServerLevel level, final Villager myBody, final long timestamp) {
41
		Villager target = (Villager)myBody.getBrain().getMemory(MemoryModuleType.INTERACTION_TARGET).get();
42
		BehaviorUtils.lockGazeAndWalkToEachOther(myBody, target, 0.5F, 2);
43
		this.trades = figureOutWhatIAmWillingToTrade(myBody, target);
44
	}
45
46
	protected void tick(final ServerLevel level, final Villager body, final long timestamp) {
47
		Villager target = (Villager)body.getBrain().getMemory(MemoryModuleType.INTERACTION_TARGET).get();
48
		if (!(body.distanceToSqr(target) > 5.0)) {
49
			BehaviorUtils.lockGazeAndWalkToEachOther(body, target, 0.5F, 2);
50
			body.gossip(level, target, timestamp);
51
			boolean isFarmer = body.getVillagerData().profession().is(VillagerProfession.FARMER);
52
			if (body.hasExcessFood() && (isFarmer || target.wantsMoreFood())) {
53
				throwHalfStack(body, itemStack -> itemStack.has(DataComponents.VILLAGER_FOOD), target);
54
			}
55
56
			if (isFarmer && body.getInventory().countItem(Items.WHEAT) > Items.WHEAT.getDefaultMaxStackSize() / 2) {
57
				throwHalfStack(body, itemStack -> itemStack.is(Items.WHEAT), target);
58
			}
59
60
			if (!this.trades.isEmpty() && body.getInventory().hasAnyOf(this.trades)) {
61
				throwHalfStack(body, itemStack -> this.trades.contains(itemStack.getItem()), target);
62
			}
63
		}
64
	}
65
66
	protected void stop(final ServerLevel level, final Villager body, final long timestamp) {
67
		body.getBrain().eraseMemory(MemoryModuleType.INTERACTION_TARGET);
68
	}
69
70
	private static Set<Item> figureOutWhatIAmWillingToTrade(final Villager myBody, final Villager target) {
71
		ImmutableSet<Item> targetItems = target.getVillagerData().profession().value().requestedItems();
72
		ImmutableSet<Item> selfItems = myBody.getVillagerData().profession().value().requestedItems();
73
		return targetItems.stream().filter(entry -> !selfItems.contains(entry)).collect(Collectors.toSet());
74
	}
75
76
	private static void throwHalfStack(final Villager villager, final Predicate<ItemStack> predicate, final LivingEntity target) {
77
		SimpleContainer inventory = villager.getInventory();
78
		ItemStack toThrow = ItemStack.EMPTY;
79
		int i = 0;
80
81
		while (i < inventory.getContainerSize()) {
82
			ItemStack itemStack;
83
			int count;
84
			label28: {
85
				itemStack = inventory.getItem(i);
86
				if (!itemStack.isEmpty() && predicate.test(itemStack)) {
87
					if (itemStack.getCount() > itemStack.getMaxStackSize() / 2) {
88
						count = itemStack.getCount() / 2;
89
						break label28;
90
					}
91
92
					if (itemStack.getCount() > 24) {
93
						count = itemStack.getCount() - 24;
94
						break label28;
95
					}
96
				}
97
98
				i++;
99
				continue;
100
			}
101
102
			toThrow = itemStack.split(count);
103
			break;
104
		}
105
106
		if (!toThrow.isEmpty()) {
107
			BehaviorUtils.throwItem(villager, toThrow, target.position());
108
		}
109
	}
110
}
111

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