-
Notifications
You must be signed in to change notification settings - Fork 754
/
ToolTankHelper.java
65 lines (55 loc) · 2.65 KB
/
ToolTankHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package slimeknights.tconstruct.library.tools.capability.fluid;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.modifiers.modules.build.ModifierTraitModule;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.stat.INumericToolStat;
import slimeknights.tconstruct.library.tools.stat.ToolStatId;
import slimeknights.tconstruct.library.tools.stat.ToolStats;
import slimeknights.tconstruct.tools.TinkerModifiers;
import java.util.function.BiFunction;
/** Helper methods for handling fluids in tools */
@SuppressWarnings("ClassCanBeRecord") // want to leave extendable
@Getter
@RequiredArgsConstructor
public class ToolTankHelper {
/** Helper function to parse a fluid from NBT */
public static final BiFunction<CompoundTag, String, FluidStack> PARSE_FLUID = (nbt, key) -> FluidStack.loadFluidStackFromNBT(nbt.getCompound(key));
/** Stat controlling the max for the default helper */
public static final TankCapacityStat CAPACITY_STAT = ToolStats.register(new TankCapacityStat(new ToolStatId(TConstruct.MOD_ID, "tank_capacity"), 0xA0A0A0, 0, Integer.MAX_VALUE));
/** Default tank helper for setting fluids */
public static final ToolTankHelper TANK_HELPER = new ToolTankHelper(CAPACITY_STAT, TConstruct.getResource("tank_fluid"));
/** Module ensuring the tool has the tank */
public static final ModifierModule TANK_HANDLER = new ModifierTraitModule(TinkerModifiers.tankHandler.getId(), 1, true);
/** Tool stat handling max tank capacity */
private final INumericToolStat<?> capacityStat;
/** Key in persistent data storing the fluid */
private final ResourceLocation fluidKey;
/** Gets the capacity for the tool */
public int getCapacity(IToolStackView tool) {
return tool.getStats().getInt(capacityStat);
}
/* Fluid */
/** Gets the fluid in the tank */
public FluidStack getFluid(IToolStackView tool) {
return tool.getPersistentData().get(getFluidKey(), PARSE_FLUID);
}
/** Sets the fluid in the tank */
public FluidStack setFluid(IToolStackView tool, FluidStack fluid) {
if (fluid.isEmpty()) {
tool.getPersistentData().remove(fluidKey);
return fluid;
}
int capacity = getCapacity(tool);
if (fluid.getAmount() > capacity) {
fluid.setAmount(capacity);
}
tool.getPersistentData().put(fluidKey, fluid.writeToNBT(new CompoundTag()));
return fluid;
}
}