Generator
Generator is the base class used for all energy-producing machines in UtilityCraft.
It extends BasicMachine and therefore inherits all base infrastructure such as:
- entity access
- block access
- dimension access
- inventory container access
- progress tracking
- energy integration
- tick refresh validation
- energy and UI display helpers
The Generator class adds behavior specific to energy generation systems, including:
- generator configuration access
- generator entity spawning
- generator destruction handling
- nearby machine detection
- energy transfer mode configuration
Hierarchy:
BasicMachine
└─ Generator
All methods and properties from BasicMachine are available here.
This page documents only the additional behavior introduced by Generator.
Index
Properties
Static Methods
Constructor
new Generator
new Generator(block: Block, settings: GeneratorSettings)
Creates a new generator instance using the infrastructure provided by BasicMachine.
Parameters
- blockBlock
Block representing the generator in the world.
- settingsGeneratorSettings
Generator configuration object defining behavior such as:
- base generation rate
- energy capacity
- fluid capacity (optional)
- entity type
Behavior
The constructor performs the following:
- Reads the base generation rate from
settings.generator.rate_speed_base. - Calls the
BasicMachineconstructor with that rate. - Validates that the generator entity exists and the tick is valid.
- Stores the generator settings for later use.
Properties
settings
Type: GeneratorSettings
Stores the generator configuration passed into the constructor.
const settings = generator.settings
This configuration usually contains:
- generator energy capacity
- fluid capacity (if the generator consumes fluids)
- generation rate
- entity identifier
- inventory size
It allows generator implementations to access their configuration without needing to re-read external definitions.
Static Methods
onDestroy
Generator.onDestroy(event): boolean
Handles the destruction of a generator block.
Behavior
When a generator is broken:
- Retrieves the generator entity associated with the block.
- Reads stored energy and fluid values.
- Encodes those values into the dropped block item lore.
- Drops all stored inventory items.
- Removes the generator entity.
- Spawns the generator block item preserving stored information.
If the player is in Creative mode, the original block drop is removed so the custom preserved drop replaces it.
Returns
Type: boolean
Returns true if a generator entity was processed.
Returns false if no entity was found.
spawnEntity
Generator.spawnEntity(event, config, callback?)
Spawns the generator entity when the block is placed.
Behavior
- Reads the item held in the player's main hand.
- Extracts stored energy and fluid data from the item.
- Spawns the generator entity.
- Initializes generator energy capacity and stored energy.
- Initializes fluid storage if the generator supports fluids.
- Registers nearby machine positions.
- Executes an optional callback after initialization.
- Updates adjacent energy networks.
Parameters
- eventobject
Placement event containing block location, player, and permutation.
- configGeneratorSettings
Generator configuration describing entity type, capacities and generator properties.
- callbackfunction
Optional callback executed after the generator entity has been initialized.
addNearbyMachines
Generator.addNearbyMachines(entity: Entity): void
Adds position tags for all blocks surrounding the generator entity.
Behavior
The generator scans the six adjacent blocks:
- East
- West
- Up
- Down
- South
- North
For each adjacent position it adds a tag using the format:
pos:[x,y,z]
These tags allow energy transfer systems to quickly identify nearby machines without performing expensive block scans.
Example
pos:[101,64,-23]
pos:[99,64,-23]
pos:[100,65,-23]
pos:[100,63,-23]
These tags are later used by energy transfer logic to determine valid targets.
openGeneratorTransferModeMenu
Generator.openGeneratorTransferModeMenu(entity: Entity, player: Player): void
Opens a configuration menu allowing players to change the generator energy distribution mode.
Transfer Modes
Generators support three distribution strategies:
| Mode | Description |
|---|---|
| nearest | Sends energy to the closest machine first |
| farthest | Sends energy to the farthest machine first |
| round | Distributes energy evenly across all connected machines |
Behavior
- Reads the current generator transfer mode.
- Opens a dropdown menu.
- Allows the player to select a new mode.
- Stores the new value as a dynamic property.
- Displays a confirmation message in the player's action bar.
Example Result
Transfer mode set to: Nearest
Example
const generator = new Generator(block, settings)
if (!generator.valid) return
generator.displayEnergy()
Notes
Generator is used as the base implementation for all UtilityCraft generators, including:
- Furnator
- Magmator
- Thermo Generator
- Solar Panel
Each generator defines its own generation logic but shares the infrastructure provided by this class.