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

CyclopsMC / CommonCapabilities / #479006749

13 Oct 2024 03:28PM UTC coverage: 39.443% (+0.03%) from 39.417%
#479006749

push

github

rubensworks
Bump mod version

893 of 2264 relevant lines covered (39.44%)

0.39 hits per line

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

95.24
/src/main/java/org/cyclops/commoncapabilities/ingredient/IngredientSerializerItemStack.java
1
package org.cyclops.commoncapabilities.ingredient;
2

3
import com.google.gson.JsonParseException;
4
import net.minecraft.core.HolderLookup;
5
import net.minecraft.nbt.CompoundTag;
6
import net.minecraft.nbt.IntTag;
7
import net.minecraft.nbt.NbtOps;
8
import net.minecraft.nbt.Tag;
9
import net.minecraft.world.item.ItemStack;
10
import org.cyclops.commoncapabilities.api.ingredient.IIngredientSerializer;
11

12
/**
13
 * Serializer for ItemStacks.
14
 * @author rubensworks
15
 */
16
public class IngredientSerializerItemStack implements IIngredientSerializer<ItemStack, Integer> {
1✔
17
    @Override
18
    public Tag serializeInstance(HolderLookup.Provider lookupProvider, ItemStack instance) {
19
        int count = instance.getCount();
1✔
20
        if (instance.getCount() > 99) {
1✔
21
            instance = instance.copy();
1✔
22
            instance.setCount(99);
1✔
23
        }
24
        Tag tag = ItemStack.OPTIONAL_CODEC.encodeStart(lookupProvider.createSerializationContext(NbtOps.INSTANCE), instance)
1✔
25
                .getOrThrow(JsonParseException::new);
1✔
26
        if (count > 127) {
1✔
27
            ((CompoundTag) tag).putInt("ExtendedCount", count);
1✔
28
        }
29
        return tag;
1✔
30
    }
31

32
    @Override
33
    public ItemStack deserializeInstance(HolderLookup.Provider lookupProvider, Tag tag) throws IllegalArgumentException {
34
        ItemStack itemStack = ItemStack.OPTIONAL_CODEC.parse(lookupProvider.createSerializationContext(NbtOps.INSTANCE), tag)
1✔
35
                .getOrThrow(JsonParseException::new);
1✔
36

37

38
        if (!(tag instanceof CompoundTag stackTag)) {
1✔
39
            throw new IllegalArgumentException("This deserializer only accepts NBTTagCompound");
×
40
        }
41
        if (stackTag.contains("ExtendedCount", Tag.TAG_INT)) {
1✔
42
            itemStack.setCount(stackTag.getInt("ExtendedCount"));
1✔
43
        }
44

45
        return itemStack;
1✔
46
    }
47

48
    @Override
49
    public Tag serializeCondition(Integer matchCondition) {
50
        return IntTag.valueOf(matchCondition);
1✔
51
    }
52

53
    @Override
54
    public Integer deserializeCondition(Tag tag) throws IllegalArgumentException {
55
        if (!(tag instanceof IntTag)) {
1✔
56
            throw new IllegalArgumentException("This deserializer only accepts NBTTagInt");
1✔
57
        }
58
        return ((IntTag) tag).getAsInt();
1✔
59
    }
60
}
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