• 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/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.network.chat.Component;
7
import net.minecraft.network.chat.MutableComponent;
8
import net.minecraft.world.item.ItemStack;
9
import net.minecraft.world.level.Level;
10
import net.minecraft.world.level.storage.ValueInput;
11
import net.minecraft.world.level.storage.ValueOutput;
12
import org.cyclops.cyclopscore.inventory.SimpleInventory;
13
import org.cyclops.cyclopscore.persist.nbt.NBTClassType;
14
import org.cyclops.integrateddynamics.Capabilities;
15
import org.cyclops.integrateddynamics.IntegratedDynamics;
16
import org.cyclops.integrateddynamics.api.block.IVariableContainer;
17
import org.cyclops.integrateddynamics.api.evaluate.IValueInterface;
18
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
19
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
20
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
21
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
22
import org.cyclops.integrateddynamics.api.network.INetwork;
23
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
24
import org.cyclops.integrateddynamics.api.part.IPartType;
25
import org.cyclops.integrateddynamics.api.part.PartCapability;
26
import org.cyclops.integrateddynamics.api.part.PartPos;
27
import org.cyclops.integrateddynamics.api.part.PartTarget;
28
import org.cyclops.integrateddynamics.capability.variablecontainer.VariableContainerDefault;
29
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
30

31
import java.util.List;
32
import java.util.Optional;
33

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

40
    private boolean checkedForWriteVariable = false;
×
41
    protected IVariableFacade currentVariableFacade = null;
×
42
    private final IVariableContainer variableContainer;
43
    @Getter
×
44
    @Setter
×
45
    private boolean deactivated = false;
46
    private SimpleInventory inventory;
47
    private List<Component> globalErrorMessages = Lists.newLinkedList();
×
48
    @Getter
×
49
    @Setter
×
50
    private boolean retryEvaluation = false;
51

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

59
    /**
60
     * @return The inner inventory
61
     */
62
    public SimpleInventory getInventory() {
63
        return this.inventory;
×
64
    }
65

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

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

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

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

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

126
        // Refresh any contained variables
127
        PartPos center = target.getCenter();
×
128
        Level level = center.getPos().getLevel(true);
×
129
        NetworkHelpers.getNetwork(level, center.getPos().getBlockPos(), center.getSide())
×
130
                .ifPresent(network -> variableContainer.refreshVariables(network, inventory, false, ValueDeseralizationContext.of(level)));
×
131
    }
×
132

133
    /**
134
     * @return All global error messages.
135
     */
136
    public List<Component> getGlobalErrors() {
137
        return globalErrorMessages;
×
138
    }
139

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

155
    @Override
156
    public void serialize(ValueOutput valueOutput) {
157
        super.serialize(valueOutput);
×
158
        NBTClassType.writeNbt(List.class, "globalErrorMessages", globalErrorMessages, valueOutput);
×
159
        inventory.writeToNBT(valueOutput, "inventory");
×
160
    }
×
161

162
    @Override
163
    public void deserialize(ValueInput valueInput) {
164
        super.deserialize(valueInput);
×
165
        //noinspection unchecked
166
        this.globalErrorMessages = NBTClassType.readNbt(List.class, "globalErrorMessages", valueInput);
×
167
        inventory.readFromNBT(valueInput, "inventory");
×
168
    }
×
169

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

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

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

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

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

213
    }
214

215
    public static class Validator implements IVariableFacade.IValidator {
216

217
        private final PartStateActiveVariableBase state;
218

219
        /**
220
         * Make a new instance
221
         * @param state The part state.
222
         */
223
        public Validator(PartStateActiveVariableBase state) {
×
224
            this.state = state;
×
225
        }
×
226

227
        @Override
228
        public void addError(MutableComponent error) {
229
            this.state.addGlobalError(error);
×
230
        }
×
231

232
    }
233

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