TryLaySpawnOnFluidNearLand .java

net.minecraft.world.entity.ai.behavior.TryLaySpawnOnFluidNearLand (class, 51 lines): members: create. imports: BehaviorBuilder, FluidTags, GameEvent.

net.minecraft.world.entity.ai.behaviorclass51 linescommon2.1 KB

API Surface (1)

BehaviorControl<LivingEntity> create(final Block spawnBlock)

Full Source

TryLaySpawnOnFluidNearLand.java51 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.sounds.SoundEvents;
6
import net.minecraft.sounds.SoundSource;
7
import net.minecraft.tags.BlockTags;
8
import net.minecraft.tags.FluidTags;
9
import net.minecraft.world.entity.LivingEntity;
10
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
11
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
12
import net.minecraft.world.level.block.Block;
13
import net.minecraft.world.level.block.state.BlockState;
14
import net.minecraft.world.level.gameevent.GameEvent;
15
16
public class TryLaySpawnOnFluidNearLand {
17
	public static BehaviorControl<LivingEntity> create(final Block spawnBlock) {
18
		return BehaviorBuilder.create(
19
			i -> i.group(i.absent(MemoryModuleType.ATTACK_TARGET), i.present(MemoryModuleType.WALK_TARGET), i.present(MemoryModuleType.IS_PREGNANT))
20
				.apply(
21
					i,
22
					(attackTarget, walkTarget, pregnant) -> (level, body, timestamp) -> {
23
						if (!body.isInWater() && body.onGround()) {
24
							BlockPos belowPos = body.blockPosition().below();
25
26
							for (Direction direction : Direction.Plane.HORIZONTAL) {
27
								BlockPos relativePos = belowPos.relative(direction);
28
								if (level.getBlockState(relativePos).getCollisionShape(level, relativePos).getFaceShape(Direction.UP).isEmpty()
29
									&& (level.getFluidState(relativePos).is(FluidTags.SUPPORTS_FROGSPAWN) || level.getBlockState(relativePos).is(BlockTags.SUPPORTS_FROGSPAWN))) {
30
									BlockPos spawnPos = relativePos.above();
31
									if (level.getBlockState(spawnPos).isAir()) {
32
										BlockState newState = spawnBlock.defaultBlockState();
33
										level.setBlockAndUpdate(spawnPos, newState);
34
										level.gameEvent(GameEvent.BLOCK_PLACE, spawnPos, GameEvent.Context.of(body, newState));
35
										level.playSound(null, body, SoundEvents.FROG_LAY_SPAWN, SoundSource.BLOCKS, 1.0F, 1.0F);
36
										pregnant.erase();
37
										return true;
38
									}
39
								}
40
							}
41
42
							return true;
43
						} else {
44
							return false;
45
						}
46
					}
47
				)
48
		);
49
	}
50
}
51

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