• 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

0.0
/src/main/java/org/cyclops/integrateddynamics/core/network/PositionedAddonsNetwork.java
1
package org.cyclops.integrateddynamics.core.network;
2

3
import com.google.common.collect.Maps;
4
import com.google.common.collect.Sets;
5
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
6
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
7
import lombok.Getter;
8
import lombok.Setter;
9
import org.apache.commons.lang3.tuple.Pair;
10
import org.cyclops.cyclopscore.datastructure.Wrapper;
11
import org.cyclops.integrateddynamics.api.network.INetwork;
12
import org.cyclops.integrateddynamics.api.network.IPartPosIteratorHandler;
13
import org.cyclops.integrateddynamics.api.network.IPositionedAddonsNetwork;
14
import org.cyclops.integrateddynamics.api.part.PartPos;
15
import org.cyclops.integrateddynamics.api.part.PrioritizedPartPos;
16

17
import javax.annotation.Nullable;
18
import java.util.Collection;
19
import java.util.Collections;
20
import java.util.Iterator;
21
import java.util.Map;
22
import java.util.Set;
23
import java.util.TreeSet;
24

25
/**
26
 * A network that can hold prioritized positions.
27
 * @author rubensworks
28
 */
29
public abstract class PositionedAddonsNetwork implements IPositionedAddonsNetwork {
×
30

31
    @Getter
×
32
    @Setter
×
33
    private INetwork network;
34
    private final Set<PrioritizedPartPos> allPositions = Sets.newTreeSet();
×
35
    private final Int2ObjectMap<Set<PrioritizedPartPos>> positions = new Int2ObjectOpenHashMap<>();
×
36
    private final Map<PartPos, Integer> positionChannels = Maps.newHashMap();
×
37
    // We store the thread id together with the disabled position.
38
    // This is to make sure that different threads can safely iterate over positions in parallel
39
    // without clashing with each other, as this could lead to problems such as in #194.
40
    // This for example applies to the ingredient observer and in-world ingredient movement.
41
    private final Set<Pair<Long, PartPos>> disabledPositions = Sets.newHashSet();
×
42

43
    private IPartPosIteratorHandler partPosIteratorHandler = null;
×
44

45
    @Override
46
    public int[] getChannels() {
47
        return positions.keySet().toIntArray();
×
48
    }
49

50
    @Override
51
    public boolean hasPositions() {
52
        return !positions.isEmpty();
×
53
    }
54

55
    @Override
56
    public Collection<PrioritizedPartPos> getPrioritizedPositions(int channel) {
57
        if (channel == WILDCARD_CHANNEL) {
×
58
            return getPrioritizedPositions();
×
59
        }
60
        Set<PrioritizedPartPos> positions = this.positions.get(channel);
×
61
        Set<PrioritizedPartPos> wildcardPositions = this.positions.get(WILDCARD_CHANNEL);
×
62
        if (positions == null) {
×
63
            if (wildcardPositions != null) {
×
64
                return wildcardPositions;
×
65
            }
66
            positions = Collections.emptySet();
×
67
        }
68
        if (wildcardPositions == null) {
×
69
            return positions;
×
70
        }
71
        TreeSet<PrioritizedPartPos> merged = Sets.newTreeSet();
×
72
        merged.addAll(positions);
×
73
        merged.addAll(wildcardPositions);
×
74
        return merged;
×
75
    }
76

77
    @Override
78
    public Collection<PrioritizedPartPos> getPrioritizedPositions() {
79
        return this.allPositions;
×
80
    }
81

82
    @Override
83
    public int getPositionChannel(PartPos pos) {
84
        return this.positionChannels.getOrDefault(pos, -1);
×
85
    }
86

87
    protected void invalidateIterators() {
88
        setPartPosIteratorHandler(null);
×
89
    }
×
90

91
    @Override
92
    public void setPartPosIteratorHandler(@Nullable IPartPosIteratorHandler iteratorHandler) {
93
        this.partPosIteratorHandler = iteratorHandler;
×
94
    }
×
95

96
    @Nullable
97
    @Override
98
    public IPartPosIteratorHandler getPartPosIteratorHandler() {
99
        if (partPosIteratorHandler != null) {
×
100
            return partPosIteratorHandler;
×
101
        }
102
        return null;
×
103
    }
104

105
    @Override
106
    public boolean addPosition(PartPos pos, int priority, int channel) {
107
        invalidateIterators();
×
108

109
        PrioritizedPartPos prioritizedPosition = PrioritizedPartPos.of(pos, priority);
×
110
        if (allPositions.add(prioritizedPosition)) {
×
111
            Set<PrioritizedPartPos> positions = this.positions.get(channel);
×
112
            if (positions == null) {
×
113
                positions = Sets.newTreeSet();
×
114
                this.positions.put(channel, positions);
×
115
            }
116
            positions.add(prioritizedPosition);
×
117
            this.positionChannels.put(pos, channel);
×
118
            this.onPositionAdded(channel, prioritizedPosition);
×
119
            return true;
×
120
        }
121
        return false;
×
122
    }
123

124
    protected void onPositionAdded(int channel, PrioritizedPartPos pos) {
125

126
    }
×
127

128
    @Override
129
    public void removePosition(PartPos pos) {
130
        invalidateIterators();
×
131

132
        Wrapper<Integer> removedChannel = new Wrapper<>(-2);
×
133
        Wrapper<PrioritizedPartPos> removedPos = new Wrapper<>(null);
×
134
        for (Int2ObjectMap.Entry<Set<PrioritizedPartPos>> entry : this.positions.int2ObjectEntrySet()) {
×
135
            int channel = entry.getIntKey();
×
136
            Set<PrioritizedPartPos> positions = entry.getValue();
×
137
            Iterator<PrioritizedPartPos> it = positions.iterator();
×
138
            while (it.hasNext()) {
×
139
                PrioritizedPartPos prioritizedPartPos = it.next();
×
140
                if (prioritizedPartPos.getPartPos().equals(pos)) {
×
141
                    it.remove();
×
142
                    allPositions.remove(prioritizedPartPos);
×
143
                    removedPos.set(prioritizedPartPos);
×
144
                    removedChannel.set(channel);
×
145
                    break;
×
146
                }
147
            }
×
148
        }
×
149
        int channel = removedChannel.get();
×
150
        if (channel != -2) {
×
151
            this.onPositionRemoved(channel, removedPos.get());
×
152
            if (positions.get(channel).isEmpty()) {
×
153
                this.positions.remove(channel);
×
154
            }
155
        }
156
        positionChannels.remove(pos);
×
157
    }
×
158

159
    protected void onPositionRemoved(int channel, PrioritizedPartPos pos) {
160

161
    }
×
162

163
    @Override
164
    public boolean isPositionDisabled(PartPos pos) {
165
        return disabledPositions.contains(Pair.of(Thread.currentThread().getId(), pos));
×
166
    }
167

168
    @Override
169
    public void disablePosition(PartPos pos) {
170
        disabledPositions.add(Pair.of(Thread.currentThread().getId(), pos));
×
171
    }
×
172

173
    @Override
174
    public void enablePosition(PartPos pos) {
175
        disabledPositions.remove(Pair.of(Thread.currentThread().getId(), pos));
×
176
    }
×
177

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