Skip to content

Mod Integration for Developers

TheIllusiveC4 edited this page Feb 22, 2021 · 3 revisions

Diet uses InterModComms (IMC) provided by Forge in order to facilitate basic compatibility between itself and food mods.

Due to the use of function currying and wrapper objects, some of the syntax is a little verbose and un-intuitive but the main benefit is that mods can implement integration with zero dependency on Diet itself. This guide is written to help illustrate compatibility steps to mitigate any confusion on its usage.

Methods

Diet currently provides two main methods - "item" and "block".

"item" - This is used for item overrides. This can be used for dictating ingredients of composite foods or providing correct food values for dynamic foods.

  • Message Type: Tuple<Item, BiFunction<PlayerEntity, ItemStack, Triple<List<ItemStack>, Integer, Float>>>

"block" This is used for block overrides. This can be used to provide diet values when interacting with an edible block. Please note that this will fire any time that the block is interacted with, not just when the player actually succeeds in eating it. Thus developers have to ensure that fail states return an empty new Tuple<>(0, 0.0f).

  • Message Type: Tuple<Item, BiFunction<BlockPos, PlayerEntity, BiFunction<Hand, Direction, Tuple<Integer, Float>>>>

Walkthrough

First, developers need to setup a listener for the InterModEnqueue event which is posted on the Mod event bus.

public ExampleMod() {
  FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueue);
}

private void enqueue(final InterModEnqueueEvent evt) {

  if (ModList.get().isLoaded("diet")) {
    // Send your IMC messages here
  }
}

To send the message, developers need to call the InterModComms#sendTo method with the appropriate arguments.

InterModComms.sendTo("mod_id", "method", () -> thing);

For example, if developers wanted to send an item override, it would look like this:

InterModComms.sendTo("diet", "item",
        () -> new Tuple<Item, BiFunction<PlayerEntity, ItemStack, Triple<List<ItemStack>, Integer, Float>>>(
            item,
            (player, stack) -> new ImmutableTriple<>(Collections.singletonList(stack), 1, 0.1f));

And if developers wanted to send a block override, it would look like this:

InterModComms.sendTo("diet", "block",
        () -> new Tuple<Block, BiFunction<BlockPos, PlayerEntity, BiFunction<Hand, Direction, Tuple<Integer, Float>>>>(
            block,
            (pos, player) -> (hand, direction) -> player.canEat(false) ? new Tuple<>(2, 0.1f) : new Tuple<>(0, 0.0f)));

And that's really it. Just send the appropriate message with the right message type and Diet will handle the processing without any dependencies needed on the developer's end.

Examples

Comments? Concerns?

Diet is a new mod and so the API and integration methods are currently experimental. If you have concerns or suggestions about improvements, please open an issue on the issue tracker and I'll be in touch to begin a discussion.

Clone this wiki locally