Skip to content

Commit

Permalink
Coolio
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Nov 12, 2023
1 parent 78f1fb8 commit 5f66eda
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
- Fixed an incompatibility with Keybind Fix.
- 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.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/client/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<LimitedModConfigServer, ModConfigServer> HANDSHAKE_CLIENT = new HandshakeClient<>(
ModConfigServer.CODEC,
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down
17 changes: 15 additions & 2 deletions src/client/java/nl/enjarai/doabarrelroll/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 5f66eda

Please sign in to comment.