Skip to content

Commit

Permalink
improve behavior around toggling a key off
Browse files Browse the repository at this point in the history
reword translation strings to describe the setting they're attached to
better

add default key bindings to the toggle keys, using right shift/ctrl to
match the default vanilla keybinds
  • Loading branch information
celestialfault committed Feb 2, 2024
1 parent 23570fa commit d4d9ffb
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 32 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ insert_final_newline = true
tab_width = 4
indent_style = tab

[*.java]
ij_any_space_before_if_parentheses = false
ij_any_space_before_for_parentheses = false
ij_any_space_before_while_parentheses = false
ij_any_space_before_try_parentheses = false
ij_any_space_before_catch_parentheses = false

[*.md]
indent_style = space
indent_size = 2
Expand Down
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1 - Unreleased

### Added

- This changelog file
- Added default bindings for the toggle keys using the right-side versions of the default vanilla key, such as Right Shift
to match the default Left Shift for the Sneak key

### Changed

- Improved behavior around toggling a key off while the vanilla key is still being held
- In other words: if you toggle sprint off while holding the vanilla sprint key, you will now continue to sprint after
toggling it off, instead of having to stop holding and then press the vanilla key again
- Changed some configuration menu names and descriptions to better describe the relevant settings

## 1.0 - 2024-01-05

- Initial release
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Toggle Toggle Sprint

