forked from Technici4n/FastTransferLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleItemEnergyIo.java
89 lines (73 loc) · 2.67 KB
/
SimpleItemEnergyIo.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package dev.technici4n.fasttransferlib.api.energy.base;
import com.google.common.base.Preconditions;
import dev.technici4n.fasttransferlib.api.Simulation;
import dev.technici4n.fasttransferlib.api.energy.EnergyIo;
import net.minecraft.item.ItemStack;
import net.fabricmc.fabric.api.lookup.v1.item.ItemApiLookup;
/**
* An energy io for an item. Energy is stored in the "energy" key of the tag if there is some, or it's 0 if there is no tag.
* This allows just-crafted empty batteries and emptied batteries to stack correctly.
* Supports stackable items, and energy will be evenly distributed among them.
*/
public class SimpleItemEnergyIo implements EnergyIo {
private final double capacity;
private final double maxInsertion;
private final double maxExtraction;
private final ItemStack stack;
public static ItemApiLookup.ItemApiProvider<EnergyIo, Void> getProvider(double capacity, double maxInsertion, double maxExtraction) {
return (stack, void_) -> new SimpleItemEnergyIo(capacity, maxInsertion, maxExtraction, stack);
}
private SimpleItemEnergyIo(double capacity, double maxInsertion, double maxExtraction, ItemStack stack) {
Preconditions.checkArgument(stack.getCount() >= 1, "Stack must have a count of at least 1");
this.capacity = capacity;
this.maxInsertion = simplify(maxInsertion);
this.maxExtraction = simplify(maxExtraction);
this.stack = stack;
}
@SuppressWarnings("ConstantConditions")
@Override
public double getEnergy() {
if (stack.hasTag()) {
return simplify(stack.getTag().getDouble("energy") * stack.getCount());
} else {
return 0;
}
}
@Override
public double getEnergyCapacity() {
return capacity;
}
@Override
public boolean supportsInsertion() {
return maxInsertion > 0;
}
@Override
public double insert(double amount, Simulation simulation) {
double inserted = Math.min(Math.min(amount, maxInsertion), capacity - getEnergy());
incrementEnergy(inserted, simulation);
return simplify(amount - inserted);
}
@Override
public boolean supportsExtraction() {
return maxExtraction > 0;
}
@Override
public double extract(double maxAmount, Simulation simulation) {
double extracted = Math.min(Math.min(maxAmount, maxExtraction), getEnergy());
incrementEnergy(-extracted, simulation);
return simplify(extracted);
}
private void incrementEnergy(double increment, Simulation simulation) {
double newEnergy = (getEnergy() + increment) / stack.getCount();
if (simulation.isActing()) {
if (newEnergy < 1e-9) {
stack.removeSubTag("energy");
} else {
stack.getOrCreateTag().putDouble("energy", newEnergy);
}
}
}
private static double simplify(double energy) {
return Math.abs(energy) < 1e-9 ? 0 : energy;
}
}