ServerExplosion .java

net.minecraft.world.level.ServerExplosion (class): members: MAX_DROPS_PER_COMBINED_STACK, LARGE_EXPLOSION_RADIUS, StackCollector, getSeenPercent, isSmall.

net.minecraft.world.levelclass331 linescommon11.3 KB

Implements

API Surface (12)

float getSeenPercent(final Vec3 center, final Entity entity)
float radius()
Vec3 center()
int explode()
Map<Player, Vec3> getHitPlayers()
ServerLevel level()
DamageSource getDamageSource()
Explosion.BlockInteraction getBlockInteraction()
boolean canTriggerBlocks()
boolean shouldAffectBlocklikeEntities()
boolean isSmall()
void tryMerge(final ItemStack input)

Fields & Constants (12)

EXPLOSION_DAMAGE_CALCULATOR
MAX_DROPS_PER_COMBINED_STACK
LARGE_EXPLOSION_RADIUS
fire
blockInteraction
level
center
source
radius
damageSource
damageCalculator
hitPlayers

String literals (1)

"explosion_blocks"

Full Source

ServerExplosion.java331 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level;
2
3
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Optional;
10
import java.util.Set;
11
import net.minecraft.core.BlockPos;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.tags.EntityTypeTags;
14
import net.minecraft.util.Mth;
15
import net.minecraft.util.Util;
16
import net.minecraft.util.profiling.Profiler;
17
import net.minecraft.util.profiling.ProfilerFiller;
18
import net.minecraft.world.damagesource.DamageSource;
19
import net.minecraft.world.entity.Entity;
20
import net.minecraft.world.entity.EntityTypes;
21
import net.minecraft.world.entity.LivingEntity;
22
import net.minecraft.world.entity.ai.attributes.Attributes;
23
import net.minecraft.world.entity.item.ItemEntity;
24
import net.minecraft.world.entity.item.PrimedTnt;
25
import net.minecraft.world.entity.player.Player;
26
import net.minecraft.world.entity.projectile.Projectile;
27
import net.minecraft.world.item.ItemStack;
28
import net.minecraft.world.level.block.BaseFireBlock;
29
import net.minecraft.world.level.block.Block;
30
import net.minecraft.world.level.block.state.BlockState;
31
import net.minecraft.world.level.gameevent.GameEvent;
32
import net.minecraft.world.level.gamerules.GameRules;
33
import net.minecraft.world.level.material.FluidState;
34
import net.minecraft.world.phys.AABB;
35
import net.minecraft.world.phys.HitResult;
36
import net.minecraft.world.phys.Vec3;
37
import org.jspecify.annotations.Nullable;
38
39
public class ServerExplosion implements Explosion {
40
	private static final ExplosionDamageCalculator EXPLOSION_DAMAGE_CALCULATOR = new ExplosionDamageCalculator();
41
	private static final int MAX_DROPS_PER_COMBINED_STACK = 16;
42
	private static final float LARGE_EXPLOSION_RADIUS = 2.0F;
43
	private final boolean fire;
44
	private final Explosion.BlockInteraction blockInteraction;
45
	private final ServerLevel level;
46
	private final Vec3 center;
47
	private final @Nullable Entity source;
48
	private final float radius;
49
	private final DamageSource damageSource;
50
	private final ExplosionDamageCalculator damageCalculator;
51
	private final Map<Player, Vec3> hitPlayers = new HashMap<>();
52
53
	public ServerExplosion(
54
		final ServerLevel level,
55
		final @Nullable Entity source,
56
		final @Nullable DamageSource damageSource,
57
		final @Nullable ExplosionDamageCalculator damageCalculator,
58
		final Vec3 center,
59
		final float radius,
60
		final boolean fire,
61
		final Explosion.BlockInteraction blockInteraction
62
	) {
63
		this.level = level;
64
		this.source = source;
65
		this.radius = radius;
66
		this.center = center;
67
		this.fire = fire;
68
		this.blockInteraction = blockInteraction;
69
		this.damageSource = damageSource == null ? level.damageSources().explosion(this) : damageSource;
70
		this.damageCalculator = damageCalculator == null ? this.makeDamageCalculator(source) : damageCalculator;
71
	}
72
73
	private ExplosionDamageCalculator makeDamageCalculator(final @Nullable Entity source) {
74
		return source == null ? EXPLOSION_DAMAGE_CALCULATOR : new EntityBasedExplosionDamageCalculator(source);
75
	}
76
77
	public static float getSeenPercent(final Vec3 center, final Entity entity) {
78
		AABB bb = entity.getBoundingBox();
79
		double xs = 1.0 / ((bb.maxX - bb.minX) * 2.0 + 1.0);
80
		double ys = 1.0 / ((bb.maxY - bb.minY) * 2.0 + 1.0);
81
		double zs = 1.0 / ((bb.maxZ - bb.minZ) * 2.0 + 1.0);
82
		double xOffset = (1.0 - Math.floor(1.0 / xs) * xs) / 2.0;
83
		double zOffset = (1.0 - Math.floor(1.0 / zs) * zs) / 2.0;
84
		if (!(xs < 0.0) && !(ys < 0.0) && !(zs < 0.0)) {
85
			int hits = 0;
86
			int count = 0;
87
88
			for (double xx = 0.0; xx <= 1.0; xx += xs) {
89
				for (double yy = 0.0; yy <= 1.0; yy += ys) {
90
					for (double zz = 0.0; zz <= 1.0; zz += zs) {
91
						double x = Mth.lerp(xx, bb.minX, bb.maxX);
92
						double y = Mth.lerp(yy, bb.minY, bb.maxY);
93
						double z = Mth.lerp(zz, bb.minZ, bb.maxZ);
94
						Vec3 from = new Vec3(x + xOffset, y, z + zOffset);
95
						if (entity.level().clip(new ClipContext(from, center, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity)).getType() == HitResult.Type.MISS) {
96
							hits++;
97
						}
98
99
						count++;
100
					}
101
				}
102
			}
103
104
			return (float)hits / count;
105
		} else {
106
			return 0.0F;
107
		}
108
	}
109
110
	@Override
111
	public float radius() {
112
		return this.radius;
113
	}
114
115
	@Override
116
	public Vec3 center() {
117
		return this.center;
118
	}
119
120
	private List<BlockPos> calculateExplodedPositions() {
121
		Set<BlockPos> toBlowSet = new HashSet<>();
122
		int size = 16;
123
124
		for (int xx = 0; xx < 16; xx++) {
125
			for (int yy = 0; yy < 16; yy++) {
126
				for (int zz = 0; zz < 16; zz++) {
127
					if (xx == 0 || xx == 15 || yy == 0 || yy == 15 || zz == 0 || zz == 15) {
128
						double xd = xx / 15.0F * 2.0F - 1.0F;
129
						double yd = yy / 15.0F * 2.0F - 1.0F;
130
						double zd = zz / 15.0F * 2.0F - 1.0F;
131
						double d = Math.sqrt(xd * xd + yd * yd + zd * zd);
132
						xd /= d;
133
						yd /= d;
134
						zd /= d;
135
						float remainingPower = this.radius * (0.7F + this.level.random.nextFloat() * 0.6F);
136
						double xp = this.center.x;
137
						double yp = this.center.y;
138
						double zp = this.center.z;
139
						float stepSize = 0.3F;
140
141
						while (remainingPower > 0.0F) {
142
							BlockPos pos = BlockPos.containing(xp, yp, zp);
143
							BlockState block = this.level.getBlockState(pos);
144
							FluidState fluid = this.level.getFluidState(pos);
145
							if (!this.level.isInWorldBounds(pos)) {
146
								break;
147
							}
148
149
							Optional<Float> resistance = this.damageCalculator.getBlockExplosionResistance(this, this.level, pos, block, fluid);
150
							if (resistance.isPresent()) {
151
								remainingPower -= (resistance.get() + 0.3F) * 0.3F;
152
							}
153
154
							if (remainingPower > 0.0F && this.damageCalculator.shouldBlockExplode(this, this.level, pos, block, remainingPower)) {
155
								toBlowSet.add(pos);
156
							}
157
158
							xp += xd * 0.3F;
159
							yp += yd * 0.3F;
160
							zp += zd * 0.3F;
161
							remainingPower -= 0.22500001F;
162
						}
163
					}
164
				}
165
			}
166
		}
167
168
		return new ObjectArrayList<>(toBlowSet);
169
	}
170
171
	private void hurtEntities() {
172
		if (!(this.radius < 1.0E-5F)) {
173
			float doubleRadius = this.radius * 2.0F;
174
			int x0 = Mth.floor(this.center.x - doubleRadius - 1.0);
175
			int x1 = Mth.floor(this.center.x + doubleRadius + 1.0);
176
			int y0 = Mth.floor(this.center.y - doubleRadius - 1.0);
177
			int y1 = Mth.floor(this.center.y + doubleRadius + 1.0);
178
			int z0 = Mth.floor(this.center.z - doubleRadius - 1.0);
179
			int z1 = Mth.floor(this.center.z + doubleRadius + 1.0);
180
181
			for (Entity entity : this.level.getEntities(this.source, new AABB(x0, y0, z0, x1, y1, z1))) {
182
				if (!entity.ignoreExplosion(this)) {
183
					double dist = Math.sqrt(entity.distanceToSqr(this.center)) / doubleRadius;
184
					if (!(dist > 1.0)) {
185
						Vec3 entityOrigin = entity instanceof PrimedTnt ? entity.position() : entity.getEyePosition();
186
						Vec3 direction = entityOrigin.subtract(this.center).normalize();
187
						boolean shouldDamageEntity = this.damageCalculator.shouldDamageEntity(this, entity);
188
						float knockbackMultiplier = this.damageCalculator.getKnockbackMultiplier(entity);
189
						float exposure = !shouldDamageEntity && knockbackMultiplier == 0.0F ? 0.0F : getSeenPercent(this.center, entity);
190
						if (shouldDamageEntity && (this.source == null || this.source.doTeamsAllowDamage(entity))) {
191
							entity.hurtServer(this.level, this.damageSource, this.damageCalculator.getEntityDamageAmount(this, entity, exposure));
192
						}
193
194
						double knockbackResistance = entity instanceof LivingEntity livingEntity
195
							? livingEntity.getAttributeValue(Attributes.EXPLOSION_KNOCKBACK_RESISTANCE)
196
							: 0.0;
197
						double knockbackPower = (1.0 - dist) * exposure * knockbackMultiplier * (1.0 - knockbackResistance);
198
						Vec3 knockback = direction.scale(knockbackPower);
199
						entity.pushFromExplosion(knockback);
200
						if (entity.is(EntityTypeTags.REDIRECTABLE_PROJECTILE) && entity instanceof Projectile projectile) {
201
							projectile.setOwner(this.damageSource.getEntity());
202
						} else if (entity instanceof Player player && !player.isSpectator() && (!player.isCreative() || !player.getAbilities().flying)) {
203
							this.hitPlayers.put(player, knockback);
204
						}
205
206
						entity.onExplosionHit(this.source);
207
					}
208
				}
209
			}
210
		}
211
	}
212
213
	private void interactWithBlocks(final List<BlockPos> targetBlocks) {
214
		List<ServerExplosion.StackCollector> stacks = new ArrayList<>();
215
		Util.shuffle(targetBlocks, this.level.random);
216
217
		for (BlockPos pos : targetBlocks) {
218
			this.level.getBlockState(pos).onExplosionHit(this.level, pos, this, (stackx, position) -> addOrAppendStack(stacks, stackx, position));
219
		}
220
221
		for (ServerExplosion.StackCollector stack : stacks) {
222
			Block.popResource(this.level, stack.pos, stack.stack);
223
		}
224
	}
225
226
	private void createFire(final List<BlockPos> targetBlocks) {
227
		for (BlockPos pos : targetBlocks) {
228
			if (this.level.random.nextInt(3) == 0 && this.level.getBlockState(pos).isAir() && this.level.getBlockState(pos.below()).isSolidRender()) {
229
				this.level.setBlockAndUpdate(pos, BaseFireBlock.getState(this.level, pos));
230
			}
231
		}
232
	}
233
234
	public int explode() {
235
		this.level.gameEvent(this.source, GameEvent.EXPLODE, this.center);
236
		List<BlockPos> toBlow = this.calculateExplodedPositions();
237
		this.hurtEntities();
238
		if (this.interactsWithBlocks()) {
239
			ProfilerFiller profiler = Profiler.get();
240
			profiler.push("explosion_blocks");
241
			this.interactWithBlocks(toBlow);
242
			profiler.pop();
243
		}
244
245
		if (this.fire) {
246
			this.createFire(toBlow);
247
		}
248
249
		return toBlow.size();
250
	}
251
252
	private static void addOrAppendStack(final List<ServerExplosion.StackCollector> stacks, final ItemStack stack, final BlockPos pos) {
253
		for (ServerExplosion.StackCollector stackCollector : stacks) {
254
			stackCollector.tryMerge(stack);
255
			if (stack.isEmpty()) {
256
				return;
257
			}
258
		}
259
260
		stacks.add(new ServerExplosion.StackCollector(pos, stack));
261
	}
262
263
	private boolean interactsWithBlocks() {
264
		return this.blockInteraction != Explosion.BlockInteraction.KEEP;
265
	}
266
267
	public Map<Player, Vec3> getHitPlayers() {
268
		return this.hitPlayers;
269
	}
270
271
	@Override
272
	public ServerLevel level() {
273
		return this.level;
274
	}
275
276
	@Override
277
	public @Nullable LivingEntity getIndirectSourceEntity() {
278
		return Explosion.getIndirectSourceEntity(this.source);
279
	}
280
281
	@Override
282
	public @Nullable Entity getDirectSourceEntity() {
283
		return this.source;
284
	}
285
286
	public DamageSource getDamageSource() {
287
		return this.damageSource;
288
	}
289
290
	@Override
291
	public Explosion.BlockInteraction getBlockInteraction() {
292
		return this.blockInteraction;
293
	}
294
295
	@Override
296
	public boolean canTriggerBlocks() {
297
		if (this.blockInteraction != Explosion.BlockInteraction.TRIGGER_BLOCK) {
298
			return false;
299
		} else {
300
			return this.source != null && this.source.is(EntityTypes.BREEZE_WIND_CHARGE) ? this.level.getGameRules().get(GameRules.MOB_GRIEFING) : true;
301
		}
302
	}
303
304
	@Override
305
	public boolean shouldAffectBlocklikeEntities() {
306
		boolean mobGriefingEnabled = this.level.getGameRules().get(GameRules.MOB_GRIEFING);
307
		boolean isNotWindCharge = this.source == null || !this.source.is(EntityTypes.BREEZE_WIND_CHARGE) && !this.source.is(EntityTypes.WIND_CHARGE);
308
		return mobGriefingEnabled ? isNotWindCharge : this.blockInteraction.shouldAffectBlocklikeEntities() && isNotWindCharge;
309
	}
310
311
	public boolean isSmall() {
312
		return this.radius < 2.0F || !this.interactsWithBlocks();
313
	}
314
315
	private static class StackCollector {
316
		private final BlockPos pos;
317
		private ItemStack stack;
318
319
		private StackCollector(final BlockPos pos, final ItemStack stack) {
320
			this.pos = pos;
321
			this.stack = stack;
322
		}
323
324
		public void tryMerge(final ItemStack input) {
325
			if (ItemEntity.areMergable(this.stack, input)) {
326
				this.stack = ItemEntity.merge(this.stack, input, 16);
327
			}
328
		}
329
	}
330
}
331

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