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

CyclopsMC / IntegratedDynamics / 16574353307

28 Jul 2025 04:15PM UTC coverage: 53.239% (+0.03%) from 53.206%
16574353307

push

github

rubensworks
Bump CommonCaps version

2890 of 8740 branches covered (33.07%)

Branch coverage included in aggregate %.

17352 of 29281 relevant lines covered (59.26%)

3.08 hits per line

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

57.66
/src/main/java/org/cyclops/integrateddynamics/core/helper/NetworkHelpers.java
1
package org.cyclops.integrateddynamics.core.helper;
2

3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.util.RandomSource;
6
import net.minecraft.world.level.Level;
7
import net.minecraft.world.level.LevelReader;
8
import net.minecraft.world.level.ScheduledTickAccess;
9
import net.minecraft.world.level.block.Block;
10
import net.minecraft.world.level.block.state.BlockState;
11
import net.minecraft.world.level.redstone.Orientation;
12
import net.neoforged.neoforge.common.extensions.ILevelExtension;
13
import org.cyclops.commoncapabilities.api.ingredient.IngredientComponent;
14
import org.cyclops.cyclopscore.helper.IModHelpersNeoForge;
15
import org.cyclops.integrateddynamics.Capabilities;
16
import org.cyclops.integrateddynamics.GeneralConfig;
17
import org.cyclops.integrateddynamics.IntegratedDynamics;
18
import org.cyclops.integrateddynamics.api.block.cable.ICableFakeable;
19
import org.cyclops.integrateddynamics.api.network.*;
20
import org.cyclops.integrateddynamics.api.part.PartPos;
21
import org.cyclops.integrateddynamics.api.path.IPathElement;
22
import org.cyclops.integrateddynamics.capability.path.SidedPathElement;
23
import org.cyclops.integrateddynamics.core.TickHandler;
24
import org.cyclops.integrateddynamics.core.network.Network;
25
import org.cyclops.integrateddynamics.core.persist.world.NetworkWorldStorage;
26

27
import javax.annotation.Nullable;
28
import java.util.Optional;
29

30
/**
31
 * Network helper methods.
32
 * @author rubensworks
33
 */
34
public class NetworkHelpers {
×
35

36
    /**
37
     * Get the network carrier capability at the given position.
38
     * If possible, prefer using the variant with block state.
39
     * @param world The world.
40
     * @param pos The position.
41
     * @param side The side.
42
     * @return The optional network carrier capability.
43
     */
44
    @Deprecated
45
    public static Optional<INetworkCarrier> getNetworkCarrier(ILevelExtension world, BlockPos pos, @Nullable Direction side) {
46
        return IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, side, Capabilities.NetworkCarrier.BLOCK);
8✔
47
    }
48

49
    /**
50
     * Get the network carrier capability at the given position.
51
     * @param world The world.
52
     * @param pos The position.
53
     * @param side The side.
54
     * @param blockState The block state.
55
     * @return The optional network carrier capability.
56
     */
57
    public static Optional<INetworkCarrier> getNetworkCarrier(ILevelExtension world, BlockPos pos, @Nullable Direction side, BlockState blockState) {
58
        return Optional.ofNullable(world.getCapability(Capabilities.NetworkCarrier.BLOCK, pos, blockState, null, side));
10✔
59
    }
60

61
    /**
62
     * Get the network element provider capability at the given position.
63
     * If possible, prefer using the variant with block state.
64
     * @param world The world.
65
     * @param pos The position.
66
     * @param side The side.
67
     * @return The optional network element provider capability.
68
     */
69
    @Deprecated
70
    public static Optional<INetworkElementProvider> getNetworkElementProvider(ILevelExtension world, BlockPos pos, @Nullable Direction side) {
71
        return IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, side, Capabilities.NetworkElementProvider.BLOCK);
8✔
72
    }
73

