---
description: Interact directly with the machine in JS scripts
---

# Machine

Machine is one of the object provided in the [function requirement event](function-event).

It contains various methods for interacting with the machine.

### Methods

<details>

<summary>Of</summary>

Create a Machine object from a BlockEntity or [BlockContainer](https://kubejs.com/wiki/kubejs/BlockContainerJS/) objects.

```javascript
let machine = CustomMachine.of(blockEntity);
let machine = CustomMachine.of(blockContainer);
```

</details>

<details>

<summary>Id</summary>

Get the id of the machine, the id is a String of the format : `namespace:path`.

You can also change the machine id, the machine will switch to the specified machine, inventory will be preserved as long as machines have similar inventories.

```javascript
let machine = ctx.machine

//Get the machine ID
let id = machine.id

//Change the machine ID
machine.id = "namespace:machine_id"
```

#### Reminder : A machine stored in `data/namespace/machines/machine.json` will have the id : `namespace:machine`

</details>

<details>

<summary>Data</summary>

Add a way to store custom data in the machine.

The stored data will be stored in the machine as NBT and saved when the machine is unloaded, preventing loosing the data, for example, when the game is quitted.

KubeJS will treat the data as a Map.

<pre class="language-javascript"><code class="lang-javascript"><strong>//Get the data :
</strong><strong>let data = ctx.machine.data;
</strong>
//Put a number in the data :
data.power = 2;

//Increment that number : 
data.power++
</code></pre>

</details>

<details>

<summary>Owner</summary>

```javascript
let machine = ctx.machine;

//Get the machine owner name as a Component, or null if the machine doesn't have an owner.
let ownerName = machine.ownerName;

//Get the machine owner UUID, or null if the machine doesn't have an owner.
let ownerId = machine.ownerId;

//Check if an entity is the owner of the machine.
let isOwner = machine.isOwner(entity);

//Get the owner as a LivingEntity, or null if the machine doesn't have an owner.
//This only works if the entity that own the machine is loaded.
//If the owner is a player they must be connected.
//IF the owner is a mob it must be in a loaded chunk.
let owner = machine.owner;

//Set a LivingEntity as the new machine owner.
machine.owner = entity;
```

</details>

<details>

<summary>Pause</summary>

Allow to check if the machine is paused, and pause or resume it.

```javascript
let machine = ctx.machine;

//Return true if the machine is paused, false otherwise
let paused = machine.paused;

//Pause the machine
machine.paused = true;

//Resume the machine
machine.pause = false;
```

</details>

<details>

<summary>Energy</summary>

```javascript
let machine = ctx.machine;

//Return the energy stored in the machine internal buffer.
let energy = machine.energyStored;

//Return the maximum capacity of the machine energy storage.
let energyCapacity = machine.energyCapacity;

//Set the current energy of the machine, this override the previous energy stored.
//newEnergyAmount must be a positive integer value.
//Max value : 9,223,372,036,854,775,807
machine.setEnergyStored(newEnergyAmount);

//Return the amount of energy that was successfully added to the machine (or amountToAdd if all the energy was inserted).
//If simulate is `false` the energy will really be inserted, otherwise it wont.
//amountToAdd MUST be a positive integer value.
let addedEnergy = machine.addEnergy(amountToAdd, simulate);

//Return the amount of energy that was successfully removed from the machine (or amountToRemove if all the energy was extracted).
//If simulate is `false` the energy will really be extracted, otherwise it wont.
//amountToRemove MUST be a positive integer value.
let removedEnergy = machine.removeEnergy(amountToRemove, simulate);
```

</details>

<details>

<summary>Fluids</summary>

```javascript
let machine = ctx.machine;

//Return the fluid stored in the specified tank.
//The returned fluid is a KubeJS FluidStackJS, see code: https://github.com/KubeJS-Mods/KubeJS/blob/1.16/main/common/src/main/java/dev/latvian/kubejs/fluid/FluidStackJS.java
let fluid = machine.getFluidStored("tankID");

//Return the capacity of the specified tank.
let capacity = machine.getFluidCapacity("tankID");

//Put the specified fluid in the specified tank, this will override the previous stored fluid.
//The passed fluid must be a KubeJS FluidStackJS, create one with Fluid.of("namespace:fluidID", amount)
machine.setFluidStored("tankID", fluid);

//Add the specified fluid to the first available tank.
//Return the amount of fluid that was added.
//The passed fluid must be a KubeJS FluidStackJS, create one with Fluid.of("namespace:fluidID", amount)
//If simulate is `false` the fluid will really be inserted, otherwise it wont.
let remaining = machine.addFluid(fluid, simulate);

//Add the specified fluid to the specified tank.
//Return the amount of fluid that was added.
//The passed fluid must be a KubeJS FluidStackJS, create one with Fluid.of("namespace:fluidID", amount)
//If simulate is `false` the fluid will really be inserted, otherwise it wont.
let remaining = machine.addFluidToTank("tankID", fluid, simulate);

//Remove the specified fluid from the first available tank.
//Return the fluid that was successfully removed.
//Both passed and returned fluids must be a KubeJS FluidStackJS, see code: https://github.com/KubeJS-Mods/KubeJS/blob/1.16/main/common/src/main/java/dev/latvian/kubejs/fluid/FluidStackJS.java
//If simulate is `false` the fluid will really be removed, otherwise it wont.
let removedFluid = machine.removeFluid(fluid, simulate);

//Remove the specified amount of fluid from the specified tank.
//Return the fluid that was successfully removed.
//The returned fluid is a KubeJS FluidStackJS, see code: https://github.com/KubeJS-Mods/KubeJS/blob/1.16/main/common/src/main/java/dev/latvian/kubejs/fluid/FluidStackJS.java
//The specified amount MUST be a positive integer value.
//If simulate is `false` the fluid will really be removed, otherwise it wont.
let remaining = machine.removeFluidFromTank("tankID", amount, simulate);
```

</details>

<details>

<summary>Items</summary>

```javascript

let machine = ctx.machine;

//Return the item stored in the specified slot.
//The returned item is a KubeJS ItemStackJS, see docs : https://kubejs.com/wiki/kubejs/ItemStackJS/
let item = machine.getItemStored("slotID");

//Return the capacity of the specified slot.
let capacity = machine.getItemCapacity("slotID");

//Set the specified item in the specified slot.
//The passed item must be a KubeJS ItemStackJS, create one using Item.of("namespace:itemID", amount)
machine.setItemStored("slotID", item);

//Add the specified item to the specified slot.
//Return the item that couldn't be added, or an empty ItemStack if all items were successfully added.
//The passed item must be a KubeJS ItemStackJS, create one using Item.of("namespace:itemID", amount)
//If simulate is `false` the item will really be inserted, otherwise it wont.
let remaining = machine.addItemToSlot("slotID", itemToAdd, simulate);

//Remove the specified amount of item from the specified slot.
//Return the item that were successfully removed, or an empty ItemStack if the slot was empty.
//The returned item is a KubeJS ItemStackJS, see docs : https://kubejs.com/wiki/kubejs/ItemStackJS/
//The specified amount MUST be a positive integer value.
//If simulate is `false` the item will really be extracted, otherwise it wont.
var extracted = machine.removeItemFromSlot("slotID", amountToRemove, simulate);

//Lock a slot (players won't be able to place or take items from the GUI but automation will still work.
machine.lockSlot("slotID");
//Unlock a slot
machine.unlockSlot("slotID");
//Return true if the slot is locked, false otherwise
let locked = slot.isSlotLocked();
```

</details>

<details>

<summary>Chunkload</summary>

```javascript
let machine = ctx.machine

//Enable chunkloading with a radius of 1
machine.enableChunkloading(1)

//Disable chunkloading
machine.disableChunkload()

//Check if chunkloading is active
let active = machine.isChunkloadEnabled()

//Get the current chunkload radius
let radius = machine.getChunkloadRadius()
```

</details>

<details>

<summary>Components IO config</summary>

The following methods let you access and change the IO (input/output) config of any components of the machine (energy/fluid/item or addons).

Not all methods will be shown there, as this method expose java internals of Custom Machinery, a solid understanding of Javascript and/or Java code will be needed.

The source code can be viewed [here](https://github.com/Frinn38/Custom-Machinery/blob/1.21/src/main/java/fr/frinn/custommachinery/impl/component/config/SideConfig.java). The usage of tools like [ProbeJS ](https://www.curseforge.com/minecraft/mc-mods/probejs)on the Javascript side is greatly encouraged.

```javascript
//Get the machine from context
let machine = ctx.machine

//Get side config for an item component with id 'input1'
let config = machine.getComponentConfig('custommachinery:item', 'input1')

//Get side config for the energy component
let config = machine.getComponentConfig('custommachinery:energy')

//Get the IO mode of the right side of the machine
let mode = config.getSideMode('right')

//Set the IO mode of the left side of the machine to output
config.setSideMode('left', 'output')
```

</details>

### Mekanism support

{% hint style="info" %}
The following methods are only available if [Custom Machinery Mekanism](https://www.curseforge.com/minecraft/mc-mods/custom-machinery-mekanism) addon is installed.
{% endhint %}

<details>

<summary>Chemicals</summary>

You can make a chemical stack with the following syntax : `"100x mekanism:steam"` where 100 is the amount in mB of the stack.

```javascript
//Get the machine from the context.
let machine = ctx.machine

//Return the chemical stored in the specified tank.
let fluid = machine.getChemicalStored("tankID")

//Set a the content of a tank.
//Doesn't return anything.
machine.setChemicalStored(chemical)

//Return the capacity of the specified tank.
let capacity = machine.getChemicalCapacity("tankID")

//Add the specified chemical to the first available tank.
//Return the chemical that couldn't be added.
//If simulate is `false` the chemical will really be inserted, otherwise it wont.
let remaining = machine.addChemical(chemical, simulate)

//Add the specified chemical to the specified tank.
//Return the chemical that couldn't be added.
//If simulate is `false` the chemical will really be inserted, otherwise it wont.
let remaining = machine.addChemicalToTank("tankID", chemical, simulate)

//Remove the specified chemical from the first available tank.
//Return the chemical that was successfully removed.
//If simulate is `false` the chemical will really be removed, otherwise it wont.
let removedFluid = machine.removeChemical(chemical, simulate)

//Remove the specified amount of chemical from the specified tank.
//Return the chemical that was successfully removed.
//The specified amount MUST be a positive integer value.
//If simulate is `false` the chemical will really be removed, otherwise it wont.
let remaining = machine.removeChemicalFromTank("tankID", amount, simulate)
```

Examples : &#x20;

Put 500mB of steam gas in the first available tank.

```javascript
machine.addChemical("500x mekanism:steam", false)
```

Remove all the slurry stored in the "slurry" tank.

<pre class="language-javascript"><code class="lang-javascript"><strong>let removedSlurry = machine.removeChemicalFromTank("slurry", machine.getChemicalCapacity("slurry"), false)
</strong></code></pre>

</details>

<details>

<summary>Heat/Temperature</summary>

```javascript
//Get the machine from the context.
let machine = ctx.machine

//Get the heat stored in the machine (as raw decimal value).
let heat = machine.heat

//Get the temperature of the machine (in Kelvin I guess).
let temperature = machine.temperature

//Add some heat to the machine (you can use negative values to actually remove heat).
machine.addHeat(amount)

//Set the heat of the machine to a specified value.
machine.heat = amount
```

</details>

<details>

<summary>Radiations</summary>

```javascript
//Get the machine from the context.
let machine = ctx.machine

//Get the radiations amount at the machine position.
let radiations = machine.radiations

//Add some radiations to the machine position.
machine.addRadiations(amount)

//Remove radiations in a radius (in blocks) around the machine.
machine.removeRadiations(amount, radius)
```

</details>

### Create support

{% hint style="info" %}
The following methods are only available if [Custom Machinery Create](https://www.curseforge.com/minecraft/mc-mods/custom-machinery-create) addon is installed.
{% endhint %}

<details>

<summary>Stress</summary>

```javascript
//Get the machine from the context.
let machine = ctx.machine

//Return the stress applied by the machine to the contraption.
let stress = machine.getStressApplied()

//Return the stress capacity added by the machine.
let stressCapacity = machine.getStressCapacity()

//Set the stress impact of the machine.
machine.setStressImpact(stressImpact)

//Set the stress capacity added by the machine.
machine.setStressCapacity(stressCapacity)
```

</details>

<details>

<summary>Speed</summary>

```javascript
//Get the machine from the context.
let machine = ctx.machine

//Return the current speed of the machine in RPM.
let speed = machine.getCurrentSpeed()

//Return the speed generated by the machine.
let generatedSpeed = machine.getGeneratedSpeed()

//Set the speed generated by the machine.
machine.setSpeed(speed)
```

</details>
