• 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

1.47
/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();
×
54
    }
55

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

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

67
                // Re-init network at the two disconnected connectors
68
                DimPos originPos = target.getCenter().getPos();
×
69
                DimPos targetPos = PartTypeConnectorMonoDirectional.State.getTargetPos(target.getCenter(), state.getOffset());
×
70
                NetworkHelpers.initNetwork(originPos.getLevel(true), originPos.getBlockPos(), target.getCenter().getSide());
×
71
                NetworkHelpers.initNetwork(targetPos.getLevel(true), targetPos.getBlockPos(), target.getCenter().getSide().getOpposite());
×
72
            }
73
        }
74
    }
×
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;
×
121
        PartTypeConnectorMonoDirectional.State state = null;
×
122
        while (++offset < GeneralConfig.maxDirectionalConnectorOffset
×
123
                && (state = PartTypeConnectorMonoDirectional.State.getUnboundTargetState(origin, offset)) == null);
×
124
        if (state != null) {
×
125
            return offset;
×
126
        }
127
        return 0;
×
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> {
×
145

146
        private int offset = 0;
×
147

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

161
        public void setTarget(int offset) {
162
            setOffset(offset);
×
163
            sendUpdate();
×
164

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

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

181
        public int getOffset() {
182
            return this.offset;
×
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;
×
192
        }
×
193

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

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

202
        @Override
203
        public void serialize(ValueOutput valueOutput) {
204
            super.serialize(valueOutput);
×
205
            if (offset > 0) {
×
206
                valueOutput.putInt("connect_offset", offset);
×
207
            }
208
        }
×
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);
×
218
            if (state != null && !state.hasTarget()) {
×
219
                return state;
×
220
            }
221
            return null;
×
222
        }
223

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

233
        protected static DimPos getTargetPos(PartPos origin, int offset) {
234
            return DimPos.of(origin.getPos().getLevelKey(),
×
235
                    origin.getPos().getBlockPos().relative(origin.getSide(), offset));
×
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

© 2026 Coveralls, Inc