74
    /**
75
     * Get the network element provider capability at the given position.
76
     * @param world The world.
77
     * @param pos The position.
78
     * @param side The side.
79
     * @param blockState The block state.
80
     * @return The optional network element provider capability.
81
     */
82
    public static Optional<INetworkElementProvider> getNetworkElementProvider(ILevelExtension world, BlockPos pos, @Nullable Direction side, BlockState blockState) {
83
        return Optional.ofNullable(world.getCapability(Capabilities.NetworkElementProvider.BLOCK, pos, blockState, null, side));
10✔
84
    }
85

86
    /**
87
     * Get the network at the given position.
88
     * @param world The world.
89
     * @param pos The position.
90
     * @param side The side.
91
     * @return The optional network.
92
     */
93
    public static Optional<INetwork> getNetwork(ILevelExtension world, BlockPos pos, @Nullable Direction side) {
94
        Optional<Optional<INetwork>> networkCarried = getNetworkCarrier(world, pos, side)
5✔
95
                .map(carrier -> {
2✔
96
                    INetwork network = carrier.getNetwork();
3✔
97
                    return network != null ? Optional.of(network) : Optional.empty();
7✔
98
                });
99
        return networkCarried.orElse(Optional.empty());
5✔
100
    }
101

102
    /**
103
     * Get the network at the given position.
104
     * @param pos The position.
105
     * @return The optional network.
106
     */
107
    public static Optional<INetwork> getNetwork(PartPos pos) {
108
        return getNetwork(pos.getPos().getLevel(true), pos.getPos().getBlockPos(), pos.getSide());
11✔
109
    }
110

111
    /**
112
     * Get the network at the given position.
113
     * If it is not present, then an illegal state exception will be thrown.
114
     *
115
     * This should only be called if you know for certain that there will be a network present.
116
     *
117
     * @param world The world.
118
     * @param pos The position.
119
     * @param side The side.
120
     * @return The network.
121
     */
122
    public static INetwork getNetworkChecked(ILevelExtension world, BlockPos pos, @Nullable Direction side) {
123
        return getNetwork(world, pos, side)
7✔
124
                .orElseThrow(() -> new IllegalStateException("Could not find a network container at " + pos.toString()));
2✔
125
    }
126

127
    /**
128
     * Get the network at the given position.
129
     * If it is not present, then an illegal state exception will be thrown.
130
     *
131
     * This should only be called if you know for certain that there will be a network present.
132
     *
133
     * @param pos The position.
134
     * @return The network.
135
     */
136
    public static INetwork getNetworkChecked(PartPos pos) {
137
        return getNetwork(pos)
×
138
                .orElseThrow(() -> new IllegalStateException("Could not find a network container at " + pos.toString()));
×
139
    }
140

141
    /**
142
     * Get the part network capability of a network.
143
     * @param optionalNetwork The optional network.
144
     * @return The optional part network.
145
     */
146
    public static Optional<IPartNetwork> getPartNetwork(Optional<INetwork> optionalNetwork) {
147
        return optionalNetwork
×
148
                .map(network -> network.getCapability(Capabilities.PartNetwork.NETWORK))
×
149
                .orElse(Optional.empty());
×
150
    }
151

152
    /**
153
     * Get the part network capability of a network.
154
     * @param network The network.
155
     * @return The optional part network.
156
     */
157
    public static Optional<IPartNetwork> getPartNetwork(@Nullable INetwork network) {
158
        if (network == null) {
2✔
159
            return Optional.empty();
2✔
160
        }
161
        return network.getCapability(Capabilities.PartNetwork.NETWORK);
4✔
162
    }
163

164
    /**
165
     * Get the part network capability of a network.
166
     * If it is not present, then an illegal state exception will be thrown.
167
     *
168
     * This should only be called if you know for certain that there will be a part network present.
169
     *
170
     * @param network The network.
171
     * @return The part network.
172
     */
