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

CyclopsMC / CyclopsCore / #479033771

03 Feb 2025 06:45PM UTC coverage: 22.157% (-0.02%) from 22.178%
#479033771

push

github

rubensworks
Bump mod version

2297 of 10367 relevant lines covered (22.16%)

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

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

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

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

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

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

57
            CompoundTag fluidTag = new CompoundTag();
×
58
            fluid.writeToNBT(fluidTag);
×
59
            this.container.getTag().put("Fluid", fluidTag);
×
60
        } else {
×
61
            this.container.getTag().remove("Fluid");
×
62
        }
63
    }
×
64

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

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

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

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

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

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

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

© 2025 Coveralls, Inc