|
| 1 | +--- |
| 2 | +title: Custom Machine Behavior |
| 3 | +--- |
| 4 | + |
| 5 | +!!! Warning |
| 6 | + Custom Machine Behavior is currently only supported in Java. |
| 7 | + |
| 8 | +Sometimes, you want to do something in a machine that's not possible via Recipe Conditions or Recipe Modifiers. For this, Custom Machine Behavior might be the correct tool. |
| 9 | + |
| 10 | +It works by registering a custom TickableSubscription, which gets called every tick. |
| 11 | +Here, we want to make a greenhouse that, while running, also turns all the dirt above it into grass. |
| 12 | +```java |
| 13 | + |
| 14 | +public class Greenhouse extends WorkableElectricMultiblockMachine { |
| 15 | + |
| 16 | + private TickableSubscription tickSubscription; |
| 17 | + |
| 18 | + @Override |
| 19 | + public void onStructureFormed() { |
| 20 | + super.onStructureFormed(); |
| 21 | + if (!isRemote()) { |
| 22 | + tickSubscription = this.subscribeServerTick(this::turnGreenery); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void onStructureInvalid() { |
| 28 | + super.onStructureInvalid(); |
| 29 | + if (!isRemote()) { |
| 30 | + tickSubscription.unsubscribe(); |
| 31 | + tickSubscription = null; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private void turnGreenery(){ |
| 36 | + if(!getRecipeLogic().isActive()) return; |
| 37 | + BlockPos currentPosition = getRecipeLogic().getMachine().getHolder().getCurrentPos(); |
| 38 | + BlockPos abovePosition = currentPosition.above(); |
| 39 | + if(this.getLevel().getBlockState(abovePosition).equals(Blocks.DIRT.defaultBlockState())){ |
| 40 | + this.getLevel().setBlock(abovePosition, Blocks.GRASS.defaultBlockState(), 3); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | +The `tickSubscription` field is a reference to the current subscription that should be called every tick. When this subscription is created, we tell the server to run `this.turnGreenery()` every tick. |
| 46 | + |
| 47 | +In there, we simply check if the recipe logic is active, and if so, we turn the block above the machine into grass if it was already dirt. |
| 48 | + |
| 49 | +To use it, you would do: |
| 50 | +```java |
| 51 | + public static final MultiblockMachineDefinition GREENHOUSE = REGISTRATE |
| 52 | + .multiblock("green_house", Greenhouse::new) |
| 53 | + .rotationState(RotationState.ALL) |
| 54 | + .recipeType(MyRecipeTypes.GREENHOUSE) |
| 55 | + .recipeModifiers(OC_PERFECT_SUBTICK, BATCH_MODE) |
| 56 | + .appearanceBlock(CASING_PTFE_INERT) |
| 57 | + .pattern(definition -> { |
| 58 | + var casing = blocks(CASING_PTFE_INERT.get()).setMinGlobalLimited(10); |
| 59 | + var abilities = Predicates.autoAbilities(definition.getRecipeTypes()) |
| 60 | + .or(Predicates.autoAbilities(true, false, false)); |
| 61 | + return FactoryBlockPattern.start() |
| 62 | + .aisle("XSX", "XXX", "XXX") |
| 63 | + .aisle("XXX", "XXX", "XXX") |
| 64 | + .where('S', Predicates.controller(blocks(definition.getBlock()))) |
| 65 | + .where('X', casing.or(abilities)) |
| 66 | + .build(); |
| 67 | + }) |
| 68 | + .workableCasingModel(GTCEu.id("block/casings/solid/machine_casing_inert_ptfe"), |
| 69 | + GTCEu.id("block/multiblock/large_chemical_reactor")) |
| 70 | + .register(); |
| 71 | +``` |
0 commit comments