Bat .java

net.minecraft.world.entity.ambient.Bat (class): members: BAT_RESTING_TARGETING, FLAP_LENGTH_SECONDS, checkBatSpawnRules, FLAG_RESTING, setResting, doPush.

net.minecraft.world.entity.ambientclass246 linescommonextends AmbientCreature7.7 KB

API Surface (21)

boolean isFlapping()
void defineSynchedData(final SynchedEntityData.Builder entityData)
float getSoundVolume()
float getVoicePitch()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
boolean isPushable()
void doPush(final Entity entity)
void pushEntities()
AttributeSupplier.Builder createAttributes()
boolean isResting()
void setResting(final boolean value)
void tick()
void customServerAiStep(final ServerLevel level)
Entity.MovementEmission getMovementEmission()
void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos)
boolean isIgnoringBlockTriggers()
boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage)
void readAdditionalSaveData(final ValueInput input)
void addAdditionalSaveData(final ValueOutput output)
boolean checkBatSpawnRules(final EntityType<Bat> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random)

Fields & Constants (10)

FLAP_LENGTH_SECONDS
TICKS_PER_FLAP
DATA_ID_FLAGS
FLAG_RESTING
BAT_RESTING_TARGETING
DEFAULT_FLAGS
flyAnimationState
restAnimationState
targetPosition
pos

String literals (1)

"BatFlags"

Full Source

Bat.java246 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ambient;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.network.syncher.EntityDataAccessor;
5
import net.minecraft.network.syncher.EntityDataSerializers;
6
import net.minecraft.network.syncher.SynchedEntityData;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.sounds.SoundEvent;
9
import net.minecraft.sounds.SoundEvents;
10
import net.minecraft.tags.BlockTags;
11
import net.minecraft.util.Mth;
12
import net.minecraft.util.RandomSource;
13
import net.minecraft.world.damagesource.DamageSource;
14
import net.minecraft.world.entity.AnimationState;
15
import net.minecraft.world.entity.Entity;
16
import net.minecraft.world.entity.EntitySpawnReason;
17
import net.minecraft.world.entity.EntityType;
18
import net.minecraft.world.entity.Mob;
19
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
20
import net.minecraft.world.entity.ai.attributes.Attributes;
21
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
22
import net.minecraft.world.level.Level;
23
import net.minecraft.world.level.LevelAccessor;
24
import net.minecraft.world.level.block.state.BlockState;
25
import net.minecraft.world.level.levelgen.Heightmap;
26
import net.minecraft.world.level.storage.ValueInput;
27
import net.minecraft.world.level.storage.ValueOutput;
28
import net.minecraft.world.phys.Vec3;
29
import org.jspecify.annotations.Nullable;
30
31
public class Bat extends AmbientCreature {
32
	public static final float FLAP_LENGTH_SECONDS = 0.5F;
33
	public static final float TICKS_PER_FLAP = 10.0F;
34
	private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(Bat.class, EntityDataSerializers.BYTE);
35
	private static final int FLAG_RESTING = 1;
36
	private static final TargetingConditions BAT_RESTING_TARGETING = TargetingConditions.forNonCombat().range(4.0);
37
	private static final byte DEFAULT_FLAGS = 0;
38
	public final AnimationState flyAnimationState = new AnimationState();
39
	public final AnimationState restAnimationState = new AnimationState();
40
	private @Nullable BlockPos targetPosition;
41
42
	public Bat(final EntityType<? extends Bat> type, final Level level) {
43
		super(type, level);
44
		if (!level.isClientSide()) {
45
			this.setResting(true);
46
		}
47
	}
48
49
	@Override
50
	public boolean isFlapping() {
51
		return !this.isResting() && this.tickCount % 10.0F == 0.0F;
52
	}
53
54
	@Override
55
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
56
		super.defineSynchedData(entityData);
57
		entityData.define(DATA_ID_FLAGS, (byte)0);
58
	}
59
60
	@Override
61
	protected float getSoundVolume() {
62
		return 0.1F;
63
	}
64
65
	@Override
66
	public float getVoicePitch() {
67
		return super.getVoicePitch() * 0.95F;
68
	}
69
70
	@Override
71
	public @Nullable SoundEvent getAmbientSound() {
72
		return this.isResting() && this.random.nextInt(4) != 0 ? null : SoundEvents.BAT_AMBIENT;
73
	}
74
75
	@Override
76
	protected SoundEvent getHurtSound(final DamageSource source) {
77
		return SoundEvents.BAT_HURT;
78
	}
79
80
	@Override
81
	protected SoundEvent getDeathSound() {
82
		return SoundEvents.BAT_DEATH;
83
	}
84
85
	@Override
86
	public boolean isPushable() {
87
		return false;
88
	}
89
90
	@Override
91
	protected void doPush(final Entity entity) {
92
	}
93
94
	@Override
95
	protected void pushEntities() {
96
	}
97
98
	public static AttributeSupplier.Builder createAttributes() {
99
		return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 6.0);
100
	}
101
102
	public boolean isResting() {
103
		return (this.entityData.get(DATA_ID_FLAGS) & 1) != 0;
104
	}
105
106
	public void setResting(final boolean value) {
107
		byte current = this.entityData.get(DATA_ID_FLAGS);
108
		if (value) {
109
			this.entityData.set(DATA_ID_FLAGS, (byte)(current | 1));
110
		} else {
111
			this.entityData.set(DATA_ID_FLAGS, (byte)(current & -2));
112
		}
113
	}
