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

CyclopsMC / IntegratedDynamics / 18385005714

09 Oct 2025 06:10PM UTC coverage: 53.023% (-0.008%) from 53.031%
18385005714

push

github

rubensworks
Deprecate IIngredientComponentHandler

2865 of 8762 branches covered (32.7%)

Branch coverage included in aggregate %.

17321 of 29308 relevant lines covered (59.1%)

3.07 hits per line

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

63.24
/src/main/java/org/cyclops/integrateddynamics/part/PartTypeConnectorMonoDirectional.java
1
package org.cyclops.integrateddynamics.part;
2

3
import com.google.common.collect.Sets;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.core.particles.DustParticleOptions;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.util.ProblemReporter;
9
import net.minecraft.world.item.ItemStack;
10
import net.minecraft.world.level.block.state.BlockState;
11
import net.minecraft.world.level.storage.ValueInput;
12
import net.minecraft.world.level.storage.ValueOutput;
13
import org.cyclops.cyclopscore.datastructure.DimPos;
14
import org.cyclops.cyclopscore.helper.IModHelpersNeoForge;
15
import org.cyclops.integrateddynamics.Capabilities;
16
import org.cyclops.integrateddynamics.GeneralConfig;
17
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
18
import org.cyclops.integrateddynamics.api.network.INetwork;
19
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
20
import org.cyclops.integrateddynamics.api.part.IPartContainer;
21
import org.cyclops.integrateddynamics.api.part.PartPos;
22
import org.cyclops.integrateddynamics.api.part.PartRenderPosition;
23
import org.cyclops.integrateddynamics.api.part.PartTarget;
24
import org.cyclops.integrateddynamics.api.path.IPathElement;
25
import org.cyclops.integrateddynamics.api.path.ISidedPathElement;
26
import org.cyclops.integrateddynamics.capability.path.SidedPathElement;
27
import org.cyclops.integrateddynamics.core.block.IgnoredBlock;
28
import org.cyclops.integrateddynamics.core.block.IgnoredBlockStatus;
29
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
30
import org.cyclops.integrateddynamics.core.helper.PartHelpers;
31

32
import java.util.Collections;
33
import java.util.Set;
34

35
/**
36
 * A monodirectional wireless connector part that can connect to
37
 * at most one other monodirectional connector in a straight line.
38
 * @author rubensworks
39
 */
