Machine

Interact directly with the machine in JS scripts

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

It contains various methods for interacting with the machine.

Methods

Of

Create a Machine object from a BlockEntity or BlockContainer objects.

let machine = CustomMachine.of(blockEntity);
let machine = CustomMachine.of(blockContainer);
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.

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

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.

KubeJS will treat the data as a Map.

//Get the data :
let data = ctx.machine.data;

//Put a number in the data :
data.power = 2;

//Increment that number : 
data.power++
Owner
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;
Pause

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

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;
Energy
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);
Fluids
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);
Items

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();
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

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

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

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

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

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

let removedSlurry = machine.removeChemicalFromTank("slurry", machine.getChemicalCapacity("slurry"), false)
Heat/Temperature
//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
Radiations
//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)

Last updated