Skip to content

Commit

Permalink
Combine cable voxel shape components in getShape rather than getColli…
Browse files Browse the repository at this point in the history
…sionShape
  • Loading branch information
embeddedt authored and rubensworks committed Sep 1, 2024
1 parent e650bcf commit 4296278
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/main/java/org/cyclops/integrateddynamics/block/BlockCable.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,36 +341,27 @@ public VoxelShapeComponents getSelectedShape(BlockState blockState, BlockGetter
return voxelShapeComponentsFactory.createShape(blockState, world, pos, selectionContext);
}

@Override
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext selectionContext) {
VoxelShapeComponents selectedShape = getSelectedShape(state, world, pos, selectionContext);
BlockRayTraceResultComponent rayTraceResult = selectedShape.rayTrace(pos, selectionContext instanceof EntityCollisionContext ? ((EntityCollisionContext) selectionContext).getEntity() : null);
if (rayTraceResult != null) {
return rayTraceResult.getComponent().getShape(state, world, pos, selectionContext);
}
return selectedShape;
}

private final Cache<String, VoxelShape> CACHE_COLLISION_SHAPES = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.build();

@SneakyThrows
@Override
public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, BlockPos pos, CollisionContext selectionContext) {
if(disableCollisionBox) {
return Shapes.empty();
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext selectionContext) {
VoxelShapeComponents selectedShape = getSelectedShape(state, world, pos, selectionContext);
BlockRayTraceResultComponent rayTraceResult = selectedShape.rayTrace(pos, selectionContext instanceof EntityCollisionContext ? ((EntityCollisionContext) selectionContext).getEntity() : null);
if (rayTraceResult != null) {
return rayTraceResult.getComponent().getShape(state, world, pos, selectionContext);
}

VoxelShapeComponents voxelShapeComponents = (VoxelShapeComponents) super.getCollisionShape(blockState, world, pos, selectionContext);
String cableState = voxelShapeComponents.getStateId();
String cableState = selectedShape.getStateId();

// Cache the operations below, as they are too expensive to execute each render tick
return CACHE_COLLISION_SHAPES.get(cableState, () -> {
// Combine all VoxelShapes using IBooleanFunction.OR,
// because for some reason our VoxelShapeComponents aggregator does not handle collisions properly.
// This can probably be fixed, but I spent too much time on this already, and the current solution works just fine.
Iterator<VoxelShape> it = voxelShapeComponents.iterator();
Iterator<VoxelShape> it = selectedShape.iterator();
if (!it.hasNext()) {
return Shapes.empty();
}
Expand All @@ -382,6 +373,11 @@ public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, Bl
});
}

@Override
public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, BlockPos pos, CollisionContext selectionContext) {
return disableCollisionBox ? Shapes.empty() : super.getCollisionShape(blockState, world, pos, selectionContext);
}

@Override
public boolean hasDynamicShape() {
return BlockCableConfig.dynamicShape;
Expand Down

0 comments on commit 4296278

Please sign in to comment.