• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / IntegratedDynamics / 16552051255

27 Jul 2025 01:58PM UTC coverage: 53.206% (+8.0%) from 45.161%
16552051255

push

github

rubensworks
Resolve minor TODOs

2888 of 8740 branches covered (33.04%)

Branch coverage included in aggregate %.

17341 of 29280 relevant lines covered (59.22%)

3.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.55
/src/main/java/org/cyclops/integrateddynamics/block/BlockCable.java
1
package org.cyclops.integrateddynamics.block;
2

3
import com.google.common.cache.Cache;
4
import com.google.common.cache.CacheBuilder;
5
import com.mojang.serialization.MapCodec;
6
import lombok.Setter;
7
import lombok.SneakyThrows;
8
import net.minecraft.core.BlockPos;
9
import net.minecraft.core.Direction;
10
import net.minecraft.server.level.ServerLevel;
11
import net.minecraft.util.RandomSource;
12
import net.minecraft.world.InteractionHand;
13
import net.minecraft.world.InteractionResult;
14
import net.minecraft.world.entity.LivingEntity;
15
import net.minecraft.world.entity.player.Player;
16
import net.minecraft.world.item.ItemStack;
17
import net.minecraft.world.item.context.BlockPlaceContext;
18
import net.minecraft.world.level.*;
19
import net.minecraft.world.level.block.BaseEntityBlock;
20
import net.minecraft.world.level.block.Block;
21
import net.minecraft.world.level.block.RenderShape;
22
import net.minecraft.world.level.block.SimpleWaterloggedBlock;
23
import net.minecraft.world.level.block.entity.BlockEntity;
24
import net.minecraft.world.level.block.entity.BlockEntityTicker;
25
import net.minecraft.world.level.block.entity.BlockEntityType;
26
import net.minecraft.world.level.block.state.BlockState;
27
import net.minecraft.world.level.block.state.StateDefinition;
28
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
29
import net.minecraft.world.level.block.state.properties.BooleanProperty;
30
import net.minecraft.world.level.material.Fluid;
31
import net.minecraft.world.level.material.FluidState;
32
import net.minecraft.world.level.material.Fluids;
33
import net.minecraft.world.level.redstone.Orientation;
34
import net.minecraft.world.phys.AABB;
35
import net.minecraft.world.phys.BlockHitResult;
36
import net.minecraft.world.phys.shapes.*;
37
import net.neoforged.neoforge.common.extensions.ILevelExtension;
38
import net.neoforged.neoforge.model.data.ModelProperty;
39
import org.cyclops.cyclopscore.block.BlockWithEntity;
40
import org.cyclops.cyclopscore.datastructure.EnumFacingMap;
41
import org.cyclops.cyclopscore.helper.IModHelpers;
42
import org.cyclops.cyclopscore.helper.IModHelpersNeoForge;
43
import org.cyclops.integrateddynamics.Capabilities;
44
import org.cyclops.integrateddynamics.RegistryEntries;
45
import org.cyclops.integrateddynamics.api.block.IDynamicLight;
46
import org.cyclops.integrateddynamics.api.block.IDynamicRedstone;
47
import org.cyclops.integrateddynamics.api.block.cable.ICableFakeable;
48
import org.cyclops.integrateddynamics.api.part.IPartContainer;
49
import org.cyclops.integrateddynamics.api.part.IPartState;
50
import org.cyclops.integrateddynamics.api.part.IPartType;
51
import org.cyclops.integrateddynamics.api.part.PartRenderPosition;
52
import org.cyclops.integrateddynamics.block.shapes.*;
53
import org.cyclops.integrateddynamics.client.model.CableModel;
54
import org.cyclops.integrateddynamics.client.model.IRenderState;
55
import org.cyclops.integrateddynamics.core.block.BlockRayTraceResultComponent;
56
import org.cyclops.integrateddynamics.core.block.VoxelShapeComponents;
57
import org.cyclops.integrateddynamics.core.block.VoxelShapeComponentsFactory;
58
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityMultipartTicking;
59
import org.cyclops.integrateddynamics.core.helper.CableHelpers;
60
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
61
import org.cyclops.integrateddynamics.core.helper.PartHelpers;
62

63
import javax.annotation.Nullable;
64
import java.util.Iterator;
65
import java.util.Map;
66
import java.util.Optional;
67
import java.util.concurrent.TimeUnit;
68

69
/**
70
 * A block that is built up from different parts.
71
 * This block refers to a ticking part entity.
72
 * @author rubensworks
73
 */
