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

CyclopsMC / IntegratedDynamics / 22132304559

18 Feb 2026 08:31AM UTC coverage: 44.734% (-0.04%) from 44.771%
22132304559

push

github

rubensworks
Run performance tests in CI

2668 of 8804 branches covered (30.3%)

Branch coverage included in aggregate %.

12036 of 24066 relevant lines covered (50.01%)

2.37 hits per line

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

7.97
/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.block.BlockCable;
20
import org.cyclops.integrateddynamics.core.helper.CableHelpers;
21
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
22
import org.cyclops.integrateddynamics.core.helper.PartHelpers;
23
import org.cyclops.integrateddynamics.core.part.PartTypeRegistry;
24

25
import java.util.ArrayList;
26
import java.util.List;
27
import java.util.Random;
28

29
/**
30
 * Command for generating networks with different presets.
31
 * @author rubensworks
32
 */
33
public class CommandGenerateNetwork implements Command<CommandSourceStack> {
×
34

35
    public static LiteralArgumentBuilder<CommandSourceStack> make() {
36
        LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal("generatenetwork")
3✔
37
                .requires((commandSource) -> commandSource.hasPermission(2));
3✔
38

39
        // Add the preset subcommand with optional size/radius argument
40
        builder.then(Commands.argument("preset", new ArgumentTypeEnum(NetworkPreset.class))
14✔
41
                .executes(new CommandGenerateNetworkExecutor(true, false))
4✔
42
                .then(Commands.argument("size", IntegerArgumentType.integer(1, 1000))
8✔
43
                        .executes(new CommandGenerateNetworkExecutor(true, true))));
1✔
44

45
        return builder;
2✔
46
    }
47

48
    @Override
49
    public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
50
        context.getSource().sendFailure(Component.literal("Please specify a preset: emptynetwork, idlenetwork, or clear")
×
51
                .withStyle(ChatFormatting.RED));
×
52
        return 0;
×
53
    }
54

55
    public enum NetworkPreset {
×
56
        EMPTYNETWORK,
×
57
        IDLENETWORK,
×
58
        CLEAR
×
59
    }
60

61
    /**
62
     * Executor for the generatenetwork command.
63
     */
64
    public static class CommandGenerateNetworkExecutor implements Command<CommandSourceStack> {
65
        private final boolean hasPreset;
66
        private final boolean hasSize;
67

68
        public CommandGenerateNetworkExecutor(boolean hasPreset, boolean hasSize) {
2✔
69
            this.hasPreset = hasPreset;
3✔
70
            this.hasSize = hasSize;
3✔
71
        }
1✔
72

73
        @Override
74
        public int run(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
75
            if (!hasPreset) {
×
76
                context.getSource().sendFailure(Component.literal("Please specify a preset: emptynetwork, idlenetwork, or clear")
×
77
                        .withStyle(ChatFormatting.RED));
×
78
                return 0;
×
79
            }
80

81
            NetworkPreset preset = ArgumentTypeEnum.getValue(context, "preset", NetworkPreset.class);
×
82
            ServerLevel level = context.getSource().getLevel();
×
83
            BlockPos playerPos = BlockPos.containing(context.getSource().getPosition());
×
84
            int size = hasSize ? IntegerArgumentType.getInteger(context, "size") : getDefaultSize(preset);
×
85

86
            switch (preset) {
×
87
                case EMPTYNETWORK:
88
                    context.getSource().sendSuccess(
×
89
                            () -> Component.literal("Generating network preset: emptynetwork (size: " + size + "x" + size + "x" + size + ")")
×
90
                                    .withStyle(ChatFormatting.GREEN),
×
91
                            true);
92
                    NetworkGenerationHelper.generateEmptyNetwork(level, playerPos.above(2), size);
×
93
                    break;
×
94
                case IDLENETWORK:
95
                    context.getSource().sendSuccess(
×
96
                            () -> Component.literal("Generating network preset: idlenetwork (size: " + size + "x" + size + "x" + size + ")")
×
97
                                    .withStyle(ChatFormatting.GREEN),
×
98
                            true);
99
                    NetworkGenerationHelper.generateIdleNetwork(level, playerPos.above(2), size);
×
100
                    break;
×
101
                case CLEAR:
102
                    context.getSource().sendSuccess(
×
103
                            () -> Component.literal("Clearing cables within radius: " + size)
×
104
                                    .withStyle(ChatFormatting.GREEN),
×
105
                            true);
106
                    NetworkGenerationHelper.clearCables(level, playerPos, size);
×
107
                    break;
108
            }
109

110
            return 1;
×
111
        }
112

113
        /**
114
         * Get the default size/radius for the given preset.
115
         */
116
        private int getDefaultSize(NetworkPreset preset) {
117
            return preset == NetworkPreset.CLEAR ? 50 : 25;
×
118
        }
119
    }
120

121
    /**
122
     * Helper class for network generation logic, shared between command and game tests.
123
     */
