From 42737446b2897a8920148ac3ba4c137572166a91 Mon Sep 17 00:00:00 2001 From: moonleay Date: Fri, 17 May 2024 01:10:54 +0200 Subject: [PATCH] feat: added player fly speed controls Signed-off-by: moonleay --- .../client/config/GimbalClientConfig.kt | 2 +- .../gimbal/client/config/ScaledRes.kt | 4 +- .../gimbal/client/screen/GimbalSettingsGui.kt | 12 +++-- .../screen/widgets/GimbalSliderWidget.kt | 4 +- .../moonleay/gimbal/client/util/NumberUtil.kt | 15 +++++-- .../gimbal/mixin/PlayerFlySpeedMixin.java | 45 +++++++++++++++++++ src/main/resources/gimbal.mixins.json | 1 + 7 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 src/main/java/net/moonleay/gimbal/mixin/PlayerFlySpeedMixin.java diff --git a/src/main/java/net/moonleay/gimbal/client/config/GimbalClientConfig.kt b/src/main/java/net/moonleay/gimbal/client/config/GimbalClientConfig.kt index 199908a..8cd6fd9 100644 --- a/src/main/java/net/moonleay/gimbal/client/config/GimbalClientConfig.kt +++ b/src/main/java/net/moonleay/gimbal/client/config/GimbalClientConfig.kt @@ -25,5 +25,5 @@ import net.moonleay.gimbal.client.config.enums.ToastSettings data class GimbalClientConfig( val guiSettings: GimbalGuiSettings = GimbalGuiSettings(), val toastSettings: ToastSettings = ToastSettings.ALL, - val playerFlySpeed: Int = 0, + val playerFlySpeed: Int = 100, ) diff --git a/src/main/java/net/moonleay/gimbal/client/config/ScaledRes.kt b/src/main/java/net/moonleay/gimbal/client/config/ScaledRes.kt index acf9a70..40d04aa 100644 --- a/src/main/java/net/moonleay/gimbal/client/config/ScaledRes.kt +++ b/src/main/java/net/moonleay/gimbal/client/config/ScaledRes.kt @@ -22,6 +22,6 @@ import kotlinx.serialization.Serializable @Serializable data class ScaledRes( - val scaledX: Double, - val scaledY: Double, + val scaledX: Double, // offsetX + val scaledY: Double, // offsetY ) diff --git a/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt b/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt index c7d33e0..8e23ee9 100644 --- a/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt +++ b/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt @@ -185,11 +185,15 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi 150, 20, Text.translatable(TranslationKeys.Gui.Config.PLAYER_FLY_SPEED), - NumberUtil.interpolate(this.playerFlySpeed.toDouble(), 50.0, 500.0, 0.0, 1.0), - 50.0, - 500.0, + NumberUtil.linearInterpolate(this.playerFlySpeed.toDouble(), 100.0, 1000.0, 0.0, 1.0), + 100.0, + 1000.0, ) { value -> - this.playerFlySpeed = NumberUtil.interpolate(value, 0.0, 1.0, 50.0, 500.0).toInt() + this.playerFlySpeed = NumberUtil.linearInterpolate(value, 0.0, 1.0, 100.0, 1000.0).toInt() + + if (client!!.player != null && client!!.player!!.isCreative) { + client!!.player!!.abilities.flySpeed = (this.playerFlySpeed / 100) * 0.05f + } val oldConfig = cfg.config val newConfig = GimbalClientConfig( diff --git a/src/main/java/net/moonleay/gimbal/client/screen/widgets/GimbalSliderWidget.kt b/src/main/java/net/moonleay/gimbal/client/screen/widgets/GimbalSliderWidget.kt index c6d2038..c244615 100644 --- a/src/main/java/net/moonleay/gimbal/client/screen/widgets/GimbalSliderWidget.kt +++ b/src/main/java/net/moonleay/gimbal/client/screen/widgets/GimbalSliderWidget.kt @@ -36,12 +36,12 @@ class GimbalSliderWidget( SliderWidget(x, y, width, height, text, value) { init { this.message = this.text.copy() - .append(": " + (NumberUtil.interpolate(this.value, 0.0, 1.0, this.lowerEnd, this.upperEnd).toInt())) + .append(": " + (NumberUtil.linearInterpolate(this.value, 0.0, 1.0, this.lowerEnd, this.upperEnd).toInt())) } override fun updateMessage() { this.message = this.text.copy() - .append(": " + (NumberUtil.interpolate(this.value, 0.0, 1.0, this.lowerEnd, this.upperEnd).toInt())) + .append(": " + (NumberUtil.linearInterpolate(this.value, 0.0, 1.0, this.lowerEnd, this.upperEnd).toInt())) } override fun applyValue() { diff --git a/src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt b/src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt index 5bdb3f3..e335842 100644 --- a/src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt +++ b/src/main/java/net/moonleay/gimbal/client/util/NumberUtil.kt @@ -21,17 +21,24 @@ package net.moonleay.gimbal.client.util object NumberUtil { /** - * Interpolate a Position between two numbers + * Interpolate a number between two numbers in a linear way */ - fun interpolate(number: Double, min: Double, max: Double, lowerEnd: Double, upperEnd: Double): Double { - val toAddToMin = (0 - min) * -1 + fun linearInterpolate(number: Double, min: Double, max: Double, lowerEnd: Double, upperEnd: Double): Double { + val subtractFromNr = (0 - min) * -1 val minMaxDiff = max - min val onePercent = minMaxDiff / 100.0 - val percentOfNumber = (number - toAddToMin) / onePercent + val percentOfNumber = (number - subtractFromNr) / onePercent val toAddToLowerEnd = (0 - lowerEnd) * -1 val upperLowerDiff = upperEnd - lowerEnd val onePercentOfEnd = upperLowerDiff / 100.0 return (percentOfNumber * onePercentOfEnd) + toAddToLowerEnd } + + /** + * Interpolate a number between two numbers in a logarithmic way + */ + fun logarithmicInterpolate(number: Double, min: Double, max: Double, lowerEnd: Double, upperEnd: Double): Double { + TODO("Not impl.") + } } diff --git a/src/main/java/net/moonleay/gimbal/mixin/PlayerFlySpeedMixin.java b/src/main/java/net/moonleay/gimbal/mixin/PlayerFlySpeedMixin.java new file mode 100644 index 0000000..e846c91 --- /dev/null +++ b/src/main/java/net/moonleay/gimbal/mixin/PlayerFlySpeedMixin.java @@ -0,0 +1,45 @@ +/* + * Gimbal + * Copyright (C) 2024 moonleay + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.moonleay.gimbal.mixin; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import net.minecraft.network.packet.s2c.play.PlayerAbilitiesS2CPacket; +import net.moonleay.gimbal.client.ClientMain; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +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 abstract class PlayerFlySpeedMixin { + + @Shadow + @Final + private MinecraftClient client; + + @Inject(method = "onPlayerAbilities", at = @At(value = "RETURN")) + private void func(PlayerAbilitiesS2CPacket packet, CallbackInfo ci) { + this.client.player.getAbilities().setFlySpeed((ClientMain.CONFIG.getConfig().getPlayerFlySpeed() / 100) * 0.05f); + //client!!.player!!.abilities.flySpeed = (this.playerFlySpeed / 100) * 0.05f + } + +} diff --git a/src/main/resources/gimbal.mixins.json b/src/main/resources/gimbal.mixins.json index 0babedd..9de8288 100644 --- a/src/main/resources/gimbal.mixins.json +++ b/src/main/resources/gimbal.mixins.json @@ -16,6 +16,7 @@ "HudMixin", "NoClipMixin$CameraMixin", "NormalModeMixin", + "PlayerFlySpeedMixin", "YouInjectButtonMixin$GameMenuScreenMixin", "YouInjectButtonMixin$TitleScreenMixin" ],