From 4664b2ec7522423e85be2291019373b9341c0ab7 Mon Sep 17 00:00:00 2001 From: Vftdan Date: Thu, 5 Sep 2024 08:35:30 +0200 Subject: [PATCH] Injecting decorator at the correct initialization state --- .../VftdansXaerosMapAddonClient.java | 54 +++++++++++++++---- .../client/ClientPlayNetworkHandlerMixin.java | 17 ++++++ .../worldmap/WaypointReaderDecorator.java | 2 + ...ftdans-xaeros-map-addon.client.mixins.json | 3 +- .../xaeromapaddon/VftdansXaerosMapAddon.java | 2 +- 5 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 src/client/java/io/github/vftdan/xaeromapaddon/mixin/client/ClientPlayNetworkHandlerMixin.java diff --git a/src/client/java/io/github/vftdan/xaeromapaddon/VftdansXaerosMapAddonClient.java b/src/client/java/io/github/vftdan/xaeromapaddon/VftdansXaerosMapAddonClient.java index dfcb118..b97dc2f 100644 --- a/src/client/java/io/github/vftdan/xaeromapaddon/VftdansXaerosMapAddonClient.java +++ b/src/client/java/io/github/vftdan/xaeromapaddon/VftdansXaerosMapAddonClient.java @@ -6,13 +6,18 @@ import net.fabricmc.loader.api.FabricLoader; import io.github.vftdan.xaeromapaddon.worldmap.WaypointRightClickOptionsRegistry; import io.github.vftdan.xaeromapaddon.worldmap.waypointactions.ClipboardCopyWaypointAction; import io.github.vftdan.xaeromapaddon.worldmap.WaypointReaderDecorator; -import xaero.map.mods.SupportMods; +import static io.github.vftdan.xaeromapaddon.VftdansXaerosMapAddon.LOGGER; +import xaero.map.WorldMapFabric; +import xaero.map.mods.SupportModsFabric; import xaero.map.mods.SupportXaeroMinimap; import xaero.map.mods.gui.WaypointRenderer; import xaero.map.mods.gui.WaypointReader; import xaero.map.element.MapElementRenderer; +import xaero.map.gui.GuiMap; public class VftdansXaerosMapAddonClient implements ClientModInitializer { + private static boolean decoratorInjected = false; + @Override public void onInitializeClient() { // This entrypoint is suitable for setting up client-specific logic, such as rendering. @@ -20,19 +25,45 @@ public class VftdansXaerosMapAddonClient implements ClientModInitializer { WaypointRightClickOptionsRegistry registry = WaypointRightClickOptionsRegistry.getInstance(); registry.addFactory(new ClipboardCopyWaypointAction()); } + } - if (FabricLoader.getInstance().isModLoaded("xaeros-world-map")) { - injectWaypointReaderDecorator(); - } else { - System.err.println("No xaeros-world-map"); + public static boolean hasXaeroWorldMap() { + return FabricLoader.getInstance().isModLoaded("xaeroworldmap"); + } + + public static void onGameJoin() { + if (!decoratorInjected) { + if (hasXaeroWorldMap()) { + injectWaypointReaderDecorator(); + } else { + LOGGER.warn("No xaeroworldmap"); + } } } - void injectWaypointReaderDecorator() { + public static boolean tryLoadXaeroWorldMap() { + if (WorldMapFabric.INSTANCE == null) { + return false; + } + ((WorldMapFabric) WorldMapFabric.INSTANCE).tryLoadLater(); + return WorldMapFabric.loaded; + } + + public static void injectWaypointReaderDecorator() { + if (decoratorInjected) { + return; + } + + LOGGER.info("Trying to inject decorator"); try { - SupportXaeroMinimap supportMinimap = SupportMods.xaeroMinimap; + if (!tryLoadXaeroWorldMap()) { + LOGGER.warn("Failed to load Xaero WorldMap"); + return; + } + + SupportXaeroMinimap supportMinimap = SupportModsFabric.xaeroMinimap; if (supportMinimap == null) { - System.err.println("No xaeroMinimap mod support"); + LOGGER.warn("No xaeroMinimap mod support"); return; } @@ -43,10 +74,13 @@ public class VftdansXaerosMapAddonClient implements ClientModInitializer { Field waypointReaderField = MapElementRenderer.class.getDeclaredField("reader"); waypointReaderField.setAccessible(true); waypointReaderField.set(waypointRenderer, new WaypointReaderDecorator((WaypointReader) waypointReaderField.get(waypointRenderer))); + + decoratorInjected = true; + LOGGER.info("Successfully injected decorator"); } catch (IllegalAccessException e) { - e.printStackTrace(); + LOGGER.error("Field access failure", e); } catch (NoSuchFieldException e) { - e.printStackTrace(); + LOGGER.error("Field lookup failure", e); } } } diff --git a/src/client/java/io/github/vftdan/xaeromapaddon/mixin/client/ClientPlayNetworkHandlerMixin.java b/src/client/java/io/github/vftdan/xaeromapaddon/mixin/client/ClientPlayNetworkHandlerMixin.java new file mode 100644 index 0000000..1016494 --- /dev/null +++ b/src/client/java/io/github/vftdan/xaeromapaddon/mixin/client/ClientPlayNetworkHandlerMixin.java @@ -0,0 +1,17 @@ +package io.github.vftdan.xaeromapaddon.mixin.client; + +import io.github.vftdan.xaeromapaddon.VftdansXaerosMapAddonClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ClientPlayNetworkHandler.class) +public class ClientPlayNetworkHandlerMixin { + @Inject(at = @At("INVOKE"), method = "onGameJoin") + private void gameJoinHandler(GameJoinS2CPacket packet, CallbackInfo info) { + VftdansXaerosMapAddonClient.onGameJoin(); + } +} diff --git a/src/client/java/io/github/vftdan/xaeromapaddon/worldmap/WaypointReaderDecorator.java b/src/client/java/io/github/vftdan/xaeromapaddon/worldmap/WaypointReaderDecorator.java index 733b96f..571d207 100644 --- a/src/client/java/io/github/vftdan/xaeromapaddon/worldmap/WaypointReaderDecorator.java +++ b/src/client/java/io/github/vftdan/xaeromapaddon/worldmap/WaypointReaderDecorator.java @@ -1,5 +1,6 @@ package io.github.vftdan.xaeromapaddon.worldmap; +import static io.github.vftdan.xaeromapaddon.VftdansXaerosMapAddon.LOGGER; import java.util.ArrayList; import net.minecraft.client.MinecraftClient; import xaero.map.mods.gui.WaypointReader; @@ -111,6 +112,7 @@ public class WaypointReaderDecorator extends WaypointReader { ArrayList rightClickOptions = wrapped.getRightClickOptions(element, target); for (WaypointRightClickOptionFactory factory: WaypointRightClickOptionsRegistry.getInstance()) { + LOGGER.debug("Trying to inject menu entry: " + factory + " at index " + rightClickOptions.size()); RightClickOption option = factory.create(element, target, rightClickOptions.size()); if (option != null) { rightClickOptions.add(option); diff --git a/src/client/resources/vftdans-xaeros-map-addon.client.mixins.json b/src/client/resources/vftdans-xaeros-map-addon.client.mixins.json index e6455e4..790a2f4 100644 --- a/src/client/resources/vftdans-xaeros-map-addon.client.mixins.json +++ b/src/client/resources/vftdans-xaeros-map-addon.client.mixins.json @@ -3,7 +3,8 @@ "package": "io.github.vftdan.xaeromapaddon.mixin.client", "compatibilityLevel": "JAVA_21", "client": [ - "ExampleClientMixin" + "ExampleClientMixin", + "ClientPlayNetworkHandlerMixin" ], "injectors": { "defaultRequire": 1 diff --git a/src/main/java/io/github/vftdan/xaeromapaddon/VftdansXaerosMapAddon.java b/src/main/java/io/github/vftdan/xaeromapaddon/VftdansXaerosMapAddon.java index c864597..81b6f45 100644 --- a/src/main/java/io/github/vftdan/xaeromapaddon/VftdansXaerosMapAddon.java +++ b/src/main/java/io/github/vftdan/xaeromapaddon/VftdansXaerosMapAddon.java @@ -21,4 +21,4 @@ public class VftdansXaerosMapAddon implements ModInitializer { LOGGER.info("Hello Fabric world!"); } -} \ No newline at end of file +}