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

CyclopsMC / IntegratedDynamics / 19236543894

10 Nov 2025 03:19PM UTC coverage: 44.832% (-0.005%) from 44.837%
19236543894

push

github

rubensworks
Fix failing testItemStackTag game test

2581 of 8546 branches covered (30.2%)

Branch coverage included in aggregate %.

11776 of 23478 relevant lines covered (50.16%)

2.38 hits per line

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

18.07
/src/main/java/org/cyclops/integrateddynamics/core/evaluate/variable/ValueObjectTypeItemStack.java
1
package org.cyclops.integrateddynamics.core.evaluate.variable;
2

3
import lombok.ToString;
4
import net.minecraft.advancements.critereon.ItemPredicate;
5
import net.minecraft.core.registries.BuiltInRegistries;
6
import net.minecraft.nbt.CompoundTag;
7
import net.minecraft.nbt.Tag;
8
import net.minecraft.network.chat.Component;
9
import net.minecraft.network.chat.MutableComponent;
10
import net.minecraft.world.item.ItemStack;
11
import org.cyclops.commoncapabilities.api.capability.itemhandler.ItemMatch;
12
import org.cyclops.cyclopscore.helper.ItemStackHelpers;
13
import org.cyclops.integrateddynamics.api.advancement.criterion.ValuePredicate;
14
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
15
import org.cyclops.integrateddynamics.api.evaluate.variable.*;
16
import org.cyclops.integrateddynamics.core.logicprogrammer.ValueTypeItemStackLPElement;
17
import org.cyclops.integrateddynamics.core.logicprogrammer.ValueTypeLPElementBase;
18

19
import java.util.Objects;
20
import java.util.Optional;
21

22
/**
23
 * Value type with values that are itemstacks.
24
 * @author rubensworks
25
 */
26
public class ValueObjectTypeItemStack extends ValueObjectTypeBase<ValueObjectTypeItemStack.ValueItemStack> implements
27
        IValueTypeNamed<ValueObjectTypeItemStack.ValueItemStack>,
28
        IValueTypeUniquelyNamed<ValueObjectTypeItemStack.ValueItemStack>,
