• 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/IngredientChannelAdapterWrapperSlotted.java
1
package org.cyclops.integrateddynamics.core.network;
2

3
import com.google.common.collect.Iterators;
4
import it.unimi.dsi.fastutil.ints.Int2IntMap;
5
import net.neoforged.neoforge.transfer.transaction.Transaction;
6
import net.neoforged.neoforge.transfer.transaction.TransactionContext;
7
import org.apache.commons.lang3.tuple.Triple;
8
import org.cyclops.commoncapabilities.api.ingredient.IngredientComponent;
9
import org.cyclops.commoncapabilities.api.ingredient.storage.IIngredientComponentStorage;
10
import org.cyclops.commoncapabilities.api.ingredient.storage.IIngredientComponentStorageSlotted;
11
import org.cyclops.cyclopscore.helper.IModHelpers;
12
import org.cyclops.integrateddynamics.api.network.IPositionedAddonsNetworkIngredients;
13
import org.cyclops.integrateddynamics.api.network.PositionedAddonsNetworkIngredientsFilter;
14
import org.cyclops.integrateddynamics.api.part.PartPos;
15

16
import javax.annotation.Nonnull;
17
import java.util.Iterator;
18

19
/**
20
 * A slotted wrapper over {@link IngredientChannelAdapter}.
21
 *
22
 * It exposes slots by chaining storages sequentially, not using the index.
23
 *
24
 * @param <T> The instance type.
25
 * @param <M> The matching condition parameter.
26
 *
27
 * @author rubensworks
28
 */
