Skip to content

Commit a0c5c90

Browse files
committed
Prevent crash with mods calling getFluidState recursively
The returned fluid is meaningless, but vanilla doesn't crash here Fixes #238
1 parent 2255913 commit a0c5c90

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/reduce_blockstate_cache_rebuilds/BlockStateBaseMixin.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,19 @@ private FluidState genCacheBeforeGettingFluid(BlockBehaviour.BlockStateBase base
8787
// don't generate the full cache here as mods will iterate for the fluid state a lot
8888
// assume blockstates will not change their contained fluidstate at runtime more than once
8989
// this is how Lithium's implementation used to work, so it should be fine
90-
if(this.cacheInvalid && this.fluidState == MFIX$VANILLA_DEFAULT_FLUID)
91-
this.fluidState = this.owner.getFluidState(this.asState());
90+
if(this.cacheInvalid && this.fluidState == MFIX$VANILLA_DEFAULT_FLUID) {
91+
synchronized (BlockBehaviour.BlockStateBase.class) {
92+
if(!buildingCache) {
93+
buildingCache = true;
94+
try {
95+
this.fluidState = this.owner.getFluidState(this.asState());
96+
} finally {
97+
buildingCache = false;
98+
}
99+
}
100+
}
101+
102+
}
92103
return this.fluidState;
93104
}
94105

fabric/src/test/java/net/minecraft/world/level/block/state/BlockStateCacheTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.minecraft.world.level.EmptyBlockGetter;
55
import net.minecraft.world.level.block.Block;
66
import net.minecraft.world.level.block.Blocks;
7+
import net.minecraft.world.level.material.FluidState;
78
import org.embeddedt.modernfix.duck.IBlockState;
89
import org.embeddedt.modernfix.testing.util.BootstrapMinecraft;
910
import org.junit.jupiter.api.*;
@@ -74,4 +75,19 @@ public void testExtraFieldCachingCorrect() {
7475
}
7576
}
7677
}
78+
79+
@Test
80+
@Order(5)
81+
public void checkRecursiveFluidState() {
82+
Block b = new Block(BlockBehaviour.Properties.copy(Blocks.STONE)) {
83+
@Override
84+
public FluidState getFluidState(BlockState state) {
85+
return state.getFluidState();
86+
}
87+
};
88+
BlockState state = b.getStateDefinition().any();
89+
((IBlockState)state).clearCache();
90+
// this should not throw
91+
state.getFluidState();
92+
}
7793
}

0 commit comments

Comments
 (0)