29
        IValueTypeNullable<ValueObjectTypeItemStack.ValueItemStack> {
30

31
    public ValueObjectTypeItemStack() {
32
        super("itemstack", ValueObjectTypeItemStack.ValueItemStack.class);
4✔
33
    }
1✔
34

35
    public static MutableComponent getItemStackDisplayNameUsSafe(ItemStack itemStack) throws NoSuchMethodException {
36
        return !itemStack.isEmpty()
×
37
                ? (itemStack.getHoverName().copy().append((itemStack.getCount() > 1 ? " (" + itemStack.getCount() + ")" : "")))
×
38
                : Component.literal("");
×
39
    }
40

41
    public static MutableComponent getItemStackDisplayNameSafe(ItemStack itemStack) {
42
        // Certain mods may call client-side only methods,
43
        // so call a server-side-safe fallback method if that fails.
44
        try {
45
            return getItemStackDisplayNameUsSafe(itemStack);
×
46
        } catch (NoSuchMethodException e) {
×
47
            return Component.translatable(itemStack.getDescriptionId());
×
48
        }
49
    }
50

51
    @Override
52
    public ValueItemStack getDefault() {
53
        return ValueItemStack.of(ItemStack.EMPTY);
×
54
    }
55

56
    @Override
57
    public MutableComponent toCompactString(ValueItemStack value) {
58
        return ValueObjectTypeItemStack.getItemStackDisplayNameSafe(value.getRawValue());
×
59
    }
60

61
    @Override
62
    public Tag serialize(ValueDeseralizationContext valueDeseralizationContext, ValueItemStack value) {
63
        CompoundTag tag = new CompoundTag();
×
64
        ItemStack itemStack = value.getRawValue();
×
65
        if(!itemStack.isEmpty()) {
×
66
            tag.putInt("count", itemStack.getCount());
×
67
            int count = itemStack.getCount();
×
68
            if (itemStack.getCount() > 99) {
×
69
                tag.putInt("ExtendedCount", count);
×
70
                itemStack = itemStack.copy();
×
71
                itemStack.setCount(99);
×
72
            }
73
            return itemStack.save(valueDeseralizationContext.holderLookupProvider(), tag);
×
74
        }
75
        return tag;
×
76
    }
77

78
    @Override
79
    public ValueItemStack deserialize(ValueDeseralizationContext valueDeseralizationContext, Tag value) {
80
        if (value instanceof CompoundTag tag && !tag.isEmpty()) {
×
81
            // Forge returns air for tags with negative count,
82
            // so we set it to 1 for deserialization and fix it afterwards.
83
            int realCount = tag.getInt("count");
×
84
            // Consider the tag immutable, to avoid changes elsewhere
85
            tag = tag.copy();
×
86
            tag.putInt("count", 1);
×
87
            ItemStack itemStack = ItemStack.parseOptional(valueDeseralizationContext.holderLookupProvider(), tag);
×
88
            if (!itemStack.isEmpty()) {
×
89
                itemStack.setCount(realCount);
×
90
            }
91
            if (tag.contains("ExtendedCount", Tag.TAG_INT)) {
×
92
                itemStack.setCount(tag.getInt("ExtendedCount"));
×
93
            }
94
            return ValueItemStack.of(itemStack);
×
95
        } else {
96
            return ValueItemStack.of(ItemStack.EMPTY);
×
97
        }
98
    }
99

100
    @Override
101
    public String getName(ValueItemStack a) {
102
        return toCompactString(a).getString();
×
103
    }
104

105
    @Override
106
    public boolean isNull(ValueItemStack a) {
107
        return a.getRawValue().isEmpty();
×
108
    }
109

110
    @Override
111
    public ValueTypeLPElementBase createLogicProgrammerElement() {
112
        return new ValueTypeItemStackLPElement<>(this, new ValueTypeItemStackLPElement.IItemStackToValue<ValueObjectTypeItemStack.ValueItemStack>() {
×
113
            @Override
114
            public boolean isNullable() {
115
                return true;
×
116
            }
117

118
            @Override
119
            public Component validate(ItemStack itemStack) {
120
                return null;
×
121
            }
122

123
            @Override
124
            public ValueObjectTypeItemStack.ValueItemStack getValue(ItemStack itemStack) {
125
                return ValueObjectTypeItemStack.ValueItemStack.of(itemStack);
×
126
            }
127

128
            @Override
129
            public ItemStack getValueAsItemStack(ValueItemStack value) {
130
                return value.getRawValue();
×
131
            }
132
        });
133
    }
134

135
    @Override
136
    public ValueItemStack materialize(ValueItemStack value) throws EvaluationException {
137
        return ValueItemStack.of(value.getRawValue().copy());
×
138
    }
139

140
    @Override
141
    public String getUniqueName(ValueItemStack value) {
142
        ItemStack itemStack = value.getRawValue();
×
143
        return !itemStack.isEmpty() ? BuiltInRegistries.ITEM.getKey(itemStack.getItem()).toString() : "";
×
144
    }
145

146
    @ToString
5✔
147
    public static class ValueItemStack extends ValueBase {
148

149
        private final ItemStack itemStack;
150

151
        private ValueItemStack(ItemStack itemStack) {
152
            super(ValueTypes.OBJECT_ITEMSTACK);
3✔
153
            this.itemStack = Objects.requireNonNull(itemStack, "Attempted to create a ValueItemStack for a null ItemStack.");
6✔
154
        }
1✔
155

156
        public static ValueItemStack of(ItemStack itemStack) {
157
            return new ValueItemStack(itemStack);
5✔
158
        }
159

160
        public ItemStack getRawValue() {
161
            return itemStack;
3✔
162
        }
163

164
        @Override
165
        public boolean equals(Object o) {
166
            return o instanceof ValueItemStack && ItemMatch.areItemStacksEqual(((ValueItemStack) o).itemStack, this.itemStack, ItemMatch.EXACT);
15!
167
        }
168

169
        @Override
170
        public int hashCode() {
171
            return 37 + ItemStackHelpers.getItemStackHashCode(itemStack);
×
172
        }
173
    }
174

175
    public static class ValueItemStackPredicate extends ValuePredicate<ValueItemStack> {
176

177
        private final Optional<ItemPredicate> itemPredicate;
178

179
        public ValueItemStackPredicate(Optional<ItemPredicate> itemPredicate) {
180
            super(Optional.of(ValueTypes.OBJECT_ITEMSTACK), Optional.empty(), Optional.empty());
6✔
181
            this.itemPredicate = itemPredicate;
3✔
182
        }
1✔
183

184
        public Optional<ItemPredicate> getItemPredicate() {
185
            return itemPredicate;
×
186
        }
187

188
        @Override
189
        protected boolean testTyped(ValueItemStack value) {
190
            return super.testTyped(value) && (itemPredicate.isEmpty() || itemPredicate.get().test(value.getRawValue()));
×
191
        }
192
    }
193

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