• 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

78.41
/src/main/java/org/cyclops/integrateddynamics/core/part/write/PartStateWriterBase.java
1
package org.cyclops.integrateddynamics.core.part.write;
2

3
import com.google.common.collect.Maps;
4
import net.minecraft.nbt.CompoundTag;
5
import net.minecraft.network.chat.MutableComponent;
6
import net.minecraft.resources.ResourceLocation;
7
import org.cyclops.cyclopscore.helper.CollectionHelpers;
8
import org.cyclops.cyclopscore.persist.nbt.NBTClassType;
9
import org.cyclops.integrateddynamics.api.evaluate.variable.ValueDeseralizationContext;
10
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
11
import org.cyclops.integrateddynamics.api.network.INetwork;
12
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
13
import org.cyclops.integrateddynamics.api.part.PartTarget;
14
import org.cyclops.integrateddynamics.api.part.aspect.IAspect;
15
import org.cyclops.integrateddynamics.api.part.aspect.IAspectWrite;
16
import org.cyclops.integrateddynamics.api.part.write.IPartStateWriter;
17
import org.cyclops.integrateddynamics.api.part.write.IPartTypeWriter;
18
import org.cyclops.integrateddynamics.core.part.PartStateActiveVariableBase;
19
import org.cyclops.integrateddynamics.part.aspect.Aspects;
20

21
import java.util.Collections;
22
import java.util.List;
23
import java.util.Map;
24

25
/**
26
 * A default implementation of the {@link IPartTypeWriter}.
27
 * @author rubensworks
28
 */
29
public class PartStateWriterBase<P extends IPartTypeWriter>
30
        extends PartStateActiveVariableBase<P> implements IPartStateWriter<P> {
31

32
    private IAspectWrite activeAspect = null;
3✔
33
    private Map<String, List<MutableComponent>> errorMessages = Maps.newHashMap();
3✔
34
    private boolean firstTick = true;
3✔
35

36
    public PartStateWriterBase(int inventorySize) {
37
        super(inventorySize);
3✔
38
    }
1✔
39

40
    @Override
41
    public void writeToNBT(ValueDeseralizationContext valueDeseralizationContext, CompoundTag tag) {
42
        if (this.activeAspect != null) tag.putString("activeAspectName", this.activeAspect.getUniqueName().toString());
10✔
43
        NBTClassType.getType(Map.class, this.errorMessages).writePersistedField("errorMessages", this.errorMessages, tag, valueDeseralizationContext.holderLookupProvider());
11✔
44
        super.writeToNBT(valueDeseralizationContext, tag);
4✔
45
    }
1✔
46

47
    @Override
48
    public void readFromNBT(ValueDeseralizationContext valueDeseralizationContext, CompoundTag tag) {
49
        IAspect aspect = Aspects.REGISTRY.getAspect(ResourceLocation.parse(tag.getString("activeAspectName")));
×
50
        if (aspect instanceof IAspectWrite) {
×
51
            this.activeAspect = (IAspectWrite) aspect;
×
52
        }
53
        this.errorMessages = (Map<String, List<MutableComponent>>) NBTClassType.getType(Map.class, this.errorMessages).readPersistedField("errorMessages", tag, valueDeseralizationContext.holderLookupProvider());
×
54
        super.readFromNBT(valueDeseralizationContext, tag);
×
55
    }
×
56

57
    @Override
58
    protected void validate(INetwork network, IPartNetwork partNetwork) {
59
        // Note that this is only called server-side, so these errors are sent via NBT to the client(s).
60
        if(getActiveAspect() != null) {
3!
61
            this.currentVariableFacade.validate(network, partNetwork,
9✔
62
                    new PartStateWriterBase.Validator(this, getActiveAspect()), getActiveAspect().getValueType());
5✔
63
        }
64
    }
1✔
65

66
    @Override
67
    protected void onCorruptedState() {
68
        super.onCorruptedState();
×
69
        this.activeAspect = null;
×
70
    }
×
71

72
    @Override
73
    public boolean hasVariable() {
74
        return getActiveAspect() != null && getErrors(getActiveAspect()).isEmpty() && super.hasVariable();
16!
75
    }
76

77
    @Override
78
    public void triggerAspectInfoUpdate(P partType, PartTarget target, IAspectWrite newAspect, boolean isNetworkInitializing) {
79
        if (!isNetworkInitializing) {
2!
80
            // We skip network content updates during network init,
81
            // as it will be called once for all parts right after network init.
82
            // This is to avoid re-updating variable contents many times during network init, which can get expensive.
83
            onVariableContentsUpdated(partType, target);
×
84
        }
85
        IAspectWrite activeAspect = getActiveAspect();
3✔
86
        if(activeAspect != null && activeAspect != newAspect) {
5!
87
            activeAspect.onDeactivate(partType, target, this);
5✔
88
        }
89
        if(newAspect != null && activeAspect != newAspect) {
5!
90
            newAspect.onActivate(partType, target, this);
5✔
91
        }
92
        this.activeAspect = newAspect;
3✔
93
    }
1✔
94

95
    @Override
96
    public void onVariableContentsUpdated(P partType, PartTarget target) {
97
        // Resets the errors for this aspect
98
        super.onVariableContentsUpdated(partType, target);
4✔
99
        IAspectWrite activeAspect = getActiveAspect();
3✔
100
        if(activeAspect != null) {
2!
101
            addError(activeAspect, null);
4✔
102
        }
103
    }
1✔
104

105
    @Override
106
    public IAspectWrite getActiveAspect() {
107
        return activeAspect;
3✔
108
    }
109

110
    @Override
111
    public List<MutableComponent> getErrors(IAspectWrite aspect) {
112
        List<MutableComponent> errors = errorMessages.get(aspect.getUniqueName().toString());
8✔
113
        if(errors == null) {
2✔
114
            return Collections.emptyList();
2✔
115
        }
116
        return errors;
2✔
117
    }
118

119
    @Override
120
    public void addError(IAspectWrite aspect, MutableComponent error) {
121
        if(error == null) {
2✔
122
            errorMessages.remove(aspect.getUniqueName().toString());
8✔
123
        } else {
124
            CollectionHelpers.addToMapList(errorMessages, aspect.getUniqueName().toString(), error);
7✔
125
        }
126
        onDirty();
2✔
127
        sendUpdate(); // We want this error messages to be sent to the client(s).
2✔
128
    }
1✔
129

130
    @Override
131
    public boolean checkAndResetFirstTick() {
132
        if(firstTick) {
3✔
133
            firstTick = false;
3✔
134
            return true;
2✔
135
        }
136
        return false;
2✔
137
    }
138

139
    public static class Validator implements IVariableFacade.IValidator {
140

141
        private final IPartStateWriter state;
142
        private final IAspectWrite aspect;
143

144
        /**
145
         * Make a new instance
146
         * @param state The part state.
147
         * @param aspect The aspect to set the error for.
148
         */
149
        public Validator(IPartStateWriter state, IAspectWrite aspect) {
2✔
150
            this.state = state;
3✔
151
            this.aspect = aspect;
3✔
152
        }
1✔
153

154
        @Override
155
        public void addError(MutableComponent error) {
156
            this.state.addError(aspect, error);
6✔
157
        }
1✔
158

159
    }
160

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