From 9f80ee41e14a7568c3d03a57535c3370d14a29a2 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Tue, 28 Nov 2023 15:07:51 +0100 Subject: [PATCH] updated docs/ folder --- docs/DEVELOPER_API.md | 36 +++++++++++++++---- ...llback.java => LoadSaveFilesCallback.java} | 6 ++-- .../viafabricplus/save/SaveManager.java | 6 ++-- 3 files changed, 36 insertions(+), 12 deletions(-) rename src/main/java/de/florianmichael/viafabricplus/event/{SaveFilesCallback.java => LoadSaveFilesCallback.java} (85%) diff --git a/docs/DEVELOPER_API.md b/docs/DEVELOPER_API.md index 2e16f057c..8d3345125 100644 --- a/docs/DEVELOPER_API.md +++ b/docs/DEVELOPER_API.md @@ -4,20 +4,44 @@ ViaFabricPlus provides various events and APIs for developers to use. This page ## Events ViaFabricPlus events are using the [Fabric Event API](https://fabricmc.net/wiki/tutorial:events). You can register to them like this: ```java -ChangeProtocolVersionCallback.EVENT.register(versionEnum -> { - System.out.println("Version changed to " + versionEnum.getName()); +ChangeProtocolVersionCallback.EVENT.register((oldVersion, newVersion) -> { + System.out.println("Version changed to " + newVersion.getName()); }); ``` -### ViaFabricPlus has 7 events at the moment +### ViaFabricPlus has 8 events at the moment | 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. | -| DisconnectCallback | Called when the user disconnects from a server. | +| 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**) | | 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 | -| LoadClassicProtocolExtensionCallback | Called when the classic server sends the protocol extensions (only in **c0.30 CPE**) | -| LoadCallback | Called at the earliest point ViaFabricPlus is injecting too | +| 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 +``` ## ViaVersion internals ### Add CustomPayload channels for versions below 1.13 diff --git a/src/main/java/de/florianmichael/viafabricplus/event/SaveFilesCallback.java b/src/main/java/de/florianmichael/viafabricplus/event/LoadSaveFilesCallback.java similarity index 85% rename from src/main/java/de/florianmichael/viafabricplus/event/SaveFilesCallback.java rename to src/main/java/de/florianmichael/viafabricplus/event/LoadSaveFilesCallback.java index 94b160a05..be96bcf82 100644 --- a/src/main/java/de/florianmichael/viafabricplus/event/SaveFilesCallback.java +++ b/src/main/java/de/florianmichael/viafabricplus/event/LoadSaveFilesCallback.java @@ -26,10 +26,10 @@ /** * This event is fired when ViaFabricPlus has loaded its save files, and before it starts reading the values from the save files. */ -public interface SaveFilesCallback { +public interface LoadSaveFilesCallback { - Event EVENT = EventFactory.createArrayBacked(SaveFilesCallback.class, listeners -> (saveManager, state) -> { - for (SaveFilesCallback listener : listeners) { + Event EVENT = EventFactory.createArrayBacked(LoadSaveFilesCallback.class, listeners -> (saveManager, state) -> { + for (LoadSaveFilesCallback listener : listeners) { listener.onLoadSaveFiles(saveManager, state); } }); diff --git a/src/main/java/de/florianmichael/viafabricplus/save/SaveManager.java b/src/main/java/de/florianmichael/viafabricplus/save/SaveManager.java index 5d32f52a2..9be1908bf 100644 --- a/src/main/java/de/florianmichael/viafabricplus/save/SaveManager.java +++ b/src/main/java/de/florianmichael/viafabricplus/save/SaveManager.java @@ -19,7 +19,7 @@ package de.florianmichael.viafabricplus.save; -import de.florianmichael.viafabricplus.event.SaveFilesCallback; +import de.florianmichael.viafabricplus.event.LoadSaveFilesCallback; import de.florianmichael.viafabricplus.save.impl.AccountsSave; import de.florianmichael.viafabricplus.save.impl.SettingsSave; import de.florianmichael.viafabricplus.settings.SettingsManager; @@ -35,7 +35,7 @@ public class SaveManager { private final AccountsSave accountsSave; public SaveManager(final SettingsManager settingsManager) { - SaveFilesCallback.EVENT.invoker().onLoadSaveFiles(this, SaveFilesCallback.State.PRE); + LoadSaveFilesCallback.EVENT.invoker().onLoadSaveFiles(this, LoadSaveFilesCallback.State.PRE); // Register saves add( @@ -55,7 +55,7 @@ public SaveManager(final SettingsManager settingsManager) { } })); - SaveFilesCallback.EVENT.invoker().onLoadSaveFiles(this, SaveFilesCallback.State.POST); + LoadSaveFilesCallback.EVENT.invoker().onLoadSaveFiles(this, LoadSaveFilesCallback.State.POST); } public void add(final AbstractSave... saves) {