[![Requires Fabric API](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/compact/requires/fabric-api_vector.svg)](https://modrinth.com/mod/fabric-api)
[![Available on Modrinth](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/compact/available/modrinth_vector.svg)](https://modrinth.com/mod/toggle-toggle-sprint)

This is a Fabric mod for Minecraft adding toggle-sprint/sneak keybinds, making use of the existing Toggle options
in the Controls menu, ensuring it also remains as anti-cheat friendly as possible.

This is a Fabric mod for Minecraft adding toggle-sprint/sneak keybinds, making use of the vanilla toggle states in the
Controls menu to ensure that it remains as anti-cheat friendly as possible.
This also includes a fix for preventing the game from resetting your toggle sprint upon death.

If both [Mod Menu] and [YACL] are installed, this mod also contains a few configuration options that can be modified in-game.
Expand All @@ -20,6 +22,12 @@ to do so in vanilla!)*, making it trivially easy to be flagged (and subsequently
Instead, as this mod simply reuses the existing Toggle option in the Controls menu, this ensures that it remains
as close to the vanilla sprinting behavior as possible, avoiding anti-cheats getting angry in the process.

## Modpacks?

Go for it. I'd prefer that the download remains within the Modrinth ecosystem, but as this mod is licensed under [CC-0],
you're free to redistribute it however you see fit (but I'd still appreciate credit where possible).

[YACL]: https://modrinth.com/mod/yacl
[Mod Menu]: https://modrinth.com/mod/modmenu
[Toggle Sneak & Sprint]: https://modrinth.com/mod/toggle-sneak-sprint
[CC-0]: https://github.com/celestialfault/toggle-toggle-sprint/blob/main/LICENSE
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.2+build.4
loader_version=0.15.3

# Mod Properties
mod_version=1.0.0
mod_version=1.1
maven_group=me.celestialfault.toggletogglesprint
archives_base_name=toggle-toggle-sprint

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/me/celestialfault/toggletogglesprint/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Screen getConfigScreen(Screen parent) {
return YetAnotherConfigLib.createBuilder()
.title(Text.translatable("toggle-toggle-sprint.name"))
.category(ConfigCategory.createBuilder()
.name(Text.translatable("toggle-toggle-sprint.name"))
.name(Text.translatable("toggle-toggle-sprint.config"))
.group(buildGeneral())
.group(buildSprint())
.group(buildSneak())
Expand All @@ -108,7 +108,12 @@ private OptionGroup buildGeneral() {
.name(Text.translatable("toggle-toggle-sprint.general"))
.option(Option.<Boolean>createBuilder()
.name(Text.translatable("toggle-toggle-sprint.sprintOnDeath"))
.description(OptionDescription.of(Text.translatable("toggle-toggle-sprint.sprintOnDeath.description")))
.description(OptionDescription.of(
Text.translatable("toggle-toggle-sprint.sprintOnDeath.description.line1")
.append("\n\n")
.append(Text.translatable("toggle-toggle-sprint.sprintOnDeath.description.line2"))
.append("\n\n")
.append(Text.translatable("toggle-toggle-sprint.sprintOnDeath.description.line3"))))
.binding(true, () -> keepSprintingOnDeath, value -> this.keepSprintingOnDeath = value)
.controller(TickBoxControllerBuilderImpl::new)
.build())
Expand Down Expand Up @@ -154,7 +159,7 @@ private OptionGroup buildSneak() {
.description(OptionDescription.of(
Text.translatable("toggle-toggle-sprint.sneakOnJoin.description")
.append("\n\n")
.append(Text.translatable("toggle-toggle-sprint.sneakOnJoin.warning").formatted(Formatting.RED))))
.append(Text.translatable("toggle-toggle-sprint.potentiallyCheaty").formatted(Formatting.RED))))
.binding(false, () -> sneakOnJoin, sneakOnJoin -> this.sneakOnJoin = sneakOnJoin)
.controller(TickBoxControllerBuilderImpl::new)
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.option.SimpleOption;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;

public class ToggleToggleSprint implements ClientModInitializer {

public static final Logger LOGGER = LogUtils.getLogger();
private boolean inWorld = false;

public static final OnPressKeyBinding TOGGLE_SPRINT = new OnPressKeyBinding("key.toggle-toggle-sprint.sprint", InputUtil.UNKNOWN_KEY.getCode(), KeyBinding.MOVEMENT_CATEGORY, () -> {
public static final OnPressKeyBinding TOGGLE_SPRINT = new OnPressKeyBinding("key.toggle-toggle-sprint.sprint", GLFW.GLFW_KEY_RIGHT_CONTROL, KeyBinding.MOVEMENT_CATEGORY, () -> {
MinecraftClient client = MinecraftClient.getInstance();
toggleOption(client.options.sprintToggled, client.options.sprintKey, Config.INSTANCE.alsoStartSprinting);
});

public static final OnPressKeyBinding TOGGLE_SNEAK = new OnPressKeyBinding("key.toggle-toggle-sprint.sneak", InputUtil.UNKNOWN_KEY.getCode(), KeyBinding.MOVEMENT_CATEGORY, () -> {
public static final OnPressKeyBinding TOGGLE_SNEAK = new OnPressKeyBinding("key.toggle-toggle-sprint.sneak", GLFW.GLFW_KEY_RIGHT_SHIFT, KeyBinding.MOVEMENT_CATEGORY, () -> {
MinecraftClient client = MinecraftClient.getInstance();
toggleOption(client.options.sneakToggled, client.options.sneakKey, Config.INSTANCE.alsoStartSneaking);
});
Expand All @@ -36,17 +37,16 @@ private void onTick(MinecraftClient client) {
if(client.world == null && inWorld) {
inWorld = false;
} else if(client.world != null && !inWorld) {
doDefaultState(client);
setKeybindStates(client);
applyJoinStates(client);
inWorld = true;
}
}

private void doDefaultState(MinecraftClient client) {
private void applyJoinStates(MinecraftClient client) {
// Default sprint state
if(Config.INSTANCE.defaultSprintState == Config.ToggleState.ON) {
client.options.sprintToggled.setValue(true);
// pressing the key is dealt with by #setKeybindStates()
// pressing the key is done later
} else if(Config.INSTANCE.defaultSprintState == Config.ToggleState.OFF) {
client.options.sprintToggled.setValue(false);
// ... but, we still want to ensure that the key is unpressed if we're loading into a world for a
Expand All @@ -61,9 +61,8 @@ private void doDefaultState(MinecraftClient client) {
client.options.sneakToggled.setValue(false);
client.options.sneakKey.setPressed(false);
}
}

private void setKeybindStates(MinecraftClient client) {
// Press the keys if they're configured to be pressed when joining a world, and the relevant toggle latch is on
if(Config.INSTANCE.sprintOnJoin && client.options.sprintToggled.getValue() && !client.options.sprintKey.isPressed()) {
client.options.sprintKey.setPressed(true);
}
Expand All @@ -74,6 +73,16 @@ private void setKeybindStates(MinecraftClient client) {

private static void toggleOption(SimpleOption<Boolean> toggle, KeyBinding keybind, boolean activateKey) {
toggle.setValue(!toggle.getValue());
if(activateKey && !keybind.isPressed() || !toggle.getValue()) keybind.setPressed(toggle.getValue());
if(toggle.getValue() && activateKey && !keybind.isPressed()) {
keybind.setPressed(true);
} else if(!toggle.getValue()) {
long handle = MinecraftClient.getInstance().getWindow().getHandle();
boolean manuallyHeld = InputUtil.isKeyPressed(handle, KeyBindingHelper.getBoundKeyOf(keybind).getCode());
// note that we always call this with the value of manuallyHeld in order to handle the case where the player
// presses the vanilla key, thereby toggling the key's held state off, and then pressing our toggle key,
// turning off the toggle latch; in such a case, the game wouldn't think the key is being pressed, when
// it should logically be pressed.
keybind.setPressed(manuallyHeld);
}
}
}
38 changes: 21 additions & 17 deletions src/main/resources/assets/toggle-toggle-sprint/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
{
"toggle-toggle-sprint.name": "Toggle Toggle Sprint",
"toggle-toggle-sprint.config": "Configuration",
"toggle-toggle-sprint.noYacl": "This requires Yet Another Config Lib to be installed!",
"toggle-toggle-sprint.general": "General",
"toggle-toggle-sprint.sprint": "Sprint",
"toggle-toggle-sprint.sneak": "Sneak",

"toggle-toggle-sprint.potentiallyCheaty": "Some servers may consider this feature to be a cheat, use at your own risk!",

"key.toggle-toggle-sprint.sprint": "Toggle Sprint",
"key.toggle-toggle-sprint.sneak": "Toggle Sneak",

"toggle-toggle-sprint.state.on": "On",
"toggle-toggle-sprint.state.off": "Off",
"toggle-toggle-sprint.state.unchanged": "Don't modify",

"toggle-toggle-sprint.sprintOnDeath": "Keep sprinting on death",
"toggle-toggle-sprint.sprintOnDeath.description": "If enabled, your sprinting won't be broken upon respawning",
"toggle-toggle-sprint.sprintOnDeath": "Keep sprinting after death",
"toggle-toggle-sprint.sprintOnDeath.description.line1": "As of 1.20.1, the game will release all toggled keys when the player respawns, including the Sprint key.",
"toggle-toggle-sprint.sprintOnDeath.description.line2": "This feature prevents vanilla from releasing your sprint key, ensuring that you continue to sprint after respawning.",
"toggle-toggle-sprint.sprintOnDeath.description.line3": "Note that this feature only affects the Sprint key, and all other toggle keys (such as the Sneak key) will still be released.",

"toggle-toggle-sprint.sprintOnJoin": "Sprint when joining",
"toggle-toggle-sprint.sprintOnJoin.description": "If enabled, you'll start sprinting upon joining a world if your sprint key is set to toggle",
"toggle-toggle-sprint.sprintOnJoin": "Start sprinting",
"toggle-toggle-sprint.sprintOnJoin.description": "If enabled, you'll start sprinting when loading into a world if your Sprint control is set to Toggle",
"toggle-toggle-sprint.sprintState": "Default sprinting state",
"toggle-toggle-sprint.sprintState.description": "Changes your default state for sprinting when joining a world",
"toggle-toggle-sprint.sprintState.on": "Your sprint option will be changed to Toggle when joining a world",
"toggle-toggle-sprint.sprintState.off": "Your sprint option will be changed to Hold when joining a world",
"toggle-toggle-sprint.sprintState.unchanged": "Your sprint option won't be changed when joining a world",
"toggle-toggle-sprint.sprintState.description": "Changes your default state for sprinting",
"toggle-toggle-sprint.sprintState.on": "Your Sprint control will be changed to Toggle when joining a world",
"toggle-toggle-sprint.sprintState.off": "Your Sprint control will be changed to Hold when joining a world",
"toggle-toggle-sprint.sprintState.unchanged": "Your Sprint control won't be modified when joining a world",
"toggle-toggle-sprint.alsoStartSprinting": "Also start sprinting",
"toggle-toggle-sprint.alsoStartSprinting.description": "When enabled, the toggle key will also simulate a sprint key press when activated",
"toggle-toggle-sprint.alsoStartSprinting.description": "When enabled, the toggle key will also simulate a Sprint key press when activated if you're not already sprinting",

"toggle-toggle-sprint.sneakOnJoin": "Sneak when joining",
"toggle-toggle-sprint.sneakOnJoin.description": "If enabled, you'll start sneaking whenever you join a world if your sneak key is set to toggle",
"toggle-toggle-sprint.sneakOnJoin.warning": "WARNING: Some servers may consider this to be a cheat, use at your own risk!",
"toggle-toggle-sprint.sneakOnJoin": "Start sneaking",
"toggle-toggle-sprint.sneakOnJoin.description": "If enabled, you'll start sneaking when loading into a world if your Sneak control is set to Toggle",
"toggle-toggle-sprint.sneakState": "Default sneaking state",
"toggle-toggle-sprint.sneakState.description": "Changes your default state for sneaking when joining a world",
"toggle-toggle-sprint.sneakState.on": "Your sneak option will be changed to Toggle when joining a world",
"toggle-toggle-sprint.sneakState.off": "Your sneak option will be changed to Hold when joining a world",
"toggle-toggle-sprint.sneakState.unchanged": "Your sneak option won't be changed when joining a world",
"toggle-toggle-sprint.sneakState.description": "Changes the default state for your Sneak control",
"toggle-toggle-sprint.sneakState.on": "Your Sneak control will be changed to Toggle when joining a world",
"toggle-toggle-sprint.sneakState.off": "Your Sneak control will be changed to Hold when joining a world",
"toggle-toggle-sprint.sneakState.unchanged": "Your Sneak control won't be modified when joining a world",
"toggle-toggle-sprint.alsoStartSneaking": "Also start sneaking",
"toggle-toggle-sprint.alsoStartSneaking.description": "When enabled, the toggle key will also simulate a sneak key press when activated"
"toggle-toggle-sprint.alsoStartSneaking.description": "When enabled, the toggle key will also simulate a Sneak key press when activated if you're not already sneaking"
}

0 comments on commit d4d9ffb

Please sign in to comment.