40
public class PartTypeConnectorMonoDirectional extends PartTypeConnector<PartTypeConnectorMonoDirectional, PartTypeConnectorMonoDirectional.State> {
41

42
    public PartTypeConnectorMonoDirectional(String name) {
43
        super(name, new PartRenderPosition(0.25F, 0.3125F, 0.5F, 0.5F));
10✔
44
    }
1✔
45

46
    @Override
47
    public int getConsumptionRate(State state) {
48
        return GeneralConfig.connectorMonoDirectionalBaseConsumption;
×
49
    }
50

51
    @Override
52
    public PartTypeConnectorMonoDirectional.State constructDefaultState() {
53
        return new PartTypeConnectorMonoDirectional.State();
4✔
54
    }
55

56
    @Override
57
    public void onNetworkAddition(INetwork network, IPartNetwork partNetwork, PartTarget target, State state) {
58
        super.onNetworkAddition(network, partNetwork, target, state);
6✔
59

60
        // Find and link two parts
61
        if (!state.hasTarget()) {
3✔
62
            int offset = findTargetOffset(target.getCenter());
5✔
63
            if (offset > 0) {
2✔
64
                state.setTarget(offset);
3✔
65
                state.getTargetState(target.getCenter()).setTarget(offset);
6✔
66

67
                // Re-init network at the two disconnected connectors
68
                DimPos originPos = target.getCenter().getPos();
4✔
69
                DimPos targetPos = PartTypeConnectorMonoDirectional.State.getTargetPos(target.getCenter(), state.getOffset());
6✔
70
                NetworkHelpers.initNetwork(originPos.getLevel(true), originPos.getBlockPos(), target.getCenter().getSide());
10✔
71
                NetworkHelpers.initNetwork(targetPos.getLevel(true), targetPos.getBlockPos(), target.getCenter().getSide().getOpposite());
11✔
72
            }
73
        }
74
    }
1✔
75

76
    @Override
77
    public void onPostRemoved(INetwork network, IPartNetwork partNetwork, PartTarget target, State state) {
78
        super.onPostRemoved(network, partNetwork, target, state);
×
79

80
        if (state.hasTarget()) {
×
81
            // Remove target information in both linked parts
82
            PartTypeConnectorMonoDirectional.State targetState = state.getTargetState(target.getCenter());
×
83
            DimPos originPos = target.getCenter().getPos();
×
84
            DimPos targetPos = null;
×
85
            if (targetState != null) {
×
86
                targetState.removeTarget();
×
87
                targetPos = PartTypeConnectorMonoDirectional.State.getTargetPos(target.getCenter(), state.getOffset());
×
88
            }
89
            state.removeTarget();
×
90

91
            // Re-init network at the two disconnected connectors
92
            NetworkHelpers.initNetwork(originPos.getLevel(true), originPos.getBlockPos(),  target.getCenter().getSide());
×
93
            if (targetPos != null) {
×
94
                NetworkHelpers.initNetwork(targetPos.getLevel(true), targetPos.getBlockPos(),  target.getCenter().getSide().getOpposite());
×
95
            }
96
        }
97
    }
×
98

99
    @Override
100
    public ItemStack getItemStack(ValueDeseralizationContext valueDeseralizationContext, ProblemReporter.PathElement problemPath, State state, boolean saveState) {
101
        // Set offset to 0 to make sure it is not stored in the item
102
        int offset = state.getOffset();
×
103
        state.setOffset(0);
×
104

105
        // Serialize to item
106
        ItemStack itemStack = super.getItemStack(valueDeseralizationContext, problemPath, state, saveState);
×
107

108
        // Set original offset back
109
        state.setOffset(offset);
×
110

111
        return itemStack;
×
112
    }
113

114
    /**
115
     * Look in the part's direction for an unbound monodirectional connector.
116
     * @param origin The origin position to start looking from.
117
     * @return The other connector's distance, or 0 if not found.
118
     */
119
    protected int findTargetOffset(PartPos origin) {
120
        int offset = 0;
2✔
121
        PartTypeConnectorMonoDirectional.State state = null;
2✔
122
        while (++offset < GeneralConfig.maxDirectionalConnectorOffset
6✔
123
                && (state = PartTypeConnectorMonoDirectional.State.getUnboundTargetState(origin, offset)) == null);
5✔
124
        if (state != null) {
2✔
125
            return offset;
2✔
126
        }
127
        return 0;
2✔
128
    }
129

130
    protected IgnoredBlockStatus.Status getStatus(PartTypeConnectorMonoDirectional.State state) {
131
        return state != null && state.hasTarget()
×
132
                ? IgnoredBlockStatus.Status.ACTIVE : IgnoredBlockStatus.Status.INACTIVE;
×
133
    }
134

135
    @Override
136
    public BlockState getBlockState(IPartContainer partContainer, Direction side) {
137
        IgnoredBlockStatus.Status status = getStatus(partContainer != null
×
138
                ? (PartTypeConnectorMonoDirectional.State) partContainer.getPartState(side) : null);
×
139
        return super.getBlockState(partContainer, side)
×
140
                .setValue(IgnoredBlock.FACING, side)
×
141
                .setValue(IgnoredBlockStatus.STATUS, status);
×
142
    }
143

144
    public static class State extends PartTypeConnector.State<PartTypeConnectorMonoDirectional> {
2✔
145

146
        private int offset = 0;
4✔
147

148
        @Override
149
        public Set<ISidedPathElement> getReachableElements() {
150
            if (getPartPos() != null) {
3!
151
                Direction targetSide = getPartPos().getSide().getOpposite();
5✔
152
                IPathElement pathElement = IModHelpersNeoForge.get().getCapabilityHelpers().getCapability(State.getTargetPos(getPartPos(), offset),
11✔
153
                        targetSide, Capabilities.PathElement.BLOCK).orElse(null);
3✔
154
                if (pathElement != null) {
2!
155
                    return Sets.newHashSet(SidedPathElement.of(pathElement, targetSide));
10✔
156
                }
157
            }
158
            return Collections.emptySet();
×
159
        }
160

161
        public void setTarget(int offset) {
162
            setOffset(offset);
3✔
163
            sendUpdate();
2✔
164

165
            DimPos dimPos = getPosition();
3✔
166
            if (dimPos != null && this.offset > 0) {
5!
167
                BlockPos pos = dimPos.getBlockPos();
3✔
168
                for (int i = 1; i < this.offset; i++) {
8✔
169
                    pos = pos.relative(getPartPos().getSide());
6✔
170
                    ((ServerLevel) getPosition().getLevel(true))
7✔
171
                            .sendParticles(DustParticleOptions.REDSTONE,
2✔
172
                                    pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D, 1, 0D, 0D, 0D, 0);
19✔
173
                }
174
            }
175
        }
1✔
176

177
        public boolean hasTarget() {
178
            return this.offset > 0;
7✔
179
        }
180

181
        public int getOffset() {
182
            return this.offset;
3✔
183
        }
184

185
        /**
186
         * Set the raw offset.
187
         * Prefer {@link #setTarget(int)}.
188
         * @param offset The new offset.
189
         */
190
        public void setOffset(int offset) {
191
            this.offset = offset;
3✔
192
        }
1✔
193

194
        public void removeTarget() {
195
            setTarget(0);
×
196
        }
×
197

198
        protected PartTypeConnectorMonoDirectional.State getTargetState(PartPos origin) {
199
            return getTargetState(origin, offset);
5✔
200
        }
201

202
        @Override
203
        public void serialize(ValueOutput valueOutput) {
204
            super.serialize(valueOutput);
3✔
205
            if (offset > 0) {
3✔
206
                valueOutput.putInt("connect_offset", offset);
5✔
207
            }
208
        }
1✔
209

210
        @Override
211
        public void deserialize(ValueInput valueInput) {
212
            super.deserialize(valueInput);
×
213
            this.offset = valueInput.getIntOr("connect_offset", 0);
×
214
        }
×
215

216
        protected static PartTypeConnectorMonoDirectional.State getUnboundTargetState(PartPos origin, int offset) {
217
            PartTypeConnectorMonoDirectional.State state = getTargetState(origin, offset);
4✔
218
            if (state != null && !state.hasTarget()) {
5!
219
                return state;
2✔
220
            }
221
            return null;
2✔
222
        }
223

224
        protected static PartTypeConnectorMonoDirectional.State getTargetState(PartPos origin, int offset) {
225
            PartPos targetPos = PartPos.of(getTargetPos(origin, offset), origin.getSide().getOpposite());
8✔
226
            PartHelpers.PartStateHolder partStateHolder = PartHelpers.getPart(targetPos);
3✔
227
            if (partStateHolder != null && partStateHolder.getPart() instanceof PartTypeConnectorMonoDirectional) {
6!
228
                return (State) partStateHolder.getState();
4✔
229
            }
230
            return null;
2✔
231
        }
232

233
        protected static DimPos getTargetPos(PartPos origin, int offset) {
234
            return DimPos.of(origin.getPos().getLevelKey(),
6✔
235
                    origin.getPos().getBlockPos().relative(origin.getSide(), offset));
6✔
236
        }
237
    }
238

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

© 2025 Coveralls, Inc