mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-24 05:54:41 +00:00
180 lines
8.2 KiB
Diff
180 lines
8.2 KiB
Diff
--- a/net/minecraft/world/entity/EntityFluidInteraction.java
|
|
+++ b/net/minecraft/world/entity/EntityFluidInteraction.java
|
|
@@ -21,16 +_,39 @@
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
public class EntityFluidInteraction {
|
|
+ private static final Map<TagKey<Fluid>, java.util.function.Supplier<? extends EntityFluidInteraction.Tracker>> MODDED_TYPES = new java.util.HashMap<>();
|
|
+ public static void register(final TagKey<Fluid> fluid) {
|
|
+ register(fluid, EntityFluidInteraction.Tracker::new);
|
|
+ }
|
|
+ public static void register(final TagKey<Fluid> fluid, final java.util.function.Supplier<? extends EntityFluidInteraction.Tracker> factory) {
|
|
+ register(fluid, factory);
|
|
+ }
|
|
+
|
|
private final Map<TagKey<Fluid>, EntityFluidInteraction.Tracker> trackerByFluid = new Reference2ObjectArrayMap<>();
|
|
+ private final Map<net.minecraftforge.fluids.FluidType, EntityFluidInteraction.Tracker> trackerByFluidType = new Reference2ObjectArrayMap();
|
|
+ private boolean isInFluid = false;
|
|
+ private net.minecraftforge.fluids.FluidType eyeFluid = net.minecraftforge.common.ForgeMod.EMPTY_TYPE.get();
|
|
|
|
public EntityFluidInteraction(final Set<TagKey<Fluid>> fluids) {
|
|
for (TagKey<Fluid> fluid : fluids) {
|
|
this.trackerByFluid.put(fluid, new EntityFluidInteraction.Tracker());
|
|
}
|
|
+ for (var entry : MODDED_TYPES.entrySet()) {
|
|
+ if (!fluids.contains(entry.getKey()))
|
|
+ this.trackerByFluid.put(entry.getKey(), entry.getValue().get());
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public EntityFluidInteraction.@Nullable Tracker getTracker(final TagKey<Fluid> fluid) {
|
|
+ return this.trackerByFluid.get(fluid);
|
|
}
|
|
|
|
public void update(final Entity entity, final boolean ignoreCurrent) {
|
|
this.trackerByFluid.values().forEach(EntityFluidInteraction.Tracker::reset);
|
|
+ this.trackerByFluidType.values().forEach(EntityFluidInteraction.Tracker::reset);
|
|
+ this.isInFluid = false;
|
|
+ this.eyeFluid = net.minecraftforge.common.ForgeMod.EMPTY_TYPE.get();
|
|
+
|
|
AABB box = entity.getFluidInteractionBox();
|
|
if (box != null) {
|
|
int x0 = Mth.floor(box.minX);
|
|
@@ -45,7 +_,7 @@
|
|
double eyeY = entity.getEyeY();
|
|
int eyeBlockZ = entity.getBlockZ();
|
|
Fluid lastFluidType = null;
|
|
- EntityFluidInteraction.Tracker tracker = null;
|
|
+ EntityFluidInteraction.Tracker[] trackers = null;
|
|
BlockGetter level = entity.level();
|
|
BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
|
|
|
|
@@ -55,23 +_,26 @@
|
|
mutablePos.set(x, y, z);
|
|
FluidState fluidState = level.getFluidState(mutablePos);
|
|
if (!fluidState.isEmpty()) {
|
|
+ isInFluid = true;
|
|
double fluidBottom = mutablePos.getY();
|
|
double fluidTop = fluidBottom + fluidState.getHeight(level, mutablePos);
|
|
if (!(fluidTop < box.minY)) {
|
|
Fluid fluidType = fluidState.getType();
|
|
if (fluidType != lastFluidType) {
|
|
lastFluidType = fluidType;
|
|
- tracker = this.getTrackerFor(fluidType);
|
|
+ trackers = this.getTrackersFor(fluidType);
|
|
}
|
|
|
|
- if (tracker != null) {
|
|
+ for (var tracker : trackers) {
|
|
if (x == eyeBlockX && z == eyeBlockZ && eyeY >= fluidBottom && eyeY <= fluidTop) {
|
|
tracker.eyesInside = true;
|
|
+ this.eyeFluid = lastFluidType.getFluidType();
|
|
}
|
|
|
|
tracker.height = Math.max(fluidTop - entityY, tracker.height);
|
|
- if (!ignoreCurrent) {
|
|
- Vec3 flow = fluidState.getFlow(level, mutablePos);
|
|
+ if (!ignoreCurrent || !fluidState.getType().getFluidType().canPushEntity(entity)) {
|
|
+ Vec3 flow = fluidState.getFlow(level, mutablePos, entity);
|
|
+
|
|
if (tracker.height < 0.4) {
|
|
flow = flow.scale(tracker.height);
|
|
}
|
|
@@ -118,15 +_,27 @@
|
|
return hasFluid;
|
|
}
|
|
|
|
- private EntityFluidInteraction.@Nullable Tracker getTrackerFor(final Fluid fluid) {
|
|
+ private final EntityFluidInteraction.Tracker[] NONE = new EntityFluidInteraction.Tracker[0];
|
|
+
|
|
+ private EntityFluidInteraction.@Nullable Tracker[] getTrackersFor(final Fluid fluid) {
|
|
+ var ret = new java.util.ArrayList<EntityFluidInteraction.Tracker>(1);
|
|
for (Entry<TagKey<Fluid>, EntityFluidInteraction.Tracker> entry : this.trackerByFluid.entrySet()) {
|
|
TagKey<Fluid> tag = entry.getKey();
|
|
if (fluid.is(tag)) {
|
|
- return entry.getValue();
|
|
+ ret.add(entry.getValue());
|
|
}
|
|
}
|
|
|
|
- return null;
|
|
+ ret.add(this.trackerByFluidType.computeIfAbsent(fluid.getFluidType(), k -> new EntityFluidInteraction.Tracker()));
|
|
+ return ret.isEmpty() ? NONE : ret.toArray(EntityFluidInteraction.Tracker[]::new);
|
|
+ }
|
|
+
|
|
+ public void applyCurrentTo(final Entity entity) {
|
|
+ for (var entry : this.trackerByFluidType.entrySet()) {
|
|
+ if (!entity.isPushedByFluid(entry.getKey()))
|
|
+ continue;
|
|
+ entry.getValue().applyCurrentTo(entity, entity.getFluidMotionScale(entry.getKey()));
|
|
+ }
|
|
}
|
|
|
|
public void applyCurrentTo(final TagKey<Fluid> fluid, final Entity entity, final double scale) {
|
|
@@ -141,6 +_,19 @@
|
|
return tracker != null ? tracker.height : 0.0;
|
|
}
|
|
|
|
+ public double getFluidHeight(final Fluid fluid) {
|
|
+ return getFluidHeight(fluid.getFluidType());
|
|
+ }
|
|
+
|
|
+ public double getFluidHeight(final net.minecraftforge.fluids.FluidType fluid) {
|
|
+ var tracker = this.trackerByFluidType.get(fluid);
|
|
+ return tracker == null ? 0 : tracker.height;
|
|
+ }
|
|
+
|
|
+ public boolean isInFluid() {
|
|
+ return this.isInFluid;
|
|
+ }
|
|
+
|
|
public boolean isInFluid(final TagKey<Fluid> fluid) {
|
|
return this.getFluidHeight(fluid) > 0.0;
|
|
}
|
|
@@ -148,6 +_,45 @@
|
|
public boolean isEyeInFluid(final TagKey<Fluid> fluid) {
|
|
EntityFluidInteraction.Tracker tracker = this.trackerByFluid.get(fluid);
|
|
return tracker != null && tracker.eyesInside;
|
|
+ }
|
|
+
|
|
+ public boolean isEyeInFluid(final Fluid fluid) {
|
|
+ return isEyeInFluid(fluid.getFluidType());
|
|
+ }
|
|
+
|
|
+ public boolean isEyeInFluid(final net.minecraftforge.fluids.FluidType fluid) {
|
|
+ var tracker = this.trackerByFluidType.get(fluid);
|
|
+ return tracker != null && tracker.eyesInside;
|
|
+ }
|
|
+
|
|
+ public net.minecraftforge.fluids.FluidType getEyeFluid() {
|
|
+ return this.eyeFluid;
|
|
+ }
|
|
+
|
|
+ public net.minecraftforge.fluids.FluidType getMaxHeightFluid() {
|
|
+ var ret = net.minecraftforge.common.ForgeMod.EMPTY_TYPE.get();
|
|
+ double height = 0;
|
|
+ EntityFluidInteraction.Tracker max = null;
|
|
+ for (var entry : this.trackerByFluidType.entrySet()) {
|
|
+ if (entry.getValue().height > height) {
|
|
+ height = entry.getValue().height;
|
|
+ ret = entry.getKey();
|
|
+ }
|
|
+ }
|
|
+ return ret;
|
|
+ }
|
|
+
|
|
+ public final boolean isInFluid(java.util.function.BiPredicate<net.minecraftforge.fluids.FluidType, Double> predicate, boolean forAllTypes) {
|
|
+ boolean match = false;
|
|
+ for (var entry : this.trackerByFluidType.entrySet()) {
|
|
+ if (entry.getValue().height > 0 && predicate.test(entry.getKey(), entry.getValue().height)) {
|
|
+ match = true;
|
|
+ if (!forAllTypes)
|
|
+ return true;
|
|
+ } else if (forAllTypes)
|
|
+ return false;
|
|
+ }
|
|
+ return match;
|
|
}
|
|
|
|
private static class Tracker {
|