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

CyclopsMC / IntegratedDynamics / 16552051255

27 Jul 2025 01:58PM UTC coverage: 53.206% (+8.0%) from 45.161%
16552051255

push

github

rubensworks
Resolve minor TODOs

2888 of 8740 branches covered (33.04%)

Branch coverage included in aggregate %.

17341 of 29280 relevant lines covered (59.22%)

3.08 hits per line

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

86.67
/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.ResourceLocation;
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(ResourceLocation.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);
×
87
        }
88
        IAspectWrite activeAspect = getActiveAspect();
3✔
89
        if(activeAspect != null && activeAspect != newAspect) {
5!
90
            activeAspect.onDeactivate(partType, target, this);
5✔
91
        }
92
        if(newAspect != null && activeAspect != newAspect) {
5!
93
            newAspect.onActivate(partType, target, this);
5✔
94
        }
95
        this.activeAspect = newAspect;
3✔
96
    }
1✔
97

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

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

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

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

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

142
    public static class Validator implements IVariableFacade.IValidator {
143

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

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

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

162
    }
163

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