BellBlockEntity .java

net.minecraft.world.level.block.entity.BellBlockEntity (class, 170 lines): members: MIN_TICKS_BETWEEN_SEARCHES, HIGHLIGHT_RAIDERS_RADIUS, HEAR_BELL_RADIUS.

net.minecraft.world.level.block.entityclass170 linescommonextends BlockEntity6.2 KB

API Surface (4)

boolean triggerEvent(final int b0, final int b1)
void clientTick(final Level level, final BlockPos pos, final BlockState state, final BellBlockEntity entity)
void serverTick(final Level level, final BlockPos pos, final BlockState state, final BellBlockEntity entity)
void onHit(final Direction clickDirection)

Fields & Constants (16)

DURATION
GLOW_DURATION
MIN_TICKS_BETWEEN_SEARCHES
MAX_RESONATION_TICKS
TICKS_BEFORE_RESONATION
SEARCH_RADIUS
HEAR_BELL_RADIUS
HIGHLIGHT_RAIDERS_RADIUS
lastRingTimestamp
ticks
shaking
clickDirection
nearbyEntities
resonating
resonationTicks
entity

Full Source

BellBlockEntity.java170 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import java.util.List;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.core.particles.ColorParticleOption;
7
import net.minecraft.core.particles.ParticleTypes;
8
import net.minecraft.sounds.SoundEvents;
9
import net.minecraft.sounds.SoundSource;
10
import net.minecraft.tags.EntityTypeTags;
11
import net.minecraft.util.Mth;
12
import net.minecraft.world.effect.MobEffectInstance;
13
import net.minecraft.world.effect.MobEffects;
14
import net.minecraft.world.entity.LivingEntity;
15
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
16
import net.minecraft.world.level.Level;
17
import net.minecraft.world.level.block.state.BlockState;
18
import net.minecraft.world.phys.AABB;
19
import org.apache.commons.lang3.mutable.MutableInt;
20
21
public class BellBlockEntity extends BlockEntity {
22
	private static final int DURATION = 50;
23
	private static final int GLOW_DURATION = 60;
24
	private static final int MIN_TICKS_BETWEEN_SEARCHES = 60;
25
	private static final int MAX_RESONATION_TICKS = 40;
26
	private static final int TICKS_BEFORE_RESONATION = 5;
27
	private static final int SEARCH_RADIUS = 48;
28
	private static final int HEAR_BELL_RADIUS = 32;
29
	private static final int HIGHLIGHT_RAIDERS_RADIUS = 48;
30
	private long lastRingTimestamp;
31
	public int ticks;
32
	public boolean shaking;
33
	public Direction clickDirection;
34
	private List<LivingEntity> nearbyEntities;
35
	private boolean resonating;
36
	private int resonationTicks;
37
38
	public BellBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
39
		super(BlockEntityTypes.BELL, worldPosition, blockState);
40
	}
41
42
	@Override
43
	public boolean triggerEvent(final int b0, final int b1) {
44
		if (b0 == 1) {
45
			this.updateEntities();
46
			this.resonationTicks = 0;
47
			this.clickDirection = Direction.from3DDataValue(b1);
48
			this.ticks = 0;
49
			this.shaking = true;
50
			return true;
51
		} else {
52
			return super.triggerEvent(b0, b1);
53
		}
54
	}
55
56
	private static void tick(
57
		final Level level, final BlockPos pos, final BlockState state, final BellBlockEntity entity, final BellBlockEntity.ResonationEndAction onResonationEnd
58
	) {
59
		if (entity.shaking) {
60
			entity.ticks++;
61
		}
62
63
		if (entity.ticks >= 50) {
64
			entity.shaking = false;
65
			entity.ticks = 0;
66
		}
67
68
		if (entity.ticks >= 5 && entity.resonationTicks == 0 && areRaidersNearby(pos, entity.nearbyEntities)) {
69
			entity.resonating = true;
70
			level.playSound(null, pos, SoundEvents.BELL_RESONATE, SoundSource.BLOCKS, 1.0F, 1.0F);
71
		}
72
73
		if (entity.resonating) {
74
			if (entity.resonationTicks < 40) {
75
				entity.resonationTicks++;
76
			} else {
77
				onResonationEnd.run(level, pos, entity.nearbyEntities);
78
				entity.resonating = false;
79
			}
80
		}
81
	}
82
83
	public static void clientTick(final Level level, final BlockPos pos, final BlockState state, final BellBlockEntity entity) {
84
		tick(level, pos, state, entity, BellBlockEntity::showBellParticles);
85
	}
