forked from TechReborn/Energy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSidedEnergyContainer.java
More file actions
115 lines (92 loc) · 3.05 KB
/
SimpleSidedEnergyContainer.java
File metadata and controls
115 lines (92 loc) · 3.05 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
package team.reborn.energy.api.base;
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant;
import net.minecraft.core.Direction;
import org.jetbrains.annotations.Nullable;
import team.reborn.energy.api.EnergyStorage;
/**
* A base energy storage implementation with a dynamic capacity, and per-side per-operation insertion and extraction limits.
* {@link #getSideStorage} can be used to get an {@code EnergyStorage} implementation for a given side.
* Make sure to override {@link #onFinalCommit} to call {@code markDirty} and similar functions.
*/
@SuppressWarnings({"unused"})
public abstract class SimpleSidedEnergyContainer extends SnapshotParticipant<Long> {
public long amount = 0;
private final SideStorage[] sideStorages = new SideStorage[7];
public SimpleSidedEnergyContainer() {
for (int i = 0; i < 7; ++i) {
sideStorages[i] = new SideStorage(i == 6 ? null : Direction.from3DDataValue(i));
}
}
/**
* @return The current capacity of this storage.
*/
public abstract long getCapacity();
/**
* @return The maximum amount of energy that can be inserted in a single operation from the passed side.
*/
public abstract long getMaxInsert(@Nullable Direction side);
/**
* @return The maximum amount of energy that can be extracted in a single operation from the passed side.
*/
public abstract long getMaxExtract(@Nullable Direction side);
/**
* @return An {@link EnergyStorage} implementation for the passed side.
*/
public EnergyStorage getSideStorage(@Nullable Direction side) {
return sideStorages[side == null ? 6 : side.get3DDataValue()];
}
@Override
protected Long createSnapshot() {
return amount;
}
@Override
protected void readSnapshot(Long snapshot) {
amount = snapshot;
}
private class SideStorage implements EnergyStorage {
private final Direction side;
private SideStorage(Direction side) {
this.side = side;
}
@Override
public boolean supportsInsertion() {
return getMaxInsert(side) > 0;
}
@Override
public long insert(long maxAmount, TransactionContext transaction) {
StoragePreconditions.notNegative(maxAmount);
long inserted = Math.min(getMaxInsert(side), Math.min(maxAmount, getCapacity() - amount));
if (inserted > 0) {
updateSnapshots(transaction);
amount += inserted;
return inserted;
}
return 0;
}
@Override
public boolean supportsExtraction() {
return getMaxExtract(side) > 0;
}
@Override
public long extract(long maxAmount, TransactionContext transaction) {
StoragePreconditions.notNegative(maxAmount);
long extracted = Math.min(getMaxExtract(side), Math.min(maxAmount, amount));
if (extracted > 0) {
updateSnapshots(transaction);
amount -= extracted;
return extracted;
}
return 0;
}
@Override
public long getAmount() {
return amount;
}
@Override
public long getCapacity() {
return SimpleSidedEnergyContainer.this.getCapacity();
}
}
}