forked from TechReborn/Energy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleItemEnergyStorageImpl.java
More file actions
117 lines (96 loc) · 3.74 KB
/
SimpleItemEnergyStorageImpl.java
File metadata and controls
117 lines (96 loc) · 3.74 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
package team.reborn.energy.impl;
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import team.reborn.energy.api.EnergyStorage;
import team.reborn.energy.api.base.DelegatingEnergyStorage;
import team.reborn.energy.api.base.SimpleEnergyItem;
/**
* Note: instances of this class do not perform any context validation,
* that is handled by the DelegatingEnergyStorage they are wrapped behind.
*/
@ApiStatus.Internal
public class SimpleItemEnergyStorageImpl implements EnergyStorage {
public static EnergyStorage createSimpleStorage(ContainerItemContext ctx, long capacity, long maxInsert, long maxExtract) {
StoragePreconditions.notNegative(capacity);
StoragePreconditions.notNegative(maxInsert);
StoragePreconditions.notNegative(maxExtract);
Item startingItem = ctx.getItemVariant().getItem();
return new DelegatingEnergyStorage(
new SimpleItemEnergyStorageImpl(ctx, capacity, maxInsert, maxExtract),
() -> ctx.getItemVariant().isOf(startingItem) && ctx.getAmount() > 0
);
}
private final ContainerItemContext ctx;
private final long capacity;
private final long maxInsert, maxExtract;
private SimpleItemEnergyStorageImpl(ContainerItemContext ctx, long capacity, long maxInsert, long maxExtract) {
this.ctx = ctx;
this.capacity = capacity;
this.maxInsert = maxInsert;
this.maxExtract = maxExtract;
}
/**
* Try to set the energy of the stack to {@code energyAmountPerCount}, return true if success.
*/
private boolean trySetEnergy(long energyAmountPerCount, long count, TransactionContext transaction) {
ItemStack newStack = ctx.getItemVariant().toStack();
SimpleEnergyItem.setStoredEnergyUnchecked(newStack, energyAmountPerCount);
ItemVariant newVariant = ItemVariant.of(newStack);
// Try to convert exactly `count` items.
try (Transaction nested = transaction.openNested()) {
if (ctx.extract(ctx.getItemVariant(), count, nested) == count && ctx.insert(newVariant, count, nested) == count) {
nested.commit();
return true;
}
}
return false;
}
@Override
public boolean supportsInsertion() {
return maxInsert > 0;
}
@Override
public long insert(long maxAmount, TransactionContext transaction) {
long count = ctx.getAmount();
long maxAmountPerCount = maxAmount / count;
long currentAmountPerCount = getAmount() / count;
long insertedPerCount = Math.min(maxInsert, Math.min(maxAmountPerCount, capacity - currentAmountPerCount));
if (insertedPerCount > 0) {
if (trySetEnergy(currentAmountPerCount + insertedPerCount, count, transaction)) {
return insertedPerCount * count;
}
}
return 0;
}
@Override
public boolean supportsExtraction() {
return maxExtract > 0;
}
@Override
public long extract(long maxAmount, TransactionContext transaction) {
long count = ctx.getAmount();
long maxAmountPerCount = maxAmount / count;
long currentAmountPerCount = getAmount() / count;
long extractedPerCount = Math.min(maxExtract, Math.min(maxAmountPerCount, currentAmountPerCount));
if (extractedPerCount > 0) {
if (trySetEnergy(currentAmountPerCount - extractedPerCount, count, transaction)) {
return extractedPerCount * count;
}
}
return 0;
}
@Override
public long getAmount() {
return ctx.getAmount() * SimpleEnergyItem.getStoredEnergyUnchecked(ctx.getItemVariant().getComponents());
}
@Override
public long getCapacity() {
return ctx.getAmount() * capacity;
}
}