From 4bc41e3b38a670e9a318664c5a75f0ef079e8e4d Mon Sep 17 00:00:00 2001 From: Username404 Date: Fri, 16 Jul 2021 14:37:40 +0200 Subject: [PATCH] Add Ok Zoomer compatibility --- .../username404/snowygui/gui/feature/Zoom.kt | 4 +-- .../mixins/OkZoomerAlternativeMixin.java | 22 +++++++++++++++ .../snowygui/mixins/ZoomMixin.java | 3 +- .../username404/snowygui/fabric/FabricInit.kt | 28 +++++++++++++++++++ .../resources/snowygui-mixins-fabric.json | 3 +- 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 fabric/src/main/java/fr/username404/snowygui/mixins/OkZoomerAlternativeMixin.java diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Zoom.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Zoom.kt index be776e0..b69ce72 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Zoom.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/Zoom.kt @@ -5,8 +5,8 @@ import net.minecraft.client.Minecraft @ButtonInfo(Category.MISC, shouldSave = false) object Zoom: ButtonImpl() { - internal var smoothCameraOnZoom: Boolean by Configuration - internal var zoomFactor: Double by Configuration + var smoothCameraOnZoom: Boolean by Configuration + var zoomFactor: Double by Configuration @JvmStatic fun getNewZoom(fov: Double): Double = fov / zoomFactor public override fun execAction() { diff --git a/fabric/src/main/java/fr/username404/snowygui/mixins/OkZoomerAlternativeMixin.java b/fabric/src/main/java/fr/username404/snowygui/mixins/OkZoomerAlternativeMixin.java new file mode 100644 index 0000000..bd63608 --- /dev/null +++ b/fabric/src/main/java/fr/username404/snowygui/mixins/OkZoomerAlternativeMixin.java @@ -0,0 +1,22 @@ +package fr.username404.snowygui.mixins; + +import fr.username404.snowygui.fabric.FabricInitKt; +import fr.username404.snowygui.gui.feature.Zoom; +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(Zoom.class) +abstract class OkZoomerAlternativeMixin { + /** + * @author Username404 + * @reason Needed to use the Ok Zoomer mod when it is present + */ + @Inject(remap = false, at = @At("HEAD"), method = "execAction", cancellable = true) + public void execAction(CallbackInfo ci) { + if (FabricInitKt.isOkZoomerPresent) { + FabricInitKt.fabricZoom(); + } + } +} diff --git a/fabric/src/main/java/fr/username404/snowygui/mixins/ZoomMixin.java b/fabric/src/main/java/fr/username404/snowygui/mixins/ZoomMixin.java index 8cfca69..283f5c4 100644 --- a/fabric/src/main/java/fr/username404/snowygui/mixins/ZoomMixin.java +++ b/fabric/src/main/java/fr/username404/snowygui/mixins/ZoomMixin.java @@ -1,5 +1,6 @@ package fr.username404.snowygui.mixins; +import fr.username404.snowygui.fabric.FabricInitKt; import fr.username404.snowygui.gui.feature.Zoom; import net.minecraft.client.Camera; import net.minecraft.client.renderer.GameRenderer; @@ -12,7 +13,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; abstract class ZoomMixin { @Inject(at = @At(value = "RETURN"), method = "getFov(Lnet/minecraft/client/Camera;FZ)D", cancellable = true) private void getFov(Camera camera, float f, boolean bl, CallbackInfoReturnable cir) { - if (Zoom.INSTANCE.getToggled()) { + if (Zoom.INSTANCE.getToggled() && !FabricInitKt.isOkZoomerPresent) { cir.setReturnValue(Zoom.getNewZoom(cir.getReturnValue())); } } diff --git a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt index a61d956..fcf756f 100644 --- a/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt +++ b/fabric/src/main/kotlin/fr/username404/snowygui/fabric/FabricInit.kt @@ -4,6 +4,7 @@ import com.mojang.blaze3d.vertex.PoseStack import fr.username404.snowygui.EventSnowy import fr.username404.snowygui.Snowy import fr.username404.snowygui.gui.feature.ButtonImpl +import fr.username404.snowygui.gui.feature.Zoom import net.fabricmc.api.ClientModInitializer import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback.EVENT @@ -39,4 +40,31 @@ class FabricInit: Snowy(), ClientModInitializer { classSet }.toSet() } +} + +@JvmField +var isOkZoomerPresent: Boolean = FabricLoader.getInstance().isModLoaded("okzoomer") +private val okZoomerPairs by lazy { + try { + with(Class.forName("io.github.joaoh1.okzoomer.client.utils.ZoomUtils")) { + (getField("zoomDivisor") to null) to (getField("zoomState") to null) + } + } catch (e: ClassNotFoundException) { + with(Class.forName("io.github.ennuil.okzoomer.utils.ZoomUtils").getDeclaredField("zoomerZoom")) { + get(null).javaClass.run { + (getField("zoomDivisor") to this@with) to (getField("zoom") to this@with) + } + } + } +} + +fun fabricZoom() { + try { + with(okZoomerPairs) { + first.run { first.setDouble(second, Zoom.zoomFactor) } + second.run { first.setBoolean(second, Zoom.toggled) } + } + } catch (e: Exception) { + isOkZoomerPresent = false + } } \ No newline at end of file diff --git a/fabric/src/main/resources/snowygui-mixins-fabric.json b/fabric/src/main/resources/snowygui-mixins-fabric.json index dabb1c8..39818fc 100644 --- a/fabric/src/main/resources/snowygui-mixins-fabric.json +++ b/fabric/src/main/resources/snowygui-mixins-fabric.json @@ -3,7 +3,8 @@ "package": "fr.username404.snowygui.mixins", "compatibilityLevel": "JAVA_8", "client": [ - "ZoomMixin" + "ZoomMixin", + "OkZoomerAlternativeMixin" ], "injectors": { "defaultRequire": 1