74
public class BlockCable extends BlockWithEntity implements SimpleWaterloggedBlock {
75

76
    public static final MapCodec<BlockCable> CODEC = simpleCodec(BlockCable::new);
3✔
77

78
    public static final float BLOCK_HARDNESS = 3.0F;
79

80
    public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
2✔
81

82
    // Model Properties
83
    public static final ModelProperty<Boolean> REALCABLE = new ModelProperty<>();
4✔
84
    public static final ModelProperty<Boolean>[] CONNECTED = new ModelProperty[6];
3✔
85
    public static final ModelProperty<PartRenderPosition>[] PART_RENDERPOSITIONS = new ModelProperty[6];
3✔
86
    public static final ModelProperty<Optional<BlockState>> FACADE = new ModelProperty<>();
4✔
87
    static {
88
        for(Direction side : Direction.values()) {
16✔
89
            CONNECTED[side.ordinal()] = new ModelProperty<>();
7✔
90
            PART_RENDERPOSITIONS[side.ordinal()] = new ModelProperty<>();
7✔
91
        }
92
    }
93
    public static final ModelProperty<IPartContainer> PARTCONTAINER = new ModelProperty<>();
4✔
94
    public static final ModelProperty<IRenderState> RENDERSTATE = new ModelProperty<>();
4✔
95

96
    // Collision boxes
97
    public final static AABB CABLE_CENTER_BOUNDINGBOX = new AABB(
10✔
98
            CableModel.MIN, CableModel.MIN, CableModel.MIN, CableModel.MAX, CableModel.MAX, CableModel.MAX);
99
    private final static EnumFacingMap<AABB> CABLE_SIDE_BOUNDINGBOXES = EnumFacingMap.forAllValues(
57✔
100
            new AABB(CableModel.MIN, 0, CableModel.MIN, CableModel.MAX, CableModel.MIN, CableModel.MAX), // DOWN
101
            new AABB(CableModel.MIN, CableModel.MAX, CableModel.MIN, CableModel.MAX, 1, CableModel.MAX), // UP
102
            new AABB(CableModel.MIN, CableModel.MIN, 0, CableModel.MAX, CableModel.MAX, CableModel.MIN), // NORTH
103
            new AABB(CableModel.MIN, CableModel.MAX, CableModel.MAX, CableModel.MAX, CableModel.MIN, 1), // SOUTH
104
            new AABB(0, CableModel.MIN, CableModel.MIN, CableModel.MIN, CableModel.MAX, CableModel.MAX), // WEST
105
            new AABB(CableModel.MAX, CableModel.MIN, CableModel.MIN, 1, CableModel.MAX, CableModel.MAX) // EAST
106
    );
107

108
    private final VoxelShapeComponentsFactory voxelShapeComponentsFactory = new VoxelShapeComponentsFactory(
31✔
109
            new VoxelShapeComponentsFactoryHandlerCableCenter(),
110
            new VoxelShapeComponentsFactoryHandlerCableConnections(),
111
            new VoxelShapeComponentsFactoryHandlerParts(),
112
            new VoxelShapeComponentsFactoryHandlerFacade()
113
    );
114

115
    @Setter
7✔
116
    private boolean disableCollisionBox = false;
117

118
    public BlockCable(Properties properties) {
119
        super(properties, BlockEntityMultipartTicking::new);
4✔
120
        this.registerDefaultState(this.stateDefinition.any().setValue(WATERLOGGED, false));
11✔
121
    }
1✔
122

123
    @Override
124
    protected MapCodec<? extends BaseEntityBlock> codec() {
125
        return CODEC;
×
126
    }
127

128
    @Override
129
    public boolean useShapeForLightOcclusion(BlockState p_60576_) {
130
        return true;
2✔
131
    }
132

133
    @Override
134
    @Nullable
135
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> blockEntityType) {
136
        return level.isClientSide ? null : createTickerHelper(blockEntityType, RegistryEntries.BLOCK_ENTITY_MULTIPART_TICKING.get(), new BlockEntityMultipartTicking.Ticker<>());
12!
137
    }
138

139
    @Override
140
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
141
        super.createBlockStateDefinition(builder);
3✔
142
        builder.add(WATERLOGGED);
9✔
143
    }
1✔
144

145
    @Override
146
    protected BlockState updateShape(BlockState state, LevelReader level, ScheduledTickAccess scheduledTickAccess, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, RandomSource random) {
147
        if (level instanceof ServerLevel serverLevel) {
6!
148
            if (state.getValue(WATERLOGGED)) {
6!
149
                serverLevel.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
×
150
            }
151
            NetworkHelpers.onElementProviderBlockNeighborChange(serverLevel, pos, direction);
4✔
152
        }
153
        return super.updateShape(state, level, scheduledTickAccess, pos, direction, neighborPos, neighborState, random);
11✔
154
    }
