-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleTankComponent.java
More file actions
138 lines (122 loc) · 3.35 KB
/
Copy pathSimpleTankComponent.java
File metadata and controls
138 lines (122 loc) · 3.35 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package io.github.cottonmc.component.fluid.impl;
import io.github.cottonmc.component.api.ActionType;
import io.github.cottonmc.component.fluid.TankComponent;
import io.github.fablabsmc.fablabs.api.fluidvolume.v1.FluidVolume;
import io.github.fablabsmc.fablabs.api.fluidvolume.v1.Fraction;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.util.collection.DefaultedList;
public class SimpleTankComponent implements TankComponent {
protected DefaultedList<FluidVolume> contents;
private final List<Runnable> listeners = new ArrayList<>();
private Fraction maxCapacity;
public SimpleTankComponent(int size, Fraction maxCapacity) {
contents = DefaultedList.ofSize(size, FluidVolume.EMPTY);
this.maxCapacity = maxCapacity;
}
@Override
public int getTanks() {
return contents.size();
}
@Override
public Fraction getMaxCapacity(int tank) {
return maxCapacity;
}
@Override
public List<FluidVolume> getAllContents() {
List<FluidVolume> ret = new ArrayList<>();
for (FluidVolume vol : contents) {
ret.add(vol.copy());
}
return ret;
}
@Override
public FluidVolume getContents(int slot) {
return contents.get(slot).copy();
}
@Override
public boolean canInsert(int slot) {
return true;
}
@Override
public boolean canExtract(int slot) {
return true;
}
@Override
public FluidVolume removeFluid(int slot, Fraction amount, ActionType action) {
FluidVolume vol = contents.get(slot);
if (!action.shouldPerform()) {
vol = vol.copy();
} else {
onChanged();
}
return vol.split(amount);
}
@Override
public FluidVolume removeFluid(int slot, ActionType action) {
FluidVolume vol = contents.get(slot);
if (action.shouldPerform()) {
setFluid(slot, FluidVolume.EMPTY);
onChanged();
}
return vol;
}
@Override
public void setFluid(int slot, FluidVolume stack) {
contents.set(slot, stack);
onChanged();
}
@Override
public FluidVolume insertFluid(int tank, FluidVolume fluid, ActionType action) {
FluidVolume target = contents.get(tank);
if (!target.isEmpty() && !FluidVolume.areFluidsEqual(fluid, target)) { //TODO: NBT comparison eventually
//unstackable, can't merge!
return fluid;
}
Fraction count = target.getAmount();
Fraction maxSize = getMaxCapacity(tank);
if (count.equals(getMaxCapacity(tank))) {
//target stack is already full, can't merge!
return fluid;
}
Fraction sizeLeft = maxSize.subtract(count);
if (sizeLeft.compareTo(fluid.getAmount()) >= 0) {
//the target stack can accept our whole stack!
if (action.shouldPerform()) {
if (target.isEmpty()) {
setFluid(tank, fluid);
} else {
target.increment(fluid.getAmount());
}
onChanged();
}
return FluidVolume.EMPTY;
} else {
//the target can't accept our whole stack, we're gonna have a remainder
if (action.shouldPerform()) {
if (target.isEmpty()) {
FluidVolume newVol = fluid.copy();
newVol.setAmount(maxSize);
setFluid(tank, newVol);
} else {
target.setAmount(maxSize);
}
onChanged();
}
fluid.decrement(sizeLeft);
return fluid;
}
}
@Override
public FluidVolume insertFluid(FluidVolume fluid, ActionType action) {
for (int i = 0; i < contents.size(); i++) {
fluid = insertFluid(i, fluid, action);
if (fluid.isEmpty()) return fluid;
}
return fluid;
}
@Override
public List<Runnable> getListeners() {
return listeners;
}
}