Machine
Interact directly with the machine in JS scripts
Machine is one of the object provided in the function requirement context.
It contains various methods for interacting with the machine.
Methods
Of
Create a Machine object from a BlockEntity or BlockContainer objects.
var machine = CustomMachine.of(blockEntity);
var machine = CustomMachine.of(blockContainer);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 :
var data = ctx.machine.data;
//Put a number in the data :
data.power = 2;
//Increment that number :
data.power++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;
//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.
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 integer 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 KubeJS FluidStackJS, see code: https://github.com/KubeJS-Mods/KubeJS/blob/1.16/main/common/src/main/java/dev/latvian/kubejs/fluid/FluidStackJS.java
var fluid = machine.getFluidStored("tankID");
//Return the capacity of the specified tank.
var 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 couldn't be 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.
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 KubeJS FluidStackJS, create one with Fluid.of("namespace:fluidID", amount)
//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 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.
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 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.
var remaining = machine.removeFluidFromTank("tankID", amount, simulate);Last updated