155

156
    @Override
157
    public BlockState getStateForPlacement(BlockPlaceContext context) {
158
        FluidState ifluidstate = context.getLevel().getFluidState(context.getClickedPos());
6✔
159
        return this.defaultBlockState().setValue(WATERLOGGED, ifluidstate.getType() == Fluids.WATER);
12!
160
    }
161

162
    @Override
163
    public FluidState getFluidState(BlockState state) {
164
        return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
14✔
165
    }
166

167
    @Override
168
    public boolean canPlaceLiquid(@org.jetbrains.annotations.Nullable LivingEntity entity, BlockGetter worldIn, BlockPos pos, BlockState blockState, Fluid fluidIn) {
169
        return !blockState.getValue(BlockStateProperties.WATERLOGGED) && fluidIn == Fluids.WATER
×
170
                && !(worldIn instanceof ILevelExtension levelExtension && CableHelpers.hasFacade(levelExtension, pos));
×
171
    }
172

173
    @Override
174
    public void onBlockExploded(BlockState state, ServerLevel world, BlockPos blockPos, Explosion explosion) {
175
        CableHelpers.setRemovingCable(true);
2✔
176
        CableHelpers.onCableRemoving(world, blockPos, true, false, state, world.getBlockEntity(blockPos));
10✔
177
        super.onBlockExploded(state, world, blockPos, explosion);
6✔
178
        CableHelpers.onCableRemoved(world, blockPos);
4✔
179
        CableHelpers.setRemovingCable(false);
2✔
180
    }
1✔
181

182
    @Override
183
    public boolean onDestroyedByPlayer(BlockState state, Level world, BlockPos pos, Player player, boolean willHarvest, FluidState fluid) {
184
        BlockRayTraceResultComponent rayTraceResult = getSelectedShape(state, world, pos, CollisionContext.of(player))
9✔
185
                .rayTrace(pos, player);
2✔
186
        if (rayTraceResult != null && rayTraceResult.getComponent().destroy(world, pos, player, false)) {
10!
187
            return false;
2✔
188
        }
189
        return rayTraceResult != null && super.onDestroyedByPlayer(state, world, pos, player, willHarvest, fluid);
13!
190
    }
191

192
    @Override
193
    protected void affectNeighborsAfterRemoval(BlockState state, ServerLevel level, BlockPos pos, boolean movedByPiston) {
194
        super.affectNeighborsAfterRemoval(state, level, pos, movedByPiston);
6✔
195

196
        if (!CableHelpers.isRemovingCable()) {
2✔
197
            CableHelpers.onCableRemoved(level, pos);
4✔
198
        }
199
    }
1✔
200

201
    @Override
202
    protected InteractionResult useItemOn(ItemStack pStack, BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHitResult) {
203
        /*
204
            Wrench: sneak + right-click anywhere on cable to remove cable
205
                    right-click on a cable side to disconnect on that side
206
                    sneak + right-click on part to remove that part
207
            No wrench: right-click to open GUI
208
         */
209
        BlockEntityMultipartTicking tile = IModHelpers.get().getBlockEntityHelpers().get(pLevel, pPos, BlockEntityMultipartTicking.class).orElse(null);
10✔
210
        if(tile != null) {
2!
211
            BlockRayTraceResultComponent rayTraceResult = getSelectedShape(pState, pLevel, pPos, CollisionContext.of(pPlayer))
9✔
212
                    .rayTrace(pPos, pPlayer);
2✔
213
            if(rayTraceResult != null) {
2!
214
                InteractionResult actionResultType = rayTraceResult.getComponent().onBlockActivated(pState, pLevel, pPos, pPlayer, pHand, rayTraceResult);
10✔
215
                if (actionResultType.consumesAction()) {
3!
216
                    return actionResultType;
2✔
217
                }
218
            }
219
        }
220
        return super.useItemOn(pStack, pState, pLevel, pPos, pPlayer, pHand, pHitResult);
×
221
    }
222

223
    @Override
224
    public void onPlace(BlockState state, Level world, BlockPos pos, BlockState oldState, boolean isMoving) {
225
        super.onPlace(state, world, pos, oldState, isMoving);
7✔
226
        if (!world.isClientSide()) {
3!
227
            ICableFakeable cableFakeable = CableHelpers.getCableFakeable(world, pos, null).orElse(null);
8✔
228
            if (cableFakeable != null && cableFakeable.isRealCable()) {
5!
229
                CableHelpers.onCableAdded(world, pos);
3✔
230
            }
231
        }
232
    }
1✔
233

234
    @Override
