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

CyclopsMC / CyclopsCore / #479033781

11 Mar 2025 06:01AM UTC coverage: 22.153% (-0.002%) from 22.155%
#479033781

push

github

rubensworks
Bump mod version

2297 of 10369 relevant lines covered (22.15%)

0.22 hits per line

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

0.0
/src/main/java/org/cyclops/cyclopscore/capability/fluid/FluidHandlerItemCapacity.java
1
package org.cyclops.cyclopscore.capability.fluid;
2

3
import net.minecraft.core.Direction;
4
import net.minecraft.nbt.CompoundTag;
5
import net.minecraft.nbt.Tag;
6
import net.minecraft.world.item.ItemStack;
7
import net.minecraft.world.level.material.Fluid;
8
import net.minecraftforge.common.capabilities.Capability;
9
import net.minecraftforge.common.util.INBTSerializable;
10
import net.minecraftforge.common.util.LazyOptional;
11
import net.minecraftforge.fluids.FluidStack;
12
import net.minecraftforge.fluids.capability.templates.FluidHandlerItemStack;
13
import org.cyclops.cyclopscore.Capabilities;
14

15
import javax.annotation.Nullable;
16

17
/**
18
 * An itemfluid handler with a mutable capacity.
19
 * @author rubensworks
20
 */
21
public class FluidHandlerItemCapacity extends FluidHandlerItemStack implements IFluidHandlerItemCapacity, IFluidHandlerMutable, INBTSerializable {
22

23
    private final Fluid fluid;
24
    private final int capacityDefault;
25

26
    /**
27
     * @param container The container itemStack, data is stored on it directly as NBT.
28
     * @param capacity  The maximum capacity of this fluid tank.
29
     */
30
    public FluidHandlerItemCapacity(ItemStack container, int capacity) {
31
        this(container, capacity, null);
×
32
    }
×
33

34
    /**
35
     * @param container The container itemStack, data is stored on it directly as NBT.
36
     * @param capacity  The maximum capacity of this fluid tank.
37
     * @param fluid     The accepted fluid.
38
     */
39
    public FluidHandlerItemCapacity(ItemStack container, int capacity, Fluid fluid) {
40
        super(container, capacity);
×
41
        this.fluid = fluid;
×
42
        this.capacityDefault = capacity;
×
43
    }
×
44

45
    @Override
46
    public boolean canFillFluidType(FluidStack resource) {
47
        return fluid == null || resource == null || fluid == resource.getFluid();
×
48
    }
49

50
    @Override
51
    protected void setFluid(FluidStack fluid) {
52
//        super.setFluid(fluid); // We override the implementation completely to avoid NBT saving for empty fluids
53

54
        if (fluid != null && !fluid.isEmpty()) {
×
55
            if (!this.container.hasTag()) {
×
56
                this.container.setTag(new CompoundTag());
×
57
            }
58

59
            CompoundTag fluidTag = new CompoundTag();
×
60
            fluid.writeToNBT(fluidTag);
×
61
            this.container.getTag().put("Fluid", fluidTag);
×
62
        } else {
×
63
            if (this.container.hasTag()) {
×
64
                this.container.getTag().remove("Fluid");
×
65
            }
66
        }
67
    }
×
68

69
    @Override
70
    public void setCapacity(int capacity) {
71
        CompoundTag tag = getContainer().getOrCreateTag();
×
72
        this.capacity = capacity;
×
73
        if (this.getCapacity() != this.capacityDefault) {
×
74
            tag.putInt("capacity", capacity);
×
75
        } else {
76
            tag.remove("capacity");
×
77
        }
78
    }
×
79

80
    @Override
81
    public int getCapacity() {
82
        CompoundTag tag = getContainer().getOrCreateTag();
×
83
        return tag.contains("capacity", Tag.TAG_INT) ? tag.getInt("capacity") : this.capacity;
×
84
    }
85

86
    @Nullable
87
    @Override
88
    public FluidStack getFluid() {
89
        this.capacity = getCapacity(); // Force overriding protected capacity field as soon as possible.
×
90
        return super.getFluid();
×
91
    }
92

93
    @Nullable
94
    @Override
95
    public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction facing) {
96
        return capability == Capabilities.FLUID_HANDLER_ITEM_CAPACITY ? LazyOptional.of(() -> this).cast() : super.getCapability(capability, facing);
×
97
    }
98

99
    @Override
100
    public void setFluidInTank(int tank, FluidStack fluidStack) {
101
        if (tank == 0) {
×
102
            setFluid(fluidStack);
×
103
        }
104
    }
×
105

106
    @Override
107
    public Tag serializeNBT() {
108
        CompoundTag nbt = new CompoundTag();
×
109
        FluidStack fluid = this.getFluid();
×
110
        if (fluid != null && !fluid.isEmpty()) {
×
111
            fluid.writeToNBT(nbt);
×
112
        }
113
        if (this.getCapacity() != this.capacityDefault) {
×
114
            nbt.putInt("capacity", this.getCapacity());
×
115
        }
116
        return nbt;
×
117
    }
118

119
    @Override
120
    public void deserializeNBT(Tag nbt) {
121
        CompoundTag tags = (CompoundTag) nbt;
×
122
        if (tags.contains("capacity", Tag.TAG_INT)) {
×
123
            this.setCapacity(tags.getInt("capacity"));
×
124
        }
125
        FluidStack fluid = FluidStack.loadFluidStackFromNBT(tags);
×
126
        this.setFluid(fluid);
×
127
    }
×
128
}
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