Skip to main content

Generator class

Namespace: DoriosCore · Package: DoriosCore/index.js

Generator is the standard runtime for energy-producing blocks. It supplies scheduler-aware rates, energy storage, optional indexed liquid and gas tanks, IO processing, resource-preserving placement and destruction, and an energy-transfer mode form.

import { Generator } from "DoriosCore/index.js";

Definition

class Generator extends BasicMachine

BasicMachine
└─ Generator
└─ MultiblockGenerator

Constructor

new Generator(block, settings)

new Generator(block: Block, settings: GeneratorSettings)

ParameterTypeDescription
blockBlockGenerator block in the world.
settingsGeneratorSettingsEntity, generation-rate, storage, and optional multiblock configuration.

The base rate is settings.generator.rate_speed_base ?? 0. As with every BasicMachine subclass, check valid before using runtime properties.

const generator = new Generator(block, settings);
if (!generator.valid) return;

GeneratorSettings

interface GeneratorSettings {
entity: MachineEntityConfig;
generator: GeneratorRuntimeConfig;
spawn_offset?: Vector3;
rotation?: boolean;
ignoreTick?: boolean;
requirements?: Record<string, Requirement>;
required_case?: string;
fillBlocksConfig?: FillBlocksConfig;
deactivateConfig?: FillBlocksConfig;
missingEnergyWarning?: string;
}

GeneratorRuntimeConfig

PropertyTypeRequiredDescription
rate_speed_basenumberNoEnergy generation rate per ordinary tick before scheduler scaling. Defaults to 0.
energy_capnumberNoGenerator energy capacity.
fluid_capnumberNoCapacity assigned to every configured liquid tank.
fluid_typesnumberNoNumber of indexed liquid tanks. Defaults to 1 when liquid storage exists.
gas_capnumberNoCapacity assigned to every configured gas tank.
gas_typesnumberNoNumber of indexed gas tanks. Defaults to 1 when gas storage exists.

The entity-related properties have the same meaning as MachineSettings.

Properties

PropertyTypeDescription
settingsGeneratorSettingsOriginal settings supplied to the valid runtime.

All scheduler, block, entity, inventory, energy, rate, UI, progress, and IO properties are inherited from BasicMachine.

Static methods

Generator.spawnEntity(event, config, callback)

Generator.spawnEntity(event: PlacementEventLike, config: GeneratorSettings, callback?: (entity: Entity) => void): void

Queues creation and initialization of a generator helper entity.

ParameterTypeRequiredDescription
event.blockBlockYesBlock receiving the helper entity.
event.playerPlayerYesPlayer placing the generator; its held item may contain preserved resources.
event.permutationToPlaceBlockPermutationYesPlaced block permutation used when notifying adjacent systems.
configGeneratorSettingsYesHelper entity and generator storage configuration.
callback(entity: Entity) => voidNoRuns after storage and IO setup, before registered interface buttons are finalized.

The method initializes energy capacity, creates every configured liquid and gas index, restores preserved resource lore, prepares IO documents, runs the callback, installs interfaces, and notifies adjacent UtilityCore-managed networks.

beforeOnPlayerPlace(event, { params: settings }) {
Generator.spawnEntity(event, settings, (entity) => {
const generator = new Generator(event.block, {
...settings,
ignoreTick: true,
});
if (!generator.valid) return;
generator.displayEnergy();
});
}

Generator.onDestroy(event)

Generator.onDestroy(event: DestroyEventLike): boolean

ParameterTypeDescription
event.blockBlockGenerator block being destroyed.
event.brokenBlockPermutationBlockPermutationSupplies the preserved block item ID.
event.player`Playerundefined`
event.dimensionDimensionDimension containing the block and helper entity.

Returns false when no helper entity exists. Otherwise queues inventory drops, scheduler release, helper removal, and a generator item containing serialized energy and every liquid/gas tank.

Generator.addNearbyMachines(entity)

Generator.addNearbyMachines(entity: Entity): void

Deprecated

Network tags are rebuilt through real placed blocks and UtilityCore's update flow. Do not register all six adjacent positions for new generators.

Adds legacy pos:[x,y,z] tags for all six neighboring block locations.

Generator.openGeneratorTransferModeMenu(entity, player)

Generator.openGeneratorTransferModeMenu(entity: Entity, player: Player): void

Opens a localized modal form and stores the selected value in the entity's transferMode dynamic property.

ParameterTypeDescription
entityEntityGenerator helper entity that owns the transfer mode.
playerPlayerPlayer shown the form and confirmation action bar.

Available values:

ModeBehavior
nearestPrioritize the nearest compatible targets.
farthestPrioritize the farthest compatible targets.
roundDistribute across targets in round-robin order.

The method returns immediately if either parameter is missing. Closing the form leaves the existing mode unchanged; invalid selections fall back to nearest.

Inherited methods

Generator uses these inherited methods directly:

  • processIO() for item, liquid, and gas face transfers.
  • on() and off() for active block state.
  • setLabel() for UI status.
  • displayEnergy() and displayProgress() for standard UI slots.
  • setRate() when an addon needs to adjust the effective base generation rate.

See BasicMachine for every inherited signature.

Example: passive generator

import { EnergyStorage, Generator } from "DoriosCore/index.js";

function tick(block, settings) {
const generator = new Generator(block, settings);
if (!generator.valid) return;

generator.processIO();

const produced = generator.energy.add(generator.rate);
generator.energy.transferToNetwork(generator.rate * 4);

if (produced <= 0) {
generator.off();
generator.setLabel("§r§eEnergy Full");
generator.displayEnergy();
return;
}

generator.on();
generator.displayEnergy();
generator.setLabel([
"§r§aGenerator Running",
`§r§7Produced: §f${EnergyStorage.formatEnergyToText(produced)}`,
]);
}

For gas-fueled generation, see the Gas Turbine example.