SculkShriekerBlockEntity .java

net.minecraft.world.level.block.entity.SculkShriekerBlockEntity (class, 224 lines): members: DEFAULT_WARNING_LEVEL, WARDEN_SPAWN_ATTEMPTS, SHRIEKING_TICKS.

net.minecraft.world.level.block.entityclass224 linescommonextends BlockEntity8.3 KB

Implements

API Surface (15)

VibrationSystem.Data getVibrationData()
VibrationSystem.User getVibrationUser()
void loadAdditional(final ValueInput input)
void saveAdditional(final ValueOutput output)
void tryShriek(final ServerLevel level, final @Nullable ServerPlayer player)
void preRemoveSideEffects(final BlockPos pos, final BlockState state)
void tryRespond(final ServerLevel level)
VibrationSystem.Listener getListener()
int getListenerRadius()
PositionSource getPositionSource()
TagKey<GameEvent> getListenableEvents()
boolean canReceiveVibration(final ServerLevel level, final BlockPos pos, final Holder<GameEvent> event, final GameEvent.Context context)
void onReceiveVibration(final ServerLevel level, final BlockPos pos, final Holder<GameEvent> event, final @Nullable Entity sourceEntity, final @Nullable Entity projectileOwner, final float receivingDistance)
void onDataChanged()
boolean requiresAdjacentChunksToBeTicking()

Fields & Constants (12)

WARNING_SOUND_RADIUS
WARDEN_SPAWN_ATTEMPTS
WARDEN_SPAWN_RANGE_XZ
WARDEN_SPAWN_RANGE_Y
DARKNESS_RADIUS
SHRIEKING_TICKS
SOUND_BY_LEVEL
DEFAULT_WARNING_LEVEL
warningLevel
vibrationUser
vibrationData
vibrationListener

String literals (2)

"warning_level"
"listener"

Full Source

SculkShriekerBlockEntity.java224 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
4
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
5
import java.util.OptionalInt;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Holder;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.server.level.ServerPlayer;
10
import net.minecraft.sounds.SoundEvent;
11
import net.minecraft.sounds.SoundEvents;
12
import net.minecraft.sounds.SoundSource;
13
import net.minecraft.tags.GameEventTags;
14
import net.minecraft.tags.TagKey;
15
import net.minecraft.util.Mth;
16
import net.minecraft.util.RandomSource;
17
import net.minecraft.util.SpawnUtil;
18
import net.minecraft.util.Util;
19
import net.minecraft.world.Difficulty;
20
import net.minecraft.world.entity.Entity;
21
import net.minecraft.world.entity.EntitySpawnReason;
22
import net.minecraft.world.entity.EntityTypes;
23
import net.minecraft.world.entity.item.ItemEntity;
24
import net.minecraft.world.entity.monster.warden.Warden;
25
import net.minecraft.world.entity.monster.warden.WardenSpawnTracker;
26
import net.minecraft.world.entity.projectile.Projectile;
27
import net.minecraft.world.level.Level;
28
import net.minecraft.world.level.block.SculkShriekerBlock;
29
import net.minecraft.world.level.block.state.BlockState;
30
import net.minecraft.world.level.gameevent.BlockPositionSource;
31
import net.minecraft.world.level.gameevent.GameEvent;
32
import net.minecraft.world.level.gameevent.GameEventListener;
33
import net.minecraft.world.level.gameevent.PositionSource;
34
import net.minecraft.world.level.gameevent.vibrations.VibrationSystem;
35
import net.minecraft.world.level.gamerules.GameRules;
36
import net.minecraft.world.level.storage.ValueInput;
37
import net.minecraft.world.level.storage.ValueOutput;
38
import net.minecraft.world.phys.Vec3;
39
import org.jspecify.annotations.Nullable;
40
41
public class SculkShriekerBlockEntity extends BlockEntity implements GameEventListener.Provider<VibrationSystem.Listener>, VibrationSystem {
42
	private static final int WARNING_SOUND_RADIUS = 10;
43
	private static final int WARDEN_SPAWN_ATTEMPTS = 20;
44
	private static final int WARDEN_SPAWN_RANGE_XZ = 5;
45
	private static final int WARDEN_SPAWN_RANGE_Y = 6;
46
	private static final int DARKNESS_RADIUS = 40;
47
	private static final int SHRIEKING_TICKS = 90;
48
	private static final Int2ObjectMap<SoundEvent> SOUND_BY_LEVEL = Util.make(new Int2ObjectOpenHashMap<>(), map -> {
49
		map.put(1, SoundEvents.WARDEN_NEARBY_CLOSE);
50
		map.put(2, SoundEvents.WARDEN_NEARBY_CLOSER);
51
		map.put(3, SoundEvents.WARDEN_NEARBY_CLOSEST);
52
		map.put(4, SoundEvents.WARDEN_LISTENING_ANGRY);
53
	});
54
	private static final int DEFAULT_WARNING_LEVEL = 0;
55
	private int warningLevel = 0;
56
	private final VibrationSystem.User vibrationUser = new SculkShriekerBlockEntity.VibrationUser();
57
	private VibrationSystem.Data vibrationData = new VibrationSystem.Data();
58
	private final VibrationSystem.Listener vibrationListener = new VibrationSystem.Listener(this);
59
60
	public SculkShriekerBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
61
		super(BlockEntityTypes.SCULK_SHRIEKER, worldPosition, blockState);
62
	}
