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

CyclopsMC / IntegratedDynamics / 22180797954

19 Feb 2026 11:56AM UTC coverage: 44.45% (+0.02%) from 44.431%
22180797954

push

github

rubensworks
Rename performance benchmarks

2658 of 8846 branches covered (30.05%)

Branch coverage included in aggregate %.

12014 of 24162 relevant lines covered (49.72%)

2.36 hits per line

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

4.07
/src/main/java/org/cyclops/integrateddynamics/command/CommandGenerateNetwork.java
1
package org.cyclops.integrateddynamics.command;
2

3
import com.mojang.brigadier.Command;
4
import com.mojang.brigadier.arguments.IntegerArgumentType;
5
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
6
import com.mojang.brigadier.context.CommandContext;
7
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8
import net.minecraft.ChatFormatting;
9
import net.minecraft.commands.CommandSourceStack;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.core.BlockPos;
12
import net.minecraft.core.Direction;
13
import net.minecraft.network.chat.Component;
14
import net.minecraft.server.level.ServerLevel;
15
import net.minecraft.world.item.ItemStack;
16
import org.cyclops.cyclopscore.command.argument.ArgumentTypeEnum;
17
import org.cyclops.integrateddynamics.RegistryEntries;
18
import org.cyclops.integrateddynamics.api.part.IPartType;
19
import org.cyclops.integrateddynamics.api.part.PartPos;
20
import org.cyclops.integrateddynamics.block.BlockCable;
21
import org.cyclops.integrateddynamics.blockentity.BlockEntityVariablestore;
22
import org.cyclops.integrateddynamics.core.evaluate.operator.Operators;
23
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypeInteger;
24
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
25
import org.cyclops.integrateddynamics.core.helper.CableHelpers;
26
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
27
import org.cyclops.integrateddynamics.core.helper.PartHelpers;
28
import org.cyclops.integrateddynamics.core.part.PartTypeRegistry;
29
import org.cyclops.integrateddynamics.core.part.PartTypes;
30
import org.cyclops.integrateddynamics.gametest.GameTestHelpersIntegratedDynamics;
31
import org.cyclops.integrateddynamics.part.aspect.Aspects;
32
import org.cyclops.integrateddynamics.part.aspect.read.AspectReadBuilders;
33

34
import java.util.ArrayList;
35
import java.util.List;
36
import java.util.Random;
37

38
/**
39
 * Command for generating networks with different presets.
40
 * @author rubensworks
41
 */
42
public class CommandGenerateNetwork implements Command<CommandSourceStack> {
×
43

44
    public static LiteralArgumentBuilder<CommandSourceStack> make() {
45
        LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal("generatenetwork")
3✔
46
                .requires((commandSource) -> commandSource.hasPermission(2));
3✔
47

48
        // Add the preset subcommand with optional size/radius argument
49
        builder.then(Commands.argument("preset", new ArgumentTypeEnum(NetworkPreset.class))
14✔
50
                .executes(new CommandGenerateNetworkExecutor(true, false))
4✔
51
                .then(Commands.argument("size", IntegerArgumentType.integer(1, 1000))
8✔
52
                        .executes(new CommandGenerateNetworkExecutor(true, true))));
1✔
53

54
        return builder;
2✔
55
    }
56

57
    @Override
58
    public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
59
        context.getSource().sendFailure(Component.literal("Please specify a preset: empty, idle, redstoneioclock, or clear")
×
60
                .withStyle(ChatFormatting.RED));
×
61
        return 0;
×
62
    }
63

64
    public enum NetworkPreset {
×
65
        EMPTY,
×
66
        IDLE,
×
67
        REDSTONEIOCLOCK,
×
68
        REDSTONEIOCLOCKVARIABLES,
×
69
        CLEAR
×
70
    }
71

72
    /**
73
     * Executor for the generatenetwork command.
74
     */
