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

CyclopsMC / IntegratedDynamics / 23358440897

20 Mar 2026 07:05PM UTC coverage: 53.697% (-0.04%) from 53.738%
23358440897

push

github

rubensworks
Merge remote-tracking branch 'origin/master-1.21-lts' into master-1.21

3054 of 8913 branches covered (34.26%)

Branch coverage included in aggregate %.

18675 of 31553 relevant lines covered (59.19%)

3.06 hits per line

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

85.56
/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.network.chat.Component;
5
import net.minecraft.network.chat.MutableComponent;
6
import net.minecraft.resources.Identifier;
7
import net.minecraft.world.level.storage.ValueInput;
8
import net.minecraft.world.level.storage.ValueOutput;
9
import org.cyclops.cyclopscore.helper.CollectionHelpers;
10
import org.cyclops.cyclopscore.persist.nbt.NBTClassType;
11
import org.cyclops.integrateddynamics.api.item.IVariableFacade;
12
import org.cyclops.integrateddynamics.api.network.INetwork;
13
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
14
import org.cyclops.integrateddynamics.api.part.PartTarget;
15
import org.cyclops.integrateddynamics.api.part.aspect.IAspect;
16
import org.cyclops.integrateddynamics.api.part.aspect.IAspectWrite;
17
import org.cyclops.integrateddynamics.api.part.write.IPartStateWriter;
18
import org.cyclops.integrateddynamics.api.part.write.IPartTypeWriter;
19
import org.cyclops.integrateddynamics.core.part.PartStateActiveVariableBase;
20
import org.cyclops.integrateddynamics.part.aspect.Aspects;
21

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

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

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

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

41
    @Override
42
    public void serialize(ValueOutput valueOutput) {
43
        if (this.activeAspect != null) valueOutput.putString("activeAspectName", this.activeAspect.getUniqueName().toString());
10✔
44
        NBTClassType.getType(Map.class, this.errorMessages).writePersistedField("errorMessages", this.errorMessages, valueOutput);
9✔
45
        super.serialize(valueOutput);
3✔
46
    }
1✔
47

48
    @Override
49
    public void deserialize(ValueInput valueInput) {
50
        valueInput.getString("activeAspectName").ifPresent(activeAspect -> {
6✔
51
            IAspect aspect = Aspects.REGISTRY.getAspect(Identifier.parse(activeAspect));
5✔
52
            if (aspect instanceof IAspectWrite) {
3!
53
                this.activeAspect = (IAspectWrite) aspect;
4✔
54
            }
55
        });
1✔
56
        this.errorMessages = (Map<String, List<Component>>) NBTClassType.getType(Map.class, this.errorMessages).readPersistedField("errorMessages", valueInput);
10✔
57
        super.deserialize(valueInput);
3✔
58
    }
1✔
59

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

69
    @Override
70
    protected void onCorruptedState() {
71
        super.onCorruptedState();
×
72
        this.activeAspect = null;
×
73
    }
×
74

75
    @Override
76
    public boolean hasVariable() {
77
        return getActiveAspect() != null && getErrors(getActiveAspect()).isEmpty() && super.hasVariable();
16!
78
    }
79

80
    @Override
81
    public void triggerAspectInfoUpdate(P partType, PartTarget target, IAspectWrite newAspect, boolean isNetworkInitializing) {
82
        if (!isNetworkInitializing) {
2✔
83
            // We skip network content updates during network init,
84
            // as it will be called once for all parts right after network init.
85
            // This is to avoid re-updating variable contents many times during network init, which can get expensive.
86
            onVariableContentsUpdated(partType, target);
4✔
87

88
            IAspectWrite activeAspect = getActiveAspect();
3✔
89
            if(activeAspect != null && activeAspect != newAspect) {
2!
90
                activeAspect.onDeactivate(partType, target, this);
×
91
            }
92
            if(newAspect != null && activeAspect != newAspect) {
5!
93
                newAspect.onActivate(partType, target, this);
5✔
94
            }
95
            this.activeAspect = newAspect;
3✔
96
        }
97
    }
1✔
98

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

109
    @Override
110
    public IAspectWrite getActiveAspect() {
111
        return activeAspect;
3✔
112
    }
113

114
    @Override
115
    public List<Component> getErrors(IAspectWrite aspect) {
116
        List<Component> errors = errorMessages.get(aspect.getUniqueName().toString());
8✔
117
        if(errors == null) {
2✔
118
            return Collections.emptyList();
2✔
119
        }
120
        return errors;
2✔
121
    }
122

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

134
    @Override
135
    public boolean checkAndResetFirstTick() {
136
        if(firstTick) {
3✔
137
            firstTick = false;
3✔
138
            return true;
2✔
139
        }
140
        return false;
2✔
141
    }
142

143
    public static class Validator implements IVariableFacade.IValidator {
144

145
        private final IPartStateWriter state;
146
        private final IAspectWrite aspect;
147

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

158
        @Override
159
        public void addError(MutableComponent error) {
160
            this.state.addError(aspect, error);
6✔
161
        }
1✔
162

163
    }
164

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