forked from TechReborn/Energy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnergyStorage.java
More file actions
138 lines (126 loc) · 6.07 KB
/
EnergyStorage.java
File metadata and controls
138 lines (126 loc) · 6.07 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 team.reborn.energy.api;
import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup;
import net.fabricmc.fabric.api.lookup.v1.item.ItemApiLookup;
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.core.Direction;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.resources.Identifier;
import org.jetbrains.annotations.Nullable;
import team.reborn.energy.api.base.DelegatingEnergyStorage;
import team.reborn.energy.api.base.SimpleEnergyItem;
import team.reborn.energy.api.base.SimpleEnergyStorage;
import team.reborn.energy.api.base.SimpleSidedEnergyContainer;
import team.reborn.energy.impl.EmptyEnergyStorage;
import team.reborn.energy.impl.EnergyImpl;
import team.reborn.energy.impl.SimpleItemEnergyStorageImpl;
import java.util.Objects;
/**
* An object that can store energy.
*
* <p><ul>
* <li>{@link #supportsInsertion} and {@link #supportsExtraction} can be used to tell if insertion and extraction
* functionality are possibly supported by this storage.</li>
* <li>{@link #insert} and {@link #extract} can be used to insert or extract resources from this storage.</li>
* <li>{@link #getAmount} and {@link #getCapacity} can be used to query the current amount and capacity of this storage.
* There is no guarantee that the current amount of energy can be extracted,
* nor that something can be inserted if capacity > amount.
* If you want to know, you can simulate the operation with {@link #insert} and {@link #extract}.
* </li>
* </ul>
*
* @see Transaction
*/
@SuppressWarnings({"unused"})
public interface EnergyStorage {
/**
* Sided block access to energy storages.
* The {@code Direction} parameter may be null, meaning that the full storage (ignoring side restrictions) should be queried.
* Refer to {@link BlockApiLookup} for documentation on how to use this field.
*
* <p>The system is push based. That means that power sources are responsible for pushing power to nearby machines.
* Machines and wires should NOT pull power from other sources.
*
* <p>{@link SimpleEnergyStorage} and {@link SimpleSidedEnergyContainer} are provided as base implementations.
*
* <p>When the operations supported by an energy storage change,
* that is if the return value of {@link EnergyStorage#supportsInsertion} or {@link EnergyStorage#supportsExtraction} changes,
* the storage should notify its neighbors with a block update so that they can refresh their connections if necessary.
*
* <p>This may be queried safely both on the logical server and on the logical client threads.
* On the server thread (i.e. with a server world), all transfer functionality is always supported.
* On the client thread (i.e. with a client world), contents of queried EnergyStorages are unreliable and should not be modified.
*/
BlockApiLookup<EnergyStorage, @Nullable Direction> SIDED =
BlockApiLookup.get(Identifier.fromNamespaceAndPath("teamreborn", "sided_energy"), EnergyStorage.class, Direction.class);
/**
* Item access to energy storages.
* Querying should always happen through {@link ContainerItemContext#find}.
*
* <p>{@link SimpleItemEnergyStorageImpl} is provided as an implementation example.
* Instances of it can be optained through {@link SimpleEnergyItem#createStorage}.
* Custom implementations should treat the context as a wrapper around a single slot,
* and always check the current item variant and amount before any operation, like {@code SimpleItemEnergyStorageImpl} does it.
* The check can be handled by {@link DelegatingEnergyStorage}.
*
* <p>This may be queried both client-side and server-side.
* Returned APIs should behave the same regardless of the logical side.
*/
ItemApiLookup<EnergyStorage, ContainerItemContext> ITEM =
ItemApiLookup.get(Identifier.fromNamespaceAndPath("teamreborn", "energy"), EnergyStorage.class, ContainerItemContext.class);
/**
* Always empty energy storage.
*/
EnergyStorage EMPTY = Objects.requireNonNull(EmptyEnergyStorage.EMPTY);
/**
* Stock data component type for energy.
*
* <p><b>This component should only be used on item stacks from your mod.</b>
* Otherwise, do not query it or assume it exists.
* Inter-mod energy interactions should happen using {@link #ITEM}.</b>
*/
DataComponentType<Long> ENERGY_COMPONENT = Objects.requireNonNull(EnergyImpl.ENERGY_COMPONENT);
/**
* Return false if calling {@link #insert} will absolutely always return 0, or true otherwise or in doubt.
*
* <p>Note: This function is meant to be used by cables or other devices that can transfer energy to know if
* they should interact with this storage at all.
*/
default boolean supportsInsertion() {
return true;
}
/**
* Try to insert up to some amount of energy into this storage.
*
* @param maxAmount The maximum amount of energy to insert. May not be negative.
* @param transaction The transaction this operation is part of.
* @return A nonnegative integer not greater than maxAmount: the amount that was inserted.
*/
long insert(long maxAmount, TransactionContext transaction);
/**
* Return false if calling {@link #extract} will absolutely always return 0, or true otherwise or in doubt.
*
* <p>Note: This function is meant to be used by cables or other devices that can transfer energy to know if
* they should interact with this storage at all.
*/
default boolean supportsExtraction() {
return true;
}
/**
* Try to extract up to some amount of energy from this storage.
*
* @param maxAmount The maximum amount of energy to extract. May not be negative.
* @param transaction The transaction this operation is part of.
* @return A nonnegative integer not greater than maxAmount: the amount that was extracted.
*/
long extract(long maxAmount, TransactionContext transaction);
/**
* Return the current amount of energy that is stored.
*/
long getAmount();
/**
* Return the maximum amount of energy that could be stored.
*/
long getCapacity();
}