• 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

0.0
/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);
×
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));
×
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);
×
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));
×
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)
×
95
                .map(carrier -> {
×
96
                    INetwork network = carrier.getNetwork();
×
97
                    return network != null ? Optional.of(network) : Optional.empty();
×
98
                });
99
        return networkCarried.orElse(Optional.empty());
×
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());
×
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)
×
124
                .orElseThrow(() -> new IllegalStateException("Could not find a network container at " + pos.toString()));
×
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) {
×
159
            return Optional.empty();
×
160
        }
161
        return network.getCapability(Capabilities.PartNetwork.NETWORK);
×
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)
×
175
                .orElseThrow(() -> new IllegalStateException("Could not find a network's part network"));
×
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) {
×
196
            return Optional.empty();
×
197
        }
198
        return network.getCapability(Capabilities.EnergyNetwork.NETWORK);
×
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)
×
208
                .orElseThrow(() -> new IllegalStateException("Could not find a network's energy network"));
×
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)
×
241
                .map(pathElement -> {
×
242
                    Network network = Network.initiateNetworkSetup(SidedPathElement.of(pathElement, side));
×
243
                    network.initialize();
×
244
                    return Optional.<INetwork>of(network);
×
245
                })
246
                .orElse(Optional.empty());
×
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()) {
×
263
            getNetwork(world, pos, side).ifPresent(network -> {
×
264
                getNetworkElementProvider(world, pos, side).ifPresent(networkElementProvider -> {
×
265
                    for (INetworkElement networkElement : networkElementProvider.createNetworkElements(world, pos)) {
×
266
                        networkElement.onNeighborBlockChange(network, world, side);
×
267
                    }
×
268
                });
×
269
            });
×
270
        }
271
    }
×
272

273
    /**
274
     * @return If networks should work and evaluations should be done.
275
     */
276
    public static boolean shouldWork() {
277
        return !GeneralConfig.safeMode;
×
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)) {
×
290
            networkElement.invalidate(network);
×
291
        }
×
292
    }
×
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
     * @return If the network was revalidated in this tick.
300
     */
301
    public static boolean revalidateNetworkElements(Level world, BlockPos pos) {
302
        INetworkCarrier networkCarrier = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, Capabilities.NetworkCarrier.BLOCK).orElse(null);
×
303
        IPathElement pathElement = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, Capabilities.PathElement.BLOCK).orElse(null);
×
304
        if (TickHandler.getInstance().ticked
×
305
                && networkCarrier != null && pathElement != null && networkCarrier.getNetwork() == null
×
306
                && IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, Capabilities.CableFakeable.BLOCK).map(ICableFakeable::isRealCable).orElse(true)) {
×
307
            return IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(world, pos, Capabilities.NetworkElementProvider.BLOCK).map(networkElementProvider -> {
×
308
                // Attempt to revalidate the network elements in this provider
309
                boolean foundNetwork = false;
×
310
                for (INetwork network : NetworkWorldStorage.Access.getInstance(IntegratedDynamics._instance).get().getNetworks()) {
×
311
                    if (network.containsSidedPathElement(SidedPathElement.of(pathElement, null))) {
×
312
                        // Revalidate all network elements
313
                        for (INetworkElement networkElement : networkElementProvider.createNetworkElements(world, pos)) {
×
314
                            networkElement.revalidate(network);
×
315
                        }
×
316
                        foundNetwork = true;
×
317
                        break; // No need to check the other networks anymore
×
318
                    }
319
                }
×
320

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

329
                return foundNetwork;
×
330
            }).orElse(false);
×
331
        }
332
        return false;
×
333
    }
334

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