|
| 1 | +--- |
| 2 | +title: Machine Trait System |
| 3 | +--- |
| 4 | + |
| 5 | +# Machine Trait System |
| 6 | + |
| 7 | +The machine trait system allows for attaching extra capabilities and behaviours to a machine by attaching traits to machine instances. Traits can listen for specific machine events, interactions, and provide capabilities, allowing for machine functionality to be implemented via a composition-based design. |
| 8 | + |
| 9 | +## Attaching and using machine traits |
| 10 | + |
| 11 | +Attaching traits must be done before machine instances are fully initialised, and are typically attached in the constructor. |
| 12 | + |
| 13 | +### Attaching and using traits: |
| 14 | + |
| 15 | +Attaching a machine trait to a machine is done with the `MetaMachine::attachTrait` and `MetaMachine::attachPersistentTrait` methods. |
| 16 | + |
| 17 | +When attaching traits, a `callbackPriority` value can be given. Traits with a higher priority will have their events and interactions called |
| 18 | +first, which may prevent traits with a lower priority from handling some events. |
| 19 | + |
| 20 | +!!! warning |
| 21 | + Traits must be attached to a machine instance via either `attachTrait` or `attachPersistentTrait`. |
| 22 | + If a trait is created without either of these methods being called, the trait will not be valid. |
| 23 | + |
| 24 | +```java |
| 25 | +public class CustomMachine extends MetaMachine { |
| 26 | + |
| 27 | + @SaveField |
| 28 | + protected final NotifiableFluidTank tank; |
| 29 | + |
| 30 | + public CustomMachine(BlockEntityCreationInfo info, int capacity) { |
| 31 | + super(info); |
| 32 | + |
| 33 | + // Because the tank field is annotated with `@SaveField`, the fluid tank will be saved into the machines data. |
| 34 | + this.tank = attachTrait(new NotifiableFluidTank(1, capacity, IO.BOTH)); |
| 35 | + |
| 36 | + // Registers an auto output trait that provides fluid output behaviour for the given fluid tank. |
| 37 | + // Instead of using an annotated field to save traits, they can also be registered to be saved. |
| 38 | + // The trait save name should remain the same, otherwise the trait save data won't be loaded. |
| 39 | + AutoOutputTrait autoOutput = attachPersistentTrait("autoOutput", AutoOutputTrait.ofFluids(tank)); |
| 40 | + |
| 41 | + autoOutput.setFluidOutputDirection(Direction.DOWN); |
| 42 | + autoOutput.setFluidOutputDirectionValidator(d -> d == Direction.DOWN); |
| 43 | + } |
| 44 | + |
| 45 | + public void usingTraits() { |
| 46 | + MetaMachine machine = getMachine(); |
| 47 | + |
| 48 | + // Most trait objects have a `TYPE` static field, it can be used to get traits with a specific type. |
| 49 | + AutoOutputTrait autoOutputTrait = machine.getTrait(AutoOutputTrait.TYPE); |
| 50 | + Optional<RecipeLogic> recipeLogicOptional = machine.getTrait(RecipeLogic.TYPE); |
| 51 | + |
| 52 | + // Gets all traits with the specified type. |
| 53 | + List<NotifiableItemStackHandler> allItemStackHandlers = machine.getTraits(NotifiableItemStackHandler.TYPE); |
| 54 | + |
| 55 | + List<MachineTrait> allTraits = machine.getAllTraits(); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Creating custom traits |
| 61 | + |
| 62 | +Custom machine traits are created by extending `MachineTrait` or `MultiblockMachineTrait` |
| 63 | + |
| 64 | +Machine traits have access to a number of machine events and callbacks, but some extra behaviours can be added by having a trait implement a trait feature interface. For example, `IInteractionTrait` to add custom interaction behaviour. The full list of trait features is in `api/machine/trait/feature`. |
| 65 | + |
| 66 | +```java |
| 67 | +public class CustomMachineTrait extends MachineTrait implements IInteractionTrait { |
| 68 | + |
| 69 | + // Machine traits should have a type object defined, unless a parent class of this machine trait already defines a type |
| 70 | + public static final MachineTraitType<CustomMachineTrait> TYPE = new MachineTraitType<>(CustomMachineTrait.class); |
| 71 | + |
| 72 | + public CustomMachineTrait() { |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + // Machine traits must also define a getter for the trait type |
| 77 | + @Override |
| 78 | + public MachineTraitType<CustomMachineTrait> getType() { |
| 79 | + return TYPE; |
| 80 | + } |
| 81 | + |
| 82 | + // A list of classes or interfaces which a machine must be in order for this trait to be attached. |
| 83 | + // A machine trait must be at least one of these interfaces/classes. |
| 84 | + // By default, traits can be attached to any machine. |
| 85 | + @Override |
| 86 | + protected List<Class<?>> validMachineClasses() { |
| 87 | + return List.of(CustomMachine.class); |
| 88 | + } |
| 89 | + |
| 90 | + // An example of a machine trait event callback. |
| 91 | + // Traits with a higher trait priority will have their events called first, |
| 92 | + // which may block lower priority traits from receiving events. |
| 93 | + // All traits default to a priority of 1. |
| 94 | + @Override |
| 95 | + public Pair<@Nullable GTToolType, InteractionResult> onToolClick(ExtendedUseOnContext context) { |
| 96 | + var toolType = context.getToolType(); |
| 97 | + if (toolType.contains(GTToolType.WRENCH)) { |
| 98 | + return Pair.of(GTToolType.WRENCH, onWrenchClick(context)); |
| 99 | + } |
| 100 | + return IInteractionTrait.super.onToolClick(context); |
| 101 | + } |
| 102 | + |
| 103 | + private InteractionResult onWrenchClick(ExtendedUseOnContext context) { |
| 104 | + return InteractionResult.PASS; |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
0 commit comments