Injecting decorator at the correct initialization state

This commit is contained in:
Vftdan 2024-09-05 08:35:30 +02:00
parent d950e45e49
commit 4664b2ec75
5 changed files with 66 additions and 12 deletions

View File

@ -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")) {
public static boolean hasXaeroWorldMap() {
return FabricLoader.getInstance().isModLoaded("xaeroworldmap");
}
public static void onGameJoin() {
if (!decoratorInjected) {
if (hasXaeroWorldMap()) {
injectWaypointReaderDecorator();
} else {
System.err.println("No xaeros-world-map");
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);
}
}
}

View File

@ -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();
}
}

View File

@ -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<RightClickOption> 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);

View File

@ -3,7 +3,8 @@
"package": "io.github.vftdan.xaeromapaddon.mixin.client",
"compatibilityLevel": "JAVA_21",
"client": [
"ExampleClientMixin"
"ExampleClientMixin",
"ClientPlayNetworkHandlerMixin"
],
"injectors": {
"defaultRequire": 1