86
87
	public static void serverTick(final Level level, final BlockPos pos, final BlockState state, final BellBlockEntity entity) {
88
		tick(level, pos, state, entity, BellBlockEntity::makeRaidersGlow);
89
	}
90
91
	public void onHit(final Direction clickDirection) {
92
		BlockPos bellPos = this.getBlockPos();
93
		this.clickDirection = clickDirection;
94
		if (this.shaking) {
95
			this.ticks = 0;
96
		} else {
97
			this.shaking = true;
98
		}
99
100
		this.level.blockEvent(bellPos, this.getBlockState().getBlock(), 1, clickDirection.get3DDataValue());
101
	}
102
103
	private void updateEntities() {
104
		BlockPos blockPos = this.getBlockPos();
105
		if (this.level.getGameTime() > this.lastRingTimestamp + 60L || this.nearbyEntities == null) {
106
			this.lastRingTimestamp = this.level.getGameTime();
107
			AABB aabb = new AABB(blockPos).inflate(48.0);
108
			this.nearbyEntities = this.level.getEntitiesOfClass(LivingEntity.class, aabb);
109
		}
110
111
		if (!this.level.isClientSide()) {
112
			for (LivingEntity entity : this.nearbyEntities) {
113
				if (entity.isAlive() && !entity.isRemoved() && blockPos.closerToCenterThan(entity.position(), 32.0)) {
114
					entity.getBrain().setMemory(MemoryModuleType.HEARD_BELL_TIME, this.level.getGameTime());
115
				}
116
			}
117
		}
118
	}
119
120
	private static boolean areRaidersNearby(final BlockPos bellPos, final List<LivingEntity> nearbyEntities) {
121
		for (LivingEntity entity : nearbyEntities) {
122
			if (entity.isAlive() && !entity.isRemoved() && bellPos.closerToCenterThan(entity.position(), 32.0) && entity.is(EntityTypeTags.RAIDERS)) {
123
				return true;
124
			}
125
		}
126
127
		return false;
128
	}
129
130
	private static void makeRaidersGlow(final Level level, final BlockPos blockPos, final List<LivingEntity> nearbyEntities) {
131
		nearbyEntities.stream().filter(e -> isRaiderWithinRange(blockPos, e)).forEach(BellBlockEntity::glow);
132
	}
133
134
	private static void showBellParticles(final Level level, final BlockPos bellPos, final List<LivingEntity> nearbyEntities) {
135
		MutableInt particleColor = new MutableInt(16700985);
136
		int nearbyRaiderCount = (int)nearbyEntities.stream().filter(p -> bellPos.closerToCenterThan(p.position(), 48.0)).count();
137
		nearbyEntities.stream()
138
			.filter(e -> isRaiderWithinRange(bellPos, e))
139
			.forEach(
140
				entity -> {
141
					float distAway = 1.0F;
142
					double distBtwn = Math.sqrt(
143
						(entity.getX() - bellPos.getX()) * (entity.getX() - bellPos.getX()) + (entity.getZ() - bellPos.getZ()) * (entity.getZ() - bellPos.getZ())
144
					);
145
					double x3 = bellPos.getX() + 0.5F + 1.0 / distBtwn * (entity.getX() - bellPos.getX());
146
					double z3 = bellPos.getZ() + 0.5F + 1.0 / distBtwn * (entity.getZ() - bellPos.getZ());
147
					int particleCount = Mth.clamp((nearbyRaiderCount - 21) / -2, 3, 15);
148
149
					for (int i = 0; i < particleCount; i++) {
150
						int color = particleColor.addAndGet(5);
151
						level.addParticle(ColorParticleOption.create(ParticleTypes.ENTITY_EFFECT, color), x3, bellPos.getY() + 0.5F, z3, 0.0, 0.0, 0.0);
152
					}
153
				}
154
			);
155
	}
156
157
	private static boolean isRaiderWithinRange(final BlockPos blockPos, final LivingEntity entity) {
158
		return entity.isAlive() && !entity.isRemoved() && blockPos.closerToCenterThan(entity.position(), 48.0) && entity.is(EntityTypeTags.RAIDERS);
159
	}
160
161
	private static void glow(final LivingEntity raider) {
162
		raider.addEffect(new MobEffectInstance(MobEffects.GLOWING, 60));
163
	}
164
165
	@FunctionalInterface
166
	private interface ResonationEndAction {
167
		void run(final Level level, final BlockPos pos, List<LivingEntity> nearbyEntities);
168
	}
169
}
170

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