63
64
	@Override
65
	public VibrationSystem.Data getVibrationData() {
66
		return this.vibrationData;
67
	}
68
69
	@Override
70
	public VibrationSystem.User getVibrationUser() {
71
		return this.vibrationUser;
72
	}
73
74
	@Override
75
	protected void loadAdditional(final ValueInput input) {
76
		super.loadAdditional(input);
77
		this.warningLevel = input.getIntOr("warning_level", 0);
78
		this.vibrationData = input.read("listener", VibrationSystem.Data.CODEC).orElseGet(VibrationSystem.Data::new);
79
	}
80
81
	@Override
82
	protected void saveAdditional(final ValueOutput output) {
83
		super.saveAdditional(output);
84
		output.putInt("warning_level", this.warningLevel);
85
		output.store("listener", VibrationSystem.Data.CODEC, this.vibrationData);
86
	}
87
88
	public static @Nullable ServerPlayer tryGetPlayer(final @Nullable Entity sourceEntity) {
89
		if (sourceEntity instanceof ServerPlayer player) {
90
			return player;
91
		} else if (sourceEntity != null && sourceEntity.getControllingPassenger() instanceof ServerPlayer player) {
92
			return player;
93
		} else if (sourceEntity instanceof Projectile projectile && projectile.getOwner() instanceof ServerPlayer player) {
94
			return player;
95
		} else {
96
			return sourceEntity instanceof ItemEntity item && item.getOwner() instanceof ServerPlayer player ? player : null;
97
		}
98
	}
99
100
	public void tryShriek(final ServerLevel level, final @Nullable ServerPlayer player) {
101
		if (player != null) {
102
			BlockState state = this.getBlockState();
103
			if (!state.getValue(SculkShriekerBlock.SHRIEKING)) {
104
				this.warningLevel = 0;
105
				if (!this.canRespond(level) || this.tryToWarn(level, player)) {
106
					this.shriek(level, player);
107
				}
108
			}
109
		}
110
	}
111
112
	private boolean tryToWarn(final ServerLevel level, final ServerPlayer player) {
113
		OptionalInt maybeWarningLevel = WardenSpawnTracker.tryWarn(level, this.getBlockPos(), player);
114
		maybeWarningLevel.ifPresent(warningLevel -> this.warningLevel = warningLevel);
115
		return maybeWarningLevel.isPresent();
116
	}
117
118
	private void shriek(final ServerLevel level, final @Nullable Entity sourceEntity) {
119
		BlockPos pos = this.getBlockPos();
120
		BlockState state = this.getBlockState();
121
		level.setBlock(pos, state.setValue(SculkShriekerBlock.SHRIEKING, true), 2);
122
		level.scheduleTick(pos, state.getBlock(), 90);
123
		level.levelEvent(3007, pos, 0);
124
		level.gameEvent(GameEvent.SHRIEK, pos, GameEvent.Context.of(sourceEntity));
125
	}
