Machine

Interact directly with the machine in CT scripts

Machine is one of the object provided in the function requirement context.

It contains various methods for interacting with the machine.

Methods

Id

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.

var machine = ctx.machine;

//Return the machine id as a string.
var id = machine.id;

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

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.

var machine = ctx.machine;

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

//Put something in the data : 
data.put("power", 2)
Owner
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;
Pause

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

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;
Energy
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);
Fluids
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 couldn't be 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 couldn't be 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);
Items
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();
Chunkload
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()

Mekanism support

The following methods are only available if Custom Machinery Mekanism addon is installed.

Chemicals (Gas, Infusion, Pigment, Slurry)

The following methods are available for all supported chemical types, simply change 'XXX' by the type of chemical you want to manipulate : 'Gas', 'Infusion', 'Pigment', 'Slurry'

All chemicals used here must be a Crafttweaker ChemicalStack corresponding to the used chemical type.

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

//Return the chemical stored in the specified tank.
var fluid = machine.getXXXStored("tankID");

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

//Return the capacity of the specified tank.
var capacity = machine.getXXXCapacity("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.addXXX(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.addXXXToTank("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.removeXXX(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.removeXXXFromTank("tankID", amount, simulate);

Examples :

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

machine.addGas(<gas:mekanism:steam> * 500, false);

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

var removedSlurry = machine.removeFromTank("slurry", machine.getSlurryCapacity("slurry"), false);
Heat/Temperature
//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;
Radiations
//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);

Last updated