ViaFabricPlus/docs/DEVELOPER_API.md

87 lines
4.9 KiB
Markdown
Raw Normal View History

2023-03-22 13:33:42 +01:00
# Developer API
There is no real addon base, to create addons you can simply use the event system, which uses Fabric's Event-API.
```java
public class ViaFabricPlusExampleAddon implements ClientModInitializer {
@Override
public void onInitializeClient() {
2023-05-28 20:04:38 +02:00
ChangeProtocolVersionCallback.EVENT.register(versionEnum -> {
System.out.println("Version changed to " + versionEnum.getName());
2023-03-22 13:33:42 +01:00
});
}
}
```
#### ViaFabricPlus has 7 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. |
| FinishMinecraftLoadCallback | Called when Minecraft is finished with loading all its components |
2023-05-28 20:04:38 +02:00
| FinishViaVersionStartupCallback | Called when ViaVersion is loaded and ready to use |
2023-03-22 13:33:42 +01:00
| InitializeSettingsCallback | Called after the default setting groups are loaded and before the setting config is loaded |
| LoadClassicProtocolExtensionCallback | Called when the classic server sends the protocol extensions (only in **c0.30 CPE**) |
| PreLoadCallback | Called before everything (Pre-pre load) |
2023-08-04 23:49:11 +02:00
| DisconnectConnectionCallback | Called when the user disconnects from a server. |
2023-03-22 13:33:42 +01:00
### General API
2023-06-01 16:13:57 +02:00
#### Add CustomPayload channels for versions below 1.13
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-05-13 15:56:37 +02:00
#### Get the release version of a material:
2023-03-22 13:33:42 +01:00
```java
2023-05-28 20:04:38 +02:00
final VersionRange range = ItemReleaseVersionDefinition.INSTANCE.getItemMap().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-03-22 13:33:42 +01:00
```
#### Creating own settings for the settings screen:
```java
public class ExampleSettingGroup extends SettingGroup {
public static final ExampleSettingGroup INSTANCE = new ExampleSettingGroup();
2023-03-22 13:33:42 +01:00
public final BooleanSetting test = new BooleanSetting("Test", false);
public ExampleSettingGroup() {
super("Example");
}
}
```
and then you register the setting group in your onLoad method:
```java
PreLoadCallback.EVENT.register(() -> {
ViaFabricPlus.INSTANCE.getSettingsSystem().addGroup(ExampleSettingGroup.INSTANCE);
});
```
#### Implementing custom classic protocol extensions:
```java
public class ExampleExtensionSupport implements ClientModInitializer {
public static ClientboundPacketsc0_30cpe EXT_CLICK_DISTANCE;
@Override
public void onInitializeClient() {
PreLoadCallback.EVENT.register(() -> {
CustomClassicProtocolExtensions.allowExtension(ClassicProtocolExtension.CLICK_DISTANCE); // Register extension as supported
EXT_CLICK_DISTANCE = CustomClassicProtocolExtensions.createNewPacket(ClassicProtocolExtension.CLICK_DISTANCE, 0x12, (user, buf) -> buf.readShort());
});
FinishViaLoadingBaseStartupCallback.EVENT.register(() -> {
Via.getManager().getProtocolManager().getProtocol(Protocolc0_30toc0_30cpe.class).registerClientbound(EXT_CLICK_DISTANCE, null, new PacketHandlers() {
@Override
protected void register() {
handler(wrapper -> {
wrapper.cancel();
final short distance = wrapper.read(Type.SHORT);
// Do your stuff...
});
}
}, true);
});
}
}
```