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

CyclopsMC / IntegratedCrafting / #479011729

10 Jun 2024 03:02PM UTC coverage: 25.044% (+0.1%) from 24.947%
#479011729

push

github

rubensworks
Update to NeoForge 1.20.4

0 of 52 new or added lines in 14 files covered. (0.0%)

3 existing lines in 3 files now uncovered.

706 of 2819 relevant lines covered (25.04%)

0.25 hits per line

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

35.38
/src/main/java/org/cyclops/integratedcrafting/core/MissingIngredients.java
1
package org.cyclops.integratedcrafting.core;
2

3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import net.minecraft.nbt.CompoundTag;
6
import net.minecraft.nbt.ListTag;
7
import net.minecraft.nbt.Tag;
8
import net.minecraft.resources.ResourceLocation;
9
import org.cyclops.commoncapabilities.api.ingredient.IPrototypedIngredient;
10
import org.cyclops.commoncapabilities.api.ingredient.IngredientComponent;
11

12
import java.util.List;
13
import java.util.Map;
14

15
/**
16
 * A list with missing ingredients (non-slot-based).
17
 * @param <T> The instance type.
18
 * @param <M> The matching condition parameter, may be Void.
19
 * @author rubensworks
20
 */
21
public class MissingIngredients<T, M> {
22

23
    private final List<MissingIngredients.Element<T, M>> elements;
24

25
    public MissingIngredients(List<MissingIngredients.Element<T, M>> elements) {
1✔
26
        this.elements = elements;
1✔
27
    }
1✔
28

29
    public List<Element<T, M>> getElements() {
30
        return elements;
1✔
31
    }
32

33
    @Override
34
    public boolean equals(Object obj) {
35
        return obj instanceof MissingIngredients && this.getElements().equals(((MissingIngredients) obj).getElements());
1✔
36
    }
37

38
    @Override
39
    public String toString() {
40
        return getElements().toString();
×
41
    }
42

43
    /**
44
     * Deserialize ingredients to NBT.
45
     * @param ingredients Ingredients.
46
     * @return An NBT representation of the given ingredients.
47
     */
48
    public static CompoundTag serialize(Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> ingredients) {
49
        CompoundTag tag = new CompoundTag();
×
50
        for (Map.Entry<IngredientComponent<?, ?>, MissingIngredients<?, ?>> entry : ingredients.entrySet()) {
×
51
            ListTag missingIngredientsTag = new ListTag();
×
52
            for (Element<?, ?> element : entry.getValue().getElements()) {
×
53
                ListTag elementsTag = new ListTag();
×
54
                for (PrototypedWithRequested<?, ?> alternative : element.getAlternatives()) {
×
55
                    CompoundTag alternativeTag = new CompoundTag();
×
56
                    alternativeTag.put("requestedPrototype", IPrototypedIngredient.serialize(alternative.getRequestedPrototype()));
×
57
                    alternativeTag.putLong("quantityMissing", alternative.getQuantityMissing());
×
58
                    alternativeTag.putBoolean("inputReusable", element.isInputReusable()); // Hack, should actually be one level higher, but this is for backwards-compat
×
59
                    elementsTag.add(alternativeTag);
×
60
                }
×
61
                missingIngredientsTag.add(elementsTag);
×
62
            }
×
63
            tag.put(entry.getKey().getName().toString(), missingIngredientsTag);
×
64
        }
×
65
        return tag;
×
66
    }
67

68
    /**
69
     * Deserialize ingredients from NBT
70
     * @param tag An NBT tag.
71
     * @return A new mixed ingredients instance.
72
     * @throws IllegalArgumentException If the given tag is invalid or does not contain data on the given ingredients.
73
     */
74
    public static Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> deserialize(CompoundTag tag)
75
            throws IllegalArgumentException {
76
        Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> map = Maps.newIdentityHashMap();
×
77
        for (String componentName : tag.getAllKeys()) {
×
NEW
78
            IngredientComponent<?, ?> component = IngredientComponent.REGISTRY.get(new ResourceLocation(componentName));
×
79
            if (component == null) {
×
80
                throw new IllegalArgumentException("Could not find the ingredient component type " + componentName);
×
81
            }
82

83
            List<MissingIngredients.Element<?, ?>> elements = Lists.newArrayList();
×
84

85
            ListTag missingIngredientsTag = tag.getList(componentName, Tag.TAG_LIST);
×
86
            for (int i = 0; i < missingIngredientsTag.size(); i++) {
×
87
                ListTag elementsTag = (ListTag) missingIngredientsTag.get(i);
×
88
                List<MissingIngredients.PrototypedWithRequested<?, ?>> alternatives = Lists.newArrayList();
×
89
                boolean inputReusable = false;
×
90
                for (int j = 0; j < elementsTag.size(); j++) {
×
91
                    CompoundTag alternativeTag = elementsTag.getCompound(j);
×
92
                    IPrototypedIngredient<?, ?> requestedPrototype = IPrototypedIngredient.deserialize(alternativeTag.getCompound("requestedPrototype"));
×
93
                    long quantityMissing = alternativeTag.getLong("quantityMissing");
×
94
                    inputReusable = alternativeTag.getBoolean("inputReusable");
×
95
                    alternatives.add(new PrototypedWithRequested<>(requestedPrototype, quantityMissing));
×
96
                }
97
                elements.add(new Element(alternatives, inputReusable));
×
98
            }
99

100
            MissingIngredients<?, ?> missingIngredients = new MissingIngredients(elements);
×
101
            map.put(component, missingIngredients);
×
102
        }
×
103
        return map;
×
104
    }
105

106
    /**
107
     * A list of alternatives for the given element.
108
     * @param <T> The instance type.
109
     * @param <M> The matching condition parameter, may be Void.
110
     */
111
    public static class Element<T, M> {
112

113
        private final List<MissingIngredients.PrototypedWithRequested<T, M>> alternatives;
114
        private final boolean inputReusable;
115

116
        public Element(List<MissingIngredients.PrototypedWithRequested<T, M>> alternatives, boolean inputReusable) {
1✔
117
            this.alternatives = alternatives;
1✔
118
            this.inputReusable = inputReusable;
1✔
119
        }
1✔
120

121
        public List<PrototypedWithRequested<T, M>> getAlternatives() {
122
            return alternatives;
1✔
123
        }
124

125
        public boolean isInputReusable() {
126
            return inputReusable;
1✔
127
        }
128

129
        @Override
130
        public boolean equals(Object obj) {
131
            return obj instanceof Element
1✔
132
                    && this.getAlternatives().equals(((Element) obj).getAlternatives())
1✔
133
                    && this.isInputReusable() == ((Element) obj).isInputReusable();
1✔
134
        }
135

136
        @Override
137
        public String toString() {
138
            return getAlternatives().toString() + "::" + isInputReusable();
×
139
        }
140
    }
141

142
    /**
143
     * A prototype with a missing quantity,
144
     * together with the total requested quantity.
145
     * @param <T> The instance type.
146
     * @param <M> The matching condition parameter, may be Void.
147
     */
148
    public static class PrototypedWithRequested<T, M> {
149

150
        private final IPrototypedIngredient<T, M> requestedPrototype;
151
        private final long quantityMissing;
152

153
        public PrototypedWithRequested(IPrototypedIngredient<T, M> requestedPrototype, long quantityMissing) {
1✔
154
            this.requestedPrototype = requestedPrototype;
1✔
155
            this.quantityMissing = quantityMissing;
1✔
156
        }
1✔
157

158
        public IPrototypedIngredient<T, M> getRequestedPrototype() {
159
            return requestedPrototype;
1✔
160
        }
161

162
        public long getQuantityMissing() {
163
            return quantityMissing;
1✔
164
        }
165

166
        @Override
167
        public boolean equals(Object obj) {
168
            return obj instanceof PrototypedWithRequested
1✔
169
                    && this.getRequestedPrototype().equals(((PrototypedWithRequested) obj).getRequestedPrototype())
1✔
170
                    && this.getQuantityMissing() == ((PrototypedWithRequested) obj).getQuantityMissing();
1✔
171
        }
172

173
        @Override
174
        public String toString() {
175
            return String.format("[Prototype: %s; missing: %s]", getRequestedPrototype(), getQuantityMissing());
×
176
        }
177
    }
178

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

© 2025 Coveralls, Inc