173
    public static IPartNetwork getPartNetworkChecked(INetwork network) {
174
        return network.getCapability(Capabilities.PartNetwork.NETWORK)
5✔
175
                .orElseThrow(() -> new IllegalStateException("Could not find a network's part network"));
2✔
176
    }
177

178
    /**
179
     * Get the part network capability of a network.
180
     * @param optionalNetwork The optional network.
181
     * @return The optional energy network.
182
     */
183
    public static Optional<IEnergyNetwork> getEnergyNetwork(Optional<INetwork> optionalNetwork) {
184
        return optionalNetwork
×
185
                .map(network -> network.getCapability(Capabilities.EnergyNetwork.NETWORK))
×
186
                .orElse(Optional.empty());
×
187
    }
188

189
    /**
190
     * Get the part network capability of a network.
191
     * @param network The network.
192
     * @return The optional energy network.
193
     */
194
    public static Optional<IEnergyNetwork> getEnergyNetwork(@Nullable INetwork network) {
195
        if (network == null) {
2!
196
            return Optional.empty();
×
197
        }
198
        return network.getCapability(Capabilities.EnergyNetwork.NETWORK);
4✔
199
    }
200

201
    /**
202
     * Get the part network capability of a network.
203
     * @param network The network.
204
     * @return The energy network.
205
     */
206
    public static IEnergyNetwork getEnergyNetworkChecked(INetwork network) {
207
        return network.getCapability(Capabilities.EnergyNetwork.NETWORK)
5✔
208
                .orElseThrow(() -> new IllegalStateException("Could not find a network's energy network"));
2✔
209
    }
210

211
    /**
212
     * Get the ingredient network within a network.
213
     * @param optionalNetwork The optional network.
214
     * @param ingredientComponent The ingredient component type.
215
     * @param <T> The instance type.
216
     * @param <M> The matching condition parameter.
217
     * @return The optional ingredient network.
218
     */
219
    public static <T, M> Optional<IPositionedAddonsNetworkIngredients<T, M>> getIngredientNetwork(Optional<INetwork> optionalNetwork,
220
                                                                                                      IngredientComponent<T, M> ingredientComponent) {
221
        return optionalNetwork
×
222
                .map(network -> ingredientComponent.getCapability(Capabilities.PositionedAddonsNetworkIngredientsHandler.INGREDIENT)
×
223
                        .map(handler -> handler.getStorage(network))
×
224
                        .orElse(Optional.empty()))
×
225
                .orElse(Optional.empty());
×
226
    }
227

228
    /**
229
     * Form a new network starting from the given position.
230
     * This position should have a {@link IPathElement} capability,
231
     * otherwise this method will fail silently.
232
     * This will correctly transfer all passed network elements to this new network.
233
     * @param world The world.
234
     * @param pos The starting position.
235
     * @param side The side.
236
     * @return The optionally created part network.
237
     * Can be absent if the starting position did not have a {@link IPathElement} capability.
238
     */
239
    public static Optional<INetwork> initNetwork(ILevelExtension world, BlockPos pos, @Nullable Direction side) {
240
        return IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, side, Capabilities.PathElement.BLOCK)
10✔
241
                .map(pathElement -> {
1✔
242
                    Network network = Network.initiateNetworkSetup(SidedPathElement.of(pathElement, side));
5✔
243
                    network.initialize();
2✔
244
                    return Optional.<INetwork>of(network);
3✔
245
                })
246
                .orElse(Optional.empty());
3✔
247
    }
248

249
    /**
250
     * This MUST be called by blocks having the {@link INetworkElementProvider} capability in
251
     * when a neighbouring block is updated, more specifically when
252
     * {@link Block#neighborChanged(BlockState, Level, BlockPos, Block, Orientation, boolean)},
253
     * {@link Block#onNeighborChange(BlockState, LevelReader, BlockPos, BlockPos)}
254
     * or {@link Block#updateShape(BlockState, LevelReader, ScheduledTickAccess, BlockPos, Direction, BlockPos, BlockState, RandomSource)} is called.
255
     *
256
     * @param world The world in which the neighbour was updated.
257
     * @param pos   The position of the center block.
258
     * @param side  The side at the center block.
259
     */
