ViaFabricPlus/docs/DEVELOPER_API.md

103 lines
5.2 KiB
Markdown
Raw Normal View History

2023-03-22 13:33:42 +01:00
# Developer API
2024-10-31 15:22:11 +01:00
ViaFabricPlus provides events and various utility functions for other mods to interface with it. Note that including
ViaFabricPlus in your project comes with some requirements:
- The target version is Java 17
- Fabric loom setup (As ViaFabricPlus is a Minecraft mod and has no API-only dependency like other projects)
2023-03-22 13:33:42 +01:00
2024-10-31 15:22:11 +01:00
Since the API is not exposed as standalone submodule (yet), functions that shouldn't be used are marked with
`ApiStatus.Internal`. Further information about certain functions can be found in the Javadoc in the corresponding file.
## How to include the mod as dependency
### Gradle
2024-02-06 22:31:44 +01:00
```groovy
repositories {
mavenCentral()
2024-02-06 22:31:44 +01:00
maven {
name = "ViaVersion"
url = "https://repo.viaversion.com"
}
maven {
name = "Lenni0451"
url = "https://maven.lenni0451.net/everything"
}
maven {
2024-10-31 15:22:11 +01:00
name = "Jitpack"
url = "https://jitpack.io"
content {
includeGroup "com.github.Oryxel"
}
}
2024-02-06 22:31:44 +01:00
}
dependencies {
modImplementation("de.florianmichael:ViaFabricPlus:x.x.x") // Get the latest version from releases
2024-02-06 22:31:44 +01:00
}
```
2024-10-31 15:22:11 +01:00
### Maven
2024-02-06 22:31:44 +01:00
```xml
<repositories>
<repository>
<id>viaversion</id>
<url>https://repo.viaversion.com</url>
</repository>
<repository>
<id>lenni0451</id>
<url>https://maven.lenni0451.net/everything</url>
</repository>
<repository>
2024-10-31 15:22:11 +01:00
<id>jitpack</id>
<url>https://jitpack.io</url>
</repository>
2024-02-06 22:31:44 +01:00
</repositories>
<dependencies>
<dependency>
<groupId>de.florianmichael</groupId>
<artifactId>ViaFabricPlus</artifactId>
2024-02-06 22:31:44 +01:00
<version>x.x.x</version> <!-- Get the latest version from releases -->
</dependency>
</dependencies>
```
2024-10-31 15:22:11 +01:00
## Interacting with Events
ViaFabricPlus events are the intended way of interacting with the mod.
Events are fired in various situations and are using the [Fabric Event API](https://fabricmc.net/wiki/tutorial:events).
#### Example
2023-11-27 22:02:41 +01:00
```java
2023-11-28 15:07:51 +01:00
ChangeProtocolVersionCallback.EVENT.register((oldVersion, newVersion) -> {
System.out.println("Version changed to " + newVersion.getName());
2023-11-27 22:02:41 +01:00
});
2023-03-22 13:33:42 +01:00
```
2024-10-31 15:22:11 +01:00
### List of events/callbacks
2023-03-22 13:33:42 +01:00
| Callback class name | Description |
|--------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ChangeProtocolVersionCallback | Called when the user changes the target version in the screen, or if you connect to a server for which a specific version has been selected, you disconnect, the event for the actual version is also called. |
2023-11-28 15:07:51 +01:00
| DisconnectCallback | Called when the user disconnects from a server |
| LoadCallback | Called at the earliest point ViaFabricPlus is injecting too |
| LoadClassicProtocolExtensionCallback | Called when the classic server sends the protocol extensions (only in **c0.30 CPE**) |
2023-11-27 22:02:41 +01:00
| PostGameLoadCallback | Called when Minecraft is finished with loading all its components |
| PostViaVersionLoadCallback | Called when ViaVersion is loaded and ready to use |
| RegisterSettingsCallback | Called after the default setting groups are loaded and before the setting config is loaded |
| LoadSaveFilesCallback | Called before and after the save files are loaded |
2023-11-28 15:07:51 +01:00
2024-10-31 15:22:11 +01:00
### Other API calls
ViaFabricPlus uses [ViaVersion](https://github.com/ViaVersion/ViaVersion) for protocol translation, so the ViaVersion API can be used.
The general API endpoint for ViaFabricPlus specifics is `ProtocolTranslator`
2023-11-28 15:07:51 +01:00
```java
2024-10-31 15:22:11 +01:00
// Get and change the current selected version
final ProtocolVersion version = ProtocolTranslator.getTargetVersion();
if (version == ProtocolVersion.v1_8) {
ProtocolTranslator.setTargetVersion(ProtocolVersion.v1_9);
2023-11-28 15:07:51 +01:00
}
2024-10-31 15:22:11 +01:00
// Gets the ViaVersion user connection object for raw packet sending using ViaVersion API
final UserConnection user = ProtocolTranslator.getPlayNetworkUserConnection();
if (user == null) {
// Mod not active
2023-11-27 22:02:41 +01:00
}
2024-10-31 15:22:11 +01:00
```