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

CyclopsMC / IntegratedDynamics / 20210191346

14 Dec 2025 03:32PM UTC coverage: 19.514% (-33.5%) from 53.061%
20210191346

push

github

rubensworks
Remove deprecations

663 of 8728 branches covered (7.6%)

Branch coverage included in aggregate %.

6786 of 29445 relevant lines covered (23.05%)

1.09 hits per line

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

17.41
/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.GeneralConfig;
45
import org.cyclops.integrateddynamics.RegistryEntries;
46
import org.cyclops.integrateddynamics.api.block.IDynamicLight;
47
import org.cyclops.integrateddynamics.api.block.IDynamicRedstone;
48
import org.cyclops.integrateddynamics.api.block.cable.ICableFakeable;
49
import org.cyclops.integrateddynamics.api.part.IPartContainer;
50
import org.cyclops.integrateddynamics.api.part.IPartState;
51
import org.cyclops.integrateddynamics.api.part.IPartType;
52
import org.cyclops.integrateddynamics.api.part.PartRenderPosition;
53
import org.cyclops.integrateddynamics.block.shapes.*;
54
import org.cyclops.integrateddynamics.client.model.CableModel;
55
import org.cyclops.integrateddynamics.client.model.IRenderState;
56
import org.cyclops.integrateddynamics.core.block.BlockRayTraceResultComponent;
57
import org.cyclops.integrateddynamics.core.block.VoxelShapeComponents;
58
import org.cyclops.integrateddynamics.core.block.VoxelShapeComponentsFactory;
59
import org.cyclops.integrateddynamics.core.blockentity.BlockEntityMultipartTicking;
60
import org.cyclops.integrateddynamics.core.helper.CableHelpers;
61
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
62
import org.cyclops.integrateddynamics.core.helper.PartHelpers;
63

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

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

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

79
    public static final float BLOCK_HARDNESS = 3.0F;
80

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

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

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

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

116
    @Setter
3✔
117
    private boolean disableCollisionBox = false;
118

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

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

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

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

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

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

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

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

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

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

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

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

197
        if (!CableHelpers.isRemovingCable()) {
×
198
            CableHelpers.onCableRemoved(level, pos);
×
199
        }
200
    }
×
201

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

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

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

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

253
    @Override
254
    protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, @org.jetbrains.annotations.Nullable Orientation orientation, boolean movedByPiston) {
255
        super.neighborChanged(state, level, pos, neighborBlock, orientation, movedByPiston);
×
256
        NetworkHelpers.onElementProviderBlockNeighborChange(level, pos, orientation != null ? orientation.getFront().getOpposite() : null);
×
257
    }
×
258

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

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

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

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

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

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

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

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

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

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

329
    @Override
330
    public VoxelShape getCollisionShape(BlockState blockState, BlockGetter world, BlockPos pos, CollisionContext selectionContext) {
331
        return (disableCollisionBox || GeneralConfig.disableCableCollision) ? Shapes.empty() : super.getCollisionShape(blockState, world, pos, selectionContext);
×
332
    }
333

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

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

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

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

354
    /* --------------- Start IDynamicRedstone --------------- */
355

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

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

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

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

400
    /* --------------- Start IDynamicLight --------------- */
401

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

416
}
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