126
127
	private boolean canRespond(final ServerLevel level) {
128
		return this.getBlockState().getValue(SculkShriekerBlock.CAN_SUMMON)
129
			&& level.getDifficulty() != Difficulty.PEACEFUL
130
			&& level.getGameRules().get(GameRules.SPAWN_WARDENS);
131
	}
132
133
	@Override
134
	public void preRemoveSideEffects(final BlockPos pos, final BlockState state) {
135
		if (state.getValue(SculkShriekerBlock.SHRIEKING) && this.level instanceof ServerLevel serverLevel) {
136
			this.tryRespond(serverLevel);
137
		}
138
	}
139
140
	public void tryRespond(final ServerLevel level) {
141
		if (this.canRespond(level) && this.warningLevel > 0) {
142
			if (!this.trySummonWarden(level)) {
143
				this.playWardenReplySound(level);
144
			}
145
146
			Warden.applyDarknessAround(level, Vec3.atCenterOf(this.getBlockPos()), null, 40);
147
		}
148
	}
149
150
	private void playWardenReplySound(final Level level) {
151
		SoundEvent sound = SOUND_BY_LEVEL.get(this.warningLevel);
152
		if (sound != null) {
153
			BlockPos pos = this.getBlockPos();
154
			RandomSource random = level.getRandom();
155
			int x = pos.getX() + Mth.randomBetweenInclusive(random, -10, 10);
156
			int y = pos.getY() + Mth.randomBetweenInclusive(random, -10, 10);
157
			int z = pos.getZ() + Mth.randomBetweenInclusive(random, -10, 10);
158
			level.playSound(null, x, y, z, sound, SoundSource.HOSTILE, 5.0F, 1.0F);
159
		}
160
	}
161
162
	private boolean trySummonWarden(final ServerLevel level) {
163
		return this.warningLevel < 4
164
			? false
165
			: SpawnUtil.trySpawnMob(EntityTypes.WARDEN, EntitySpawnReason.TRIGGERED, level, this.getBlockPos(), 20, 5, 6, SpawnUtil.Strategy.ON_TOP_OF_COLLIDER, false)
166
				.isPresent();
167
	}
168
169
	public VibrationSystem.Listener getListener() {
170
		return this.vibrationListener;
171
	}
172
173
	private class VibrationUser implements VibrationSystem.User {
174
		private static final int LISTENER_RADIUS = 8;
175
		private final PositionSource positionSource = new BlockPositionSource(SculkShriekerBlockEntity.this.worldPosition);
176
177
		public VibrationUser() {
178
		}
179
180
		@Override
181
		public int getListenerRadius() {
182
			return 8;
183
		}
184
185
		@Override
186
		public PositionSource getPositionSource() {
187
			return this.positionSource;
188
		}
189
190
		@Override
191
		public TagKey<GameEvent> getListenableEvents() {
192
			return GameEventTags.SHRIEKER_CAN_LISTEN;
193
		}
194
195
		@Override
196
		public boolean canReceiveVibration(final ServerLevel level, final BlockPos pos, final Holder<GameEvent> event, final GameEvent.Context context) {
197
			return !SculkShriekerBlockEntity.this.getBlockState().getValue(SculkShriekerBlock.SHRIEKING)
198
				&& SculkShriekerBlockEntity.tryGetPlayer(context.sourceEntity()) != null;
199
		}
200
201
		@Override
202
		public void onReceiveVibration(
203
			final ServerLevel level,
204
			final BlockPos pos,
205
			final Holder<GameEvent> event,
206
			final @Nullable Entity sourceEntity,
207
			final @Nullable Entity projectileOwner,
208
			final float receivingDistance
209
		) {
210
			SculkShriekerBlockEntity.this.tryShriek(level, SculkShriekerBlockEntity.tryGetPlayer(projectileOwner != null ? projectileOwner : sourceEntity));
211
		}
212
213
		@Override
214
		public void onDataChanged() {
215
			SculkShriekerBlockEntity.this.setChanged();
216
		}
217
218
		@Override
219
		public boolean requiresAdjacentChunksToBeTicking() {
220
			return true;
221
		}
222
	}
223
}
224

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