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

CyclopsMC / IntegratedDynamics / 22186773560

19 Feb 2026 02:52PM UTC coverage: 52.603% (+0.2%) from 52.363%
22186773560

push

github

web-flow
Remove Lombok dependency (#1604)

2911 of 8664 branches covered (33.6%)

Branch coverage included in aggregate %.

17683 of 30486 relevant lines covered (58.0%)

3.01 hits per line

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

76.92
/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 net.minecraft.network.chat.Component;
5
import net.minecraft.network.chat.MutableComponent;
6
import net.minecraft.world.item.ItemStack;
7
import net.minecraft.world.level.Level;
8
import net.minecraft.world.level.storage.ValueInput;
9
import net.minecraft.world.level.storage.ValueOutput;
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
    private boolean deactivated = false;
3✔
42
    private SimpleInventory inventory;
43
    private List<Component> globalErrorMessages = Lists.newLinkedList();
3✔
44
    private boolean retryEvaluation = false;
3✔
45

46
    public boolean isDeactivated() {
47
        return deactivated;
3✔
48
    }
49

50
    public void setDeactivated(boolean deactivated) {
51
        this.deactivated = deactivated;
3✔
52
    }
1✔
53

54
    public boolean isRetryEvaluation() {
55
        return retryEvaluation;
3✔
56
    }
57

58
    public void setRetryEvaluation(boolean retryEvaluation) {
59
        this.retryEvaluation = retryEvaluation;
3✔
60
    }
1✔
61

62
    public PartStateActiveVariableBase(int inventorySize) {
2✔
63
        this.inventory = new SingularInventory(inventorySize);
6✔
64
        this.inventory.addDirtyMarkListener(this); // No need to remove myself eventually. If I am removed, inv is also removed.
4✔
65
        variableContainer = new VariableContainerDefault();
5✔
66
        addVolatileCapability(Capabilities.VariableContainer.PART, Optional.of(variableContainer));
6✔
67
    }
1✔
68

69
    /**
70
     * @return The inner inventory
71
     */
72
    public SimpleInventory getInventory() {
73
        return this.inventory;
3✔
74
    }
75

76
    protected void validate(INetwork network, IPartNetwork partNetwork) {
77
        // Note that this is only called server-side, so these errors are sent via NBT to the client(s).
78
        this.currentVariableFacade.validate(network, partNetwork,
11✔
79
                new PartStateActiveVariableBase.Validator(this), currentVariableFacade.getOutputType());
1✔
80
    }
1✔
81

82
    protected void onCorruptedState() {
83
        IntegratedDynamics.clog(org.apache.logging.log4j.Level.ERROR, "A corrupted part state was found at, repairing...");
×
84
        Thread.dumpStack();
×
85
        this.checkedForWriteVariable = false;
×
86
        this.deactivated = true;
×
87
    }
×
88

89
    /**
90
     * @return If there is an active variable present for this state.
91
     */
92
    public boolean hasVariable() {
93
        return (getGlobalErrors().isEmpty() || isRetryEvaluation()) && !getInventory().isEmpty();
15✔
94
    }
95

96
    /**
97
     * Get the active variable in this state.
98
     * @param <V> The variable value type.
99
     * @param network The network.
100
     * @param partNetwork The part network.
101
     * @param valueDeseralizationContext
102
     * @return The variable.
103
     */
104
    public <V extends IValue> IVariable<V> getVariable(INetwork network, IPartNetwork partNetwork, ValueDeseralizationContext valueDeseralizationContext) {
105
        if(!checkedForWriteVariable) {
3✔
106
            if (variableContainer.getVariableCache().isEmpty()) {
5✔
107
                variableContainer.refreshVariables(network, inventory, false, valueDeseralizationContext);
8✔
108
            }
109
            for (IVariableFacade facade : variableContainer.getVariableCache().values()) {
13✔
110
                if (facade != null) {
2!
111
                    currentVariableFacade = facade;
3✔
112
                    validate(network, partNetwork);
4✔
113
                }
114
            }
1✔
115
            this.checkedForWriteVariable = true;
3✔
116
        }
117
        if(currentVariableFacade == null) {
3!
118
            onCorruptedState();
×
119
            return null;
×
120
        }
121
        return currentVariableFacade.getVariable(network, partNetwork);
6✔
122
    }
123

124
    /**
125
     * Refresh the current variable to have its current info reset and updated.
126
     * @param partType The corresponding part type.
127
     * @param target The target of the part.
128
     */
129
    public void onVariableContentsUpdated(P partType, PartTarget target) {
130
        // Resets the errors for this aspect
131
        this.checkedForWriteVariable = false;
3✔
132
        addGlobalError(null);
3✔
133
        this.currentVariableFacade = null;
3✔
134
        //this.deactivated = false; // This *should* not be required anymore, re-activation is handled in AspectWriteBase#update.
135

136
        // Refresh any contained variables
137
        PartPos center = target.getCenter();
3✔
138
        Level level = center.getPos().getLevel(true);
5✔
139
        NetworkHelpers.getNetwork(level, center.getPos().getBlockPos(), center.getSide())
10✔
140
                .ifPresent(network -> variableContainer.refreshVariables(network, inventory, false, ValueDeseralizationContext.of(level)));
11✔
141
    }
1✔
142

143
    /**
144
     * @return All global error messages.
145
     */
146
    public List<Component> getGlobalErrors() {
147
        return globalErrorMessages;
3✔
148
    }
149

150
    /**
151
     * Add a global error message.
152
     * @param error The message to add.
153
     */
154
    public void addGlobalError(Component error) {
155
        setRetryEvaluation(false);
3✔
156
        if(error == null) {
2✔
157
            globalErrorMessages.clear();
4✔
158
        } else {
159
            globalErrorMessages.add(error);
5✔
160
        }
161
        onDirty();
2✔
162
        sendUpdate(); // We want this error messages to be sent to the client(s).
2✔
163
    }
1✔
164

165
    @Override
166
    public void serialize(ValueOutput valueOutput) {
167
        super.serialize(valueOutput);
3✔
168
        NBTClassType.writeNbt(List.class, "globalErrorMessages", globalErrorMessages, valueOutput);
6✔
169
        inventory.writeToNBT(valueOutput, "inventory");
5✔
170
    }
1✔
171

172
    @Override
173
    public void deserialize(ValueInput valueInput) {
174
        super.deserialize(valueInput);
3✔
175
        //noinspection unchecked
176
        this.globalErrorMessages = NBTClassType.readNbt(List.class, "globalErrorMessages", valueInput);
7✔
177
        inventory.readFromNBT(valueInput, "inventory");
5✔
178
    }
1✔
179

180
    @Override
181
    public <T> Optional<T> getCapability(P partType, PartCapability<T> capability, INetwork network, IPartNetwork partNetwork, PartTarget target) {
182
        if (capability == Capabilities.ValueInterface.PART) {
3✔
183
            if (hasVariable()) {
3!
184
                IVariable<IValue> variable = getVariable(network, partNetwork, ValueDeseralizationContext.of(target.getCenter().getPos().getLevel(true)));
11✔
185
                if (variable != null) {
2!
186
                    return (Optional<T>) Optional.<IValueInterface>of(() -> Optional.of(variable.getValue()));
8✔
187
                }
188
            }
189
            return Optional.empty();
×
190
        }
191
        return super.getCapability(partType, capability, network, partNetwork, target);
8✔
192
    }
193

194
    /**
195
     * An inventory that can only hold one filled slot at a time.
196
     */
197
    public static class SingularInventory extends SimpleInventory {
198

199
        /**
200
         * Make a new instance.
201
         *
202
         * @param size The amount of slots in the inventory.
203
         */
204
        public SingularInventory(int size) {
205
            super(size, 1);
4✔
206
        }
1✔
207

208
        protected boolean canInsert(int slot) {
209
            for (int i = 0; i < getContainerSize(); i++) {
×
210
                // Only allow insertion if the target slot is the same as the non-empty slot
211
                if (i != slot && !getItem(i).isEmpty()) {
×
212
                    return false;
×
213
                }
214
            }
215
            return true;
×
216
        }
217

218
        @Override
219
        public boolean canPlaceItem(int i, ItemStack itemstack) {
220
            return canInsert(i) && super.canPlaceItem(i, itemstack);
×
221
        }
222

223
    }
224

225
    public static class Validator implements IVariableFacade.IValidator {
226

227
        private final PartStateActiveVariableBase state;
228

229
        /**
230
         * Make a new instance
231
         * @param state The part state.
232
         */
233
        public Validator(PartStateActiveVariableBase state) {
2✔
234
            this.state = state;
3✔
235
        }
1✔
236

237
        @Override
238
        public void addError(MutableComponent error) {
239
            this.state.addGlobalError(error);
4✔
240
        }
1✔
241

242
    }
243

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