75
    public static class CommandGenerateNetworkExecutor implements Command<CommandSourceStack> {
76
        private final boolean hasPreset;
77
        private final boolean hasSize;
78

79
        public CommandGenerateNetworkExecutor(boolean hasPreset, boolean hasSize) {
2✔
80
            this.hasPreset = hasPreset;
3✔
81
            this.hasSize = hasSize;
3✔
82
        }
1✔
83

84
        @Override
85
        public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
86
            if (!hasPreset) {
×
87
                context.getSource().sendFailure(Component.literal("Please specify a preset: empty, idle, redstoneioclock, or clear")
×
88
                        .withStyle(ChatFormatting.RED));
×
89
                return 0;
×
90
            }
91

92
            NetworkPreset preset = ArgumentTypeEnum.getValue(context, "preset", NetworkPreset.class);
×
93
            ServerLevel level = context.getSource().getLevel();
×
94
            BlockPos playerPos = BlockPos.containing(context.getSource().getPosition());
×
95
            int size = hasSize ? IntegerArgumentType.getInteger(context, "size") : getDefaultSize(preset);
×
96

97
            switch (preset) {
×
98
                case EMPTY:
99
                    context.getSource().sendSuccess(
×
100
                            () -> Component.literal("Generating network preset: empty (size: " + size + "x" + size + "x" + size + ")")
×
101
                                    .withStyle(ChatFormatting.GREEN),
×
102
                            true);
103
                    NetworkGenerationHelper.generateEmptyNetwork(level, playerPos.above(2), size);
×
104
                    break;
×
105
                case IDLE:
106
                    context.getSource().sendSuccess(
×
107
                            () -> Component.literal("Generating network preset: idle (size: " + size + "x" + size + "x" + size + ")")
×
108
                                    .withStyle(ChatFormatting.GREEN),
×
109
                            true);
110
                    NetworkGenerationHelper.generateIdleNetwork(level, playerPos.above(2), size);
×
111
                    break;
×
112
                case REDSTONEIOCLOCK:
113
                    context.getSource().sendSuccess(
×
114
                            () -> Component.literal("Generating network preset: redstoneioclock (size: " + size + "x" + size + "x" + size + ")")
×
115
                                    .withStyle(ChatFormatting.GREEN),
×
116
                            true);
117
                    NetworkGenerationHelper.generateRedstoneNetwork(level, playerPos.above(2), size);
×
118
                    break;
×
119
                case REDSTONEIOCLOCKVARIABLES:
120
                    context.getSource().sendSuccess(
×
121
                            () -> Component.literal("Generating network preset: redstoneioclockvariables (size: " + size + "x" + size + "x" + size + ")")
×
122
                                    .withStyle(ChatFormatting.GREEN),
×
123
                            true);
124
                    NetworkGenerationHelper.generateRedstoneNetworkVariables(level, playerPos.above(2), size);
×
125
                    break;
×
126
                case CLEAR:
127
                    context.getSource().sendSuccess(
×
128
                            () -> Component.literal("Clearing cables within radius: " + size)
×
129
                                    .withStyle(ChatFormatting.GREEN),
×
130
                            true);
131
                    NetworkGenerationHelper.clearCables(level, playerPos, size);
×
132
                    break;
133
            }
134

135
            return 1;
×
136
        }
137

138
        /**
139
         * Get the default size/radius for the given preset.
140
         */
141
        private int getDefaultSize(NetworkPreset preset) {
142
            return preset == NetworkPreset.CLEAR ? 50 : 25;
×
143
        }
144
    }
145

146
    /**
147
     * Helper class for network generation logic, shared between command and game tests.
148
     */
