ShowTradesToPlayer .java

net.minecraft.world.entity.ai.behavior.ShowTradesToPlayer (class): members: STARTING_LOOK_TIME, playerItemStack, MAX_LOOK_TIME, cycleCounter, displayIndex.

net.minecraft.world.entity.ai.behaviorclass136 linescommonextends Behavior<Villager>4.9 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 body, 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 (7)

MAX_LOOK_TIME
STARTING_LOOK_TIME
playerItemStack
displayItems
cycleCounter
displayIndex
lookTime

Full Source

ShowTradesToPlayer.java136 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.Lists;
5
import java.util.List;
6
import net.minecraft.server.level.ServerLevel;
7
import net.minecraft.world.entity.EntityTypes;
8
import net.minecraft.world.entity.EquipmentSlot;
9
import net.minecraft.world.entity.LivingEntity;
10
import net.minecraft.world.entity.ai.Brain;
11
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
12
import net.minecraft.world.entity.ai.memory.MemoryStatus;
13
import net.minecraft.world.entity.npc.villager.Villager;
14
import net.minecraft.world.item.ItemStack;
15
import net.minecraft.world.item.trading.MerchantOffer;
16
import org.jspecify.annotations.Nullable;
17
18
public class ShowTradesToPlayer extends Behavior<Villager> {
19
	private static final int MAX_LOOK_TIME = 900;
20
	private static final int STARTING_LOOK_TIME = 40;
21
	private @Nullable ItemStack playerItemStack;
22
	private final List<ItemStack> displayItems = Lists.newArrayList();
23
	private int cycleCounter;
24
	private int displayIndex;
25
	private int lookTime;
26
27
	public ShowTradesToPlayer(final int minDuration, final int maxDuration) {
28
		super(ImmutableMap.of(MemoryModuleType.INTERACTION_TARGET, MemoryStatus.VALUE_PRESENT), minDuration, maxDuration);
29
	}
30
31
	public boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
32
		Brain<?> brain = body.getBrain();
33
		if (brain.getMemory(MemoryModuleType.INTERACTION_TARGET).isEmpty()) {
34
			return false;
35
		}
36
37
		LivingEntity target = brain.getMemory(MemoryModuleType.INTERACTION_TARGET).get();
38
		return target.is(EntityTypes.PLAYER) && body.isAlive() && target.isAlive() && !body.isBaby() && body.distanceToSqr(target) <= 17.0;
39
	}
40
41
	public boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
42
		return this.checkExtraStartConditions(level, body) && this.lookTime > 0 && body.getBrain().getMemory(MemoryModuleType.INTERACTION_TARGET).isPresent();
43
	}
44
45
	public void start(final ServerLevel level, final Villager body, final long timestamp) {
46
		super.start(level, body, timestamp);
47
		this.lookAtTarget(body);
48
		this.cycleCounter = 0;
49
		this.displayIndex = 0;
50
		this.lookTime = 40;
51
	}
52
53
	public void tick(final ServerLevel level, final Villager body, final long timestamp) {
54
		LivingEntity target = this.lookAtTarget(body);
55
		this.findItemsToDisplay(target, body);
56
		if (!this.displayItems.isEmpty()) {
57
			this.displayCyclingItems(body);
58
		} else {
59
			clearHeldItem(body);
60
			this.lookTime = Math.min(this.lookTime, 40);
61
		}
62
63
		this.lookTime--;
64
	}
65
66
	public void stop(final ServerLevel level, final Villager body, final long timestamp) {
67
		super.stop(level, body, timestamp);
68
		body.getBrain().eraseMemory(MemoryModuleType.INTERACTION_TARGET);
69
		clearHeldItem(body);
70
		this.playerItemStack = null;
71
	}
72
73
	private void findItemsToDisplay(final LivingEntity player, final Villager villager) {
74
		boolean changed = false;
75
		ItemStack currentPlayerItemStack = player.getMainHandItem();
76
		if (this.playerItemStack == null || !ItemStack.isSameItem(this.playerItemStack, currentPlayerItemStack)) {
77
			this.playerItemStack = currentPlayerItemStack;
78
			changed = true;
79
			this.displayItems.clear();
80
		}
81
82
		if (changed && !this.playerItemStack.isEmpty()) {
83
			this.updateDisplayItems(villager);
84
			if (!this.displayItems.isEmpty()) {
85
				this.lookTime = 900;
86
				this.displayFirstItem(villager);
87
			}
88
		}
89
	}
90
91
	private void displayFirstItem(final Villager villager) {
92
		displayAsHeldItem(villager, this.displayItems.get(0));
93
	}
94
95
	private void updateDisplayItems(final Villager villager) {
96
		for (MerchantOffer offer : villager.getOffers()) {
97
			if (!offer.isOutOfStock() && this.playerItemStackMatchesCostOfOffer(offer)) {
98
				this.displayItems.add(offer.assemble());
99
			}
100
		}
101
	}
102
103
	private boolean playerItemStackMatchesCostOfOffer(final MerchantOffer offer) {
104
		return ItemStack.isSameItem(this.playerItemStack, offer.getCostA()) || ItemStack.isSameItem(this.playerItemStack, offer.getCostB());
105
	}
106
107
	private static void clearHeldItem(final Villager body) {
108
		body.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
109
		body.setDropChance(EquipmentSlot.MAINHAND, 0.085F);
110
	}
111
112
	private static void displayAsHeldItem(final Villager body, final ItemStack itemStack) {
113
		body.setItemSlot(EquipmentSlot.MAINHAND, itemStack);
114
		body.setDropChance(EquipmentSlot.MAINHAND, 0.0F);
115
	}
116
117
	private LivingEntity lookAtTarget(final Villager myBody) {
118
		Brain<?> brain = myBody.getBrain();
119
		LivingEntity target = brain.getMemory(MemoryModuleType.INTERACTION_TARGET).get();
120
		brain.setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(target, true));
121
		return target;
122
	}
123
124
	private void displayCyclingItems(final Villager villager) {
125
		if (this.displayItems.size() >= 2 && ++this.cycleCounter >= 40) {
126
			this.displayIndex++;
127
			this.cycleCounter = 0;
128
			if (this.displayIndex > this.displayItems.size() - 1) {
129
				this.displayIndex = 0;
130
			}
131
132
			displayAsHeldItem(villager, this.displayItems.get(this.displayIndex));
133
		}
134
	}
135
}
136

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