Class ComponentEnergyStorage

java.lang.Object
net.neoforged.neoforge.energy.ComponentEnergyStorage
All Implemented Interfaces:
IEnergyStorage

public class ComponentEnergyStorage extends Object implements 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 Details

    • parent

      protected final MutableDataComponentHolder parent
    • energyComponent

      protected final DataComponentType<Integer> 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 an ItemStack
      energyComponent - The data component referencing the stored energy of the item stack
      capacity - The max capacity of the energy being stored
      maxReceive - The max per-transfer power input rate
      maxExtract - 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:
      • invalid reference
        ComponentEnergyStorage#ItemEnergyStorage(ItemStack, DataComponentType, int, int, int)
    • ComponentEnergyStorage

      public ComponentEnergyStorage(MutableDataComponentHolder parent, DataComponentType<Integer> energyComponent, int capacity)
      Creates a new ItemEnergyStorage with a transfer rate equivalent to the capacity.
      See Also:
      • invalid reference
        ComponentEnergyStorage#ItemEnergyStorage(ItemStack, DataComponentType, int, int, int)
  • 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 interface IEnergyStorage
      Parameters:
      toReceive - The amount of energy being received.
      simulate - If true, the insertion will only be simulated, meaning IEnergyStorage.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 interface IEnergyStorage
      Parameters:
      toExtract - The amount of energy being extracted.
      simulate - If true, the extraction will only be simulated, meaning IEnergyStorage.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 interface IEnergyStorage
    • getMaxEnergyStored

      public int getMaxEnergyStored()
      Description copied from interface: IEnergyStorage
      Returns the maximum amount of energy that can be stored.
      Specified by:
      getMaxEnergyStored in interface IEnergyStorage
    • 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 interface IEnergyStorage
    • 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 interface IEnergyStorage
    • 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