235
    public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
236
        super.setPlacedBy(world, pos, state, placer, itemStack);
7✔
237
        if (!world.isClientSide()) {
3!
238
            CableHelpers.onCableAddedByPlayer(world, pos, placer);
4✔
239
        }
240
    }
1✔
241

242
    @Override
243
    public ItemStack getCloneItemStack(LevelReader level, BlockPos pos, BlockState state, boolean includeData, Player player) {
244
        BlockRayTraceResultComponent rayTraceResult = getSelectedShape(state, level, pos, CollisionContext.of(player))
×
245
                .rayTrace(pos, player);
×
246
        if(rayTraceResult != null) {
×
247
            return rayTraceResult.getComponent().getCloneItemStack((Level) level, pos);
×
248
        }
249
        return super.getCloneItemStack(level, pos, state, includeData, player);
×
250
    }
251

252
    @Override
253
    protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, @org.jetbrains.annotations.Nullable Orientation orientation, boolean movedByPiston) {
254
        super.neighborChanged(state, level, pos, neighborBlock, orientation, movedByPiston);
8✔
255
        NetworkHelpers.onElementProviderBlockNeighborChange(level, pos, null);
4✔
256
    }
1✔
257

258
    @Override
259
    public void onNeighborChange(BlockState state, LevelReader world, BlockPos pos, BlockPos neighbor) {
260
        super.onNeighborChange(state, world, pos, neighbor);
6✔
261
        if (world instanceof Level level) {
6!
262
            NetworkHelpers.onElementProviderBlockNeighborChange(level, pos, null);
4✔
263
        }
264
    }
1✔
265

266
    @Override
267
    public void tick(BlockState state, ServerLevel world, BlockPos pos, RandomSource rand) {
268
        super.tick(state, world, pos, rand);
×
269
        IModHelpers.get().getBlockEntityHelpers().get(world, pos, BlockEntityMultipartTicking.class)
×
270
                .ifPresent(tile -> {
×
271
                    for (Map.Entry<Direction, PartHelpers.PartStateHolder<?, ?>> entry : tile
×
272
                            .getPartContainer().getPartData().entrySet()) {
×
273
                        updateTickPart(entry.getValue().getPart(), world, pos, entry.getValue().getState(), rand);
×
274
                    }
×
275
                });
×
276
    }
×
277

278
    protected void updateTickPart(IPartType partType, Level world, BlockPos pos, IPartState partState, RandomSource random) {
279
        partType.updateTick(world, pos, partState, random);
×
280
    }
×
281

282
    /* --------------- Start shapes and rendering --------------- */
283

284
    public AABB getCableBoundingBox(Direction side) {
285
        if (side == null) {
×
286
            return CABLE_CENTER_BOUNDINGBOX;
×
287
        } else {
288
            return CABLE_SIDE_BOUNDINGBOXES.get(side);
×
289
        }
290
    }
291

292
    public VoxelShapeComponents getSelectedShape(BlockState blockState, BlockGetter world, BlockPos pos, CollisionContext selectionContext) {
293
        return voxelShapeComponentsFactory.createShape(blockState, world, pos, selectionContext);
8✔
294
    }
295

296
    private final Cache<String, VoxelShape> CACHE_COLLISION_SHAPES = CacheBuilder.newBuilder()
4✔
297
            .expireAfterAccess(1, TimeUnit.MINUTES)
1✔
298
            .build();
2✔
299

300
    @SneakyThrows
×
301
    @Override
302
    public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext selectionContext) {
303
        VoxelShapeComponents selectedShape = getSelectedShape(state, world, pos, selectionContext);
7✔
304
        BlockRayTraceResultComponent rayTraceResult = selectedShape.rayTrace(pos, selectionContext instanceof EntityCollisionContext ? ((EntityCollisionContext) selectionContext).getEntity() : null);
11!
305
        if (rayTraceResult != null) {
2!
306
            return rayTraceResult.getComponent().getShape(state, world, pos, selectionContext);
×
307
        }
308

309
        String cableState = selectedShape.getStateId();
3✔
310

311
        // Cache the operations below, as they are too expensive to execute each render tick
312
        return CACHE_COLLISION_SHAPES.get(cableState, () -> {
8✔
313
            // Combine all VoxelShapes using IBooleanFunction.OR,
314
            // because for some reason our VoxelShapeComponents aggregator does not handle collisions properly.
315
            // This can probably be fixed, but I spent too much time on this already, and the current solution works just fine.
316
            Iterator<VoxelShape> it = selectedShape.iterator();
3✔
317
            if (!it.hasNext()) {
3✔
318
                return Shapes.empty();
2✔
319
            }
320
            VoxelShape shape = it.next();
4✔
321
            while (it.hasNext()) {
3✔
322
                shape = Shapes.join(shape, it.next(), BooleanOp.OR);
8✔
323
            }
324
            return shape.optimize();
3✔
325
        });
326
    }
