Function

Use the function requirement in a CT recipe

Use one of these methods to add a function that must be processed by the machine to continue the recipe.

Note: This has nothing to do with MC functions (or commands), this requirement allow you to make your own code in ZenScript and pass it to be executed by the machine.

//These imports are needed, put these 2 lines on top of the script.
import mods.custommachinery.Context;
import mods.custommachinery.Machine;

//Executed when the machine is idle and search a recipe to process.
//Returning success will allow the machine to process this recipe (if all other requirements allow it as well).
//Returning error will prevent the machine to process this recipe (the machine will keep searching for another valid recipe).
.requireFunctionToStart((ctx as Context) => {return ctx.success()})

//Executed the first tick of the crafting process.
//Returning success will allow the machine to continue to process this recipe.
//Returning error will put the machine in error status and display the provided error message.
.requireFunctionOnStart((ctx as Context) => {return ctx.success()})

//Executed each tick of the crafting process.
//Returning success will allow the machine to continue to process this recipe.
//Returning error will put the machine in error status and display the provided error message.
.requireFunctionEachTick((ctx as Context) => {return ctx.success()})

//Executed the last tick of the crafting process.
//Returning success will allow the machine to continue to process this recipe.
//Returning error will put the machine in error status and display the provided error message.
.requireFunctionOnEnd((ctx as Context) => {return ctx.success()})
  • The passed function MUST return a Result.

  • Valid results are : ctx.success() and ctx.error("Error message")

  • Context has various methods for interacting with the machine, see it's wiki page

  • The function can be delayed, put .delay(delay) directly after any .requireFunctionXXX() call to make the function be executed at the specified delay.

Example

import mods.custommachinery.Context;
import mods.custommachinery.Machine;

mods.custommachinery.CMRecipeBuilder.create("custommachinery:power_crusher", 100)
	.requireFunctionOnEnd((ctx as Context) => {
		var machine = ctx.machine as Machine;
		var output = machine.addItemToSlot("output1", <item:minecraft:diamond>, false);
		if(!output.empty)
			return ctx.error("Couldn't put a diamond in output slot.");
		else
		    return ctx.success();
	})
	.build();