• 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/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;
×
34
    private Map<String, List<Component>> errorMessages = Maps.newHashMap();
×
35
    private boolean firstTick = true;
×
36

37
    public PartStateWriterBase(int inventorySize) {
38
        super(inventorySize);
×
39
    }
×
40

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

48
    @Override
49
    public void deserialize(ValueInput valueInput) {
50
        valueInput.getString("activeAspectName").ifPresent(activeAspect -> {
×
51
            IAspect aspect = Aspects.REGISTRY.getAspect(ResourceLocation.parse(activeAspect));
×
52
            if (aspect instanceof IAspectWrite) {
×
53
                this.activeAspect = (IAspectWrite) aspect;
×
54
            }
55
        });
×
56
        this.errorMessages = (Map<String, List<Component>>) NBTClassType.getType(Map.class, this.errorMessages).readPersistedField("errorMessages", valueInput);
×
57
        super.deserialize(valueInput);
×
58
    }
×
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) {
×
64
            this.currentVariableFacade.validate(network, partNetwork,
×
65
                    new PartStateWriterBase.Validator(this, getActiveAspect()), getActiveAspect().getValueType());
×
66
        }
67
    }
×
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();
×
78
    }
79

80
    @Override
81
    public void triggerAspectInfoUpdate(P partType, PartTarget target, IAspectWrite newAspect, boolean isNetworkInitializing) {
82
        if (!isNetworkInitializing) {
×
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();
×
89
        if(activeAspect != null && activeAspect != newAspect) {
×
90
            activeAspect.onDeactivate(partType, target, this);
×
91
        }
92
        if(newAspect != null && activeAspect != newAspect) {
×
93
            newAspect.onActivate(partType, target, this);
×
94
        }
95
        this.activeAspect = newAspect;
×
96
    }
×
97

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

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

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

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

133
    @Override
134
    public boolean checkAndResetFirstTick() {
135
        if(firstTick) {
×
136
            firstTick = false;
×
137
            return true;
×
138
        }
139
        return false;
×
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) {
×
153
            this.state = state;
×
154
            this.aspect = aspect;
×
155
        }
×
156

157
        @Override
158
        public void addError(MutableComponent error) {
159
            this.state.addError(aspect, error);
×
160
        }
×
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