Package net.neoforged.neoforge.energy
Class ComponentEnergyStorage
java.lang.Object
net.neoforged.neoforge.energy.ComponentEnergyStorage
- All Implemented Interfaces:
IEnergyStorage
Variant of
EnergyStorage for use with data components.
The actual data storage is managed by a data component, and all changes will write back to that component.
To use this class, register a new DataComponentType which holds an Integer for your item.
Then reference that component from your ICapabilityProvider passed to RegisterCapabilitiesEvent.registerItem(net.neoforged.neoforge.capabilities.ItemCapability<T, C>, net.neoforged.neoforge.capabilities.ICapabilityProvider<net.minecraft.world.item.ItemStack, C, T>, net.minecraft.world.level.ItemLike...) to create an instance of this class.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final intprotected final DataComponentType<Integer> protected final intprotected final intprotected final MutableDataComponentHolder -
Constructor Summary
ConstructorsConstructorDescriptionComponentEnergyStorage(MutableDataComponentHolder parent, DataComponentType<Integer> energyComponent, int capacity) Creates a new ItemEnergyStorage with a transfer rate equivalent to the capacity.ComponentEnergyStorage(MutableDataComponentHolder parent, DataComponentType<Integer> energyComponent, int capacity, int maxTransfer) Creates a new ItemEnergyStorage with a unified receive / extract rate.ComponentEnergyStorage(MutableDataComponentHolder parent, DataComponentType<Integer> energyComponent, int capacity, int maxReceive, int maxExtract) Creates a new ComponentEnergyStorage with a data component as the backing store for the energy value. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns if this storage can have energy extracted.booleanUsed to determine if this storage can receive energy.intextractEnergy(int toExtract, boolean simulate) Removes energy from the storage.intReturns the amount of energy currently stored.intReturns the maximum amount of energy that can be stored.intreceiveEnergy(int toReceive, boolean simulate) Adds energy to the storage.protected voidsetEnergy(int energy) Writes a new energy value to the data component.
-
Field Details
-
parent
-
energyComponent
-
capacity
protected final int capacity -
maxReceive
protected final int maxReceive -
maxExtract
protected final int maxExtract
-
-
Constructor Details
-
ComponentEnergyStorage
public ComponentEnergyStorage(MutableDataComponentHolder parent, DataComponentType<Integer> energyComponent, int capacity, int maxReceive, int maxExtract) Creates a new ComponentEnergyStorage with a data component as the backing store for the energy value.- Parameters:
parent- The parent component holder, such as anItemStackenergyComponent- The data component referencing the stored energy of the item stackcapacity- The max capacity of the energy being storedmaxReceive- The max per-transfer power input ratemaxExtract- The max per-transfer power output rate
-
ComponentEnergyStorage
public ComponentEnergyStorage(MutableDataComponentHolder parent, DataComponentType<Integer> energyComponent, int capacity, int maxTransfer) Creates a new ItemEnergyStorage with a unified receive / extract rate.- See Also:
-
ComponentEnergyStorage
public ComponentEnergyStorage(MutableDataComponentHolder parent, DataComponentType<Integer> energyComponent, int capacity) Creates a new ItemEnergyStorage with a transfer rate equivalent to the capacity.- See Also:
-
-
Method Details
-
receiveEnergy
public int receiveEnergy(int toReceive, boolean simulate) Description copied from interface:IEnergyStorageAdds energy to the storage. Returns the amount of energy that was accepted.- Specified by:
receiveEnergyin interfaceIEnergyStorage- Parameters:
toReceive- The amount of energy being received.simulate- If true, the insertion will only be simulated, meaningIEnergyStorage.getEnergyStored()will not change.- Returns:
- Amount of energy that was (or would have been, if simulated) accepted by the storage.
-
extractEnergy
public int extractEnergy(int toExtract, boolean simulate) Description copied from interface:IEnergyStorageRemoves energy from the storage. Returns the amount of energy that was removed.- Specified by:
extractEnergyin interfaceIEnergyStorage- Parameters:
toExtract- The amount of energy being extracted.simulate- If true, the extraction will only be simulated, meaningIEnergyStorage.getEnergyStored()will not change.- Returns:
- Amount of energy that was (or would have been, if simulated) extracted from the storage.
-
getEnergyStored
public int getEnergyStored()Description copied from interface:IEnergyStorageReturns the amount of energy currently stored.- Specified by:
getEnergyStoredin interfaceIEnergyStorage
-
getMaxEnergyStored
public int getMaxEnergyStored()Description copied from interface:IEnergyStorageReturns the maximum amount of energy that can be stored.- Specified by:
getMaxEnergyStoredin interfaceIEnergyStorage
-
canExtract
public boolean canExtract()Description copied from interface:IEnergyStorageReturns if this storage can have energy extracted. If this is false, then any calls to extractEnergy will return 0.- Specified by:
canExtractin interfaceIEnergyStorage
-
canReceive
public boolean canReceive()Description copied from interface:IEnergyStorageUsed to determine if this storage can receive energy. If this is false, then any calls to receiveEnergy will return 0.- Specified by:
canReceivein interfaceIEnergyStorage
-
setEnergy
protected void setEnergy(int energy) Writes a new energy value to the data component. Clamps to [0, capacity]- Parameters:
energy- The new energy value
-