Skip to content

Commit

Permalink
Fix gamemode switcher
Browse files Browse the repository at this point in the history
This ties the f3+f4 and f3+n keybinds to the gamemode command instead of the permission level
  • Loading branch information
TheEpicBlock committed Jul 31, 2024
1 parent 6f18c03 commit f7fd585
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.modfest.fireblanket.mixin.gamemode_selection;

import net.minecraft.client.Keyboard;
import net.minecraft.client.network.ClientPlayerEntity;
import net.modfest.fireblanket.util.CanSwitchGameMode;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

/**
* @see CanSwitchGameMode#canSwitchGameMode(ClientPlayerEntity)
*/
@Mixin(Keyboard.class)
public class MixinKeyboard {
/**
* Replaces the {@link net.minecraft.entity.Entity#hasPermissionLevel(int)} check for
* using {@code f3+n} with the one in {@link CanSwitchGameMode}.
*/
@Redirect(method = "processF3", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;hasPermissionLevel(I)Z", ordinal = 1))
private boolean redirectPermissionCheck(ClientPlayerEntity instance, int i) {
return CanSwitchGameMode.canSwitchGameMode(instance);
}

/**
* Replaces the {@link net.minecraft.entity.Entity#hasPermissionLevel(int)} check for
* using {@code f3+f4} with the one in {@link CanSwitchGameMode}.
*/
@Redirect(method = "processF3", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;hasPermissionLevel(I)Z", ordinal = 2))
private boolean redirectPermissionCheck2(ClientPlayerEntity instance, int i) {
return CanSwitchGameMode.canSwitchGameMode(instance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.modfest.fireblanket.mixin.gamemode_selection;

import net.minecraft.client.gui.screen.GameModeSelectionScreen;
import net.minecraft.client.network.ClientPlayerEntity;
import net.modfest.fireblanket.util.CanSwitchGameMode;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

/**
* @see CanSwitchGameMode#canSwitchGameMode(ClientPlayerEntity)
*/
@Mixin(GameModeSelectionScreen.class)
public class MixinSelectionScreen {
/**
* Replaces a {@link net.minecraft.entity.Entity#hasPermissionLevel(int)} check with the one in {@link CanSwitchGameMode}.
*/
@Redirect(method = "apply(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/gui/screen/GameModeSelectionScreen$GameModeSelection;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;hasPermissionLevel(I)Z"))
private static boolean redirectPermissionCheck(ClientPlayerEntity instance, int i) {
return CanSwitchGameMode.canSwitchGameMode(instance);
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/modfest/fireblanket/util/CanSwitchGameMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.modfest.fireblanket.util;

import net.minecraft.client.network.ClientPlayerEntity;

public class CanSwitchGameMode {
/**
* By default, minecraft checks the permission level to see if a player can
* switch game modes using the quick-switcher. This is a more advanced check
* that looks at if the client can use the gamemode command.
* <p>
* This is useful when using player-roles as that can be used to
* give /gamemode without giving the whole permission level
* @see net.modfest.fireblanket.mixin.gamemode_selection.MixinSelectionScreen
*/
public static boolean canSwitchGameMode(ClientPlayerEntity player) {
var dispatcher = player.networkHandler.getCommandDispatcher();
return dispatcher.getRoot().getChild("gamemode") != null;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/fireblanket.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
"client.timing.MixinObservables",
"client.timing.MixinWorld",
"client.vbo_opto.MixinShaderProgram",
"client.vbo_opto.MixinVertexBuffer"
"client.vbo_opto.MixinVertexBuffer",
"gamemode_selection.MixinKeyboard",
"gamemode_selection.MixinSelectionScreen"
]
}

0 comments on commit f7fd585

Please sign in to comment.