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

CyclopsMC / IntegratedDynamics / 20503720448

25 Dec 2025 10:37AM UTC coverage: 45.064% (+0.009%) from 45.055%
20503720448

push

github

web-flow
Fix documentation on NBT Path field selector array indexing (#1580)

Closes #1579

2609 of 8568 branches covered (30.45%)

Branch coverage included in aggregate %.

11866 of 23553 relevant lines covered (50.38%)

2.39 hits per line

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

72.57
/src/main/java/org/cyclops/integrateddynamics/core/part/PartStateActiveVariableBase.java
1
package org.cyclops.integrateddynamics.core.part;
2

3
import com.google.common.collect.Lists;
4
import lombok.Getter;
5
import lombok.Setter;
6
import net.minecraft.nbt.CompoundTag;
7
import net.minecraft.network.chat.MutableComponent;
8
import net.minecraft.world.item.ItemStack;
9
import net.minecraft.world.level.Level;
10
import org.cyclops.cyclopscore.inventory.SimpleInventory;
11
import org.cyclops.cyclopscore.persist.nbt.NBTClassType;
12
import org.cyclops.integrateddynamics.Capabilities;
13
import org.cyclops.integrateddynamics.IntegratedDynamics;
14
import org.cyclops.integrateddynamics.api.block.IVariableContainer;
15
import org.cyclops.integrateddynamics.api.evaluate.IValueInterface;
16
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
17
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
18
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
19
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
20
import org.cyclops.integrateddynamics.api.network.INetwork;
21
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
22
import org.cyclops.integrateddynamics.api.part.IPartType;
23
import org.cyclops.integrateddynamics.api.part.PartCapability;
24
import org.cyclops.integrateddynamics.api.part.PartPos;
25
import org.cyclops.integrateddynamics.api.part.PartTarget;
26
import org.cyclops.integrateddynamics.capability.variablecontainer.VariableContainerDefault;
27
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
28

29
import java.util.List;
30
import java.util.Optional;
31

32
/**
33
 * An abstract part state with a focus on activatable variables.
34
 * @author rubensworks
35
 */
36
public abstract class PartStateActiveVariableBase<P extends IPartType> extends PartStateBase<P> {
37

38
    private boolean checkedForWriteVariable = false;
3✔
39
    protected IVariableFacade currentVariableFacade = null;
3✔
40
    private final IVariableContainer variableContainer;
41
    @Getter
6✔
42
    @Setter
4✔
43
    private boolean deactivated = false;
44
    private SimpleInventory inventory;
45
    private List<MutableComponent> globalErrorMessages = Lists.newLinkedList();
3✔
46
    @Getter
6✔
47
    @Setter
4✔
48
    private boolean retryEvaluation = false;
49

50
    public PartStateActiveVariableBase(int inventorySize) {
2✔
51
        this.inventory = new SingularInventory(inventorySize);
6✔
52
        this.inventory.addDirtyMarkListener(this); // No need to remove myself eventually. If I am removed, inv is also removed.
4✔
53
        variableContainer = new VariableContainerDefault();
5✔
54
        addVolatileCapability(Capabilities.VariableContainer.PART, Optional.of(variableContainer));
6✔
55
    }
1✔
56

57
    /**
58
     * @return The inner inventory
59
     */
60
    public SimpleInventory getInventory() {
61
        return this.inventory;
3✔
62
    }
63

64
    protected void validate(INetwork network, IPartNetwork partNetwork) {
65
        // Note that this is only called server-side, so these errors are sent via NBT to the client(s).
66
        this.currentVariableFacade.validate(network, partNetwork,
11✔
67
                new PartStateActiveVariableBase.Validator(this), currentVariableFacade.getOutputType());
1✔
68
    }
1✔
69

70
    protected void onCorruptedState() {
71
        IntegratedDynamics.clog(org.apache.logging.log4j.Level.ERROR, "A corrupted part state was found at, repairing...");
×
72
        Thread.dumpStack();
×
73
        this.checkedForWriteVariable = false;
×
74
        this.deactivated = true;
×
75
    }
×
76

77
    /**
78
     * @return If there is an active variable present for this state.
79
     */
80
    public boolean hasVariable() {
81
        return (getGlobalErrors().isEmpty() || isRetryEvaluation()) && !getInventory().isEmpty();
15✔
82
    }
83

84
    /**
85
     * Get the active variable in this state.
86
     * @param <V> The variable value type.
87
     * @param network The network.
88
     * @param partNetwork The part network.
89
     * @param valueDeseralizationContext
90
     * @return The variable.
91
     */
92
    public <V extends IValue> IVariable<V> getVariable(INetwork network, IPartNetwork partNetwork, ValueDeseralizationContext valueDeseralizationContext) {
93
        if(!checkedForWriteVariable) {
3✔
94
            if (variableContainer.getVariableCache().isEmpty()) {
5✔
95
                variableContainer.refreshVariables(network, inventory, false, valueDeseralizationContext);
8✔
96
            }
97
            for (IVariableFacade facade : variableContainer.getVariableCache().values()) {
13✔
98
                if (facade != null) {
2!
99
                    currentVariableFacade = facade;
3✔
100
                    validate(network, partNetwork);
4✔
101
                }
102
            }
1✔
103
            this.checkedForWriteVariable = true;
3✔
104
        }
105
        if(currentVariableFacade == null) {
3!
106
            onCorruptedState();
×
107
            return null;
×
108
        }
109
        return currentVariableFacade.getVariable(network, partNetwork);
6✔
110
    }
111

112
    /**
113
     * Refresh the current variable to have its current info reset and updated.
114
     * @param partType The corresponding part type.
115
     * @param target The target of the part.
116
     */
117
    public void onVariableContentsUpdated(P partType, PartTarget target) {
118
        // Resets the errors for this aspect
119
        this.checkedForWriteVariable = false;
3✔
120
        addGlobalError(null);
3✔
121
        this.currentVariableFacade = null;
3✔
122
        //this.deactivated = false; // This *should* not be required anymore, re-activation is handled in AspectWriteBase#update.
123

124
        // Refresh any contained variables
125
        PartPos center = target.getCenter();
3✔
126
        Level level = center.getPos().getLevel(true);
5✔
127
        NetworkHelpers.getNetwork(level, center.getPos().getBlockPos(), center.getSide())
10✔
128
                .ifPresent(network -> variableContainer.refreshVariables(network, inventory, false, ValueDeseralizationContext.of(level)));
11✔
129
    }
1✔
130

131
    /**
132
     * @return All global error messages.
133
     */
134
    public List<MutableComponent> getGlobalErrors() {
135
        return globalErrorMessages;
3✔
136
    }
137

138
    /**
139
     * Add a global error message.
140
     * @param error The message to add.
141
     */
142
    public void addGlobalError(MutableComponent error) {
143
        setRetryEvaluation(false);
3✔
144
        if(error == null) {
2✔
145
            globalErrorMessages.clear();
4✔
146
        } else {
147
            globalErrorMessages.add(error);
5✔
148
        }
149
        onDirty();
2✔
150
        sendUpdate(); // We want this error messages to be sent to the client(s).
2✔
151
    }
1✔
152

153
    @Override
154
    public void writeToNBT(ValueDeseralizationContext valueDeseralizationContext, CompoundTag tag) {
155
        super.writeToNBT(valueDeseralizationContext, tag);
4✔
156
        NBTClassType.writeNbt(List.class, "globalErrorMessages", globalErrorMessages, tag, valueDeseralizationContext.holderLookupProvider());
8✔
157
        inventory.writeToNBT(valueDeseralizationContext.holderLookupProvider(), tag, "inventory");
7✔
158
    }
1✔
159

160
    @Override
161
    public void readFromNBT(ValueDeseralizationContext valueDeseralizationContext, CompoundTag tag) {
162
        super.readFromNBT(valueDeseralizationContext, tag);
×
163
        //noinspection unchecked
164
        this.globalErrorMessages = NBTClassType.readNbt(List.class, "globalErrorMessages", tag, valueDeseralizationContext.holderLookupProvider());
×
165
        inventory.readFromNBT(valueDeseralizationContext.holderLookupProvider(), tag, "inventory");
×
166
    }
×
167

168
    @Override
169
    public <T> Optional<T> getCapability(P partType, PartCapability<T> capability, INetwork network, IPartNetwork partNetwork, PartTarget target) {
170
        if (capability == Capabilities.ValueInterface.PART) {
3✔
171
            if (hasVariable()) {
3!
172
                IVariable<IValue> variable = getVariable(network, partNetwork, ValueDeseralizationContext.of(target.getCenter().getPos().getLevel(true)));
11✔
173
                if (variable != null) {
2!
174
                    return (Optional<T>) Optional.<IValueInterface>of(() -> Optional.of(variable.getValue()));
8✔
175
                }
176
            }
177
            return Optional.empty();
×
178
        }
179
        return super.getCapability(partType, capability, network, partNetwork, target);
8✔
180
    }
181

182
    /**
183
     * An inventory that can only hold one filled slot at a time.
184
     */
185
    public static class SingularInventory extends SimpleInventory {
186

187
        /**
188
         * Make a new instance.
189
         *
190
         * @param size The amount of slots in the inventory.
191
         */
192
        public SingularInventory(int size) {
193
            super(size, 1);
4✔
194
        }
1✔
195

196
        protected boolean canInsert(int slot) {
197
            for (int i = 0; i < getContainerSize(); i++) {
×
198
                // Only allow insertion if the target slot is the same as the non-empty slot
199
                if (i != slot && !getItem(i).isEmpty()) {
×
200
                    return false;
×
201
                }
202
            }
203
            return true;
×
204
        }
205

206
        @Override
207
        public boolean canPlaceItem(int i, ItemStack itemstack) {
208
            return canInsert(i) && super.canPlaceItem(i, itemstack);
×
209
        }
210

211
    }
212

213
    public static class Validator implements IVariableFacade.IValidator {
214

215
        private final PartStateActiveVariableBase state;
216

217
        /**
218
         * Make a new instance
219
         * @param state The part state.
220
         */
221
        public Validator(PartStateActiveVariableBase state) {
2✔
222
            this.state = state;
3✔
223
        }
1✔
224

225
        @Override
226
        public void addError(MutableComponent error) {
227
            this.state.addGlobalError(error);
4✔
228
        }
1✔
229

230
    }
231

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