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

CyclopsMC / IntegratedDynamics / 14009362267

22 Mar 2025 02:14PM UTC coverage: 38.611%. Remained the same
14009362267

push

github

rubensworks
Bump mod version

1956 of 8379 branches covered (23.34%)

Branch coverage included in aggregate %.

10127 of 22915 relevant lines covered (44.19%)

2.07 hits per line

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

74.3
/src/main/java/org/cyclops/integrateddynamics/core/helper/NbtHelpers.java
1
package org.cyclops.integrateddynamics.core.helper;
2

3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Sets;
5
import net.minecraft.nbt.*;
6
import net.minecraft.network.chat.Component;
7
import net.minecraft.network.chat.MutableComponent;
8
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
9
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
10
import org.cyclops.integrateddynamics.core.evaluate.variable.*;
11

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

15
/**
16
 * @author rubensworks
17
 */
18
public class NbtHelpers {
×
19

20
    /**
21
     * Check if the first tag is a subset of the second tag.
22
     * @param a An NBT tag.
23
     * @param b An NBT tag.
24
     * @param recursive If tags and list should be checked recursively. (list must be in the same order)
25
     * @return If tag a is a subset (or equal) of tag b.
26
     */
27
    public static boolean nbtMatchesSubset(CompoundTag a, CompoundTag b, boolean recursive) {
28
        for (String key : a.getAllKeys()) {
11✔
29
            Tag valueA = a.get(key);
4✔
30
            if (recursive && (valueA instanceof CompoundTag || valueA instanceof ListTag)) {
8!
31
                Tag valueB = b.get(key);
4✔
32
                if (valueA instanceof CompoundTag) {
3✔
33
                    if (!(valueB instanceof CompoundTag)) {
3!
34
                        return false;
×
35
                    }
36
                    CompoundTag tagA = (CompoundTag) valueA;
3✔
37
                    CompoundTag tagB = (CompoundTag) valueB;
3✔
38
                    if (!nbtMatchesSubset(tagA, tagB, recursive)) {
5!
39
                        return false;
×
40
                    }
41
                } else if (valueA instanceof ListTag) {
4!
42
                    if (!(valueB instanceof ListTag)) {
3!
43
                        return false;
×
44
                    }
45
                    ListTag tagA = (ListTag) valueA;
3✔
46
                    ListTag tagB = (ListTag) valueB;
3✔
47
                    for (int i = 0; i < tagA.size(); i++) {
8✔
48
                        CompoundTag subTagA = tagA.getCompound(i);
4✔
49
                        boolean foundA = false;
2✔
50
                        for (int j = 0; j < tagB.size(); j++) {
6!
51
                            CompoundTag subTagB = tagB.getCompound(j);
4✔
52
                            if (nbtMatchesSubset(subTagA, subTagB, recursive)) {
5!
53
                                foundA = true;
2✔
54
                                break;
1✔
55
                            }
56
                        }
57
                        if (!foundA) {
2!
58
                            return false;
×
59
                        }
60
                    }
61
                }
62
            } else {
1✔
63
                if (!valueA.equals(b.get(key))) {
6✔
64
                    return false;
2✔
65
                }
66
            }
67
        }
1✔
68
        return true;
2✔
69
    }
70

71
    /**
72
     * Create a new NBT tag that contains all entries from the given tags.
73
     * If multiple tags contain the same entry, the entry from the latest tag will be given preference.
74
     * If nested tags are present, these will be combined recursively.
75
     * @param tags NBT tags.
76
     * @return A new tag containing the combined entries from the given tags.
77
     */
78
    public static CompoundTag union(CompoundTag... tags) {
79
        CompoundTag tag = new CompoundTag();
4✔
80
        for (CompoundTag inputTag : tags) {
16✔
81
            tag.merge(inputTag);
4✔
82
        }
83
        return tag;
2✔
84
    }
85

86
    /**
87
     * Create a new NBT tag that contains the entries that are present in all given tags.
88
     * If nested tags are present, these will be intersected recursively.
89
     * @param tags NBT tags.
90
     * @return A new tag containing the intersected entries from the given tags.
91
     */
92
    public static CompoundTag intersection(CompoundTag... tags) {
93
        if (tags.length == 0) {
3!
94
            return new CompoundTag();
×
95
        }
96
        CompoundTag tag = null;
2✔
97
        for (CompoundTag inputTag : tags) {
16✔
98
            if (tag == null) {
2✔
99
                tag = inputTag.copy();
4✔
100
            } else {
101
                Set<String> keys = Sets.newHashSet(tag.getAllKeys());
4✔
102
                for (String key : keys) {
10✔
103
                    int type = tag.get(key).getId();
5✔
104
                    if (!inputTag.contains(key, type)) {
5✔
105
                        tag.remove(key);
4✔
106
                    } else if (type == Tag.TAG_COMPOUND) {
3✔
107
                        tag.put(key, intersection(tag.getCompound(key), inputTag.getCompound(key)));
19✔
108
                    }
109
                }
1✔
110
            }
111
        }
112
        return tag;
2✔
113
    }
114

115
    /**
116
     * Create a new NBT tag that contains all entries of the first tag minus the entries of the second tag.
117
     * If nested tags are present, these will be operated recursively.
118
     * @param a an NBT tag.
119
     * @param b an NBT tag.
120
     * @return A new tag containing the entries of a minus b.
121
     */
122
    public static CompoundTag minus(CompoundTag a, CompoundTag b) {
123
        CompoundTag tag = a.copy();
3✔
124
        for (String key : b.getAllKeys()) {
11✔
125
            int type = b.get(key).getId();
5✔
126
            if (tag.contains(key, type)) {
5✔
127
                if (type == Tag.TAG_COMPOUND) {
3✔
128
                    CompoundTag difference = minus(tag.getCompound(key), b.getCompound(key));
8✔
129
                    if (difference.isEmpty()) {
3!
130
                        tag.remove(key);
4✔
131
                    } else {
132
                        tag.put(key, difference);
×
133
                    }
134
                } else {
1✔
135
                    tag.remove(key);
3✔
136
                }
137
            }
138
        }
1✔
139
        return tag;
2✔
140
    }
141

142
    /**
143
     * Create an NBT List from the given NBT value list.
144
     * @param value An NBT value list.
145
     * @param operatorName An operator name for error reporting.
146
     * @return An NBT list.
147
     */
148
    public static ListTag getListNbtTag(ValueTypeList.ValueList<?, ?> value, Component operatorName) {
149
        ListTag list = new ListTag();
4✔
150
        for (IValue valueNbt : value.getRawValue()) {
11✔
151
            if (!(value.getRawValue().getValueType() == ValueTypes.NBT ||
5!
152
                    (value.getRawValue().getValueType() == ValueTypes.CATEGORY_ANY && valueNbt.getType() == ValueTypes.NBT))) {
×
153
                MutableComponent error = Component.translatable(
×
154
                        L10NValues.OPERATOR_ERROR_WRONGTYPE,
155
                        operatorName,
156
                        Component.translatable(value.getRawValue().getValueType().getTranslationKey()),
×
157
                        1,
×
158
                        Component.translatable(ValueTypes.NBT.getTranslationKey()));
×
159
                Helpers.sneakyThrow(new EvaluationException(error));
×
160
            }
161
            ((ValueTypeNbt.ValueNbt) valueNbt).getRawValue().ifPresent(list::add);
9✔
162
        }
1✔
163
        return list;
2✔
164
    }
165

166
    /**
167
     * Create an NBT byte array from the given integer value list.
168
     * @param value An integer value list.
169
     * @param operatorName An operator name for error reporting.
170
     * @return An NBT byte array.
171
     */
172
    public static ByteArrayTag getListNbtByte(ValueTypeList.ValueList<?, ?> value, Component operatorName) {
173
        List<Byte> list = Lists.newLinkedList();
2✔
174
        for (IValue valueNbt : value.getRawValue()) {
11✔
175
            if (valueNbt.getType() != ValueTypes.INTEGER) {
4!
176
                MutableComponent error = Component.translatable(
×
177
                        L10NValues.OPERATOR_ERROR_WRONGTYPE,
178
                        operatorName,
179
                        Component.translatable(valueNbt.getType().getTranslationKey()),
×
180
                        1,
×
181
                        Component.translatable(ValueTypes.INTEGER.getTranslationKey()));
×
182
                Helpers.sneakyThrow(new EvaluationException(error));
×
183
            }
184
            list.add((byte) ((ValueTypeInteger.ValueInteger) valueNbt).getRawValue());
8✔
185
        }
1✔
186
        return new ByteArrayTag(list);
5✔
187
    }
188

189
    /**
190
     * Create an NBT int array from the given integer value list.
191
     * @param value An integer value list.
192
     * @param operatorName An operator name for error reporting.
193
     * @return An NBT int array.
194
     */
195
    public static IntArrayTag getListNbtInt(ValueTypeList.ValueList<?, ?> value, Component operatorName) {
196
        List<Integer> list = Lists.newLinkedList();
2✔
197
        for (IValue valueNbt : value.getRawValue()) {
11✔
198
            if (valueNbt.getType() != ValueTypes.INTEGER) {
4!
199
                MutableComponent error = Component.translatable(
×
200
                        L10NValues.OPERATOR_ERROR_WRONGTYPE,
201
                        operatorName,
202
                        Component.translatable(valueNbt.getType().getTranslationKey()),
×
203
                        1,
×
204
                        Component.translatable(ValueTypes.INTEGER.getTranslationKey()));
×
205
                Helpers.sneakyThrow(new EvaluationException(error));
×
206
            }
207
            list.add(((ValueTypeInteger.ValueInteger) valueNbt).getRawValue());
7✔
208
        }
1✔
209
        return new IntArrayTag(list);
5✔
210
    }
211

212
    /**
213
     * Create an NBT long array from the given long value list.
214
     * @param value A long value list.
215
     * @param operatorName An operator name for error reporting.
216
     * @return An NBT long list.
217
     */
218
    public static LongArrayTag getListNbtLong(ValueTypeList.ValueList<?, ?> value, Component operatorName) {
219
        List<Long> list = Lists.newLinkedList();
2✔
220
        for (IValue valueNbt : value.getRawValue()) {
11✔
221
            if (valueNbt.getType() != ValueTypes.LONG) {
4!
222
                MutableComponent error = Component.translatable(
×
223
                        L10NValues.OPERATOR_ERROR_WRONGTYPE,
224
                        operatorName,
225
                        Component.translatable(valueNbt.getType().getTranslationKey()),
×
226
                        1,
×
227
                        Component.translatable(ValueTypes.LONG.getTranslationKey()));
×
228
                Helpers.sneakyThrow(new EvaluationException(error));
×
229
            }
230
            list.add(((ValueTypeLong.ValueLong) valueNbt).getRawValue());
7✔
231
        }
1✔
232
        return new LongArrayTag(list);
5✔
233
    }
234

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