EntityFluidInteraction .java

net.minecraft.world.entity.EntityFluidInteraction (class): members: currentAccumulators, CurrentAccumulator, applyCurrentTo, getFluidHeight, fluidTrackers.

net.minecraft.world.entityclass251 linescommon8.0 KB

API Surface (9)

boolean update(final Entity entity, final boolean ignoreCurrent)
void applyCurrentTo(final TagKey<Fluid> fluid, final Entity entity, final double scale)
double getFluidHeight(final TagKey<Fluid> fluid)
boolean isInFluid(final TagKey<Fluid> fluid)
boolean isEyeInFluid(final TagKey<Fluid> fluid)
void reset()
void accumulate(final Vec3 flow)
void applyTo(final Entity entity, final double scale)
boolean reset()

Fields & Constants (2)

fluidTrackers
currentAccumulators

Full Source

EntityFluidInteraction.java251 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity;
2
3
import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap;
4
import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
5
import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
6
import it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry;
7
import java.util.ArrayList;
8
import java.util.List;
9
import java.util.Set;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.Holder;
12
import net.minecraft.core.SectionPos;
13
import net.minecraft.tags.TagKey;
14
import net.minecraft.util.Mth;
15
import net.minecraft.world.entity.player.Player;
16
import net.minecraft.world.level.BlockGetter;
17
import net.minecraft.world.level.Level;
18
import net.minecraft.world.level.chunk.ChunkAccess;
19
import net.minecraft.world.level.chunk.LevelChunkSection;
20
import net.minecraft.world.level.chunk.status.ChunkStatus;
21
import net.minecraft.world.level.material.Fluid;
22
import net.minecraft.world.level.material.FluidState;
23
import net.minecraft.world.phys.AABB;
24
import net.minecraft.world.phys.Vec3;
25
import org.jspecify.annotations.Nullable;
26
27
public class EntityFluidInteraction {
28
	private final List<EntityFluidInteraction.Tracker> fluidTrackers = new ArrayList<>();
29
	private final Reference2ObjectMap<TagKey<Fluid>, EntityFluidInteraction.CurrentAccumulator> currentAccumulators = new Reference2ObjectArrayMap<>();
30
31
	public EntityFluidInteraction(final Set<TagKey<Fluid>> fluidsWithCurrent) {
32
		for (TagKey<Fluid> fluid : fluidsWithCurrent) {
33
			this.currentAccumulators.put(fluid, new EntityFluidInteraction.CurrentAccumulator());
34
		}
35
	}
36
37
	public boolean update(final Entity entity, final boolean ignoreCurrent) {
38
		this.fluidTrackers.removeIf(EntityFluidInteraction.Tracker::reset);
39
		this.currentAccumulators.values().forEach(EntityFluidInteraction.CurrentAccumulator::reset);
40
		AABB box = entity.getFluidInteractionBox();
41
		if (box == null) {
42
			return false;
43
		}
44
45
		int x0 = Mth.floor(box.minX);
46
		int y0 = Mth.floor(box.minY);
47
		int z0 = Mth.floor(box.minZ);
48
		int x1 = Mth.ceil(box.maxX) - 1;
49
		int y1 = Mth.ceil(box.maxY) - 1;
50
		int z1 = Mth.ceil(box.maxZ) - 1;
51
		if (!hasFluidAndLoaded(entity.level(), x0 - 1, y0, z0 - 1, x1 + 1, y1, z1 + 1)) {
52
			return false;
53
		}
54
55
		double entityY = entity.getBoundingBox().minY;
56
		int eyeBlockX = entity.getBlockX();
57
		double eyeY = entity.getEyeY();
58
		int eyeBlockZ = entity.getBlockZ();
59
		Holder<Fluid> lastFluidType = null;
60
		EntityFluidInteraction.Tracker tracker = null;
61
		EntityFluidInteraction.CurrentAccumulator current = null;
62
		BlockGetter level = entity.level();
63
		BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
64
65
		for (int x = x0; x <= x1; x++) {
66
			for (int y = y0; y <= y1; y++) {
67
				for (int z = z0; z <= z1; z++) {
68
					mutablePos.set(x, y, z);
69
					FluidState fluidState = level.getFluidState(mutablePos);
70
					if (!fluidState.isEmpty()) {
71
						double fluidBottom = mutablePos.getY();
72
						double fluidTop = fluidBottom + fluidState.getHeight(level, mutablePos);
73
						if (!(fluidTop < box.minY)) {
74
							Holder<Fluid> fluidType = fluidState.typeHolder();
75
							if (fluidType != lastFluidType) {
76
								lastFluidType = fluidType;
77
								tracker = this.getOrCreateTrackerFor(fluidType);
78
								if (!ignoreCurrent) {
79
									current = this.getCurrentAccumulatorFor(fluidType);
80
								}
81
							}
82
83
							if (x == eyeBlockX && z == eyeBlockZ && eyeY >= fluidBottom) {
84
								double fluidTopForCamera = fluidBottom + fluidState.getHeightForCamera(level, mutablePos);
85
								if (eyeY <= fluidTopForCamera) {
86
									tracker.eyesInside = true;
87
								}
88
							}
89
90
							tracker.height = Math.max(fluidTop - entityY, tracker.height);
91
							if (current != null) {
92
								Vec3 flow = fluidState.getFlow(level, mutablePos);
93
								current.height = Math.max(tracker.height, current.height);
94
								if (current.height < 0.4) {
95
									flow = flow.scale(current.height);
96
								}
97
98
								current.accumulate(flow);
99
							}
100
						}
101
					}
102
				}
103
			}
104
		}
105
106
		return lastFluidType != null;
107
	}
108
109
	private static boolean hasFluidAndLoaded(final Level level, final int x0, final int y0, final int z0, final int x1, final int y1, final int z1) {
110
		int sectionX0 = SectionPos.blockToSectionCoord(x0);
111
		int sectionY0 = SectionPos.blockToSectionCoord(y0);
112
		int sectionZ0 = SectionPos.blockToSectionCoord(z0);
113
		int sectionX1 = SectionPos.blockToSectionCoord(x1);
114
		int sectionY1 = SectionPos.blockToSectionCoord(y1);
115
		int sectionZ1 = SectionPos.blockToSectionCoord(z1);
116
		boolean hasFluid = false;
117
118
		for (int chunkZ = sectionZ0; chunkZ <= sectionZ1; chunkZ++) {
119
			for (int chunkX = sectionX0; chunkX <= sectionX1; chunkX++) {
120
				ChunkAccess chunk = level.getChunk(chunkX, chunkZ, ChunkStatus.FULL, false);
121
				if (chunk == null) {
122
					return false;
123
				}
124
125
				LevelChunkSection[] sections = chunk.getSections();
126
127
				for (int sectionY = sectionY0; sectionY <= sectionY1; sectionY++) {
128
					int sectionIndex = chunk.getSectionIndexFromSectionY(sectionY);
129
					if (sectionIndex >= 0 && sectionIndex < sections.length) {
130
						hasFluid |= sections[sectionIndex].hasFluid();
131
					}
132
				}
133
			}
134
		}
135
136
		return hasFluid;
137
	}
138
139
	private EntityFluidInteraction.Tracker getOrCreateTrackerFor(final Holder<Fluid> fluidType) {
140
		for (EntityFluidInteraction.Tracker tracker : this.fluidTrackers) {
141
			if (tracker.fluidType.equals(fluidType)) {
142
				return tracker;
143
			}
144
		}
145
146
		EntityFluidInteraction.Tracker tracker = new EntityFluidInteraction.Tracker(fluidType);
147
		this.fluidTrackers.add(tracker);
148
		return tracker;
149
	}
150
151
	private EntityFluidInteraction.@Nullable CurrentAccumulator getCurrentAccumulatorFor(final Holder<Fluid> fluid) {
152
		for (Entry<TagKey<Fluid>, EntityFluidInteraction.CurrentAccumulator> entry : Reference2ObjectMaps.fastIterable(this.currentAccumulators)) {
153
			if (fluid.is(entry.getKey())) {
154
				return entry.getValue();
155
			}
156
		}
157
158
		return null;
159
	}
160
161
	public void applyCurrentTo(final TagKey<Fluid> fluid, final Entity entity, final double scale) {
162
		EntityFluidInteraction.CurrentAccumulator current = this.currentAccumulators.get(fluid);
163
		if (current != null) {
164
			current.applyTo(entity, scale);
165
		}
166
	}
167
168
	public double getFluidHeight(final TagKey<Fluid> fluid) {
169
		double height = 0.0;
170
171
		for (EntityFluidInteraction.Tracker tracker : this.fluidTrackers) {
172
			if (tracker.height > height && tracker.fluidType.is(fluid)) {
173
				height = tracker.height;
174
			}
175
		}
176
177
		return height;
178
	}
179
180
	public boolean isInFluid(final TagKey<Fluid> fluid) {
181
		return this.getFluidHeight(fluid) > 0.0;
182
	}
183
184
	public boolean isEyeInFluid(final TagKey<Fluid> fluid) {
185
		for (EntityFluidInteraction.Tracker tracker : this.fluidTrackers) {
186
			if (tracker.eyesInside && tracker.fluidType.is(fluid)) {
187
				return true;
188
			}
189
		}
190
191
		return false;
192
	}
193
194
	private static class CurrentAccumulator {
195
		private double height;
196
		private Vec3 accumulatedCurrent = Vec3.ZERO;
197
		private int currentCount;
198
199
		public void reset() {
200
			this.height = 0.0;
201
			this.accumulatedCurrent = Vec3.ZERO;
202
			this.currentCount = 0;
203
		}
204
205
		public void accumulate(final Vec3 flow) {
206
			this.accumulatedCurrent = this.accumulatedCurrent.add(flow);
207
			this.currentCount++;
208
		}
209
210
		public void applyTo(final Entity entity, final double scale) {
211
			if (this.currentCount != 0 && !(this.accumulatedCurrent.lengthSqr() < 1.0E-5F)) {
212
				Vec3 impulse;
213
				if (!(entity instanceof Player)) {
214
					impulse = this.accumulatedCurrent.normalize();
215
				} else {
216
					impulse = this.accumulatedCurrent.scale(1.0 / this.currentCount);
217
				}
218
219
				Vec3 oldMovement = entity.getDeltaMovement();
220
				impulse = impulse.scale(scale);
221
				double min = 0.003;
222
				if (Math.abs(oldMovement.x) < 0.003 && Math.abs(oldMovement.z) < 0.003 && impulse.length() < 0.0045000000000000005) {
223
					impulse = impulse.normalize().scale(0.0045000000000000005);
224
				}
225
226
				entity.addDeltaMovement(impulse);
227
			}
228
		}
229
	}
230
231
	private static class Tracker {
232
		private final Holder<Fluid> fluidType;
233
		private double height;
234
		private boolean eyesInside;
235
236
		public Tracker(final Holder<Fluid> fluidType) {
237
			this.fluidType = fluidType;
238
		}
239
240
		public boolean reset() {
241
			if (this.height == 0.0 && !this.eyesInside) {
242
				return true;
243
			}
244
245
			this.height = 0.0;
246
			this.eyesInside = false;
247
			return false;
248
		}
249
	}
250
}
251

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