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

CyclopsMC / IntegratedDynamics / 22140355816

18 Feb 2026 12:48PM UTC coverage: 44.46% (-0.2%) from 44.674%
22140355816

push

github

rubensworks
Add redstone io clocks performance test with variables

2661 of 8844 branches covered (30.09%)

Branch coverage included in aggregate %.

12014 of 24163 relevant lines covered (49.72%)

4.72 hits per line

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

4.0
/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")
6✔
46
                .requires((commandSource) -> commandSource.hasPermission(2));
6✔
47

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

54
        return builder;
4✔
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) {
4✔
80
            this.hasPreset = hasPreset;
6✔
81
            this.hasSize = hasSize;
6✔
82
        }
2✔
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
         * Clear all cable blocks within a radius of the given position.
202
         */
203
        public static void clearCables(ServerLevel level, BlockPos centerPos, int radius) {
204
            int radiusSquared = radius * radius;
×
205

206
            BlockCable.SKIP_NETWORK_INIT = true;
×
207

208
            try {
209
                for (int x = centerPos.getX() - radius; x <= centerPos.getX() + radius; x++) {
×
210
                    for (int y = centerPos.getY() - radius; y <= centerPos.getY() + radius; y++) {
×
211
                        for (int z = centerPos.getZ() - radius; z <= centerPos.getZ() + radius; z++) {
×
212
                            BlockPos pos = new BlockPos(x, y, z);
×
213

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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