/
BasicSink.java
373 lines (316 loc) · 9.3 KB
/
BasicSink.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package ic2.api.energy.prefab;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.info.Info;
import ic2.api.item.ElectricItem;
/**
* BasicSink is a simple adapter to provide an ic2 energy sink.
*
* It's designed to be attached to a tile entity as a delegate. Functionally BasicSink acts as a
* one-time configurable input energy buffer, thus providing a common use case for machines.
*
* Sub-classing BasicSink instead of using it as a delegate works as well, but isn't recommended.
* The delegate can be extended with additional functionality through a sub class though.
*
* The constraints set by BasicSink like the strict tank-like energy buffering should provide a
* more easy to use and stable interface than using IEnergySink directly while aiming for
* optimal performance.
*
* Using BasicSink involves the following steps:
* - create a BasicSink instance in your TileEntity, typically in a field
* - forward invalidate, onChunkUnload, readFromNBT, writeToNBT and updateEntity to the BasicSink
* instance.
* If you have other means of determining when the tile entity is fully loaded, notify onLoaded
* that way instead of forwarding updateEntity.
* - call useEnergy whenever appropriate. canUseEnergy determines if enough energy is available
* without consuming the energy.
* - optionally use getEnergyStored to display the output buffer charge level
* - optionally use setEnergyStored to sync the stored energy to the client (e.g. in the Container)
*
* Example implementation code:
* @code{.java}
* public class SomeTileEntity extends TileEntity {
* // new basic energy sink, 1000 EU buffer, tier 1 (32 EU/t, LV)
* private BasicSink ic2EnergySink = new BasicSink(this, 1000, 1);
*
* @Override
* public void invalidate() {
* ic2EnergySink.invalidate(); // notify the energy sink
* ...
* super.invalidate(); // this is important for mc!
* }
*
* @Override
* public void onChunkUnload() {
* ic2EnergySink.onChunkUnload(); // notify the energy sink
* ...
* }
*
* @Override
* public void readFromNBT(NBTTagCompound tag) {
* super.readFromNBT(tag);
*
* ic2EnergySink.readFromNBT(tag);
* ...
* }
*
* @Override
* public void writeToNBT(NBTTagCompound tag) {
* super.writeToNBT(tag);
*
* ic2EnergySink.writeToNBT(tag);
* ...
* }
*
* @Override
* public void updateEntity() {
* ic2EnergySink.updateEntity(); // notify the energy sink
* ...
* if (ic2EnergySink.useEnergy(5)) { // use 5 eu from the sink's buffer this tick
* ... // do something with the energy
* }
* ...
* }
*
* ...
* }
* @endcode
*/
public class BasicSink extends TileEntity implements IEnergySink {
// **********************************
// *** Methods for use by the mod ***
// **********************************
/**
* Constructor for a new BasicSink delegate.
*
* @param parent1 TileEntity represented by this energy sink.
* @param capacity1 Maximum amount of eu to store.
* @param tier1 IC2 tier, 1 = LV, 2 = MV, ...
*/
public BasicSink(TileEntity parent1, int capacity1, int tier1) {
this.parent = parent1;
this.capacity = capacity1;
this.tier = tier1;
}
// in-world te forwards >>
/**
* Forward for the base TileEntity's updateEntity(), used for creating the energy net link.
* Either updateEntity or onLoaded have to be used.
*/
@Override
public void updateEntity() {
if (!addedToEnet) onLoaded();
}
/**
* Notification that the base TileEntity finished loaded, for advanced uses.
* Either updateEntity or onLoaded have to be used.
*/
public void onLoaded() {
if (!addedToEnet &&
!FMLCommonHandler.instance().getEffectiveSide().isClient() &&
Info.isIc2Available()) {
worldObj = parent.getWorldObj();
xCoord = parent.xCoord;
yCoord = parent.yCoord;
zCoord = parent.zCoord;
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
addedToEnet = true;
}
}
/**
* Forward for the base TileEntity's invalidate(), used for destroying the energy net link.
* Both invalidate and onChunkUnload have to be used.
*/
@Override
public void invalidate() {
super.invalidate();
onChunkUnload();
}
/**
* Forward for the base TileEntity's onChunkUnload(), used for destroying the energy net link.
* Both invalidate and onChunkUnload have to be used.
*/
@Override
public void onChunkUnload() {
if (addedToEnet &&
Info.isIc2Available()) {
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
addedToEnet = false;
}
}
/**
* Forward for the base TileEntity's readFromNBT(), used for loading the state.
*
* @param tag Compound tag as supplied by TileEntity.readFromNBT()
*/
@Override
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
NBTTagCompound data = tag.getCompoundTag("IC2BasicSink");
energyStored = data.getDouble("energy");
}
/**
* Forward for the base TileEntity's writeToNBT(), used for saving the state.
*
* @param tag Compound tag as supplied by TileEntity.writeToNBT()
*/
@Override
public void writeToNBT(NBTTagCompound tag) {
try {
super.writeToNBT(tag);
} catch (RuntimeException e) {
// happens if this is a delegate, ignore
}
NBTTagCompound data = new NBTTagCompound();
data.setDouble("energy", energyStored);
tag.setTag("IC2BasicSink", data);
}
// << in-world te forwards
// methods for using this adapter >>
/**
* Get the maximum amount of energy this sink can hold in its buffer.
*
* @return Capacity in EU.
*/
public int getCapacity() {
return capacity;
}
/**
* Set the maximum amount of energy this sink can hold in its buffer.
*
* @param capacity1 Capacity in EU.
*/
public void setCapacity(int capacity1) {
this.capacity = capacity1;
}
/**
* Get the IC2 energy tier for this sink.
*
* @return IC2 Tier.
*/
public int getTier() {
return tier;
}
/**
* Set the IC2 energy tier for this sink.
*
* @param tier1 IC2 Tier.
*/
public void setTier(int tier1) {
this.tier = tier1;
}
/**
* Determine the energy stored in the sink's input buffer.
*
* @return amount in EU, may be above capacity
*/
public double getEnergyStored() {
return energyStored;
}
/**
* Set the stored energy to the specified amount.
*
* This is intended for server -> client synchronization, e.g. to display the stored energy in
* a GUI through getEnergyStored().
*
* @param amount
*/
public void setEnergyStored(double amount) {
energyStored = amount;
}
/**
* Determine if the specified amount of energy is available.
*
* @param amount in EU
* @return true if the amount is available
*/
public boolean canUseEnergy(double amount) {
return energyStored >= amount;
}
/**
* Use the specified amount of energy, if available.
*
* @param amount amount to use
* @return true if the amount was available
*/
public boolean useEnergy(double amount) {
if (canUseEnergy(amount) && !FMLCommonHandler.instance().getEffectiveSide().isClient()) {
energyStored -= amount;
return true;
}
return false;
}
/**
* Discharge the supplied ItemStack into this sink's energy buffer.
*
* @param stack ItemStack to discharge (null is ignored)
* @param limit Transfer limit, values <= 0 will use the battery's limit
* @return true if energy was transferred
*/
public boolean discharge(ItemStack stack, int limit) {
if (stack == null || !Info.isIc2Available()) return false;
double amount = capacity - energyStored;
if (amount <= 0) return false;
if (limit > 0 && limit < amount) amount = limit;
amount = ElectricItem.manager.discharge(stack, amount, tier, limit > 0, true, false);
energyStored += amount;
return amount > 0;
}
// << methods for using this adapter
// backwards compatibility (ignore these) >>
@Deprecated
public void onUpdateEntity() {
updateEntity();
}
@Deprecated
public void onInvalidate() {
invalidate();
}
@Deprecated
public void onOnChunkUnload() {
onChunkUnload();
}
@Deprecated
public void onReadFromNbt(NBTTagCompound tag) {
readFromNBT(tag);
}
@Deprecated
public void onWriteToNbt(NBTTagCompound tag) {
writeToNBT(tag);
}
// << backwards compatibility
// ******************************
// *** Methods for use by ic2 ***
// ******************************
// energy net interface >>
@Override
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) {
return true;
}
@Override
public double getDemandedEnergy() {
return Math.max(0, capacity - energyStored);
}
@Override
public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage) {
energyStored += amount;
return 0;
}
@Override
public int getSinkTier() {
return tier;
}
// << energy net interface
public final TileEntity parent;
protected int capacity;
protected int tier;
protected double energyStored;
protected boolean addedToEnet;
}