ViaFabricPlus/docs/DEVELOPER_API.md

89 lines
5.2 KiB
Markdown
Raw 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
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 |
2023-11-28 15:07:51 +01:00
| LoadSaveFiles | Called before and after the save files are loaded |
## Get and set the current protocol version
```java
final VersionEnum version = ProtocolHack.getTargetVersion();
if (version == VersionEnum.r1_8) {
ProtocolHack.setTargetVersion(VersionEnum.r1_9);
}
```
## Get a Minecraft ClientConnection by channel
```java
final ClientConnection connection = channel.attr(ProtocolHack.CLIENT_CONNECTION_ATTRIBUTE_KEY).get();
```
## Interact with UserConnection objects
```java
// If ViaVersion is translating, this field will return the user connection of the client
final UserConnection userConnection = ProtocolHack.getPlayNetworkUserConnection();
// If you need a dummy user connection for testing, you can use this method
final UserConnection cursedDummy = ProtocolHack.createDummyUserConnection(ProtocolHack.NATIVE_VERSION, VersionEnum.r1_18_2);
// 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
2023-11-27 22:02:41 +01:00
if (ItemRegistryDiff.contains(Items.STONE, VersionRange.andOlder(VersionEnum.r1_8))) {
// Do something
}
2023-03-22 13:33:42 +01:00
```
2023-11-27 22:02:41 +01:00
### Creating own settings for the settings screen
2023-03-22 13:33:42 +01:00
```java
public class ExampleSettingGroup extends SettingGroup {
2023-11-27 22:02:41 +01:00
private static final ExampleSettingGroup instance = new ExampleSettingGroup();
2023-03-22 13:33:42 +01:00
2023-11-27 22:02:41 +01:00
public final BooleanSetting test = new BooleanSetting(this, Text.of("Test"), false);
2023-03-22 13:33:42 +01:00
public ExampleSettingGroup() {
super("Example");
}
2023-11-27 22:02:41 +01:00
public static ExampleSettingGroup global() {
return instance;
}
2023-03-22 13:33:42 +01:00
}
```
2023-11-27 22:02:41 +01:00
and then you register the setting group in your onLoad method
2023-03-22 13:33:42 +01:00
```java
2023-11-27 22:02:41 +01:00
RegisterSettingsCallback.EVENT.register(state -> {
if (state == RegisterSettingsCallback.State.POST) {
ViaFabricPlus.global().getSettingsManager().addGroup(ExampleSettingGroup.INSTANCE);
}
2023-03-22 13:33:42 +01:00
});
```