|
| 1 | +--- |
| 2 | +title: Guide API |
| 3 | +--- |
| 4 | + |
| 5 | +_Learn how to use existing Guide domains, and register your own, for use in your own Screens._ |
| 6 | + |
| 7 | +<Callout variant="info"> |
| 8 | + For instructions on modifying rules for existing Guide domains, |
| 9 | + see the [resource pack page](../resource-packs/guides) |
| 10 | +</Callout> |
| 11 | + |
| 12 | +## How does this API work? |
| 13 | + |
| 14 | +The Guide API has a few distinct concepts: |
| 15 | + |
| 16 | +- **Guide Domain**: Holds a list of valid facts in the domain, |
| 17 | + for example, `controlify:in_game` is a domain which holds facts such as `controlify:on_ground`. |
| 18 | +- **Guide Context**: Holds data which facts use to determine if they are true or false. |
| 19 | +- **Guide Instance**: Applies to a specific domain, holds current state of the facts applied to a context and the rules which pass. |
| 20 | + |
| 21 | +## Registering facts to a `GuideDomain` |
| 22 | + |
| 23 | +You may want to add additional facts relating to your mod to an existing `GuideDomain`, such as `controlify:in_game`, |
| 24 | +which you would then use in an embedded resource pack to add additional rules. |
| 25 | + |
| 26 | +You may want to create and render your own `GuideDomain`, and you need to register your own set of facts. |
| 27 | + |
| 28 | +```java |
| 29 | +@Override |
| 30 | +public void onControlifyPreInit(PreInitContext ctx) { |
| 31 | + ctx.guides().inGame().registerFact( |
| 32 | + Identifier.fromNamespaceAndPath("my_mod", "holding_wand"), |
| 33 | + ctx -> ctx.player().isItemInMainHandModdedWand() |
| 34 | + ); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Now resource packs can use your additional rule. Refer to the [resource pack page](../resource-packs/guides) |
| 39 | +to create rules using resource packs. |
| 40 | + |
| 41 | +If your mod is not loaded or does not register a referenced fact for whatever reason, the state of the fact |
| 42 | +will be `false`, and a warning will be issued on resource reload that an unknown fact was referenced. |
| 43 | + |
| 44 | +## Rendering a Guide |
| 45 | + |
| 46 | +To render a Guide, you must create a new `GuideInstance`, update its facts, and then finally render it. |
| 47 | + |
| 48 | +Because the only time you have API-safe access to existing Guide Domains is during pre-init, |
| 49 | +you must capture a reference to it to use it later. |
| 50 | + |
| 51 | +```java |
| 52 | +private GuideDomain<InGameCtx> inGameGuideDomain; |
| 53 | + |
| 54 | +@Override |
| 55 | +public void onControlifyPreInit(PreInitContext ctx) { |
| 56 | + this.inGameGuideDomain = ctx.guides().inGame(); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Once you have a reference to a domain, you can create an instance from it. |
| 61 | + |
| 62 | +```java |
| 63 | +GuideInstance<InGameCtx> guideInstance = guideDomain.createInstance(); |
| 64 | +``` |
| 65 | + |
| 66 | +Ruleset and fact resolution is completely independent of rendering, you may update the guide once every tick, |
| 67 | +while you render it every frame. |
| 68 | + |
| 69 | +Every fact in a `GuideDomain` requires a specific context type (`FactCtx`), you must construct this type to be |
| 70 | +able to update an instance. |
| 71 | + |
| 72 | +```java |
| 73 | +void tick() { |
| 74 | + guideInstance.update(new InGameCtx(...), font); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Then, you can render it. You can either use the `extractRenderState` method, or you can get a `Renderable` |
| 79 | +which you can then add to a `Screen`. |
| 80 | + |
| 81 | +```java |
| 82 | +class MyScreen extends Screen { |
| 83 | + @Override |
| 84 | + protected void init() { |
| 85 | + boolean bottomAligned = true; // if false, top aligned |
| 86 | + boolean textContrast = false; // adds a translucent background behind text |
| 87 | + this.addRenderableOnly(this.guideInstance.renderable(bottomAligned, textContrast)); |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Registering your own domain |
| 93 | + |
| 94 | +It is very simple to register your own domain. |
| 95 | +Once registered, Controlify handles resource reloading for you. |
| 96 | + |
| 97 | +Before registering a new domain, you need to decide whether it will use an existing fact context |
| 98 | +(either `InGameCtx` or `ContainerCtx`), or your own. This is the data you give to the guide instance |
| 99 | +each update, which the facts use to resolve their state. Here is an example fact context. |
| 100 | + |
| 101 | +```java |
| 102 | +public record ModdedCtx( |
| 103 | + ModClient modClient |
| 104 | +) implements FactCtx {} |
| 105 | +``` |
| 106 | + |
| 107 | +Each fact you register would then be able to reference `modClient` and therefore the rest of your mod's state. |
| 108 | + |
| 109 | +```java |
| 110 | +@Override |
| 111 | +public void onControlifyPreInit(PreInitContext ctx) { |
| 112 | + GuideDomain<ModdedCtx> domain = ctx.guides().registerCustomDomain(Identifier.fromNamespaceAndPath("my_mod", "mod_context")); |
| 113 | + domain.registerFact(...); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | + |
0 commit comments