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
Modifier and TypeFieldDescriptionprotected final int
protected final DataComponentType
<Integer> protected final int
protected final int
protected final MutableDataComponentHolder
-
Constructor Summary
ConstructorDescriptionComponentEnergyStorage
(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 TypeMethodDescriptionboolean
Returns if this storage can have energy extracted.boolean
Used to determine if this storage can receive energy.int
extractEnergy
(int toExtract, boolean simulate) Removes energy from the storage.int
Returns the amount of energy currently stored.int
Returns the maximum amount of energy that can be stored.int
receiveEnergy
(int toReceive, boolean simulate) Adds energy to the storage.protected void
setEnergy
(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 anItemStack
energyComponent
- 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:IEnergyStorage
Adds energy to the storage. Returns the amount of energy that was accepted.- Specified by:
receiveEnergy
in 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:IEnergyStorage
Removes energy from the storage. Returns the amount of energy that was removed.- Specified by:
extractEnergy
in 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:IEnergyStorage
Returns the amount of energy currently stored.- Specified by:
getEnergyStored
in interfaceIEnergyStorage
-
getMaxEnergyStored
public int getMaxEnergyStored()Description copied from interface:IEnergyStorage
Returns the maximum amount of energy that can be stored.- Specified by:
getMaxEnergyStored
in interfaceIEnergyStorage
-
canExtract
public boolean canExtract()Description copied from interface:IEnergyStorage
Returns if this storage can have energy extracted. If this is false, then any calls to extractEnergy will return 0.- Specified by:
canExtract
in interfaceIEnergyStorage
-
canReceive
public boolean canReceive()Description copied from interface:IEnergyStorage
Used to determine if this storage can receive energy. If this is false, then any calls to receiveEnergy will return 0.- Specified by:
canReceive
in 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
-