• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

CyclopsMC / CyclopsCore / #479033777

16 Feb 2025 01:43PM UTC coverage: 22.155% (-0.002%) from 22.157%
#479033777

push

github

rubensworks
Bump mod version

2297 of 10368 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
            this.container.getTag().remove("Fluid");
×
64
        }
65
    }
×
66

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

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

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

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

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

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

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