ViaFabricPlus/docs/DEVELOPER_API.md

111 lines
5.7 KiB
Markdown
Raw Permalink Normal View History

2023-03-22 13:33:42 +01:00
# Developer API
2023-11-27 22:02:41 +01:00
ViaFabricPlus provides various events and APIs for developers to use. This page explains how to use them.
2023-03-22 13:33:42 +01:00
2024-02-06 22:31:44 +01:00
## Include via Gradle/Maven
```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 {
name = "OpenCollab Snapshots"
url = "https://repo.opencollab.dev/maven-snapshots/"
}
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
}
```
```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>
<id>opencollab-snapshots</id>
<url>https://repo.opencollab.dev/maven-snapshots/</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>
```
2023-11-27 22:02:41 +01:00
## Events
ViaFabricPlus events are using the [Fabric Event API](https://fabricmc.net/wiki/tutorial:events). You can register to them like this:
```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
```
2023-11-28 15:07:51 +01:00
### ViaFabricPlus has 8 events at the moment
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
## Get and set the current protocol version
```java
final ProtocolVersion version = ProtocolTranslator.getTargetVersion();
if (version == ProtocolVersion.v1_8) {
ProtocolTranslator.setTargetVersion(ProtocolVersion.v1_9);
2023-11-28 15:07:51 +01:00
}
```
## Get a Minecraft ClientConnection by channel
```java
final ClientConnection connection = channel.attr(ProtocolTranslator.CLIENT_CONNECTION_ATTRIBUTE_KEY).get();
2023-11-28 15:07:51 +01:00
```
## Interact with UserConnection objects
```java
// If ViaVersion is translating, this field will return the user connection of the client
final UserConnection userConnection = ProtocolTranslator.getPlayNetworkUserConnection();
2023-11-28 15:07:51 +01:00
// If you need a dummy user connection for testing, you can use this method
final UserConnection cursedDummy = ProtocolTranslator.createDummyUserConnection(ProtocolTranslator.NATIVE_VERSION, ProtocolVersion.v1_18_2);
2023-11-28 15:07:51 +01:00
// The cursedDummy field now contains all protocols from the native version to 1.18.2
```
2023-03-22 13:33:42 +01:00
2023-11-27 22:02:41 +01:00
## ViaVersion internals
### Add CustomPayload channels for versions below 1.13
2023-06-01 16:13:57 +02:00
In order to receive custom payloads with custom channels in versions below 1.13, you need to register them, that's what you do:
```java
Protocol1_13To1_12_2.MAPPINGS.getChannelMappings().put("FML|HS", "fml:hs");
```
2023-11-27 22:02:41 +01:00
### Check if an item exists in a specific version
2023-03-22 13:33:42 +01:00
```java
2023-11-27 22:02:41 +01:00
final VersionRange range = ItemRegistryDiff.ITEM_DIFF.get(Items.WRITABLE_BOOK); // If an item does not appear in the item map, it has always existed
2023-03-22 13:33:42 +01:00
// The Range class then contains all versions in which the item occurs.
2023-06-01 16:13:57 +02:00
// https://github.com/ViaVersion/ViaLoader
if (ItemRegistryDiff.contains(Items.STONE, VersionRange.andOlder(ProtocolVersion.v1_8))) {
2023-11-27 22:02:41 +01:00
// Do something
}
2023-03-22 13:33:42 +01:00
```