Class SimpleEnergyHandler
- All Implemented Interfaces:
ValueIOSerializable,EnergyHandler
EnergyHandler, that can store any level of energy up to a given capacity,
with per-operation insertion and extraction limits.
While new instances of SimpleEnergyHandler can be created and used directly,
overriding onEnergyChanged(int) to react to changes in the energy level is recommended,
for example to trigger setChanged().
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intprotected intprivate final SimpleEnergyHandler.EnergyJournalprotected intprotected int -
Constructor Summary
ConstructorsConstructorDescriptionSimpleEnergyHandler(int capacity) Creates a newSimpleEnergyHandlerinstance with0stored energy, and no per-operation limit.SimpleEnergyHandler(int capacity, int maxTransfer) Creates a newSimpleEnergyHandlerinstance with0stored energy, and the same per-insert and per-extraction limit.SimpleEnergyHandler(int capacity, int maxInsert, int maxExtract) Creates a newSimpleEnergyHandlerinstance with0stored energy.SimpleEnergyHandler(int capacity, int maxInsert, int maxExtract, int energy) Creates a newSimpleEnergyHandlerinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoiddeserialize(ValueInput input) intextract(int amount, TransactionContext transaction) Extracts up to the given amount of energy from the handler.longReturns the amount of energy currently stored, as along.longReturns the capacity of the handler, irrespective of the current amount, as along.intinsert(int amount, TransactionContext transaction) Inserts up to the given amount of energy into the handler.protected voidonEnergyChanged(int previousAmount) Called after the amount of energy in the handler changed.voidserialize(ValueOutput output) voidset(int amount) Directly overwrites the energy amount of the handler.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.neoforged.neoforge.transfer.energy.EnergyHandler
getAmountAsInt, getCapacityAsInt
-
Field Details
-
energy
protected int energy -
capacity
protected int capacity -
maxInsert
protected int maxInsert -
maxExtract
protected int maxExtract -
energyJournal
-
-
Constructor Details
-
SimpleEnergyHandler
public SimpleEnergyHandler(int capacity) Creates a newSimpleEnergyHandlerinstance with0stored energy, and no per-operation limit.- See Also:
-
SimpleEnergyHandler
public SimpleEnergyHandler(int capacity, int maxTransfer) Creates a newSimpleEnergyHandlerinstance with0stored energy, and the same per-insert and per-extraction limit.- See Also:
-
SimpleEnergyHandler
public SimpleEnergyHandler(int capacity, int maxInsert, int maxExtract) Creates a newSimpleEnergyHandlerinstance with0stored energy.- See Also:
-
SimpleEnergyHandler
public SimpleEnergyHandler(int capacity, int maxInsert, int maxExtract, int energy) Creates a newSimpleEnergyHandlerinstance.- Parameters:
capacity- maximum capacity of the handlermaxInsert- maximum amount that can be accepted in each call toinsert(int, net.neoforged.neoforge.transfer.transaction.TransactionContext)maxExtract- maximum amount that can be accepted in each call toextract(int, net.neoforged.neoforge.transfer.transaction.TransactionContext)energy- current amount of energy in the handler. May be larger than the passed capacity, for example if the capacity changed since the energy was put into the handler.- Throws:
IllegalArgumentException- if any of the arguments is negative
-
-
Method Details
-
serialize
- Specified by:
serializein interfaceValueIOSerializable
-
deserialize
- Specified by:
deserializein interfaceValueIOSerializable
-
set
public void set(int amount) Directly overwrites the energy amount of the handler.- Throws:
IllegalArgumentException- if the amount is negative
-
onEnergyChanged
protected void onEnergyChanged(int previousAmount) Called after the amount of energy in the handler changed.For changes that happen through
set(int), this method is called immediately. For changes that happen throughinsert(int, net.neoforged.neoforge.transfer.transaction.TransactionContext)orextract(int, net.neoforged.neoforge.transfer.transaction.TransactionContext), this function will be called at the end of the transaction. -
getAmountAsLong
public long getAmountAsLong()Description copied from interface:EnergyHandlerReturns the amount of energy currently stored, as along.In general, energy handlers can report
longamounts. However, if the handler is known to only support amounts up toInteger.MAX_VALUE, or if the caller prefers to deal inints only, the int-returning overload can be used instead.The returned amount must be non-negative.
- Specified by:
getAmountAsLongin interfaceEnergyHandler- Returns:
- the amount as a long
- See Also:
-
getCapacityAsLong
public long getCapacityAsLong()Description copied from interface:EnergyHandlerReturns the capacity of the handler, irrespective of the current amount, as along.In general, energy handlers can report
longcapacities. However, if the handler is known to only support capacities up toInteger.MAX_VALUE, or if the caller prefers to deal inints only, the int-returning overload can be used instead.This function serves as a hint on the maximum amount the energy handler might contain, for example the handler can be considered full if
amount >= capacity. Note that the returned capacity may overestimate the actual allowed amount, and it might be smaller than the current amount. The only way to know if a handler will accept a resource, is to try toinsertit.- Specified by:
getCapacityAsLongin interfaceEnergyHandler- Returns:
- the capacity, as a long
- See Also:
-
insert
Description copied from interface:EnergyHandlerInserts up to the given amount of energy into the handler.Changes to the handler are made in the context of a transaction.
- Specified by:
insertin interfaceEnergyHandler- Parameters:
amount- The maximum amount of energy to insert. Must be non-negative.transaction- The transaction that this operation is part of.- Returns:
- The amount that was inserted. Between
0(inclusive, nothing was inserted) andamount(inclusive, everything was inserted).
-
extract
Description copied from interface:EnergyHandlerExtracts up to the given amount of energy from the handler.Changes to the handler are made in the context of a transaction.
- Specified by:
extractin interfaceEnergyHandler- Parameters:
amount- The maximum amount of energy to extract. Must be non-negative.transaction- The transaction that this operation is part of.- Returns:
- The amount that was extracted. Between
0(inclusive, nothing was extracted) andamount(inclusive, everything was extracted).
-