From 5f66eda78f54c1a7c22b3b0afd874bb6582fe303 Mon Sep 17 00:00:00 2001 From: enjarai Date: Sun, 12 Nov 2023 22:04:29 +0100 Subject: [PATCH] Coolio --- CHANGELOG.md | 5 ++- gradle.properties | 2 +- .../doabarrelroll/DoABarrelRollClient.java | 37 +++++++++++++++++++ .../enjarai/doabarrelroll/ModKeybindings.java | 2 +- .../compat/yacl/YACLImplementation.java | 6 ++- .../doabarrelroll/config/ModConfig.java | 17 ++++++++- 6 files changed, 63 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79da7ab9..ea3feadb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,4 @@ -- Fixed an incompatibility with Keybind Fix. \ No newline at end of file +- As an exception to existing limitations, thrusting will now be allowed when the player is an operator on a Realm. + This does not apply to normal servers. +- Changed the default toggle keybind to I to avoid conflicting with the advancements button. +- Fixed an incompatibility with Keybind Fix. diff --git a/gradle.properties b/gradle.properties index e5d0da48..366c1bdd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,7 +11,7 @@ yarn_version=1.20+build.1 loader_version=0.14.24 fabric_version=0.83.0+1.20 -cicada_version=0.4.5 +cicada_version=0.4.6 # https://modrinth.com/mod/modmenu/versions#all-versions modmenu_version=7.2.1 # https://github.com/isXander/YetAnotherConfigLib/releases diff --git a/src/client/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java b/src/client/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java index 3065a7c8..4c088057 100644 --- a/src/client/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java +++ b/src/client/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java @@ -1,8 +1,10 @@ package nl.enjarai.doabarrelroll; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.network.ServerInfo; import net.minecraft.client.util.SmoothUtil; import nl.enjarai.doabarrelroll.api.RollEntity; import nl.enjarai.doabarrelroll.api.RollMouse; @@ -18,8 +20,11 @@ import nl.enjarai.doabarrelroll.render.HorizonLineWidget; import nl.enjarai.doabarrelroll.render.MomentumCrosshairWidget; import nl.enjarai.doabarrelroll.util.MixinHooks; +import org.jetbrains.annotations.Nullable; import org.joml.Vector2d; +import java.lang.reflect.Method; + public class DoABarrelRollClient { public static final HandshakeClient HANDSHAKE_CLIENT = new HandshakeClient<>( ModConfigServer.CODEC, @@ -32,6 +37,23 @@ public class DoABarrelRollClient { public static final RollGroup FALL_FLYING_GROUP = RollGroup.of(DoABarrelRoll.id("fall_flying")); public static double throttle = 0; + @Nullable + private static final Method isRealmsMethod; + + static { + Method method; + var resolver = FabricLoader.getInstance().getMappingResolver(); + var className = resolver.mapClassName("intermediary", "class_8599"); + var methodName = resolver.mapMethodName("intermediary", className, "method_52811", "()Z"); + + try { + method = ServerInfo.class.getMethod(methodName); + } catch (NoSuchMethodException e) { + method = null; + } + isRealmsMethod = method; + } + public static void init() { FALL_FLYING_GROUP.trueIf(DoABarrelRollClient::isFallFlying); @@ -114,4 +136,19 @@ public static boolean isFallFlying() { } return player.isFallFlying(); } + + public static boolean isConnectedToRealms() { + if (isRealmsMethod == null) return false; + + var serverInfo = MinecraftClient.getInstance().getCurrentServerEntry(); + if (serverInfo == null) return false; + + // We don't have to care about supporting 1.20 for realms, as realms servers are always on the latest version. + // But I also want to keep the project on 1.20 as a base for now, therefore the reflection jank. + try { + return (boolean) isRealmsMethod.invoke(serverInfo); + } catch (ReflectiveOperationException | NullPointerException | ClassCastException f) { + return false; + } + } } diff --git a/src/client/java/nl/enjarai/doabarrelroll/ModKeybindings.java b/src/client/java/nl/enjarai/doabarrelroll/ModKeybindings.java index 9d4ae1ef..d9d8c025 100644 --- a/src/client/java/nl/enjarai/doabarrelroll/ModKeybindings.java +++ b/src/client/java/nl/enjarai/doabarrelroll/ModKeybindings.java @@ -17,7 +17,7 @@ public class ModKeybindings { public static final KeyBinding TOGGLE_ENABLED = new KeyBinding( "key.do_a_barrel_roll.toggle_enabled", InputUtil.Type.KEYSYM, - GLFW.GLFW_KEY_L, + GLFW.GLFW_KEY_I, "category.do_a_barrel_roll.do_a_barrel_roll" ); public static final KeyBinding TOGGLE_THRUST = new KeyBinding( diff --git a/src/client/java/nl/enjarai/doabarrelroll/compat/yacl/YACLImplementation.java b/src/client/java/nl/enjarai/doabarrelroll/compat/yacl/YACLImplementation.java index 7711ca00..9476be74 100644 --- a/src/client/java/nl/enjarai/doabarrelroll/compat/yacl/YACLImplementation.java +++ b/src/client/java/nl/enjarai/doabarrelroll/compat/yacl/YACLImplementation.java @@ -8,6 +8,7 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ConfirmScreen; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.screen.ScreenTexts; import net.minecraft.text.MutableText; import net.minecraft.text.Text; @@ -31,9 +32,12 @@ public class YACLImplementation { public static Screen generateConfigScreen(Screen parent) { var inWorld = MinecraftClient.getInstance().world != null; + ClientPlayerEntity player; + var onRealms = DoABarrelRollClient.isConnectedToRealms() && + (player = MinecraftClient.getInstance().player) != null && player.hasPermissionLevel(2); var serverConfig = DoABarrelRollClient.HANDSHAKE_CLIENT.getConfig(); - var thrustingAllowed = new Dependable(serverConfig.map(LimitedModConfigServer::allowThrusting).orElse(!inWorld)); + var thrustingAllowed = new Dependable(serverConfig.map(LimitedModConfigServer::allowThrusting).orElse(!inWorld || onRealms)); var allowDisabled = new Dependable(!serverConfig.map(LimitedModConfigServer::forceEnabled).orElse(false)); var builder = YetAnotherConfigLib.createBuilder() diff --git a/src/client/java/nl/enjarai/doabarrelroll/config/ModConfig.java b/src/client/java/nl/enjarai/doabarrelroll/config/ModConfig.java index 07850e4c..1f8bad15 100644 --- a/src/client/java/nl/enjarai/doabarrelroll/config/ModConfig.java +++ b/src/client/java/nl/enjarai/doabarrelroll/config/ModConfig.java @@ -4,6 +4,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; import nl.enjarai.doabarrelroll.DoABarrelRollClient; import nl.enjarai.doabarrelroll.api.event.RollContext; import nl.enjarai.doabarrelroll.api.rotation.RotationInstant; @@ -161,8 +163,19 @@ public boolean getSimulateControlSurfaceEfficacy() { }// = false; public boolean getEnableThrust() { - return general.thrust.enable_thrust && DoABarrelRollClient.HANDSHAKE_CLIENT - .getConfig().map(LimitedModConfigServer::allowThrusting).orElse(false); + if (general.thrust.enable_thrust) { + ClientPlayerEntity player; + if (DoABarrelRollClient.isConnectedToRealms() && + (player = MinecraftClient.getInstance().player) != null && player.hasPermissionLevel(2)) { + return true; + } + + return DoABarrelRollClient.HANDSHAKE_CLIENT.getConfig() + .map(LimitedModConfigServer::allowThrusting) + .orElse(false); + } + + return false; } public boolean getEnableThrustClient() {