29
public class IngredientChannelAdapterWrapperSlotted<T, M> implements IIngredientComponentStorageSlotted<T, M> {
30

31
    private final IngredientChannelAdapter<T, M> channel;
32
    private final Int2IntMap cacheChannelSlots;
33

34
    public IngredientChannelAdapterWrapperSlotted(IngredientChannelAdapter<T, M> channel, Int2IntMap cacheChannelSlots) {
×
35
        this.channel = channel;
×
36
        this.cacheChannelSlots = cacheChannelSlots;
×
37
    }
×
38

39
    protected static int getIngredientComponentStorageSize(IIngredientComponentStorage<?, ?> storage) {
40
        if (storage instanceof IIngredientComponentStorageSlotted) {
×
41
            return ((IIngredientComponentStorageSlotted<?, ?>) storage).getSlots();
×
42
        } else {
43
            return Iterators.size(storage.iterator()) + 1;
×
44
        }
45
    }
46

47
    @Override
48
    public int getSlots() {
49
        int slots = this.cacheChannelSlots.getOrDefault(this.channel.getChannel(), -1);
×
50
        if (slots != -1) {
×
51
            return slots;
×
52
        }
53

54
        slots = 0;
×
55
        IPositionedAddonsNetworkIngredients<T, M> network = this.channel.getNetwork();
×
56

57
        boolean hasDisabledPosition = false;
×
58
        for (PartPos pos : network.getPositions(this.channel.getChannel())) {
×
59
            // Skip if the position is not loaded or disabled
60
            if (!pos.getPos().isLoaded()) {
×
61
                continue;
×
62
            }
63
            if (network.isPositionDisabled(pos)) {
×
64
                hasDisabledPosition = true;
×
65
                continue;
×
66
            }
67
            network.disablePosition(pos);
×
68
            IIngredientComponentStorage<T, M> storage = network.getPositionedStorage(pos);
×
69
            slots = IModHelpers.get().getBaseHelpers().addSafe(slots, getIngredientComponentStorageSize(storage));
×
70
            network.enablePosition(pos);
×
71
        }
×
72

73
        if (!hasDisabledPosition) {
×
74
            this.cacheChannelSlots.put(this.channel.getChannel(), slots);
×
75
        }
76
        return slots;
×
77
    }
78

79
    protected Triple<IIngredientComponentStorage<T, M>, Integer, PartPos> getStorageAndRelativeSlot(int slot) {
80
        IPositionedAddonsNetworkIngredients<T, M> network = this.channel.getNetwork();
×
81

82
        for (PartPos pos : network.getPositions(this.channel.getChannel())) {
×
83
            // Skip if the position is not loaded or disabled
84
            if (!pos.getPos().isLoaded() || network.isPositionDisabled(pos)) {
×
85
                continue;
×
86
            }
87
            network.disablePosition(pos);
×
88
            IIngredientComponentStorage<T, M> storage = network.getPositionedStorage(pos);
×
89
            int storageSize = getIngredientComponentStorageSize(storage);
×
90
            network.enablePosition(pos);
×
91
            if (slot < storageSize) {
×
92
                return Triple.of(storage, slot, pos);
×
93
            } else {
94
                slot -= storageSize;
×
95
            }
96
        }
×
97

98
        return Triple.of(null, -1, null);
×
99
    }
100

101
    @Override
102
    public T getSlotContents(int slotAbsolute) {
103
        Triple<IIngredientComponentStorage<T, M>, Integer, PartPos> storageAndSlot = getStorageAndRelativeSlot(slotAbsolute);
×
104
        IIngredientComponentStorage<T, M> storage = storageAndSlot.getLeft();
×
105
        int slotRelative = storageAndSlot.getMiddle();
×
106
        PartPos pos = storageAndSlot.getRight();
×
107
        if (storage == null) {
×
108
            return getComponent().getMatcher().getEmptyInstance();
×
109
        }
110

111
        if (storage instanceof IIngredientComponentStorageSlotted) {
×
112
            return ((IIngredientComponentStorageSlotted<T, M>) storage).getSlotContents(slotRelative);
×
113
        } else {
114
            try {
115
                T ingredient = Iterators.get(storage.iterator(), slotRelative);
×
116
                PositionedAddonsNetworkIngredientsFilter<T> filter = this.channel.getNetwork().getPositionedStorageFilter(pos);
×
117
                if (filter != null && !filter.testView(ingredient)) {
×
118
                    return getComponent().getMatcher().getEmptyInstance();
×
119
                }
120
                return ingredient;
×
121
            } catch (IndexOutOfBoundsException e) {
×
122
                return getComponent().getMatcher().getEmptyInstance();
×
123
            }
124
        }
125
    }
126

127
    @Override
128
    public long getMaxQuantity(int slotAbsolute) {
129
        Triple<IIngredientComponentStorage<T, M>, Integer, PartPos> storageAndSlot = getStorageAndRelativeSlot(slotAbsolute);
×
130
        IIngredientComponentStorage<T, M> storage = storageAndSlot.getLeft();
×
131
        int slotRelative = storageAndSlot.getMiddle();
×
132
        if (storage == null) {
×
133
            return 0;
×
134
        }
135

136
        if (storage instanceof IIngredientComponentStorageSlotted) {
×
137
            return ((IIngredientComponentStorageSlotted<T, M>) storage).getMaxQuantity(slotRelative);
×
138
        } else {
139
            return IModHelpers.get().getBaseHelpers().castSafe(getComponent().getMatcher().getMaximumQuantity());
×
140
        }
141
    }
142

143
    @Override
144
    public T insert(int slotAbsolute, @Nonnull T ingredient, TransactionContext transaction) {
145
        Triple<IIngredientComponentStorage<T, M>, Integer, PartPos> storageAndSlot = getStorageAndRelativeSlot(slotAbsolute);
×
146
        IIngredientComponentStorage<T, M> storage = storageAndSlot.getLeft();
×
147
        int slotRelative = storageAndSlot.getMiddle();
×
148
        PartPos pos = storageAndSlot.getRight();
×
149
        if (storage == null) {
×
150
            return ingredient;
×
151
        }
152

153
        PositionedAddonsNetworkIngredientsFilter<T> filter = this.channel.getNetwork().getPositionedStorageFilter(pos);
×
154
        if (filter != null && !filter.testInsertion(ingredient)) {
×
155
            return ingredient;
×
156
        }
157

158
        if (storage instanceof IIngredientComponentStorageSlotted) {
×
159
            return ((IIngredientComponentStorageSlotted<T, M>) storage).insert(slotRelative, ingredient, transaction);
×
160
        } else {
161
            return storage.insert(ingredient, transaction);
×
162
        }
163
    }
164

165
    @Override
166
    public T extract(int slotAbsolute, long maxQuantity, TransactionContext transaction) {
167
        Triple<IIngredientComponentStorage<T, M>, Integer, PartPos> storageAndSlot = getStorageAndRelativeSlot(slotAbsolute);
×
168
        IIngredientComponentStorage<T, M> storage = storageAndSlot.getLeft();
×
169
        int slotRelative = storageAndSlot.getMiddle();
×
170
        PartPos pos = storageAndSlot.getRight();
×
171
        if (storage == null) {
×
172
            return getComponent().getMatcher().getEmptyInstance();
×
173
        }
174

175
        // If we do an effective extraction, first simulate to check if it matches the filter
176
        PositionedAddonsNetworkIngredientsFilter<T> filter = this.channel.getNetwork().getPositionedStorageFilter(pos);
×
177
        if (filter != null) {
×
178
            try (var tx = Transaction.open(transaction)) {
×
179
                T extractedSimulated;
180
                if (storage instanceof IIngredientComponentStorageSlotted) {
×
181
                    extractedSimulated = ((IIngredientComponentStorageSlotted<T, M>) storage).extract(slotRelative, maxQuantity, tx);
×
182
                } else {
183
                    extractedSimulated = storage.extract(maxQuantity, tx);
×
184
                }
185
                if (!filter.testExtraction(extractedSimulated)) {
×
186
                    return getComponent().getMatcher().getEmptyInstance();
×
187
                }
188
            }
×
189
        }
190

191
        T extracted;
192
        if (storage instanceof IIngredientComponentStorageSlotted) {
×
193
            extracted = ((IIngredientComponentStorageSlotted<T, M>) storage).extract(slotRelative, maxQuantity, transaction);
×
194
        } else {
195
            extracted = storage.extract(maxQuantity, transaction);
×
196
        }
197

198
        return extracted;
×
199
    }
200

201
    @Override
202
    public IngredientComponent<T, M> getComponent() {
203
        return channel.getComponent();
×
204
    }
205

206
    @Override
207
    public Iterator<T> iterator() {
208
        return channel.iterator();
×
209
    }
210

211
    @Override
212
    public Iterator<T> iterator(@Nonnull T prototype, M matchCondition) {
213
        return channel.iterator(prototype, matchCondition);
×
214
    }
215

216
    @Override
217
    public long getMaxQuantity() {
218
        return channel.getMaxQuantity();
×
219
    }
220

221
    @Override
222
    public T insert(@Nonnull T ingredient, TransactionContext transaction) {
223
        return channel.insert(ingredient, transaction);
×
224
    }
225

226
    @Override
227
    public T extract(@Nonnull T prototype, M matchCondition, TransactionContext transaction) {
228
        return channel.extract(prototype, matchCondition, transaction);
×
229
    }
230

231
    @Override
232
    public T extract(long maxQuantity, TransactionContext transaction) {
233
        return channel.extract(maxQuantity, transaction);
×
234
    }
235
}
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