149
    public static class NetworkGenerationHelper {
×
150
        /**
151
         * Generate a size x size x size cube of only logic cables.
152
         */
153
        public static void generateEmptyNetwork(ServerLevel level, BlockPos startPos, int size) {
154
            List<BlockPos> placedPositions = new ArrayList<>();
×
155

156
            BlockCable.SKIP_NETWORK_INIT = true;
×
157
            try {
158
                for (int x = 0; x < size; x++) {
×
159
                    for (int y = 0; y < size; y++) {
×
160
                        for (int z = 0; z < size; z++) {
×
161
                            BlockPos pos = startPos.offset(x, y, z);
×
162
                            level.setBlock(pos, RegistryEntries.BLOCK_CABLE.value().defaultBlockState(), 2);
×
163
                            placedPositions.add(pos);
×
164
                        }
165
                    }
166
                }
167
            } finally {
168
                BlockCable.SKIP_NETWORK_INIT = false;
×
169
            }
170

171
            for (BlockPos pos : placedPositions) {
×
172
                CableHelpers.updateConnectionsNeighbours(level, pos, CableHelpers.ALL_SIDES);
×
173
            }
×
174

175
            NetworkHelpers.initNetwork(level, startPos, null);
×
176
        }
×
177

178
        /**
179
         * Generate a size x size x size cube of logic cables where all cables on the outer sides
180
         * contain random parts facing outwards.
181
         */
182
        public static void generateIdleNetwork(ServerLevel level, BlockPos startPos, int size) {
183
            generateEmptyNetwork(level, startPos, size);
×
184

185
            Random random = new Random();
×
186
            List<BlockPos> updatePositions = new ArrayList<>();
×
187

188
            addPartsToFace(level, startPos, size, 0, size - 1, size - 1, size - 1, 0, size - 1, Direction.UP, random, updatePositions);
×
189
            addPartsToFace(level, startPos, size, 0, size - 1, 0, 0, 0, size - 1, Direction.DOWN, random, updatePositions);
×
190
            addPartsToFace(level, startPos, size, 0, size - 1, 0, size - 1, 0, 0, Direction.NORTH, random, updatePositions);
×
191
            addPartsToFace(level, startPos, size, 0, size - 1, 0, size - 1, size - 1, size - 1, Direction.SOUTH, random, updatePositions);
×
192
            addPartsToFace(level, startPos, size, 0, 0, 0, size - 1, 0, size - 1, Direction.WEST, random, updatePositions);
×
193
            addPartsToFace(level, startPos, size, size - 1, size - 1, 0, size - 1, 0, size - 1, Direction.EAST, random, updatePositions);
×
194

195
            for (BlockPos pos : updatePositions) {
×
196
                level.blockUpdated(pos, RegistryEntries.BLOCK_CABLE.value());
×
197
            }
×
198
        }
×
199

200
        /**
201
         * Place a single cable block at the given position.
202
         */
203
        public static void placeCable(ServerLevel level, BlockPos pos) {
204
            level.setBlock(pos, RegistryEntries.BLOCK_CABLE.value().defaultBlockState(), 2);
×
205
        }
×
206

207
        /**
208
         * Clear all cable blocks within a radius of the given position.
209
         */
210
        public static void clearCables(ServerLevel level, BlockPos centerPos, int radius) {
211
            BlockCable.SKIP_NETWORK_INIT = true;
×
212

213
            try {
214
                for (int x = centerPos.getX() - radius; x <= centerPos.getX() + radius; x++) {
×
215
                    for (int y = centerPos.getY() - radius; y <= centerPos.getY() + radius; y++) {
×
216
                        for (int z = centerPos.getZ() - radius; z <= centerPos.getZ() + radius; z++) {
×
217
                            BlockPos pos = new BlockPos(x, y, z);
×
218
                            if (level.getBlockState(pos).getBlock() == RegistryEntries.BLOCK_CABLE.value()) {
×
219
                                level.destroyBlock(pos, false);
×
220
                            }
221
                        }
222
                    }
223
                }
224
            } finally {
225
                BlockCable.SKIP_NETWORK_INIT = false;
×
226
            }
227
        }
×
228

229
        private static void addPartsToFace(ServerLevel level, BlockPos startPos, int size,
230
                                          int minX, int maxX, int minY, int maxY, int minZ, int maxZ,
231
                                          Direction side, Random random, List<BlockPos> updatePositions) {
232
            for (int x = minX; x <= maxX; x++) {
×
233
                for (int y = minY; y <= maxY; y++) {
×
234
                    for (int z = minZ; z <= maxZ; z++) {
×
235
                        BlockPos pos = startPos.offset(x, y, z);
×
236
                        addRandomPartDeferred(level, pos, side, random, updatePositions);
×
237
                    }
238
                }
239
            }
240
        }
×
241

242
        private static void addRandomPartDeferred(ServerLevel level, BlockPos pos, Direction side, Random random, List<BlockPos> updatePositions) {
243
            List<IPartType> partTypes = new ArrayList<>(PartTypeRegistry.getInstance().getPartTypes());
×
244

245
            if (partTypes.isEmpty()) {
×
246
                return;
×
247
            }
248

249
            IPartType partType = partTypes.get(random.nextInt(partTypes.size()));
×
250
            ItemStack itemStack = new ItemStack(partType.getItem());
×
251
            PartHelpers.addPart(level, pos, side, partType, itemStack);
×
252
            updatePositions.add(pos);
×
253
        }
×
254

255
        /**
256
         * Generate a size x size x size cube of logic cables where all cables on the EAST side
257
         * contain redstone readers, and all cables on the WEST side contain redstone writers.
258
         * For each reader-writer pair at the same Y and Z coordinates, a variable is created
259
         * that reads the BOOLEAN_CLOCK aspect from the reader and connects it to the
260
         * BOOLEAN aspect of the writer at the opposite side.
261
         */
262
        public static void generateRedstoneNetwork(ServerLevel level, BlockPos startPos, int size) {
263
            generateEmptyNetwork(level, startPos, size);
×
264

265
            List<BlockPos> updatePositions = new ArrayList<>();
×
266

267
            // Add redstone readers to EAST side and redstone writers to WEST side
268
            // EAST side is at x = size - 1, WEST side is at x = 0
269
            for (int y = 0; y < size; y++) {
×
270
                for (int z = 0; z < size; z++) {
×
271
                    // EAST side: redstone reader
272
                    BlockPos eastPos = startPos.offset(size - 1, y, z);
×
273
                    PartHelpers.addPart(level, eastPos, Direction.EAST, PartTypes.REDSTONE_READER, new ItemStack(PartTypes.REDSTONE_READER.getItem()));
×
274
                    updatePositions.add(eastPos);
×
275

276
                    // WEST side: redstone writer
277
                    BlockPos westPos = startPos.offset(0, y, z);
×
278
                    PartHelpers.addPart(level, westPos, Direction.WEST, PartTypes.REDSTONE_WRITER, new ItemStack(PartTypes.REDSTONE_WRITER.getItem()));
×
279
                    updatePositions.add(westPos);
×
280
                }
281
            }
282

283
            // Update all positions and create variable connections
284
            for (BlockPos pos : updatePositions) {
×
285
                level.blockUpdated(pos, RegistryEntries.BLOCK_CABLE.value());
×
286
            }
×
287

288
            // Create variables connecting readers to writers
289
            for (int y = 0; y < size; y++) {
×
290
                for (int z = 0; z < size; z++) {
×
291
                    BlockPos eastPos = startPos.offset(size - 1, y, z);
×
292
                    BlockPos westPos = startPos.offset(0, y, z);
×
293

294
                    // Create variable from reader's BOOLEAN_CLOCK aspect
295
                    org.cyclops.integrateddynamics.api.part.PartPos eastPartPos = org.cyclops.integrateddynamics.api.part.PartPos.of(level, eastPos, Direction.EAST);
×
296
                    PartHelpers.PartStateHolder<?, ?> eastPartStateHolder = PartHelpers.getPart(eastPartPos);
×
297
                    if (eastPartStateHolder != null) {
×
298
                        ItemStack variableCard = GameTestHelpersIntegratedDynamics.createVariableFromReader(level,
×
299
                                Aspects.Read.Redstone.BOOLEAN_CLOCK, eastPartStateHolder.getState());
×
300

301
                        // Place variable in writer's BOOLEAN aspect
302
                        org.cyclops.integrateddynamics.api.part.PartPos westPartPos = org.cyclops.integrateddynamics.api.part.PartPos.of(level, westPos, Direction.WEST);
×
303
                        GameTestHelpersIntegratedDynamics.placeVariableInWriter(level, westPartPos,
×
304
                                Aspects.Write.Redstone.BOOLEAN, variableCard);
305
                    }
306
                }
307
            }
308
        }
×
309

310
        /**
311
         * Generate a size x size x size cube of logic cables where all cables on the EAST side
312
         * contain redstone readers, and all cables on the WEST side contain redstone writers.
313
         * For each reader-writer pair at the same Y and Z coordinates, a CHOICE operator is created
314
         * that reads the BOOLEAN_CLOCK aspect from the reader and chooses between constants 0 and 10.
315
         * The result is written to the INTEGER aspect of the writer.
316
         * Variable cards are stored in variable store blocks placed on the SOUTH side of the network,
317
         * stacked vertically. Each variable store can hold multiple CHOICE operator configurations.
318
         * All redstone readers have PROPERTY_LENGTH set to 10.
319
         */
320
        public static void generateRedstoneNetworkVariables(ServerLevel level, BlockPos startPos, int size) {
321
            generateEmptyNetwork(level, startPos, size);
×
322

323
            List<BlockPos> updatePositions = new ArrayList<>();
×
324

325
            // Add redstone readers to EAST side and redstone writers to WEST side
326
            for (int y = 0; y < size; y++) {
×
327
                for (int z = 0; z < size; z++) {
×
328
                    // EAST side: redstone reader
329
                    BlockPos eastPos = startPos.offset(size - 1, y, z);
×
330
                    PartHelpers.addPart(level, eastPos, Direction.EAST, PartTypes.REDSTONE_READER, new ItemStack(PartTypes.REDSTONE_READER.getItem()));
×
331
                    updatePositions.add(eastPos);
×
332

333
                    // WEST side: redstone writer
334
                    BlockPos westPos = startPos.offset(0, y, z);
×
335
                    PartHelpers.addPart(level, westPos, Direction.WEST, PartTypes.REDSTONE_WRITER, new ItemStack(PartTypes.REDSTONE_WRITER.getItem()));
×
336
                    updatePositions.add(westPos);
×
337
                }
338
            }
339

340
            // Update all positions
341
            for (BlockPos pos : updatePositions) {
×
342
                level.blockUpdated(pos, RegistryEntries.BLOCK_CABLE.value());
×
343
            }
×
344

345
            // Create variable stores on the SOUTH side of the network
346
            // Place stores at (z = size, y varying) stacked vertically
347
            // Each store can hold 4 items: clock variable, constant 0, constant 10, and choice operator
348
            int storeX = startPos.getX(); // Aligned with the network
×
349
            int storeZ = startPos.getZ() + size; // SOUTH side
×
350
            int currentStoreY = startPos.getY();
×
351
            int currentSlot = 0;
×
352
            BlockEntityVariablestore currentVariableStore = null;
×
353
            BlockPos currentStorePos = null;
×
354

355
            // Create variables connecting readers to writers using CHOICE operator
356
            for (int y = 0; y < size; y++) {
×
357
                for (int z = 0; z < size; z++) {
×
358
                    BlockPos eastPos = startPos.offset(size - 1, y, z);
×
359
                    BlockPos westPos = startPos.offset(0, y, z);
×
360

361
                    // Get or create a new variable store if current one is full
362
                    if (currentVariableStore == null || currentSlot >= BlockEntityVariablestore.INVENTORY_SIZE) {
×
363
                        if (currentSlot >= BlockEntityVariablestore.INVENTORY_SIZE) {
×
364
                            // Current store is full, move to next store (stack vertically)
365
                            currentStoreY++;
×
366
                        }
367

368
                        currentStorePos = new BlockPos(storeX, currentStoreY, storeZ);
×
369
                        level.setBlock(currentStorePos, RegistryEntries.BLOCK_VARIABLE_STORE.get().defaultBlockState(), 2);
×
370
                        currentVariableStore = (BlockEntityVariablestore) level.getBlockEntity(currentStorePos);
×
371
                        currentSlot = 0;
×
372
                    }
373

374
                    if (currentVariableStore != null) {
×
375
                        // Create variable from reader's BOOLEAN_CLOCK aspect
376
                        org.cyclops.integrateddynamics.api.part.PartPos eastPartPos = org.cyclops.integrateddynamics.api.part.PartPos.of(level, eastPos, Direction.EAST);
×
377
                        PartHelpers.PartStateHolder<?, ?> eastPartStateHolder = PartHelpers.getPart(eastPartPos);
×
378

379
                        if (eastPartStateHolder != null) {
×
380
                            // Create constant integer variables (0 and 10) - reuse from first slot if already created
381
                            ItemStack variable0, variable10;
382
                            int variable0Id, variable10Id;
383

384
                            int currentSlotIncrement;
385
                            if (currentSlot == 0) {
×
386
                                // First time, create and store constants
387
                                variable0 = GameTestHelpersIntegratedDynamics.createVariableForValue(level, ValueTypes.INTEGER, ValueTypeInteger.ValueInteger.of(0));
×
388
                                variable10 = GameTestHelpersIntegratedDynamics.createVariableForValue(level, ValueTypes.INTEGER, ValueTypeInteger.ValueInteger.of(10));
×
389
                                currentVariableStore.getInventory().setItem(1, variable0);
×
390
                                currentVariableStore.getInventory().setItem(2, variable10);
×
391
                                variable0Id = GameTestHelpersIntegratedDynamics.getVariableFacade(level, variable0).getId();
×
392
                                variable10Id = GameTestHelpersIntegratedDynamics.getVariableFacade(level, variable10).getId();
×
393
                                currentSlotIncrement = 4;
×
394
                            } else {
395
                                // Reuse constants from slots 1 and 2
396
                                variable0Id = GameTestHelpersIntegratedDynamics.getVariableFacade(level, currentVariableStore.getInventory().getItem(1)).getId();
×
397
                                variable10Id = GameTestHelpersIntegratedDynamics.getVariableFacade(level, currentVariableStore.getInventory().getItem(2)).getId();
×
398
                                currentSlotIncrement = 2;
×
399
                            }
400

401
                            // Create variable from reader's BOOLEAN_CLOCK aspect
402
                            ItemStack variableClock = GameTestHelpersIntegratedDynamics.createVariableFromReader(level,
×
403
                                    Aspects.Read.Redstone.BOOLEAN_CLOCK, eastPartStateHolder.getState());
×
404
                            currentVariableStore.getInventory().setItem(currentSlot, variableClock);
×
405

406
                            // Create CHOICE operator variable
407
                            ItemStack variableChoice = GameTestHelpersIntegratedDynamics.createVariableForOperator(level, Operators.GENERAL_CHOICE, new int[]{
×
408
                                    GameTestHelpersIntegratedDynamics.getVariableFacade(level, variableClock).getId(),
×
409
                                    variable0Id,
410
                                    variable10Id
411
                            });
412
                            currentVariableStore.getInventory().setItem(currentSlot + currentSlotIncrement - 1, variableChoice);
×
413

414
                            // Place CHOICE variable in writer's INTEGER aspect
415
                            org.cyclops.integrateddynamics.api.part.PartPos westPartPos = org.cyclops.integrateddynamics.api.part.PartPos.of(level, westPos, Direction.WEST);
×
416
                            GameTestHelpersIntegratedDynamics.placeVariableInWriter(level, westPartPos,
×
417
                                    Aspects.Write.Redstone.INTEGER, variableChoice);
418

419
                            currentSlot += currentSlotIncrement;
×
420
                        }
421
                    }
422
                }
423
            }
424

425
            // Set PROPERTY_LENGTH to 10 for all redstone readers
426
            for (int y = 0; y < size; y++) {
×
427
                for (int z = 0; z < size; z++) {
×
428
                    BlockPos eastPos = startPos.offset(size - 1, y, z);
×
429
                    PartPos eastPartPos = PartPos.of(level, eastPos, Direction.EAST);
×
430
                    GameTestHelpersIntegratedDynamics.setAspectProperty(eastPartPos, Aspects.Read.Redstone.BOOLEAN_CLOCK, AspectReadBuilders.Redstone.PROPERTY_LENGTH, ValueTypeInteger.ValueInteger.of(10));
×
431
                }
432
            }
433
        }
×
434
    }
435
}
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