327

328
    @Override
329
    public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, BlockPos pos, CollisionContext selectionContext) {
330
        return disableCollisionBox ? Shapes.empty() : super.getCollisionShape(blockState, world, pos, selectionContext);
10!
331
    }
332

333
    @Override
334
    public boolean hasDynamicShape() {
335
        return BlockCableConfig.dynamicShape;
2✔
336
    }
337

338
    @Override
339
    public RenderShape getRenderShape(BlockState blockState) {
340
        return RenderShape.MODEL;
×
341
    }
342

343
    @Override
344
    protected VoxelShape getBlockSupportShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) {
345
        return this.getShape(pState, pLevel, pPos, new CollisionContextBlockSupport());
9✔
346
    }
347

348
    @Override
349
    public boolean shouldDisplayFluidOverlay(BlockState state, BlockAndTintGetter world, BlockPos pos, FluidState fluidState) {
350
        return world instanceof ILevelExtension levelExtension && CableHelpers.getFacade(levelExtension, pos).isPresent();
×
351
    }
352

353
    /* --------------- Start IDynamicRedstone --------------- */
354

355
    @SuppressWarnings("deprecation")
356
    @Override
357
    public boolean isSignalSource(BlockState blockState) {
358
        return true;
×
359
    }
360

361
    @Override
362
    public boolean canConnectRedstone(BlockState blockState, BlockGetter world, BlockPos pos, Direction side) {
363
        if (world instanceof ILevelExtension levelExtension) {
6!
364
            if (side == null) {
2!
365
                for (Direction dummySide : Direction.values()) {
×
366
                    IDynamicRedstone dynamicRedstone = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(levelExtension, pos, dummySide, Capabilities.DynamicRedstone.BLOCK).orElse(null);
×
367
                    if (dynamicRedstone != null && (dynamicRedstone.getRedstoneLevel() >= 0 || dynamicRedstone.isAllowRedstoneInput())) {
×
368
                        return true;
×
369
                    }
370
                }
371
                return false;
×
372
            }
373
            IDynamicRedstone dynamicRedstone = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(levelExtension, pos, side.getOpposite(), Capabilities.DynamicRedstone.BLOCK).orElse(null);
12✔
374
            return dynamicRedstone != null && (dynamicRedstone.getRedstoneLevel() >= 0 || dynamicRedstone.isAllowRedstoneInput());
12!
375
        }
376
        return false;
×
377
    }
378

379
    @SuppressWarnings("deprecation")
380
    @Override
381
    public int getDirectSignal(BlockState blockState, BlockGetter world, BlockPos pos, Direction side) {
382
        if (world instanceof ILevelExtension levelExtension) {
6!
383
            IDynamicRedstone dynamicRedstone = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(levelExtension, pos, side.getOpposite(), Capabilities.DynamicRedstone.BLOCK).orElse(null);
12✔
384
            return dynamicRedstone != null && dynamicRedstone.isDirect() ? dynamicRedstone.getRedstoneLevel() : 0;
7!
385
        }
386
        return 0;
×
387
    }
388

389
    @SuppressWarnings("deprecation")
390
    @Override
391
    public int getSignal(BlockState blockState, BlockGetter world, BlockPos pos, Direction side) {
392
        if (world instanceof ILevelExtension levelExtension) {
6!
393
            IDynamicRedstone dynamicRedstone = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(levelExtension, pos, side.getOpposite(), Capabilities.DynamicRedstone.BLOCK).orElse(null);
12✔
394
            return dynamicRedstone != null ? dynamicRedstone.getRedstoneLevel() : 0;
6!
395
        }
396
        return 0;
×
397
    }
398

399
    /* --------------- Start IDynamicLight --------------- */
400

401
    @Override
402
    public int getLightEmission(BlockState blockState, BlockGetter world, BlockPos pos) {
403
        int light = 0;
2✔
404
        if (world instanceof ILevelExtension levelExtension) {
6✔
405
            for (Direction side : Direction.values()) {
16✔
406
                IDynamicLight dynamicLight = levelExtension.getCapability(Capabilities.DynamicLight.BLOCK, pos, blockState, null, side);
9✔
407
                if (dynamicLight != null) {
2!
408
                    light = Math.max(light, dynamicLight.getLightLevel());
×
409
                }
410
            }
411
        }
412
        return light;
2✔
413
    }
414

415
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc