Skip to content

Commit 7cfccf4

Browse files
committed
Change VoxelNeighborhoodState to use state identity
1 parent 0c4cb76 commit 7cfccf4

1 file changed

Lines changed: 36 additions & 21 deletions

File tree

common/src/main/java/dev/ryanhcode/sable/physics/chunk/VoxelNeighborhoodState.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import dev.ryanhcode.sable.api.block.BlockWithSubLevelCollisionCallback;
44
import dev.ryanhcode.sable.util.LevelAccelerator;
5-
import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap;
5+
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
6+
import it.unimi.dsi.fastutil.objects.Reference2BooleanOpenHashMap;
67
import net.minecraft.core.BlockPos;
78
import net.minecraft.core.Direction;
89
import net.minecraft.world.level.BlockGetter;
@@ -13,7 +14,6 @@
1314
import net.minecraft.world.level.block.state.BlockState;
1415
import net.minecraft.world.level.chunk.LevelChunk;
1516
import org.jetbrains.annotations.Nullable;
16-
1717
import java.util.function.BiFunction;
1818

1919
public enum VoxelNeighborhoodState {
@@ -23,36 +23,50 @@ public enum VoxelNeighborhoodState {
2323
CORNER(0xeb6c0b),
2424
INTERIOR(0x000000);
2525

26-
public static BiFunction<BlockGetter, BlockState, Boolean> IS_SOLID_MEMOIZED = new BiFunction<>() {
27-
private final Int2BooleanOpenHashMap cache = new Int2BooleanOpenHashMap();
26+
private static final BiFunction<BlockGetter, BlockState, Boolean> IS_SOLID_MEMOIZED = new BiFunction<>() {
27+
private final Reference2BooleanMap<BlockState> cache = new Reference2BooleanOpenHashMap<>();
2828

2929
@Override
3030
public Boolean apply(final BlockGetter blockGetter, final BlockState state) {
31-
return this.cache.computeIfAbsent(state.hashCode(), x -> {
32-
// TODO add the blockgetter and position as context
33-
if (state.isAir())
34-
return false;
31+
if (this.cache.containsKey(state)) {
32+
return this.cache.getBoolean(state);
33+
}
3534

36-
if (state.getBlock() instanceof MovingPistonBlock)
37-
return true;
35+
// TODO add the blockgetter and position as context
36+
if (state.isAir()) {
37+
this.cache.put(state, false);
38+
return false;
39+
}
40+
41+
if (state.getBlock() instanceof MovingPistonBlock) {
42+
this.cache.put(state, true);
43+
return true;
44+
}
3845

39-
return !state.getCollisionShape(blockGetter, BlockPos.ZERO).isEmpty();
40-
});
46+
final boolean notEmpty = !state.getCollisionShape(blockGetter, BlockPos.ZERO).isEmpty();
47+
this.cache.put(state, notEmpty);
48+
return notEmpty;
4149
}
4250
};
4351

44-
public static BiFunction<BlockGetter, BlockState, Boolean> IS_FULL_BLOCK = new BiFunction<>() {
45-
private final Int2BooleanOpenHashMap cache = new Int2BooleanOpenHashMap();
52+
private static final BiFunction<BlockGetter, BlockState, Boolean> IS_FULL_BLOCK = new BiFunction<>() {
53+
private final Reference2BooleanMap<BlockState> cache = new Reference2BooleanOpenHashMap<>();
4654

4755
@Override
4856
public Boolean apply(final BlockGetter blockGetter, final BlockState state) {
49-
return this.cache.computeIfAbsent(state.hashCode(), x -> {
50-
// TODO add the blockgetter and position as context
51-
if (state.isAir())
52-
return false;
57+
if (this.cache.containsKey(state)) {
58+
return this.cache.getBoolean(state);
59+
}
60+
61+
// TODO add the blockgetter and position as context
62+
if (state.isAir()) {
63+
this.cache.put(state, false);
64+
return false;
65+
}
5366

54-
return state.isCollisionShapeFullBlock(blockGetter, BlockPos.ZERO);
55-
});
67+
final boolean fullBlock = state.isCollisionShapeFullBlock(blockGetter, BlockPos.ZERO);
68+
this.cache.put(state, fullBlock);
69+
return fullBlock;
5670
}
5771
};
5872

@@ -78,8 +92,9 @@ public static VoxelNeighborhoodState getState(final LevelAccelerator level, fina
7892
final ChunkPos initialPos = new ChunkPos(pos);
7993
final BlockState state = chunk != null ? level.getBlockState(chunk, pos) : level.getBlockState(pos);
8094

81-
if (isLiquid(state) || BlockWithSubLevelCollisionCallback.hasCallback(state))
95+
if (isLiquid(state) || BlockWithSubLevelCollisionCallback.hasCallback(state)) {
8296
return CORNER;
97+
}
8398

8499
if (!isSolid(level, pos, state)) {
85100
return EMPTY;

0 commit comments

Comments
 (0)