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

CyclopsMC / IntegratedDynamics / 13812833643

12 Mar 2025 01:41PM UTC coverage: 38.611% (-0.02%) from 38.629%
13812833643

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

11.76
/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
                itemStack = itemStack.copy();
×
70
                itemStack.setCount(99);
×
71
            }
72
            if (count > 127) {
×
73
                tag.putInt("ExtendedCount", count);
×
74
            }
75
            return itemStack.save(valueDeseralizationContext.holderLookupProvider(), tag);
×
76
        }
77
        return tag;
×
78
    }
79

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

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

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

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

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

125
            @Override
126
            public ValueObjectTypeItemStack.ValueItemStack getValue(ItemStack itemStack) {
127
                return ValueObjectTypeItemStack.ValueItemStack.of(itemStack);
×
128
            }
129
        });
130
    }
131

132
    @Override
133
    public ValueItemStack materialize(ValueItemStack value) throws EvaluationException {
134
        return ValueItemStack.of(value.getRawValue().copy());
×
135
    }
136

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

143
    @ToString
×
144
    public static class ValueItemStack extends ValueBase {
145

146
        private final ItemStack itemStack;
147

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

153
        public static ValueItemStack of(ItemStack itemStack) {
154
            return new ValueItemStack(itemStack);
5✔
155
        }
156

157
        public ItemStack getRawValue() {
158
            return itemStack;
3✔
159
        }
160

161
        @Override
162
        public boolean equals(Object o) {
163
            return o instanceof ValueItemStack && ItemMatch.areItemStacksEqual(((ValueItemStack) o).itemStack, this.itemStack, ItemMatch.EXACT);
×
164
        }
165

166
        @Override
167
        public int hashCode() {
168
            return 37 + ItemStackHelpers.getItemStackHashCode(itemStack);
×
169
        }
170
    }
171

172
    public static class ValueItemStackPredicate extends ValuePredicate<ValueItemStack> {
173

174
        private final Optional<ItemPredicate> itemPredicate;
175

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

181
        public Optional<ItemPredicate> getItemPredicate() {
182
            return itemPredicate;
×
183
        }
184

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

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