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

CyclopsMC / CommonCapabilities / #479006782

09 Feb 2025 06:53AM UTC coverage: 40.018% (+0.3%) from 39.763%
#479006782

push

github

rubensworks
Add debug logger for DataComparator

2 of 3 new or added lines in 2 files covered. (66.67%)

914 of 2284 relevant lines covered (40.02%)

0.4 hits per line

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

96.0
/src/main/java/org/cyclops/commoncapabilities/ingredient/DataComparator.java
1
package org.cyclops.commoncapabilities.ingredient;
2

3
import net.minecraft.core.component.DataComponentMap;
4
import net.minecraft.core.component.DataComponentType;
5
import net.minecraft.core.registries.BuiltInRegistries;
6
import net.minecraft.resources.ResourceLocation;
7
import org.cyclops.commoncapabilities.CommonCapabilities;
8
import org.cyclops.commoncapabilities.GeneralConfig;
9

10
import javax.annotation.Nullable;
11
import java.util.Arrays;
12
import java.util.Comparator;
13
import java.util.Set;
14
import java.util.stream.Collectors;
15

16
/**
17
 * A comparator implementation for Data Components.
18
 * @author rubensworks
19
 */
20
public class DataComparator implements Comparator<DataComponentMap> {
21

22
    /**
23
     * A comparator for Data Components. (This is set in GeneralConfig)
24
     */
25
    public static Comparator<DataComponentMap> INSTANCE = new DataComparator(null);
1✔
26

27
    private final Set<ResourceLocation> ignoreDataComponentTypes;
28
    private Set<DataComponentType<?>> ignoreDataComponentTypeInstances;
29

30
    public DataComparator(@Nullable Set<ResourceLocation> ignoreDataComponentTypes) {
1✔
31
        this.ignoreDataComponentTypes = ignoreDataComponentTypes;
1✔
32
    }
1✔
33

34
    @Override
35
    public int compare(DataComponentMap o1, DataComponentMap o2) {
36
        return this.compare(o1, o2, this.ignoreDataComponentTypes);
1✔
37
    }
38

39
    protected int compare(DataComponentMap o1, DataComponentMap o2, @Nullable Set<ResourceLocation> ignoreDataComponentTypes) {
40
        // Return immediately if identical
41
        if (o1 == o2 || o1.equals(o2)) {
1✔
42
            return 0;
1✔
43
        }
44

45
        // Determine keys to compare
46
        Set<DataComponentType<?>> k1 = o1.keySet();
1✔
47
        Set<DataComponentType<?>> k2 = o2.keySet();
1✔
48

49
        // If relevant, ignore data components
50
        if (ignoreDataComponentTypes != null) {
1✔
51
            if (ignoreDataComponentTypeInstances == null) {
1✔
52
                ignoreDataComponentTypeInstances = ignoreDataComponentTypes.stream().map(BuiltInRegistries.DATA_COMPONENT_TYPE::get).collect(Collectors.toSet());
1✔
53
            }
54
            boolean filterK1 = false;
1✔
55
            boolean filterK2 = false;
1✔
56
            for (DataComponentType<?> dataComponentType : ignoreDataComponentTypeInstances) {
1✔
57
                if (!filterK1 && k1.contains(dataComponentType)) {
1✔
58
                    filterK1 = true;
1✔
59
                }
60
                if (!filterK2 && k2.contains(dataComponentType)) {
1✔
61
                    filterK2 = true;
1✔
62
                }
63
                if (filterK1 && filterK2) {
1✔
64
                    break;
1✔
65
                }
66
            }
1✔
67
            if (filterK1) {
1✔
68
                k1 = k1.stream().filter(k -> !ignoreDataComponentTypeInstances.contains(k)).collect(Collectors.toSet());
1✔
69
            }
70
            if (filterK2) {
1✔
71
                k2 = k2.stream().filter(k -> !ignoreDataComponentTypeInstances.contains(k)).collect(Collectors.toSet());
1✔
72
            }
73
        }
74

75
        // Check if keys are equal
76
        if (!k1.equals(k2)) {
1✔
77
            String[] k1a = k1.stream().map(k -> BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(k).toString()).toArray(String[]::new);
1✔
78
            String[] k2a = k2.stream().map(k -> BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(k).toString()).toArray(String[]::new);
1✔
79
            Arrays.sort(k1a);
1✔
80
            Arrays.sort(k2a);
1✔
81
            int minLength = Math.min(k1a.length, k2a.length);
1✔
82
            for (int i = 0; i < minLength; i++) {
1✔
83
                int result = k1a[i].compareTo(k2a[i]);
1✔
84
                if (result != 0) {
1✔
85
                    return result;
1✔
86
                }
87
            }
88
            return k1a.length - k2a.length;
1✔
89
        }
90

91
        // Compare values
92
        for (DataComponentType<?> key : k1) {
1✔
93
            int comp = this.compareRaw(o1.get(key), o2.get(key));
1✔
94
            if (comp != 0) {
1✔
95
                if (GeneralConfig.debugLogUnequalItemDataComponents) {
1✔
NEW
96
                    CommonCapabilities.clog(String.format("Data component mismatch on key '%s' for values '%s' and '%s'.", key, o1.get(key), o2.get(key)));
×
97
                }
98
                return comp;
1✔
99
            }
100
        }
1✔
101

102
        // Otherwise, assume equality
103
        return 0;
1✔
104
    }
105

106
    private int compareRaw(Object o1, Object o2) {
107
        if (o1 == o2 || o1.equals(o2)) {
1✔
108
            return 0;
1✔
109
        }
110
        if (o1 instanceof Comparable comparable) {
1✔
111
            return comparable.compareTo(o2);
1✔
112
        }
113
        return Integer.compare(System.identityHashCode(o1), System.identityHashCode(o2));
×
114
    }
115

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