-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTankComponent.java
More file actions
108 lines (85 loc) · 2.8 KB
/
Copy pathTankComponent.java
File metadata and controls
108 lines (85 loc) · 2.8 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package io.github.cottonmc.component.fluid;
import io.github.cottonmc.component.api.ActionType;
import io.github.cottonmc.component.api.Observable;
import io.github.fablabsmc.fablabs.api.fluidvolume.v1.FluidVolume;
import io.github.fablabsmc.fablabs.api.fluidvolume.v1.Fraction;
import dev.onyxstudios.cca.api.v3.component.Component;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.fluid.Fluid;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/**
* DISCLAIMER: ALL CODE HERE NOT FINAL, MAY ENCOUNTER BREAKING CHANGES REGULARLY
* CROSS-COMPATIBILITY WILL BE IMPLEMENTED WHEN API STABILIZES; PLEASE BE PATIENT
* Currently works effectively identically to an InventoryComponent, and <i>ignores fluid stack NBT</i>
*/
public interface TankComponent extends Component, Observable {
int getTanks();
Fraction getMaxCapacity(int tank);
default boolean isEmpty() {
for (FluidVolume volume : getAllContents()) {
if (!volume.isEmpty()) return false;
}
return true;
}
List<FluidVolume> getAllContents();
FluidVolume getContents(int tank);
boolean canInsert(int tank);
boolean canExtract(int tank);
FluidVolume removeFluid(int tank, Fraction amount, ActionType action);
FluidVolume removeFluid(int tank, ActionType action);
void setFluid(int tank, FluidVolume fluid);
FluidVolume insertFluid(int tank, FluidVolume fluid, ActionType action);
FluidVolume insertFluid(FluidVolume fluid, ActionType action);
default void clear() {
for (int i = 0; i < getTanks(); i++) {
removeFluid(i, ActionType.PERFORM);
}
}
default boolean isAcceptableFluid(int tank) {
return true;
}
default Fraction amountOf(Fluid fluid) {
return amountOf(Collections.singleton(fluid));
}
default Fraction amountOf(Set<Fluid> fluids) {
Fraction amount = Fraction.ZERO;
for (FluidVolume volume : getAllContents()) {
if (fluids.contains(volume.getFluid())) {
amount = amount.add(volume.getAmount());
}
}
return amount;
}
default boolean contains(Fluid fluid) {
return contains(Collections.singleton(fluid));
}
default boolean contains(Set<Fluid> fluids) {
for (FluidVolume volume : getAllContents()) {
if (fluids.contains(volume.getFluid())) {
return true;
}
}
return false;
}
@Override
default void readFromNbt(NbtCompound tag) {
clear();
NbtList contents = tag.getList("Contents", NbtType.COMPOUND);
for (int i = 0; i < contents.size(); i++) {
NbtCompound volTag = (NbtCompound)contents.get(i);
setFluid(i, FluidVolume.fromNbt(volTag));
}
}
@Override
default void writeToNbt(NbtCompound tag) {
NbtList contents = new NbtList();
for (FluidVolume vol : getAllContents()) {
contents.add(vol.toNbt(new NbtCompound()));
}
tag.put("Contents", contents);
}
}