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

# Machine

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

It contains various methods for interacting with the machine.

### Methods

<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.

<pre class="language-java"><code class="lang-java"><strong>var machine = ctx.machine;
</strong>
//Return the machine id as a string.
var id = machine.id;

//Change the machine ID
machine.id = "namespace:machine_id"
</code></pre>

</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.

In a CT script the data is retrieved as a [MapData](https://docs.blamejared.com/1.18/en/vanilla/api/data/MapData).

```java
var machine = ctx.machine;

//Get the MapData from the machine.
var data = machine.data;

//Put something in the data : 
data.put("power", 2)
```

</details>

<details>

<summary>Owner</summary>

```java
var machine = ctx.machine;

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

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

//Check if an entity is the owner of the machine.
var 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.
var 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.

```java
var machine = ctx.machine;

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

//Pause the machine
machine.paused = true;

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

</details>

<details>

<summary>Energy</summary>

```java
var machine = ctx.machine;

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

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

//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 value.
var 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 value.
var removedEnergy = machine.removeEnergy(amountToRemove, simulate);
```

</details>

<details>

<summary>Fluids</summary>

```java
var machine = ctx.machine;

//Return the fluid stored in the specified tank.
//The returned fluid is a CT IFluidStack, see docs : https://docs.blamejared.com/1.16/en/vanilla/api/fluid/IFluidStack
var fluid = machine.getFluidStored("tankID");

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

//Add the specified fluid to the first available tank.
//Return the amount of fluid that was added.
//The passed fluid must be a CT IFluidStack, see docs : https://docs.blamejared.com/1.16/en/vanilla/api/fluid/IFluidStack
//If simulate is `false` the fluid will really be inserted, otherwise it wont.
var 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 CT IFluidStack, see docs : https://docs.blamejared.com/1.16/en/vanilla/api/fluid/IFluidStack
//If simulate is `false` the fluid will really be inserted, otherwise it wont.
var 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 CT IFluidStack, see docs : https://docs.blamejared.com/1.16/en/vanilla/api/fluid/IFluidStack
//If simulate is `false` the fluid will really be removed, otherwise it wont.
var 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 CT IFluidStack, see docs : https://docs.blamejared.com/1.16/en/vanilla/api/fluid/IFluidStack
//The specified amount MUST be a positive integer value.
//If simulate is `false` the fluid will really be removed, otherwise it wont.
var remaining = machine.removeFluidFromTank("tankID", amount, simulate);
```

</details>

<details>

<summary>Items</summary>

```java
var machine = ctx.machine;

//Return the item stored in the specified slot.
//The returned item is a CT IItemStack, see docs : https://docs.blamejared.com/1.16/en/vanilla/api/items/IItemStack
var item = machine.getItemStored("slotID");

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

//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 CT IItemStack, see docs : https://docs.blamejared.com/1.16/en/vanilla/api/items/IItemStack
//If simulate is `false` the item will really be inserted, otherwise it wont.
var 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 CT IItemStack, see docs : https://docs.blamejared.com/1.16/en/vanilla/api/items/IItemStack
//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
var 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>

### 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>

All chemicals used here must be a Crafttweaker [ChemicalStack](https://docs.blamejared.com/1.16/en/mods/Mekanism/api/chemical/stack/ChemicalStack).

```javascript
//Get the machine from the context.
var machine = ctx.machine;

//Return the chemical stored in the specified tank.
var 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.
var 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.
var 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.
var 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.
var 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.
var remaining = machine.removeChemicalFromTank("tankID", amount, simulate);
```

Examples : &#x20;

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

```javascript
machine.addChemical(<chemical:mekanism:steam> * 500, false);
```

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

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

</details>

<details>

<summary>Heat/Temperature</summary>

```javascript
//Get the machine from the context.
var machine = ctx.machine;

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

//Get the temperature of the machine (in Kelvin I guess).
var 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.
var machine = ctx.machine;

//Get the radiations amount at the machine position.
var 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>
