frogmc.dev/exts/entrypoints.md
2024-06-10 19:39:12 -05:00

91 lines
4.1 KiB
Markdown

# Entrypoints
Entrypoints are a specific type of extension that allows mods to inject code that will run as the game is starting up. They can be thought of as like the `main()` function in a full Program, and are primarily used for adding things to registries, declaring callbacks and events, and other tasks your mod needs to do once during startup.
## `MainExtension`
The `MainExtension` entrypoint contains the core of your mod's initialization code. `MainExtension` will run during the startup sequence on both the server and the client, and thus should be populated with things like registrations (i.e. of blocks, items, entities) and events that are triggered on both the server and client side.
The first step to creating a MainExtension entrypoint is to create a class. This should usually be in the root package of your mod, but does not have to be. For this example, we will create a class called `ExampleInit` that implements the interface `MainExtension`:
```java
package com.example.examplemod;
public class ExampleInit implements MainExtension {
...
}
```
Once this class is created, add it under the `[frog.extensions]` table in your `frog.mod.toml` file with the key `init`:
```toml
[frog.extensions]
...
init = "com/example/examplemod/ExampleInit"
...
```
The interface `MainExtension` contains one method, called `onInit`. It takes one `ModProperties` object and returns `void`. We will implement this method by adding a Logger and printing a message:
```java
package com.example.examplemod;
import dev.frogmc.frogloader.api.mod.ModProperties;
import dev.frogmc.froglib.entrypoints.MainExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final Logger LOGGER = LoggerFactory.getLogger("ExampleMod");
public class ExampleInit implements MainExtension {
@Override
public void onInit(ModProperties mod) {
LOGGER.info("ribbit world");
}
}
```
Now, when we launch the game with the example mod installed, we will see that message sent when our mod is initialized.
## `ClientExtension`
The `ClientExtension` is very similar to the `MainExtension` in its function as an entrypoint, except it will only run if the mod is installed on the client. This is primarily used for modifying the behavior of rendering, like through custom models or renderers, as well as keybinds and other interactions that are only processed on the client side. To create a ClientExtension entrypoint, the process is very similar; first, we will create a class implementing the interface `ClientExtension`. This class should be annotated with the `@ClientOnly` annotation so that it is only loaded into the classpath on the client. Usually it should be placed in a package specifically for client-related classes.
```java
package com.example.examplemod.client;
import dev.frogmc.frogloader.api.mod.ModProperties;
import dev.frogmc.frogloader.api.env.ClientOnly;
import dev.frogmc.froglib.entrypoints.ClientExtension;
@ClientOnly
public class ExampleClientInit implements ClientExtension {
@Override
public void onClientInit(ModProperties mod) {
...
}
}
```
This should also be included in the `[frog.extensions]` table in your `frog.mod.toml`, under the key `client`:
```toml
[frog.extensions]
...
client = "com/example/examplemod/client/ExampleClientInit"
...
```
## `ServerExtension`
`ServerExtension` is very similar to `ClientExtension` in implementation, except it runs only on the dedicated server. It does **not** run on the internal server. To create a ServerExtension entrypoint, create a class implementing `ServerExtension` annotated with `@ServerOnly`.
```java
package com.example.examplemod.server;
import dev.frogmc.frogloader.api.mod.ModProperties;
import dev.frogmc.frogloader.api.env.ServerOnly;
import dev.frogmc.froglib.entrypoints.ServerExtension;
@ServerOnly
public class ExampleServerInit implements ServerExtension {
@Override
public void onServerInit(ModProperties mod) {
...
}
}
```
Add this to the `[frog.extensions] table in your `frog.mod.toml` file, under the key `server`:
```toml
[frog.extensions]
...
server = "com/example/examplemod/server/ExampleServerInit"
...
```