124
    public static class NetworkGenerationHelper {
×
125
        /**
126
         * Generate a size x size x size cube of only logic cables.
127
         */
128
        public static void generateEmptyNetwork(ServerLevel level, BlockPos startPos, int size) {
129
            List<BlockPos> placedPositions = new ArrayList<>();
×
130

131
            BlockCable.SKIP_NETWORK_INIT = true;
×
132
            try {
133
                for (int x = 0; x < size; x++) {
×
134
                    for (int y = 0; y < size; y++) {
×
135
                        for (int z = 0; z < size; z++) {
×
136
                            BlockPos pos = startPos.offset(x, y, z);
×
137
                            level.setBlock(pos, RegistryEntries.BLOCK_CABLE.value().defaultBlockState(), 2);
×
138
                            placedPositions.add(pos);
×
139
                        }
140
                    }
141
                }
142
            } finally {
143
                BlockCable.SKIP_NETWORK_INIT = false;
×
144
            }
145

146
            for (BlockPos pos : placedPositions) {
×
147
                CableHelpers.updateConnectionsNeighbours(level, pos, CableHelpers.ALL_SIDES);
×
148
            }
×
149

150
            NetworkHelpers.initNetwork(level, startPos, null);
×
151
        }
×
152

153
        /**
154
         * Generate a size x size x size cube of logic cables where all cables on the outer sides
155
         * contain random parts facing outwards.
156
         */
157
        public static void generateIdleNetwork(ServerLevel level, BlockPos startPos, int size) {
158
            generateEmptyNetwork(level, startPos, size);
×
159

160
            Random random = new Random();
×
161
            List<BlockPos> updatePositions = new ArrayList<>();
×
162

163
            addPartsToFace(level, startPos, size, 0, size - 1, size - 1, size - 1, 0, size - 1, Direction.UP, random, updatePositions);
×
164
            addPartsToFace(level, startPos, size, 0, size - 1, 0, 0, 0, size - 1, Direction.DOWN, random, updatePositions);
×
165
            addPartsToFace(level, startPos, size, 0, size - 1, 0, size - 1, 0, 0, Direction.NORTH, random, updatePositions);
×
166
            addPartsToFace(level, startPos, size, 0, size - 1, 0, size - 1, size - 1, size - 1, Direction.SOUTH, random, updatePositions);
×
167
            addPartsToFace(level, startPos, size, 0, 0, 0, size - 1, 0, size - 1, Direction.WEST, random, updatePositions);
×
168
            addPartsToFace(level, startPos, size, size - 1, size - 1, 0, size - 1, 0, size - 1, Direction.EAST, random, updatePositions);
×
169

170
            for (BlockPos pos : updatePositions) {
×
171
                level.blockUpdated(pos, RegistryEntries.BLOCK_CABLE.value());
×
172
            }
×
173
        }
×
174

175
        /**
176
         * Clear all cable blocks within a radius of the given position.
177
         */
178
        public static void clearCables(ServerLevel level, BlockPos centerPos, int radius) {
179
            int radiusSquared = radius * radius;
×
180

181
            BlockCable.SKIP_NETWORK_INIT = true;
×
182

183
            try {
184
                for (int x = centerPos.getX() - radius; x <= centerPos.getX() + radius; x++) {
×
185
                    for (int y = centerPos.getY() - radius; y <= centerPos.getY() + radius; y++) {
×
186
                        for (int z = centerPos.getZ() - radius; z <= centerPos.getZ() + radius; z++) {
×
187
                            BlockPos pos = new BlockPos(x, y, z);
×
188

189
                            int dx = x - centerPos.getX();
×
190
                            int dy = y - centerPos.getY();
×
191
                            int dz = z - centerPos.getZ();
×
192
                            if (dx * dx + dy * dy + dz * dz <= radiusSquared) {
×
193
                                if (level.getBlockState(pos).getBlock() == RegistryEntries.BLOCK_CABLE.value()) {
×
194
                                    level.destroyBlock(pos, false);
×
195
                                }
196
                            }
197
                        }
198
                    }
199
                }
200
            } finally {
201
                BlockCable.SKIP_NETWORK_INIT = false;
×
202
            }
203
        }
×
204

205
        private static void addPartsToFace(ServerLevel level, BlockPos startPos, int size,
206
                                          int minX, int maxX, int minY, int maxY, int minZ, int maxZ,
207
                                          Direction side, Random random, List<BlockPos> updatePositions) {
208
            for (int x = minX; x <= maxX; x++) {
×
209
                for (int y = minY; y <= maxY; y++) {
×
210
                    for (int z = minZ; z <= maxZ; z++) {
×
211
                        BlockPos pos = startPos.offset(x, y, z);
×
212
                        addRandomPartDeferred(level, pos, side, random, updatePositions);
×
213
                    }
214
                }
215
            }
216
        }
×
217

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

221
            if (partTypes.isEmpty()) {
×
222
                return;
×
223
            }
224

225
            IPartType partType = partTypes.get(random.nextInt(partTypes.size()));
×
226
            ItemStack itemStack = new ItemStack(partType.getItem());
×
227
            PartHelpers.addPart(level, pos, side, partType, itemStack);
×
228
            updatePositions.add(pos);
×
229
        }
×
230
    }
231
}
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