114
115
	@Override
116
	public void tick() {
117
		super.tick();
118
		if (this.isResting()) {
119
			this.setDeltaMovement(Vec3.ZERO);
120
			this.setPosRaw(this.getX(), Mth.floor(this.getY()) + 1.0 - this.getBbHeight(), this.getZ());
121
		} else {
122
			this.setDeltaMovement(this.getDeltaMovement().multiply(1.0, 0.6, 1.0));
123
		}
124
125
		this.setupAnimationStates();
126
	}
127
128
	@Override
129
	protected void customServerAiStep(final ServerLevel level) {
130
		super.customServerAiStep(level);
131
		BlockPos pos = this.blockPosition();
132
		BlockPos above = pos.above();
133
		if (this.isResting()) {
134
			boolean isSilent = this.isSilent();
135
			if (level.getBlockState(above).isRedstoneConductor(level, pos)) {
136
				if (this.random.nextInt(200) == 0) {
137
					this.yHeadRot = this.random.nextInt(360);
138
				}
139
140
				if (level.getNearestPlayer(BAT_RESTING_TARGETING, this) != null) {
141
					this.setResting(false);
142
					if (!isSilent) {
143
						level.levelEvent(null, 1025, pos, 0);
144
					}
145
				}
146
			} else {
147
				this.setResting(false);
148
				if (!isSilent) {
149
					level.levelEvent(null, 1025, pos, 0);
150
				}
151
			}
152
		} else {
153
			if (this.targetPosition != null && (!level.isEmptyBlock(this.targetPosition) || this.targetPosition.getY() <= level.getMinY())) {
154
				this.targetPosition = null;
155
			}
156
157
			if (this.targetPosition == null || this.random.nextInt(30) == 0 || this.targetPosition.closerToCenterThan(this.position(), 2.0)) {
158
				this.targetPosition = BlockPos.containing(
159
					this.getX() + this.random.nextInt(7) - this.random.nextInt(7),
160
					this.getY() + this.random.nextInt(6) - 2.0,
161
					this.getZ() + this.random.nextInt(7) - this.random.nextInt(7)
162
				);
163
			}
164
165
			double dx = this.targetPosition.getX() + 0.5 - this.getX();
166
			double dy = this.targetPosition.getY() + 0.1 - this.getY();
167
			double dz = this.targetPosition.getZ() + 0.5 - this.getZ();
168
			Vec3 movement = this.getDeltaMovement();
169
			Vec3 newMovement = movement.add(
170
				(Math.signum(dx) * 0.5 - movement.x) * 0.1F, (Math.signum(dy) * 0.7F - movement.y) * 0.1F, (Math.signum(dz) * 0.5 - movement.z) * 0.1F
171
			);
172
			this.setDeltaMovement(newMovement);
173
			float yRotD = (float)(Mth.atan2(newMovement.z, newMovement.x) * 180.0F / (float)Math.PI) - 90.0F;
174
			float rotDiff = Mth.wrapDegrees(yRotD - this.getYRot());
175
			this.zza = 0.5F;
176
			this.setYRot(this.getYRot() + rotDiff);
177
			if (this.random.nextInt(100) == 0 && level.getBlockState(above).isRedstoneConductor(level, above)) {
178
				this.setResting(true);
179
			}
180
		}
181
	}
182
183
	@Override
184
	protected Entity.MovementEmission getMovementEmission() {
185
		return Entity.MovementEmission.EVENTS;
186
	}
187
188
	@Override
189
	protected void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos) {
190
	}
191
192
	@Override
193
	public boolean isIgnoringBlockTriggers() {
194
		return true;
195
	}
196
197
	@Override
198
	public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
199
		if (this.isInvulnerableTo(level, source)) {
200
			return false;
201
		}
202
203
		if (this.isResting()) {
204
			this.setResting(false);
205
		}
206
207
		return super.hurtServer(level, source, damage);
208
	}
209
210
	@Override
211
	protected void readAdditionalSaveData(final ValueInput input) {
212
		super.readAdditionalSaveData(input);
213
		this.entityData.set(DATA_ID_FLAGS, input.getByteOr("BatFlags", (byte)0));
214
	}
215
216
	@Override
217
	protected void addAdditionalSaveData(final ValueOutput output) {
218
		super.addAdditionalSaveData(output);
219
		output.putByte("BatFlags", this.entityData.get(DATA_ID_FLAGS));
220
	}
221
222
	public static boolean checkBatSpawnRules(
223
		final EntityType<Bat> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
224
	) {
225
		if (pos.getY() >= level.getHeightmapPos(Heightmap.Types.WORLD_SURFACE, pos).getY()) {
226
			return false;
227
		} else if (random.nextBoolean()) {
228
			return false;
229
		} else if (level.getMaxLocalRawBrightness(pos) > random.nextInt(4)) {
230
			return false;
231
		} else {
232
			return !level.getBlockState(pos.below()).is(BlockTags.BATS_SPAWNABLE_ON) ? false : checkMobSpawnRules(type, level, spawnReason, pos, random);
233
		}
234
	}
235
236
	private void setupAnimationStates() {
237
		if (this.isResting()) {
238
			this.flyAnimationState.stop();
239
			this.restAnimationState.startIfStopped(this.tickCount);
240
		} else {
241
			this.restAnimationState.stop();
242
			this.flyAnimationState.startIfStopped(this.tickCount);
243
		}
244
	}
245
}
246

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