GiveGiftToHero .java

net.minecraft.world.entity.ai.behavior.GiveGiftToHero (class, 138 lines): extends Behavior. members: TIME_TO_DELAY_FOR_HEAD_TO_FINISH_TURNING, GIFTS, stop.

net.minecraft.world.entity.ai.behaviorclass138 linescommonextends Behavior<Villager>5.7 KB

API Surface (5)

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

Fields & Constants (9)

THROW_GIFT_AT_DISTANCE
MIN_TIME_BETWEEN_GIFTS
MAX_TIME_BETWEEN_GIFTS
TIME_TO_DELAY_FOR_HEAD_TO_FINISH_TURNING
GIFTS
SPEED_MODIFIER
timeUntilNextGift
giftGivenDuringThisRun
timeSinceStart

Full Source

GiveGiftToHero.java138 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import com.google.common.collect.ImmutableMap;
4
import java.util.Map;
5
import java.util.Optional;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.resources.ResourceKey;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.world.effect.MobEffects;
10
import net.minecraft.world.entity.LivingEntity;
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.entity.npc.villager.VillagerProfession;
15
import net.minecraft.world.entity.player.Player;
16
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
17
import net.minecraft.world.level.storage.loot.LootTable;
18
19
public class GiveGiftToHero extends Behavior<Villager> {
20
	private static final int THROW_GIFT_AT_DISTANCE = 5;
21
	private static final int MIN_TIME_BETWEEN_GIFTS = 600;
22
	private static final int MAX_TIME_BETWEEN_GIFTS = 6600;
23
	private static final int TIME_TO_DELAY_FOR_HEAD_TO_FINISH_TURNING = 20;
24
	private static final Map<ResourceKey<VillagerProfession>, ResourceKey<LootTable>> GIFTS = ImmutableMap.<ResourceKey<VillagerProfession>, ResourceKey<LootTable>>builder()
25
		.put(VillagerProfession.ARMORER, BuiltInLootTables.ARMORER_GIFT)
26
		.put(VillagerProfession.BUTCHER, BuiltInLootTables.BUTCHER_GIFT)
27
		.put(VillagerProfession.CARTOGRAPHER, BuiltInLootTables.CARTOGRAPHER_GIFT)
28
		.put(VillagerProfession.CLERIC, BuiltInLootTables.CLERIC_GIFT)
29
		.put(VillagerProfession.FARMER, BuiltInLootTables.FARMER_GIFT)
30
		.put(VillagerProfession.FISHERMAN, BuiltInLootTables.FISHERMAN_GIFT)
31
		.put(VillagerProfession.FLETCHER, BuiltInLootTables.FLETCHER_GIFT)
32
		.put(VillagerProfession.LEATHERWORKER, BuiltInLootTables.LEATHERWORKER_GIFT)
33
		.put(VillagerProfession.LIBRARIAN, BuiltInLootTables.LIBRARIAN_GIFT)
34
		.put(VillagerProfession.MASON, BuiltInLootTables.MASON_GIFT)
35
		.put(VillagerProfession.SHEPHERD, BuiltInLootTables.SHEPHERD_GIFT)
36
		.put(VillagerProfession.TOOLSMITH, BuiltInLootTables.TOOLSMITH_GIFT)
37
		.put(VillagerProfession.WEAPONSMITH, BuiltInLootTables.WEAPONSMITH_GIFT)
38
		.build();
39
	private static final float SPEED_MODIFIER = 0.5F;
40
	private int timeUntilNextGift = 600;
41
	private boolean giftGivenDuringThisRun;
42
	private long timeSinceStart;
43
44
	public GiveGiftToHero(final int timeout) {
45
		super(
46
			ImmutableMap.of(
47
				MemoryModuleType.WALK_TARGET,
48
				MemoryStatus.REGISTERED,
49
				MemoryModuleType.LOOK_TARGET,
50
				MemoryStatus.REGISTERED,
51
				MemoryModuleType.INTERACTION_TARGET,
52
				MemoryStatus.REGISTERED,
53
				MemoryModuleType.NEAREST_VISIBLE_PLAYER,
54
				MemoryStatus.VALUE_PRESENT
55
			),
56
			timeout
57
		);
58
	}
59
60
	protected boolean checkExtraStartConditions(final ServerLevel level, final Villager body) {
61
		if (!this.isHeroVisible(body)) {
62
			return false;
63
		} else if (this.timeUntilNextGift > 0) {
64
			this.timeUntilNextGift--;
65
			return false;
66
		} else {
67
			return true;
68
		}
69
	}
70
71
	protected void start(final ServerLevel level, final Villager body, final long timestamp) {
72
		this.giftGivenDuringThisRun = false;
73
		this.timeSinceStart = timestamp;
74
		Player player = this.getNearestTargetableHero(body).get();
75
		body.getBrain().setMemory(MemoryModuleType.INTERACTION_TARGET, player);
76
		BehaviorUtils.lookAtEntity(body, player);
77
	}
78
79
	protected boolean canStillUse(final ServerLevel level, final Villager body, final long timestamp) {
80
		return this.isHeroVisible(body) && !this.giftGivenDuringThisRun;
81
	}
82
83
	protected void tick(final ServerLevel level, final Villager villager, final long timestamp) {
84
		Player player = this.getNearestTargetableHero(villager).get();
85
		BehaviorUtils.lookAtEntity(villager, player);
86
		if (this.isWithinThrowingDistance(villager, player)) {
87
			if (timestamp - this.timeSinceStart > 20L) {
88
				this.throwGift(level, villager, player);
89
				this.giftGivenDuringThisRun = true;
90
			}
91
		} else {
92
			BehaviorUtils.setWalkAndLookTargetMemories(villager, player, 0.5F, 5);
93
		}
94
	}
95
96
	protected void stop(final ServerLevel level, final Villager body, final long timestamp) {
97
		this.timeUntilNextGift = calculateTimeUntilNextGift(level);
98
		body.getBrain().eraseMemory(MemoryModuleType.INTERACTION_TARGET);
99
		body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
100
		body.getBrain().eraseMemory(MemoryModuleType.LOOK_TARGET);
101
	}
102
103
	private void throwGift(final ServerLevel level, final Villager villager, final LivingEntity target) {
104
		villager.dropFromGiftLootTable(level, getLootTableToThrow(villager), (l, itemStack) -> BehaviorUtils.throwItem(villager, itemStack, target.position()));
105
	}
106
107
	private static ResourceKey<LootTable> getLootTableToThrow(final Villager villager) {
108
		if (villager.isBaby()) {
109
			return BuiltInLootTables.BABY_VILLAGER_GIFT;
110
		}
111
112
		Optional<ResourceKey<VillagerProfession>> profession = villager.getVillagerData().profession().unwrapKey();
113
		return profession.isEmpty() ? BuiltInLootTables.UNEMPLOYED_GIFT : GIFTS.getOrDefault(profession.get(), BuiltInLootTables.UNEMPLOYED_GIFT);
114
	}
115
116
	private boolean isHeroVisible(final Villager body) {
117
		return this.getNearestTargetableHero(body).isPresent();
118
	}
119
120
	private Optional<Player> getNearestTargetableHero(final Villager body) {
121
		return body.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_PLAYER).filter(this::isHero);
122
	}
123
124
	private boolean isHero(final Player player) {
125
		return player.hasEffect(MobEffects.HERO_OF_THE_VILLAGE);
126
	}
127
128
	private boolean isWithinThrowingDistance(final Villager villager, final Player player) {
129
		BlockPos playerPos = player.blockPosition();
130
		BlockPos villagerPos = villager.blockPosition();
131
		return villagerPos.closerThan(playerPos, 5.0);
132
	}
133
134
	private static int calculateTimeUntilNextGift(final ServerLevel level) {
135
		return 600 + level.getRandom().nextInt(6001);
136
	}
137
}
138

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