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

3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Sets;
5
import org.cyclops.commoncapabilities.api.capability.Capabilities;
6
import org.cyclops.commoncapabilities.api.capability.inventorystate.IInventoryState;
7
import org.cyclops.cyclopscore.helper.IModHelpersNeoForge;
8
import org.cyclops.integrateddynamics.api.network.IPositionedAddonsNetworkIngredients;
9
import org.cyclops.integrateddynamics.api.part.PartPos;
10

11
import java.util.*;
12
import java.util.concurrent.ConcurrentHashMap;
13

14
/**
15
 * A thread-safe proxy for reading ingredients within a world.
16
 * @author rubensworks
17
 */
18
public class ConcurrentWorldIngredientsProxy<T, M> {
19

20
    private final IPositionedAddonsNetworkIngredients<T, M> network;
21
    private Set<PartPos> oldPositions;
22
    private final Map<PartPos, Integer> states;
23
    private final Map<PartPos, Collection<T>> instances;
24
    private final Set<PartPos> readStates;
25
    private final Set<PartPos> readInstances;
26

27
    public ConcurrentWorldIngredientsProxy(IPositionedAddonsNetworkIngredients<T, M> network) {
×
28
        this.network = network;
×
29
        this.oldPositions = Sets.newHashSet();
×
30
        this.states = new ConcurrentHashMap<>();
×
31
        this.instances = new ConcurrentHashMap<>();
×
32
        this.readStates = Collections.newSetFromMap(new ConcurrentHashMap<>());
×
33
        this.readInstances = Collections.newSetFromMap(new ConcurrentHashMap<>());
×
34
    }
×
35

36
    public IPositionedAddonsNetworkIngredients<T, M> getNetwork() {
37
        return network;
×
38
    }
39

40
    public Optional<Integer> getInventoryState(PartPos pos) {
41
        Optional<Integer> value = Optional.ofNullable(this.states.get(pos));
×
42
        this.readStates.add(pos);
×
43
        return value;
×
44
    }
45

46
    public void setRead(PartPos pos) {
47
        this.readInstances.add(pos);
×
48
    }
×
49

50
    public Collection<T> getInstances(PartPos pos) {
51
        Collection<T> value = instances.getOrDefault(pos, Collections.emptyList());
×
52
        this.setRead(pos);
×
53
        return value;
×
54
    }
55

56
    protected Set<PartPos> getPositions() {
57
        return Sets.newHashSet(getNetwork().getPositions());
×
58
    }
59

60
    public void onWorldTick() {
61
        Set<PartPos> newPositions = getPositions();
×
62

63
        // Do nothing if nothing was read, and no positions were changed
64
        boolean positionsChanged = newPositions.size() != this.oldPositions.size() || !newPositions.containsAll(this.oldPositions);
×
65
        if (this.readStates.isEmpty() && this.readInstances.isEmpty() && !positionsChanged) {
×
66
            return;
×
67
        }
68

69
        // Update states and ingredients for all positions
70
        for (PartPos pos : newPositions) {
×
71
            if (positionsChanged) {
×
72
                this.oldPositions.remove(pos);
×
73
            }
74

75
            // Fetch inventory states
76
            if (this.readStates.contains(pos) || !this.states.containsKey(pos)) {
×
77
                IInventoryState inventoryState = IModHelpersNeoForge.get().getCapabilityHelpers()
×
78
                        .getCapability(pos.getPos(), pos.getSide(), Capabilities.InventoryState.BLOCK)
×
79
                        .orElse(null);
×
80
                if (inventoryState != null) {
×
81
                    int newState = inventoryState.getState();
×
82
                    Integer previousState = this.states.put(pos, newState);
×
83

84
                    // If we find a state change, make sure that we also reload the instances in this iteration
85
                    if (previousState == null || newState != previousState) {
×
86
                        this.readInstances.add(pos);
×
87
                    }
88
                } else {
×
89
                    this.states.remove(pos);
×
90
                }
91
                this.readStates.remove(pos);
×
92
            }
93

94
            // Fetch ingredient instances
95
            if (this.readInstances.contains(pos) || !this.instances.containsKey(pos)) {
×
96
                ArrayList<T> instances = Lists.newArrayList(getNetwork().getRawInstances(pos));
×
97
                this.instances.put(pos, instances);
×
98
                this.readInstances.remove(pos);
×
99
            }
100
        }
×
101

102
        // Remove positions that are not in the network anymore
103
        if (positionsChanged) {
×
104
            for (PartPos oldPosition : this.oldPositions) {
×
105
                this.readStates.remove(oldPosition);
×
106
                this.readInstances.remove(oldPosition);
×
107
                this.states.remove(oldPosition);
×
108
                this.instances.remove(oldPosition);
×
109
            }
×
110
            this.oldPositions = newPositions;
×
111
        }
112
    }
×
113

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