260
    public static void onElementProviderBlockNeighborChange(Level world, BlockPos pos,
261
                                                            @Nullable Direction side) {
262
        if (!world.isClientSide()) {
3!
263
            getNetwork(world, pos, side).ifPresent(network -> {
9✔
264
                getNetworkElementProvider(world, pos, side).ifPresent(networkElementProvider -> {
9✔
265
                    for (INetworkElement networkElement : networkElementProvider.createNetworkElements(world, pos)) {
13✔
266
                        networkElement.onNeighborBlockChange(network, world);
4✔
267
                    }
1✔
268
                });
1✔
269
            });
1✔
270
        }
271
    }
1✔
272

273
    /**
274
     * @return If networks should work and evaluations should be done.
275
     */
276
    public static boolean shouldWork() {
277
        return !GeneralConfig.safeMode;
5!
278
    }
279

280
    /**
281
     * Invalidate all network elements at the given position.
282
     * Warning: this assumes unsided network carrier capabilities, for example full-block network elements.
283
     * @param world The world.
284
     * @param pos The position.
285
     * @param network The network.
286
     * @param networkElementProvider The network element provider.
287
     */
288
    public static void invalidateNetworkElements(Level world, BlockPos pos, INetwork network, INetworkElementProvider networkElementProvider) {
289
        for (INetworkElement networkElement : networkElementProvider.createNetworkElements(world, pos)) {
13✔
290
            networkElement.invalidate(network);
3✔
291
        }
1✔
292
    }
1✔
293

294
    /**
295
     * Revalidate all network elements at the given position.
296
     * Warning: this assumes unsided network carrier capabilities, for example full-block network elements.
297
     * @param world The world.
298
     * @param pos The position.
299
     */
300
    public static void revalidateNetworkElements(Level world, BlockPos pos) {
301
        INetworkCarrier networkCarrier = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, Capabilities.NetworkCarrier.BLOCK).orElse(null);
10✔
302
        IPathElement pathElement = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, Capabilities.PathElement.BLOCK).orElse(null);
10✔
303
        if (TickHandler.getInstance().ticked
8!
304
                && networkCarrier != null && pathElement != null && networkCarrier.getNetwork() == null
2✔
305
                && IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, Capabilities.CableFakeable.BLOCK).map(ICableFakeable::isRealCable).orElse(false)) {
14!
306
            IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, Capabilities.NetworkElementProvider.BLOCK).ifPresent(networkElementProvider -> {
×
307
                // Attempt to revalidate the network elements in this provider
308
                boolean foundNetwork = false;
×
309
                for (INetwork network : NetworkWorldStorage.Access.getInstance(IntegratedDynamics._instance).get().getNetworks()) {
×
310
                    if (network.containsSidedPathElement(SidedPathElement.of(pathElement, null))) {
×
311
                        // Revalidate all network elements
312
                        for (INetworkElement networkElement : networkElementProvider.createNetworkElements(world, pos)) {
×
313
                            networkElement.revalidate(network);
×
314
                        }
×
315
                        foundNetwork = true;
×
316
                        break; // No need to check the other networks anymore
×
317
                    }
318
                }
×
319

320
                // If no existing network was found, create a new network
321
                if (!foundNetwork && GeneralConfig.recreateCorruptedNetworks) {
×
322
                    IntegratedDynamics.clog(org.apache.logging.log4j.Level.WARN, String.format("Detected network position at " +
×
323
                            "position %s in world %s with corrupted network, recreating network...", pos, world.dimension().location()));
×
324
                    NetworkHelpers.initNetwork(world, pos, null);
×
325
                }
326
            });
×
327
        }
328
    }
1✔
329

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