• 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/evaluate/InventoryVariableEvaluator.java
1
package org.cyclops.integrateddynamics.core.evaluate;
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.Container;
7
import net.minecraft.world.item.ItemStack;
8
import org.cyclops.integrateddynamics.IntegratedDynamics;
9
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
10
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
11
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
12
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
13
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
14
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
15
import org.cyclops.integrateddynamics.api.network.INetwork;
16
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
17
import org.cyclops.integrateddynamics.core.helper.L10NValues;
18
import org.cyclops.integrateddynamics.core.helper.NetworkHelpers;
19
import org.cyclops.integrateddynamics.core.network.event.VariableContentsUpdatedEvent;
20

21
import javax.annotation.Nullable;
22
import java.util.List;
23
import java.util.function.Supplier;
24

25
/**
26
 * A convenience holder class for getting variables from variable cards in a certain inventory slot.
27
 * @param <V> The variable value type
28
 * @author rubensworks
29
 */
30
public class InventoryVariableEvaluator<V extends IValue> implements IVariableFacade.IValidator {
31

32
    private final IVariableFacadeHandlerRegistry handler = IntegratedDynamics._instance.getRegistryManager()
×
33
            .getRegistry(IVariableFacadeHandlerRegistry.class);
×
34
    private final Container inventory;
35
    private final int slot;
36
    private final Supplier<ValueDeseralizationContext> valueDeseralizationContext;
37
    private final IValueType containingValueType;
38

39
    private IVariableFacade variableStored = null;
×
40
    private List<MutableComponent> errors = Lists.newLinkedList();
×
41

42
    public InventoryVariableEvaluator(Container inventory, int slot, Supplier<ValueDeseralizationContext> valueDeseralizationContext, IValueType<V> containingValueType) {
×
43
        this.inventory = inventory;
×
44
        this.slot = slot;
×
45
        this.valueDeseralizationContext = valueDeseralizationContext;
×
46
        this.containingValueType = containingValueType;
×
47
    }
×
48

49
    public InventoryVariableEvaluator(Container inventory, int slot, ValueDeseralizationContext valueDeseralizationContext, IValueType<V> containingValueType) {
50
        this(inventory, slot, () -> valueDeseralizationContext, containingValueType);
×
51
    }
×
52

53
    /**
54
     * @return If the configured slot has an item.
55
     */
56
    public boolean hasVariable() {
57
        return !inventory.getItem(slot).isEmpty();
×
58
    }
59

60
    /**
61
     * Refresh the variable reference by checking the inventory,
62
     * and validating the containing variable.
63
     * @param network The network.
64
     * @param sendVariablesUpdateEvent If a {@link VariableContentsUpdatedEvent} event must be sent
65
     *                                 if the variable has changed.
66
     */
67
    public void refreshVariable(@Nullable INetwork network, boolean sendVariablesUpdateEvent) {
68
        IPartNetwork partNetwork = NetworkHelpers.getPartNetwork(network).orElse(null);
×
69

70
        int lastVariabledId = this.variableStored == null ? -1 : this.variableStored.getId();
×
71
        int variableId = -1;
×
72
        if (!inventory.getItem(slot).isEmpty() && NetworkHelpers.shouldWork()) {
×
73
            // Update proxy input
74
            ItemStack itemStack = inventory.getItem(slot);
×
75
            this.variableStored = handler.handle(valueDeseralizationContext.get(), itemStack);
×
76
            if(this.variableStored != null) {
×
77
                variableId = this.variableStored.getId();
×
78
            }
79
        } else {
×
80
            this.variableStored = null;
×
81
        }
82

83
        clearErrors();
×
84
        if (partNetwork == null) {
×
85
            addError(Component.translatable(L10NValues.GENERAL_ERROR_NONETWORK));
×
86
        } else if (this.variableStored != null) {
×
87
            preValidate();
×
88
            try {
89
                variableStored.validate(network, partNetwork, this, containingValueType);
×
90
            } catch (IllegalArgumentException e) {
×
91
                addError(Component.translatable(e.getMessage()));
×
92
            }
×
93
        }
94
        if(sendVariablesUpdateEvent && partNetwork != null && lastVariabledId != variableId) {
×
95
            network.getEventBus().post(new VariableContentsUpdatedEvent(network));
×
96
        }
97
    }
×
98

99
    @Nullable
100
    public IVariable<V> getVariable(INetwork network) {
101
        return getVariable(network, NetworkHelpers.getPartNetworkChecked(network));
×
102
    }
103

104
    @Nullable
105
    public IVariable<V> getVariable(INetwork network, IPartNetwork partNetwork) {
106
        if(getVariableFacade() == null || !getErrors().isEmpty()) return null;
×
107
        try {
108
            return getVariableFacade().getVariable(network, partNetwork);
×
109
        } catch (IllegalArgumentException e) {
×
110
            addError(Component.translatable(e.getMessage()));
×
111
            return null;
×
112
        }
113
    }
114

115
    public IVariableFacade getVariableFacade() {
116
        return variableStored;
×
117
    }
118

119
    protected void preValidate() {
120

121
    }
×
122

123
    public void clearErrors() {
124
        this.errors.clear();
×
125
        onErrorsChanged();
×
126
    }
×
127

128
    public void setErrors(List<MutableComponent> errors) {
129
        this.errors = errors;
×
130
        onErrorsChanged();
×
131
    }
×
132

133
    public List<MutableComponent> getErrors() {
134
        return errors;
×
135
    }
136

137
    @Override
138
    public void addError(MutableComponent error) {
139
        errors.add(error);
×
140
        onErrorsChanged();
×
141
    }
×
142

143
    public void onErrorsChanged() {
144

145
    }
×
146
}
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