1
0
mirror of https://github.com/chylex/Better-Controls.git synced 2025-08-17 16:31:49 +02:00

22 Commits

Author SHA1 Message Date
d416883dbb Release v1.2.0 for Minecraft 1.15.x 2020-12-10 05:52:43 +01:00
681fd75997 Fix tapping sneak to stop flying on ground not working when flight inertia is disabled 2020-12-10 05:52:22 +01:00
04408ddfb1 Add option to disable double tapping 'Jump' key to flight
Fixes #6
2020-12-09 17:23:54 +01:00
15e314eb73 Change flight to 'Hold to Sprint' mode at all times & fix not boost unless flying forward 2020-12-09 17:13:46 +01:00
b292a3b621 Add option to disable flight inertia after releasing movement keys
Fixes #2
2020-12-09 17:13:17 +01:00
4248293230 Minor code tweaks 2020-12-09 17:12:50 +01:00
9634c33ca2 Add README 2020-12-09 17:12:06 +01:00
54a6b4ea56 Refactor code to make porting easier 2020-12-09 17:04:36 +01:00
9437932b97 Release v1.1.0 for Minecraft 1.15.x 2020-10-27 17:28:37 +01:00
50c035bb91 Fix compile error after cherry-pick 2020-10-27 17:28:13 +01:00
81e135469e Avoid sudden FOV jump when starting to fly with disabled FOV changing 2020-10-27 17:25:39 +01:00
188f66b4d3 Add options for vertical flight speed boost in creative/spectator and default/sprinting modes 2020-10-27 17:25:39 +01:00
ab4fa452f7 Add more flight speed options (5x and 7x) 2020-10-27 17:22:07 +01:00
116bbe241d Add option to prevent FOV changing while flying 2020-10-27 17:21:23 +01:00
9baa7c4460 Add key binding to reset all toggles at once 2020-10-27 17:20:11 +01:00
7f258966de Implement third sprinting mode (hold key to sprint) & option to change sprinting mode when flying 2020-10-27 17:20:11 +01:00
9af10ad04c Release v1.0.1 for Minecraft 1.15.x 2020-10-19 21:15:32 +02:00
6e316eff89 Avoid wobble sprinting when resuming sprinting after hitting an obstacle, by simulating holding sprint for multiple ticks 2020-10-19 21:14:56 +02:00
21ee424cef Fix not removing custom category from Controls screen 2020-10-18 16:54:40 +02:00
c7c5b67c3b Fix sprinting not stopping when untoggling sometimes 2020-10-18 16:02:42 +02:00
06b2782372 Fix Mod Menu support 2020-10-18 15:37:12 +02:00
83b2dfdfa5 Backport to 1.15.x 2020-10-18 15:04:04 +02:00
41 changed files with 700 additions and 229 deletions

3
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1,3 @@
github: chylex
patreon: chylex
ko_fi: chylex

View File

@@ -1,3 +1,78 @@
# Better Controls
Better Controls
===============
WIP
![Logo](https://raw.githubusercontent.com/chylex/Better-Controls/master/logo.png "Depiction of Zero Regrets")
Better Controls is a reimagining of [Better Sprinting](https://www.curseforge.com/minecraft/mc-mods/better-sprinting) with even more customizability. It features several new keybindings and many configurable options for how sprinting, sneaking, and flying should behave.
Better Controls only focuses on complementing vanilla mechanics rather than adding new mechanics (such as [Better Sprinting](https://www.curseforge.com/minecraft/mc-mods/better-sprinting)'s option to allow sprinting in all directions) that only work on client side and server owners must explicitly opt in to enable them. By not having such features in Better Controls, it significantly reduces complexity by not having a server side version of the mod, and not needing a way to completely disable the mod while the game is running.
Another major difference is the amount and granularity of options. Better Controls could be considered an *advanced controls mod* - by default, none of the keybinds are bound, and all options are set to match vanilla behavior. To take full advantage, go into *Options - Controls - Better Controls*, check out all of the available options, and figure out the best configuration for you.
## Features
The mod adds **Toggle Keybinds** for sprinting, sneaking, flying (creative mode), walking, and jumping. You can use modifier keys (`Control` / `Shift` / `Alt`) for each, including for example setting `Control` to Sneak, and `Control + Y` to Toggle Sneak. If you press the original key, the toggle will be canceled (in the previous example, you can Toggle Sneak by pressing `Control + Y`, and stop sneaking by simply tapping Sneak). Note that the vanilla options for toggling sprinting/sneaking are disabled to avoid conflicts with the custom keybinds.
You can also bind a key that resets all **Toggle Keybinds** at once. That makes it easy to for ex. turn on walking, jumping, and sprinting, and then turn all of them off again by pressing one key instead of three.
#### Sprinting
* **Sprint Key Mode** changes how the Sprint key behaves. You can choose between *Tap To Start Sprinting*, *Tap To Start / Stop Sprinting*, and *Hold To Sprint*.
* **Double Tap 'Walk Forwards' To Sprint** can be turned off to prevent accidental sprinting.
* **Resume Sprinting After Hitting Obstacle** automatically presses the Sprint key once you are no longer touching any blocks (helpful when climbing hills, especially if the previous option is enabled).
#### Sneaking
* **Move Camera Smoothly** lets you disable the smooth movement when sneaking or unsneaking.
#### Flying
* **Sprint Key Mode While Flying** changes how the Sprint key behaves during flight.
* **Disable Field Of View Changing** prevents sprinting, potions, and other factors from changing the FOV while flying in creative and spectator mode.
* **Fly On Ground** lets you fly while touching the ground in creative mode (and also lets you stop flying by tapping Sneak while touching the ground).
* **Flight Speed Multiplier** (0.25x - 8x) changes how fast you fly in creative and spectator mode.
* **Vertical Speed Boost** (up to +300%) adds additional vertical speed boost while flying in creative and spectator mode.
## Installation
The following mod loaders are supported:
* **[Fabric](https://fabricmc.net/use/)** (note: this mod does not require Fabric API)
* **[Forge](https://files.minecraftforge.net/)**
After you install the mod loader of your choice, [download the mod](https://www.curseforge.com/minecraft/mc-mods/better-controls/files) and place the `.jar` file into `.minecraft/mods`.
### Minecraft Updates
The mod will attempt to load on new versions of Minecraft (including snapshots) as they come out. I made it that way so that if the mod works on a newly released version of Minecraft without any changes, I can simply mark it as compatible and you don't have to wait for an update.
While this is convenient, there is potential for bugs which can affect server play. If you plan to use the mod on a server, please always use the latest version of the mod, and wait until I either mark the version as compatible with new Minecraft updates, or release a new version of the mod that is marked as compatible.
### Compatibility
* Better Controls is a client-side mod, it will do nothing when installed on a server.
* If you run into a conflict with another mod that modifies the *Controls* screen, hold Alt while opening *Controls* to prevent Better Controls from adding its button. If another mod prevents the button from appearing, you can install [Mod Menu](https://www.curseforge.com/minecraft/mc-mods/modmenu) which adds a mod list with configuration buttons for mods.
## Screenshot
![Options Menu Screenshot](https://repo.chylex.com/better-controls.png)
Source Code
===========
The `main` branch includes the latest version for Fabric. Older versions and their Forge equivalents are in branches named `fabric/<version>` and `forge/<version>`.
## Contributing
All contributions should target the `main` branch, unless the contribution is specifically made for older versions. Every feature must be portable to Forge to ensure parity, unless the feature adds integration with another mod that is only compatible with one mod loader. Integrations for Forge-only mods should target the most recent `forge/<version>` branch.
For any larger contributions, please [open an issue](https://github.com/chylex/Better-Controls/issues/new) first before you make a PR.
Please keep in mind that this mod is designed to be very simple and easy to test. Over the years, [Better Sprinting](https://github.com/chylex/Better-Sprinting) has gained a fair amount of bloat that requires extensive testing before every release, and it is the reason why I will not be accepting PRs which add:
* **Translations**
* If you want to translate the mod, you are welcome to create a separate version of it. Unfortunately, I've always ran into enough problems with maintaining and testing community mod translations that I will not be adding official support for localization.
* **Server-side code**
* Better Controls will always be exclusively client-side. The server-side support in Better Sprinting is a major time sink which requires testing 3 separate client/server configurations per mod loader, and is the main reason I decided to drop support in favor of this mod.
* **Singleplayer-only features**
* Better Controls intends to add accessibility options that can be used on servers, so features that only work in singleplayer or anything that could be used for cheating on servers will not be accepted.

View File

@@ -14,7 +14,6 @@ dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
// modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
}
processResources {
@@ -34,6 +33,8 @@ tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
version = 'v' + version
jar {
from "LICENSE"
exclude "io/github/prospector/"

View File

@@ -2,17 +2,14 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
minecraft_version=1.16.3
yarn_mappings=1.16.3+build.47
minecraft_version=1.15.2
yarn_mappings=1.15.2+build.17
loader_version=0.10.2+build.210
# Mod Properties
mod_version=1.0.0
mod_version=1.2.0
maven_group=chylex.bettercontrols
archives_base_name=BetterControls
# Dependencies
fabric_version=0.22.0+build.408-1.16
archives_base_name=BetterControls-1.15.x
# https://fabricmc.net/use
# https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api

BIN
logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@@ -4,6 +4,11 @@ import io.github.prospector.modmenu.api.ConfigScreenFactory;
import io.github.prospector.modmenu.api.ModMenuApi;
public class ModMenuSupport implements ModMenuApi{
@Override
public String getModId(){
return "bettercontrols";
}
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory(){
return BetterControlsScreen::new;

View File

@@ -1,6 +1,6 @@
package chylex.bettercontrols.config;
import chylex.bettercontrols.input.KeyBindingWithModifier;
import net.minecraft.client.options.KeyBinding;
import chylex.bettercontrols.input.SprintMode;
import java.nio.file.Path;
public final class BetterControlsConfig{
@@ -11,22 +11,30 @@ public final class BetterControlsConfig{
private Path path;
public final KeyBindingWithModifier keyToggleSprint = new KeyBindingWithModifier("key.bettercontrols.toggle_sprint");
public SprintMode sprintMode = SprintMode.TAP_TO_START;
public boolean doubleTapForwardToSprint = true;
public boolean tapSprintKeyAgainToStopSprinting = false;
public boolean resumeSprintingAfterHittingObstacle = false;
public final KeyBindingWithModifier keyToggleSneak = new KeyBindingWithModifier("key.bettercontrols.toggle_sneak");
public boolean sneakingMovesCameraSmoothly = true;
public final KeyBindingWithModifier keyToggleFlight = new KeyBindingWithModifier("key.bettercontrols.toggle_flight");
public boolean doubleTapJumpToToggleFlight = true;
public boolean disableFlightInertia = false;
public boolean disableChangingFovWhileFlying = false;
public boolean flyOnGroundInCreative = false;
public float flightSpeedMpCreativeDefault = 1F;
public float flightSpeedMpCreativeSprinting = 2F;
public float flightSpeedMpSpectatorDefault = 1F;
public float flightSpeedMpSpectatorSprinting = 2F;
public float flightVerticalBoostCreativeDefault = 0F;
public float flightVerticalBoostCreativeSprinting = 0F;
public float flightVerticalBoostSpectatorDefault = 0F;
public float flightVerticalBoostSpectatorSprinting = 0F;
public final KeyBindingWithModifier keyToggleWalkForward = new KeyBindingWithModifier("key.bettercontrols.toggle_forward");
public final KeyBindingWithModifier keyToggleJump = new KeyBindingWithModifier("key.bettercontrols.toggle_jump");
public final KeyBindingWithModifier keyResetAllToggles = new KeyBindingWithModifier("key.bettercontrols.reset_all_toggles");
public final KeyBindingWithModifier keyOpenMenu = new KeyBindingWithModifier("key.bettercontrols.open_menu");
BetterControlsConfig(){}
@@ -36,13 +44,14 @@ public final class BetterControlsConfig{
return this;
}
public KeyBinding[] getAllKeyBindings(){
return new KeyBinding[]{
public KeyBindingWithModifier[] getAllKeyBindings(){
return new KeyBindingWithModifier[]{
keyToggleSprint,
keyToggleSneak,
keyToggleFlight,
keyToggleWalkForward,
keyToggleJump,
keyResetAllToggles,
keyOpenMenu
};
}

View File

@@ -1,4 +1,5 @@
package chylex.bettercontrols.config;
import chylex.bettercontrols.input.SprintMode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
@@ -10,7 +11,6 @@ import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.minecraft.util.math.MathHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.FileNotFoundException;
@@ -32,22 +32,30 @@ final class ConfigSerializer implements JsonSerializer<BetterControlsConfig>, Js
final JsonObject obj = new JsonObject();
Json.writeKeyBinding(obj, "Sprint.KeyToggle", cfg.keyToggleSprint);
Json.setEnum(obj, "Sprint.Mode", cfg.sprintMode);
Json.setBool(obj, "Sprint.DoubleTapForward", cfg.doubleTapForwardToSprint);
Json.setBool(obj, "Sprint.TapToStop", cfg.tapSprintKeyAgainToStopSprinting);
Json.setBool(obj, "Sprint.ResumeAfterHittingObstacle", cfg.resumeSprintingAfterHittingObstacle);
Json.writeKeyBinding(obj, "Sneak.KeyToggle", cfg.keyToggleSneak);
Json.setBool(obj, "Sneak.SmoothCamera", cfg.sneakingMovesCameraSmoothly);
Json.writeKeyBinding(obj, "Flight.KeyToggle.Creative", cfg.keyToggleFlight);
Json.setBool(obj, "Flight.DoubleTapJump", cfg.doubleTapJumpToToggleFlight);
Json.setBool(obj, "Flight.DisableInertia", cfg.disableFlightInertia);
Json.setBool(obj, "Flight.DisableChangingFOV", cfg.disableChangingFovWhileFlying);
Json.setBool(obj, "Flight.FlyOnGround.Creative", cfg.flyOnGroundInCreative);
Json.setFloat(obj, "Flight.SpeedMp.Creative.Default", cfg.flightSpeedMpCreativeDefault);
Json.setFloat(obj, "Flight.SpeedMp.Creative.Sprinting", cfg.flightSpeedMpCreativeSprinting);
Json.setFloat(obj, "Flight.SpeedMp.Spectator.Default", cfg.flightSpeedMpSpectatorDefault);
Json.setFloat(obj, "Flight.SpeedMp.Spectator.Sprinting", cfg.flightSpeedMpSpectatorSprinting);
Json.setFloat(obj, "Flight.VerticalBoost.Creative.Default", cfg.flightVerticalBoostCreativeDefault);
Json.setFloat(obj, "Flight.VerticalBoost.Creative.Sprinting", cfg.flightVerticalBoostCreativeSprinting);
Json.setFloat(obj, "Flight.VerticalBoost.Spectator.Default", cfg.flightVerticalBoostSpectatorDefault);
Json.setFloat(obj, "Flight.VerticalBoost.Spectator.Sprinting", cfg.flightVerticalBoostSpectatorSprinting);
Json.writeKeyBinding(obj, "Misc.KeyToggleWalkForward", cfg.keyToggleWalkForward);
Json.writeKeyBinding(obj, "Misc.KeyToggleJump", cfg.keyToggleJump);
Json.writeKeyBinding(obj, "Misc.KeyResetAllToggles", cfg.keyResetAllToggles);
Json.writeKeyBinding(obj, "Misc.KeyOpenMenu", cfg.keyOpenMenu);
return obj;
@@ -58,23 +66,35 @@ final class ConfigSerializer implements JsonSerializer<BetterControlsConfig>, Js
final BetterControlsConfig cfg = new BetterControlsConfig();
final JsonObject obj = json.getAsJsonObject();
if (obj.has("Sprint.TapToStop") && obj.get("Sprint.TapToStop").getAsBoolean()){
cfg.sprintMode = SprintMode.TAP_TO_TOGGLE;
}
Json.readKeyBinding(obj, "Sprint.KeyToggle", cfg.keyToggleSprint);
cfg.sprintMode = Json.getEnum(obj, "Sprint.Mode", cfg.sprintMode, SprintMode.class);
cfg.doubleTapForwardToSprint = Json.getBool(obj, "Sprint.DoubleTapForward", cfg.doubleTapForwardToSprint);
cfg.tapSprintKeyAgainToStopSprinting = Json.getBool(obj, "Sprint.TapToStop", cfg.tapSprintKeyAgainToStopSprinting);
cfg.resumeSprintingAfterHittingObstacle = Json.getBool(obj, "Sprint.ResumeAfterHittingObstacle", cfg.resumeSprintingAfterHittingObstacle);
Json.readKeyBinding(obj, "Sneak.KeyToggle", cfg.keyToggleSneak);
cfg.sneakingMovesCameraSmoothly = Json.getBool(obj, "Sneak.SmoothCamera", cfg.sneakingMovesCameraSmoothly);
Json.readKeyBinding(obj, "Flight.KeyToggle.Creative", cfg.keyToggleFlight);
cfg.doubleTapJumpToToggleFlight = Json.getBool(obj, "Flight.DoubleTapJump", cfg.doubleTapJumpToToggleFlight);
cfg.disableFlightInertia = Json.getBool(obj, "Flight.DisableInertia", cfg.disableFlightInertia);
cfg.disableChangingFovWhileFlying = Json.getBool(obj, "Flight.DisableChangingFOV", cfg.disableChangingFovWhileFlying);
cfg.flyOnGroundInCreative = Json.getBool(obj, "Flight.FlyOnGround.Creative", cfg.flyOnGroundInCreative);
cfg.flightSpeedMpCreativeDefault = MathHelper.clamp(Json.getFloat(obj, "Flight.SpeedMp.Creative.Default", cfg.flightSpeedMpCreativeDefault), 0.25F, 8F);
cfg.flightSpeedMpCreativeSprinting = MathHelper.clamp(Json.getFloat(obj, "Flight.SpeedMp.Creative.Sprinting", cfg.flightSpeedMpCreativeSprinting), 0.25F, 8F);
cfg.flightSpeedMpSpectatorDefault = MathHelper.clamp(Json.getFloat(obj, "Flight.SpeedMp.Spectator.Default", cfg.flightSpeedMpSpectatorDefault), 0.25F, 8F);
cfg.flightSpeedMpSpectatorSprinting = MathHelper.clamp(Json.getFloat(obj, "Flight.SpeedMp.Spectator.Sprinting", cfg.flightSpeedMpSpectatorSprinting), 0.25F, 8F);
cfg.flightSpeedMpCreativeDefault = Json.getFloat(obj, "Flight.SpeedMp.Creative.Default", cfg.flightSpeedMpCreativeDefault, 0.25F, 8F);
cfg.flightSpeedMpCreativeSprinting = Json.getFloat(obj, "Flight.SpeedMp.Creative.Sprinting", cfg.flightSpeedMpCreativeSprinting, 0.25F, 8F);
cfg.flightSpeedMpSpectatorDefault = Json.getFloat(obj, "Flight.SpeedMp.Spectator.Default", cfg.flightSpeedMpSpectatorDefault, 0.25F, 8F);
cfg.flightSpeedMpSpectatorSprinting = Json.getFloat(obj, "Flight.SpeedMp.Spectator.Sprinting", cfg.flightSpeedMpSpectatorSprinting, 0.25F, 8F);
cfg.flightVerticalBoostCreativeDefault = Json.getFloat(obj, "Flight.VerticalBoost.Creative.Default", cfg.flightVerticalBoostCreativeDefault, 0F, 3F);
cfg.flightVerticalBoostCreativeSprinting = Json.getFloat(obj, "Flight.VerticalBoost.Creative.Sprinting", cfg.flightVerticalBoostCreativeSprinting, 0F, 3F);
cfg.flightVerticalBoostSpectatorDefault = Json.getFloat(obj, "Flight.VerticalBoost.Spectator.Default", cfg.flightVerticalBoostSpectatorDefault, 0F, 3F);
cfg.flightVerticalBoostSpectatorSprinting = Json.getFloat(obj, "Flight.VerticalBoost.Spectator.Sprinting", cfg.flightVerticalBoostSpectatorSprinting, 0F, 3F);
Json.readKeyBinding(obj, "Misc.KeyToggleWalkForward", cfg.keyToggleWalkForward);
Json.readKeyBinding(obj, "Misc.KeyToggleJump", cfg.keyToggleJump);
Json.readKeyBinding(obj, "Misc.KeyResetAllToggles", cfg.keyResetAllToggles);
Json.readKeyBinding(obj, "Misc.KeyOpenMenu", cfg.keyOpenMenu);
return cfg;

View File

@@ -1,26 +1,19 @@
package chylex.bettercontrols.config;
import chylex.bettercontrols.input.KeyBindingWithModifier;
import chylex.bettercontrols.input.ModifierKey;
import chylex.bettercontrols.util.Key;
import com.google.gson.JsonObject;
import net.minecraft.client.util.InputUtil;
final class Json{
private Json(){}
static void setInt(final JsonObject obj, final String key, final int value){
obj.addProperty(key, Integer.valueOf(value));
}
static int getInt(final JsonObject obj, final String key, final int defaultValue){
return obj.has(key) ? obj.get(key).getAsInt() : defaultValue;
}
static void setFloat(final JsonObject obj, final String key, final float value){
obj.addProperty(key, Float.valueOf(value));
}
static float getFloat(final JsonObject obj, final String key, final float defaultValue){
return obj.has(key) ? obj.get(key).getAsFloat() : defaultValue;
static float getFloat(final JsonObject obj, final String key, final float defaultValue, final float minValue, final float maxValue){
final float value = obj.has(key) ? obj.get(key).getAsFloat() : defaultValue;
return Math.max(minValue, Math.min(maxValue, value));
}
static void setBool(final JsonObject obj, final String key, final boolean value){
@@ -31,11 +24,35 @@ final class Json{
return obj.has(key) ? obj.get(key).getAsBoolean() : defaultValue;
}
static <T extends Enum<T>> void setEnum(final JsonObject obj, final String key, final T value){
obj.addProperty(key, value.name());
}
static <T extends Enum<T>> T getEnum(final JsonObject obj, final String key, final T defaultValue, final Class<T> enumClass){
if (!obj.has(key)){
return defaultValue;
}
final T[] constants = enumClass.getEnumConstants();
if (constants != null){
final String value = obj.get(key).getAsString();
for(final T constant : constants){
if (constant.name().equalsIgnoreCase(value)){
return constant;
}
}
}
return defaultValue;
}
private static final String KEY_SUFFIX = ".Key";
private static final String MOD_SUFFIX = ".Mod";
static void writeKeyBinding(final JsonObject obj, final String key, final KeyBindingWithModifier keyBinding){
obj.addProperty(key + KEY_SUFFIX, keyBinding.getBoundKeyTranslationKey());
obj.addProperty(key + KEY_SUFFIX, Key.writeBinding(keyBinding));
if (keyBinding.getModifier() != null){
obj.addProperty(key + MOD_SUFFIX, Integer.valueOf(keyBinding.getModifier().id));
@@ -44,7 +61,7 @@ final class Json{
static void readKeyBinding(final JsonObject obj, final String key, final KeyBindingWithModifier keyBinding){
if (obj.has(key + KEY_SUFFIX)){
keyBinding.setBoundKey(InputUtil.fromTranslationKey(obj.get(key + KEY_SUFFIX).getAsString()));
Key.readBinding(keyBinding, obj.get(key + KEY_SUFFIX).getAsString());
}
if (obj.has(key + MOD_SUFFIX)){

View File

@@ -9,17 +9,16 @@ import chylex.bettercontrols.gui.elements.Option;
import chylex.bettercontrols.gui.elements.TextWidget;
import chylex.bettercontrols.input.KeyBindingWithModifier;
import chylex.bettercontrols.input.ModifierKey;
import net.minecraft.client.MinecraftClient;
import chylex.bettercontrols.input.SprintMode;
import chylex.bettercontrols.util.Key;
import chylex.bettercontrols.util.LiteralText;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ScreenTexts;
import net.minecraft.client.gui.screen.options.GameOptionsScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import java.util.ArrayList;
import java.util.Arrays;
@@ -30,35 +29,43 @@ import static chylex.bettercontrols.gui.OptionListWidget.ROW_WIDTH;
import static chylex.bettercontrols.gui.OptionListWidget.col2;
import static chylex.bettercontrols.gui.OptionListWidget.col4;
import static chylex.bettercontrols.gui.elements.TextWidget.CENTER;
import static chylex.bettercontrols.util.LiteralText.text;
import static chylex.bettercontrols.util.Statics.OPTIONS;
public class BetterControlsScreen extends GameOptionsScreen{
public static final LiteralText TITLE = new LiteralText("Better Controls");
public static final LiteralText TITLE = text("Better Controls");
private static final int BOTTOM_PADDING = 3;
private static final int TEXT_PADDING_RIGHT = 4;
private static final int TITLE_MARGIN_TOP = 3;
private static final int ROW_HEIGHT = 22;
private final List<Option<SprintMode>> SPRINT_MODE_OPTIONS = Arrays.asList(
new Option<>(SprintMode.TAP_TO_START, text("Tap To Start Sprinting")),
new Option<>(SprintMode.TAP_TO_TOGGLE, text("Tap To Start / Stop Sprinting")),
new Option<>(SprintMode.HOLD, text("Hold To Sprint"))
);
// Options
private int generateSprintingOptions(int y, final List<Element> elements){
final BetterControlsConfig cfg = BetterControlsMod.config;
generateKeyBindingWithModifierOption(y, elements, Text.of("Toggle Sprint"), cfg.keyToggleSprint);
generateKeyBindingWithModifierOption(y, elements, text("Toggle Sprint"), cfg.keyToggleSprint);
y += ROW_HEIGHT;
generateLeftSideText(y, elements, Text.of("Double Tap 'Walk Forwards' To Sprint"));
generateLeftSideText(y, elements, text("Sprint Key Mode"));
elements.add(new CycleButtonWidget<>(col2(1), y, COL2_W, SPRINT_MODE_OPTIONS, cfg.sprintMode, value -> cfg.sprintMode = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, text("Double Tap 'Walk Forwards' To Sprint"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.doubleTapForwardToSprint, value -> cfg.doubleTapForwardToSprint = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, Text.of("Tap 'Sprint' While Sprinting To Stop"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.tapSprintKeyAgainToStopSprinting, value -> cfg.tapSprintKeyAgainToStopSprinting = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, Text.of("Resume Sprinting After Hitting Obstacle"));
generateLeftSideText(y, elements, text("Resume Sprinting After Hitting Obstacle"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.resumeSprintingAfterHittingObstacle, value -> cfg.resumeSprintingAfterHittingObstacle = value));
y += ROW_HEIGHT;
@@ -68,11 +75,11 @@ public class BetterControlsScreen extends GameOptionsScreen{
private int generateSneakingOptions(int y, final List<Element> elements){
final BetterControlsConfig cfg = BetterControlsMod.config;
generateKeyBindingWithModifierOption(y, elements, Text.of("Toggle Sneak"), cfg.keyToggleSneak);
generateKeyBindingWithModifierOption(y, elements, text("Toggle Sneak"), cfg.keyToggleSneak);
y += ROW_HEIGHT;
generateLeftSideText(y, elements, Text.of("Move Camera Smoothly"));
generateLeftSideText(y, elements, text("Move Camera Smoothly"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.sneakingMovesCameraSmoothly, value -> cfg.sneakingMovesCameraSmoothly = value));
y += ROW_HEIGHT;
@@ -84,44 +91,82 @@ public class BetterControlsScreen extends GameOptionsScreen{
final BetterControlsConfig cfg = BetterControlsMod.config;
final List<Option<Float>> flightSpeedOptions = Arrays.asList(
new Option<>(Float.valueOf(0.25F), Text.of("0.25x")),
new Option<>(Float.valueOf(0.50F), Text.of("0.5x")),
new Option<>(Float.valueOf(0.75F), Text.of("0.75x")),
new Option<>(Float.valueOf(1.00F), Text.of("1x")),
new Option<>(Float.valueOf(1.50F), Text.of("1.5x")),
new Option<>(Float.valueOf(2.00F), Text.of("2x")),
new Option<>(Float.valueOf(3.00F), Text.of("3x")),
new Option<>(Float.valueOf(4.00F), Text.of("4x")),
new Option<>(Float.valueOf(6.00F), Text.of("6x")),
new Option<>(Float.valueOf(8.00F), Text.of("8x"))
new Option<>(Float.valueOf(0.25F), text("0.25x")),
new Option<>(Float.valueOf(0.50F), text("0.5x")),
new Option<>(Float.valueOf(0.75F), text("0.75x")),
new Option<>(Float.valueOf(1.00F), text("1x")),
new Option<>(Float.valueOf(1.50F), text("1.5x")),
new Option<>(Float.valueOf(2.00F), text("2x")),
new Option<>(Float.valueOf(3.00F), text("3x")),
new Option<>(Float.valueOf(4.00F), text("4x")),
new Option<>(Float.valueOf(5.00F), text("5x")),
new Option<>(Float.valueOf(6.00F), text("6x")),
new Option<>(Float.valueOf(7.00F), text("7x")),
new Option<>(Float.valueOf(8.00F), text("8x"))
);
generateKeyBindingWithModifierOption(y, elements, Text.of("Toggle Flight (Creative)"), cfg.keyToggleFlight);
final List<Option<Float>> flightVerticalBoostOptions = Arrays.asList(
new Option<>(Float.valueOf(0.00F), text("None")),
new Option<>(Float.valueOf(0.25F), text("+25%")),
new Option<>(Float.valueOf(0.50F), text("+50%")),
new Option<>(Float.valueOf(0.75F), text("+75%")),
new Option<>(Float.valueOf(1.00F), text("+100%")),
new Option<>(Float.valueOf(1.50F), text("+150%")),
new Option<>(Float.valueOf(2.00F), text("+200%")),
new Option<>(Float.valueOf(2.50F), text("+250%")),
new Option<>(Float.valueOf(3.00F), text("+300%"))
);
generateKeyBindingWithModifierOption(y, elements, text("Toggle Flight (Creative)"), cfg.keyToggleFlight);
y += ROW_HEIGHT;
generateLeftSideText(y, elements, Text.of("Fly On Ground (Creative Mode)"));
generateLeftSideText(y, elements, text("Double Tap 'Jump' To Fly (Creative)"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.doubleTapJumpToToggleFlight, value -> cfg.doubleTapJumpToToggleFlight = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, text("Disable Flight Inertia"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.disableFlightInertia, value -> cfg.disableFlightInertia = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, text("Disable Field Of View Changing"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.disableChangingFovWhileFlying, value -> cfg.disableChangingFovWhileFlying = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, text("Fly On Ground (Creative Mode)"));
elements.add(new BooleanValueWidget(col2(1), y, COL2_W, cfg.flyOnGroundInCreative, value -> cfg.flyOnGroundInCreative = value));
y += ROW_HEIGHT * 4 / 3;
generateLeftSideText(y, elements, Text.of("Speed Multiplier (Creative)"));
elements.add(new DiscreteValueSliderWidget<>(col2(1), y, COL2_W, flightSpeedOptions, cfg.flightSpeedMpCreativeDefault, value -> cfg.flightSpeedMpCreativeDefault = value));
elements.add(new TextWidget(col4(2), y, COL4_W - TEXT_PADDING_RIGHT, text("Creative"), CENTER));
elements.add(new TextWidget(col4(3), y, COL4_W - TEXT_PADDING_RIGHT, text("Spectator"), CENTER));
y += ROW_HEIGHT * 7 / 8;
generateLeftSideText(y, elements, text("Speed Multiplier (Default)"));
elements.add(new DiscreteValueSliderWidget<>(col4(2), y, COL4_W, flightSpeedOptions, cfg.flightSpeedMpCreativeDefault, value -> cfg.flightSpeedMpCreativeDefault = value));
elements.add(new DiscreteValueSliderWidget<>(col4(3), y, COL4_W, flightSpeedOptions, cfg.flightSpeedMpSpectatorDefault, value -> cfg.flightSpeedMpSpectatorDefault = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, Text.of("Speed Multiplier (Creative + Sprinting)"));
elements.add(new DiscreteValueSliderWidget<>(col2(1), y, COL2_W, flightSpeedOptions, cfg.flightSpeedMpCreativeSprinting, value -> cfg.flightSpeedMpCreativeSprinting = value));
generateLeftSideText(y, elements, text("Speed Multiplier (Sprinting)"));
elements.add(new DiscreteValueSliderWidget<>(col4(2), y, COL4_W, flightSpeedOptions, cfg.flightSpeedMpCreativeSprinting, value -> cfg.flightSpeedMpCreativeSprinting = value));
elements.add(new DiscreteValueSliderWidget<>(col4(3), y, COL4_W, flightSpeedOptions, cfg.flightSpeedMpSpectatorSprinting, value -> cfg.flightSpeedMpSpectatorSprinting = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, Text.of("Speed Multiplier (Spectator)"));
elements.add(new DiscreteValueSliderWidget<>(col2(1), y, COL2_W, flightSpeedOptions, cfg.flightSpeedMpSpectatorDefault, value -> cfg.flightSpeedMpSpectatorDefault = value));
generateLeftSideText(y, elements, text("Vertical Speed Boost (Default)"));
elements.add(new DiscreteValueSliderWidget<>(col4(2), y, COL4_W, flightVerticalBoostOptions, cfg.flightVerticalBoostCreativeDefault, value -> cfg.flightVerticalBoostCreativeDefault = value));
elements.add(new DiscreteValueSliderWidget<>(col4(3), y, COL4_W, flightVerticalBoostOptions, cfg.flightVerticalBoostSpectatorDefault, value -> cfg.flightVerticalBoostSpectatorDefault = value));
y += ROW_HEIGHT;
generateLeftSideText(y, elements, Text.of("Speed Multiplier (Spectator + Sprinting)"));
elements.add(new DiscreteValueSliderWidget<>(col2(1), y, COL2_W, flightSpeedOptions, cfg.flightSpeedMpSpectatorSprinting, value -> cfg.flightSpeedMpSpectatorSprinting = value));
generateLeftSideText(y, elements, text("Vertical Speed Boost (Sprinting)"));
elements.add(new DiscreteValueSliderWidget<>(col4(2), y, COL4_W, flightVerticalBoostOptions, cfg.flightVerticalBoostCreativeSprinting, value -> cfg.flightVerticalBoostCreativeSprinting = value));
elements.add(new DiscreteValueSliderWidget<>(col4(3), y, COL4_W, flightVerticalBoostOptions, cfg.flightVerticalBoostSpectatorSprinting, value -> cfg.flightVerticalBoostSpectatorSprinting = value));
y += ROW_HEIGHT;
return y;
@@ -130,15 +175,19 @@ public class BetterControlsScreen extends GameOptionsScreen{
private int generateMiscellaneousOptions(int y, final List<Element> elements){
final BetterControlsConfig cfg = BetterControlsMod.config;
generateKeyBindingWithModifierOption(y, elements, Text.of("Toggle Walk Forwards"), cfg.keyToggleWalkForward);
generateKeyBindingWithModifierOption(y, elements, text("Toggle Walk Forwards"), cfg.keyToggleWalkForward);
y += ROW_HEIGHT;
generateKeyBindingWithModifierOption(y, elements, Text.of("Toggle Jump"), cfg.keyToggleJump);
generateKeyBindingWithModifierOption(y, elements, text("Toggle Jump"), cfg.keyToggleJump);
y += ROW_HEIGHT;
generateKeyBindingWithModifierOption(y, elements, text("Reset All Toggles"), cfg.keyResetAllToggles);
y += ROW_HEIGHT * 4 / 3;
generateKeyBindingWithModifierOption(y, elements, Text.of("Open Better Controls Menu"), cfg.keyOpenMenu);
generateKeyBindingWithModifierOption(y, elements, text("Open Better Controls Menu"), cfg.keyOpenMenu);
y += ROW_HEIGHT;
return y;
@@ -147,13 +196,13 @@ public class BetterControlsScreen extends GameOptionsScreen{
// Helpers
private static final List<Option<ModifierKey>> MODIFIER_OPTIONS = Arrays.asList(
new Option<>(null, Text.of("(No Modifier)")),
new Option<>(ModifierKey.CONTROL, Text.of("Control")),
new Option<>(ModifierKey.SHIFT, Text.of("Shift")),
new Option<>(ModifierKey.ALT, Text.of("Alt"))
new Option<>(null, text("(No Modifier)")),
new Option<>(ModifierKey.CONTROL, text("Control")),
new Option<>(ModifierKey.SHIFT, text("Shift")),
new Option<>(ModifierKey.ALT, text("Alt"))
);
private void generateKeyBindingWithModifierOption(final int y, final List<Element> elements, final Text text, final KeyBindingWithModifier binding){
private void generateKeyBindingWithModifierOption(final int y, final List<Element> elements, final LiteralText text, final KeyBindingWithModifier binding){
final CycleButtonWidget<ModifierKey> modifierButton = new CycleButtonWidget<>(col4(2), y, COL4_W, MODIFIER_OPTIONS, binding.getModifier(), binding::setModifier);
final KeyBindingWidget bindingButton = new KeyBindingWidget(col4(3), y, COL4_W, binding, this::startEditingKeyBinding);
bindingButton.linkButtonToBoundState(modifierButton);
@@ -164,7 +213,7 @@ public class BetterControlsScreen extends GameOptionsScreen{
allKeyBindings.add(bindingButton);
}
private static void generateLeftSideText(final int y, final List<Element> elements, final Text text){
private static void generateLeftSideText(final int y, final List<Element> elements, final LiteralText text){
elements.add(new TextWidget(col2(0), y, COL2_W - TEXT_PADDING_RIGHT, text));
}
@@ -175,7 +224,7 @@ public class BetterControlsScreen extends GameOptionsScreen{
private final List<KeyBindingWidget> allKeyBindings = new ArrayList<>();
public BetterControlsScreen(final Screen parentScreen){
super(parentScreen, MinecraftClient.getInstance().options, TITLE);
super(parentScreen, OPTIONS, TITLE);
}
@Override
@@ -185,20 +234,20 @@ public class BetterControlsScreen extends GameOptionsScreen{
final List<Element> elements = new ArrayList<>();
int y = 0;
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, Text.of("Sprinting"), CENTER));
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, text("Sprinting"), CENTER));
y = generateSprintingOptions(y + ROW_HEIGHT, elements) + TITLE_MARGIN_TOP;
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, Text.of("Sneaking"), CENTER));
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, text("Sneaking"), CENTER));
y = generateSneakingOptions(y + ROW_HEIGHT, elements) + TITLE_MARGIN_TOP;
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, Text.of("Flying"), CENTER));
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, text("Flying"), CENTER));
y = generateFlightOptions(y + ROW_HEIGHT, elements) + TITLE_MARGIN_TOP;
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, Text.of("Miscellaneous"), CENTER));
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, text("Miscellaneous"), CENTER));
y = generateMiscellaneousOptions(y + ROW_HEIGHT, elements) + TITLE_MARGIN_TOP;
addButton(new ButtonWidget(width / 2 - 99, height - 29, 200, 20, ScreenTexts.DONE, btn -> client.openScreen(parent)));
addChild(optionsWidget = new OptionListWidget(21, height - 32, width, height, elements, y - TITLE_MARGIN_TOP + BOTTOM_PADDING));
addButton(new ButtonWidget(width / 2 - 99, height - 29, 200, 20, I18n.translate("gui.done"), btn -> minecraft.openScreen(parent)));
children.add(optionsWidget = new OptionListWidget(21, height - 32, width, height, elements, y - TITLE_MARGIN_TOP + BOTTOM_PADDING));
}
@Override
@@ -207,11 +256,11 @@ public class BetterControlsScreen extends GameOptionsScreen{
}
@Override
public void render(final MatrixStack matrices, final int mouseX, final int mouseY, final float delta){
renderBackground(matrices);
optionsWidget.render(matrices, mouseX, mouseY, delta);
drawCenteredText(matrices, textRenderer, title, width / 2, 8, (255 << 16) | (255 << 8) | 255);
super.render(matrices, mouseX, mouseY, delta);
public void render(final int mouseX, final int mouseY, final float delta){
renderBackground();
optionsWidget.render(mouseX, mouseY, delta);
drawCenteredString(font, title.asFormattedString(), width / 2, 8, (255 << 16) | (255 << 8) | 255);
super.render(mouseX, mouseY, delta);
}
private void startEditingKeyBinding(final KeyBindingWidget widget){
@@ -225,7 +274,7 @@ public class BetterControlsScreen extends GameOptionsScreen{
@Override
public boolean mouseClicked(final double mouseX, final double mouseY, final int button){
if (editingKeyBinding != null){
editingKeyBinding.bindAndStopEditing(InputUtil.Type.MOUSE.createFromCode(button));
editingKeyBinding.bindAndStopEditing(Key.inputFromMouse(button));
onKeyBindingEditingFinished();
return true;
}
@@ -238,10 +287,10 @@ public class BetterControlsScreen extends GameOptionsScreen{
public boolean keyPressed(final int keyCode, final int scanCode, final int modifiers){
if (editingKeyBinding != null){
if (keyCode == GLFW.GLFW_KEY_ESCAPE){
editingKeyBinding.bindAndStopEditing(InputUtil.UNKNOWN_KEY);
editingKeyBinding.bindAndStopEditing(Key.INVALID);
}
else{
editingKeyBinding.bindAndStopEditing(InputUtil.fromKeyCode(keyCode, scanCode));
editingKeyBinding.bindAndStopEditing(Key.inputFromKeyboard(keyCode, scanCode));
}
onKeyBindingEditingFinished();

View File

@@ -1,43 +1,32 @@
package chylex.bettercontrols.gui;
import chylex.bettercontrols.gui.OptionListWidget.Entry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Drawable;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.widget.AbstractButtonWidget;
import net.minecraft.client.gui.widget.ElementListWidget;
import net.minecraft.client.util.math.MatrixStack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import static chylex.bettercontrols.util.Statics.MINECRAFT;
public final class OptionListWidget extends ElementListWidget<Entry>{
public static final int ROW_WIDTH = 408;
public static final int ROW_PADDING = 2;
public static final int COL2_W = (ROW_WIDTH / 2) - ROW_PADDING;
public static final int COL3_W = (ROW_WIDTH / 3) - ROW_PADDING;
public static final int COL4_W = (ROW_WIDTH / 4) - ROW_PADDING;
public static final int COL6_W = (ROW_WIDTH / 6) - ROW_PADDING;
public static int col2(final int column){
return (column * ROW_WIDTH) / 2;
}
public static int col3(final int column){
return (column * ROW_WIDTH) / 3;
}
public static int col4(final int column){
return (column * ROW_WIDTH) / 4;
}
public static int col6(final int column){
return (column * ROW_WIDTH) / 6;
}
private static Offset getElementOffset(final Element element){
if (element instanceof Widget){
return new Offset(((Widget)element).getX(), ((Widget)element).getY());
@@ -68,7 +57,7 @@ public final class OptionListWidget extends ElementListWidget<Entry>{
}
public OptionListWidget(final int top, final int bottom, final int width, final int height, final List<Element> widgets, final int innerHeight){
super(MinecraftClient.getInstance(), width, height, top, bottom, innerHeight);
super(MINECRAFT, width, height, top, bottom, innerHeight);
addEntry(new Entry(widgets));
}
@@ -83,7 +72,7 @@ public final class OptionListWidget extends ElementListWidget<Entry>{
}
@Override
protected int getScrollbarPositionX(){
protected int getScrollbarPosition(){
return (width + ROW_WIDTH) / 2 + 4;
}
@@ -102,7 +91,7 @@ public final class OptionListWidget extends ElementListWidget<Entry>{
}
@Override
public void render(final MatrixStack matrices, final int index, final int y, final int x, final int entryWidth, final int entryHeight, final int mouseX, final int mouseY, final boolean hovered, final float tickDelta){
public void render(final int index, final int y, final int x, final int entryWidth, final int entryHeight, final int mouseX, final int mouseY, final boolean hovered, final float tickDelta){
for(final Element element : elements){
final Offset offset = offsets.get(element);
@@ -118,7 +107,7 @@ public final class OptionListWidget extends ElementListWidget<Entry>{
}
if (element instanceof Drawable){
((Drawable)element).render(matrices, mouseX, mouseY, tickDelta);
((Drawable)element).render(mouseX, mouseY, tickDelta);
}
}
}

View File

@@ -1,15 +1,17 @@
package chylex.bettercontrols.gui;
import chylex.bettercontrols.mixin.AccessOptionButtonWidgetOption;
import chylex.bettercontrols.mixin.AccessScreenButtons;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.options.AccessibilityOptionsScreen;
import net.minecraft.client.gui.ParentElement;
import net.minecraft.client.gui.screen.options.AccessibilityScreen;
import net.minecraft.client.gui.screen.options.ControlsOptionsScreen;
import net.minecraft.client.gui.widget.AbstractButtonWidget;
import net.minecraft.client.gui.widget.ButtonListWidget;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.OptionButtonWidget;
import net.minecraft.client.options.Option;
import java.util.List;
import java.util.function.Consumer;
import static chylex.bettercontrols.util.Statics.MINECRAFT;
public final class ScreenPatcher{
private ScreenPatcher(){}
@@ -22,7 +24,7 @@ public final class ScreenPatcher{
final AbstractButtonWidget autoJump = buttons
.stream()
.filter(it -> it instanceof OptionButtonWidget && ((OptionButtonWidget)it).getOption() == Option.AUTO_JUMP)
.filter(it -> it instanceof OptionButtonWidget && ((AccessOptionButtonWidgetOption)it).getOption() == Option.AUTO_JUMP)
.findAny()
.orElse(null);
@@ -30,28 +32,32 @@ public final class ScreenPatcher{
children.remove(autoJump);
buttons.remove(autoJump);
accessor.callAddButton(new ButtonWidget(autoJump.x, autoJump.y, autoJump.getWidth(), autoJump.getHeight(), BetterControlsScreen.TITLE.copy().append("..."), btn -> {
MinecraftClient.getInstance().openScreen(new BetterControlsScreen(screen));
accessor.callAddButton(new ButtonWidget(autoJump.x, autoJump.y, autoJump.getWidth(), 20, BetterControlsScreen.TITLE.copy().append("...").asFormattedString(), btn -> {
MINECRAFT.openScreen(new BetterControlsScreen(screen));
}));
}
}
public static void onAccessibilityScreenOpened(final AccessibilityOptionsScreen screen){
final AccessScreenButtons accessor = (AccessScreenButtons)screen;
screen.children()
.stream()
.filter(it -> it instanceof ButtonListWidget)
.flatMap(it -> ((ButtonListWidget)it).children().stream())
.flatMap(it -> it.children().stream())
.filter(it -> it instanceof OptionButtonWidget)
.forEach(it -> {
public static void onAccessibilityScreenOpened(final AccessibilityScreen screen){
walkChildren(screen.children(), it -> {
if (it instanceof OptionButtonWidget){
final OptionButtonWidget button = (OptionButtonWidget)it;
final Option option = button.getOption();
final Option option = ((AccessOptionButtonWidgetOption)button).getOption();
if (option == Option.SPRINT_TOGGLED || option == Option.SNEAK_TOGGLED){
button.active = false;
}
});
}
});
}
private static void walkChildren(final List<? extends Element> elements, final Consumer<Element> callback){
for(final Element element : elements){
callback.accept(element);
if (element instanceof ParentElement){
walkChildren(((ParentElement)element).children(), callback);
}
}
}
}

View File

@@ -1,14 +1,14 @@
package chylex.bettercontrols.gui.elements;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.minecraft.client.gui.screen.ScreenTexts;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.resource.language.I18n;
public final class BooleanValueWidget extends ButtonWidget{
private final BooleanConsumer onChanged;
private boolean value;
public BooleanValueWidget(final int x, final int y, final int width, final int height, final boolean currentValue, final BooleanConsumer onChanged){
super(x, y, width, height, currentValue ? ScreenTexts.ON : ScreenTexts.OFF, ignore -> {});
super(x, y, width, height, I18n.translate(currentValue ? "options.on" : "options.off"), ignore -> {});
this.value = currentValue;
this.onChanged = onChanged;
}
@@ -21,7 +21,7 @@ public final class BooleanValueWidget extends ButtonWidget{
public void onPress(){
super.onPress();
value = !value;
setMessage(value ? ScreenTexts.ON : ScreenTexts.OFF);
setMessage(I18n.translate(value ? "options.on" : "options.off"));
onChanged.accept(value);
}
}

View File

@@ -9,7 +9,7 @@ public class CycleButtonWidget<T> extends ButtonWidget{
private T selectedValue;
public CycleButtonWidget(final int x, final int y, final int width, final int height, final List<Option<T>> options, final T selectedValue, final Consumer<T> onChanged){
super(x, y, width, height, Option.find(options, selectedValue).getText(), btn -> {});
super(x, y, width, height, Option.find(options, selectedValue).getText().asFormattedString(), btn -> {});
this.options = options;
this.selectedValue = selectedValue;
this.onChanged = onChanged;
@@ -31,6 +31,6 @@ public class CycleButtonWidget<T> extends ButtonWidget{
selectedValue = newSelectedOption.getValue();
onChanged.accept(selectedValue);
setMessage(newSelectedOption.getText());
setMessage(newSelectedOption.getText().asFormattedString());
}
}

View File

@@ -10,10 +10,11 @@ public final class DiscreteValueSliderWidget<T> extends SliderWidget{
private T selectedValue;
public DiscreteValueSliderWidget(final int x, final int y, final int width, final int height, final List<Option<T>> options, final T selectedValue, final Consumer<T> onChanged){
super(x, y, width, height, Option.find(options, selectedValue).getText(), options.indexOf(Option.find(options, selectedValue)) / (options.size() - 1.0));
super(x, y, width, height, options.indexOf(Option.find(options, selectedValue)) / (options.size() - 1.0));
this.options = options;
this.selectedValue = selectedValue;
this.onChanged = onChanged;
updateMessage();
}
public DiscreteValueSliderWidget(final int x, final int y, final int width, final List<Option<T>> options, final T selectedValue, final Consumer<T> onChanged){
@@ -26,7 +27,7 @@ public final class DiscreteValueSliderWidget<T> extends SliderWidget{
@Override
protected void updateMessage(){
setMessage(getSelectedOption().getText());
setMessage(getSelectedOption().getText().asFormattedString());
}
@Override

View File

@@ -1,21 +1,19 @@
package chylex.bettercontrols.gui.elements;
import net.minecraft.client.MinecraftClient;
import chylex.bettercontrols.util.Key;
import net.minecraft.client.gui.widget.AbstractButtonWidget;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import static chylex.bettercontrols.util.Statics.OPTIONS;
public final class KeyBindingWidget extends ButtonWidget{
private final KeyBinding binding;
private final Text bindingName;
private final String bindingName;
private final List<AbstractButtonWidget> linkedButtons = new ArrayList<>(1);
@@ -23,9 +21,9 @@ public final class KeyBindingWidget extends ButtonWidget{
private boolean isEditing;
public KeyBindingWidget(final int x, final int y, final int width, final int height, final KeyBinding binding, final Consumer<KeyBindingWidget> onEditingStarted){
super(x, y, width, height, LiteralText.EMPTY, btn -> {});
super(x, y, width, height, "", btn -> {});
this.binding = binding;
this.bindingName = new TranslatableText(binding.getTranslationKey());
this.bindingName = I18n.translate(binding.getId());
this.onEditingStarted = onEditingStarted;
updateKeyBindingText();
}
@@ -36,12 +34,12 @@ public final class KeyBindingWidget extends ButtonWidget{
public void linkButtonToBoundState(final AbstractButtonWidget button){
linkedButtons.add(button);
button.active = !binding.isUnbound();
button.active = !Key.isUnbound(binding);
}
@Override
protected MutableText getNarrationMessage(){
return binding.isUnbound() ? new TranslatableText("narrator.controls.unbound", bindingName) : new TranslatableText("narrator.controls.bound", bindingName, super.getNarrationMessage());
protected String getNarrationMessage(){
return Key.isUnbound(binding) ? I18n.translate("narrator.controls.unbound", bindingName) : I18n.translate("narrator.controls.bound", bindingName, super.getNarrationMessage());
}
@Override
@@ -51,12 +49,12 @@ public final class KeyBindingWidget extends ButtonWidget{
updateKeyBindingText();
}
public void bindAndStopEditing(final InputUtil.Key key){
binding.setBoundKey(key);
public void bindAndStopEditing(final InputUtil.KeyCode key){
Key.bind(binding, key);
stopEditing();
for(final AbstractButtonWidget button : linkedButtons){
button.active = !binding.isUnbound();
button.active = !Key.isUnbound(binding);
}
}
@@ -68,22 +66,23 @@ public final class KeyBindingWidget extends ButtonWidget{
public void updateKeyBindingText(){
boolean hasConflict = false;
if (!binding.isUnbound()){
for(final KeyBinding other : MinecraftClient.getInstance().options.keysAll){
if (!Key.isUnbound(binding)){
for(final KeyBinding other : OPTIONS.keysAll){
if (binding != other && binding.equals(other)){
hasConflict = true;
break;
}
}
}
if (isEditing){
setMessage((new LiteralText("> ")).append(binding.getBoundKeyLocalizedText().shallowCopy().formatted(Formatting.YELLOW)).append(" <").formatted(Formatting.YELLOW));
setMessage(Formatting.WHITE + "> " + Formatting.YELLOW + binding.getLocalizedName() + Formatting.WHITE + " <");
}
else if (hasConflict){
setMessage(binding.getBoundKeyLocalizedText().shallowCopy().formatted(Formatting.RED));
setMessage(Formatting.RED + Key.getBoundKeyText(binding));
}
else{
setMessage(binding.isUnbound() ? Text.of("(No Binding)") : binding.getBoundKeyLocalizedText());
setMessage(Key.isUnbound(binding) ? "(No Binding)" : Key.getBoundKeyText(binding));
}
}
}

View File

@@ -1,13 +1,13 @@
package chylex.bettercontrols.gui.elements;
import net.minecraft.text.Text;
import chylex.bettercontrols.util.LiteralText;
import java.util.List;
import java.util.Objects;
public final class Option<T>{
private final T value;
private final Text text;
private final LiteralText text;
public Option(final T value, final Text text){
public Option(final T value, final LiteralText text){
this.value = value;
this.text = text;
}
@@ -16,7 +16,7 @@ public final class Option<T>{
return value;
}
public Text getText(){
public LiteralText getText(){
return text;
}

View File

@@ -1,25 +1,23 @@
package chylex.bettercontrols.gui.elements;
import chylex.bettercontrols.gui.OptionListWidget.Widget;
import chylex.bettercontrols.util.LiteralText;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import java.util.List;
public final class TextWidget extends DrawableHelper implements Widget{
public static final int LEFT = 0;
public static final int CENTER = 1;
private final Text text;
private final LiteralText text;
private int x;
private int y;
private final int width;
private final int height;
private final int align;
public TextWidget(final int x, final int y, final int width, final int height, final Text text, final int align){
public TextWidget(final int x, final int y, final int width, final int height, final LiteralText text, final int align){
this.x = x;
this.y = y;
this.width = width;
@@ -28,11 +26,11 @@ public final class TextWidget extends DrawableHelper implements Widget{
this.align = align;
}
public TextWidget(final int x, final int y, final int width, final Text text, final int align){
public TextWidget(final int x, final int y, final int width, final LiteralText text, final int align){
this(x, y, width, 20, text, align);
}
public TextWidget(final int x, final int y, final int width, final Text text){
public TextWidget(final int x, final int y, final int width, final LiteralText text){
this(x, y, width, 20, text, LEFT);
}
@@ -57,17 +55,17 @@ public final class TextWidget extends DrawableHelper implements Widget{
}
@Override
public void render(final MatrixStack matrices, final int mouseX, final int mouseY, final float delta){
public void render(final int mouseX, final int mouseY, final float delta){
final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
final List<OrderedText> lines = textRenderer.wrapLines(text, width);
final List<String> lines = textRenderer.wrapStringToWidthAsList(text.asFormattedString(), width);
final int lineHeight = textRenderer.fontHeight + 1;
final int finalX = align == CENTER ? x + (width / 2) - (lines.stream().mapToInt(textRenderer::getWidth).max().orElse(0) / 2) : x;
final int finalX = align == CENTER ? x + (width / 2) - (lines.stream().mapToInt(textRenderer::getStringWidth).max().orElse(0) / 2) : x;
final int finalY = y + (height / 2) - (lineHeight * lines.size() / 2) + 1;
for(int i = 0; i < lines.size(); i++){
final OrderedText line = lines.get(i);
textRenderer.drawWithShadow(matrices, line, finalX, finalY + (i * lineHeight), (255 << 16) | (255 << 8) | 255);
final String line = lines.get(i);
textRenderer.drawWithShadow(line, finalX, finalY + (i * lineHeight), (255 << 16) | (255 << 8) | 255);
}
}
}

View File

@@ -1,15 +1,14 @@
package chylex.bettercontrols.input;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.InputUtil.Type;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import org.jetbrains.annotations.Nullable;
public class KeyBindingWithModifier extends KeyBinding{
public static final String CATEGORY = "key.categories.bettercontrols";
public static boolean checkCategoryMatches(final Text text){
return text instanceof TranslatableText && CATEGORY.equals(((TranslatableText)text).getKey());
public static boolean checkCategoryMatches(final String name){
return I18n.translate(CATEGORY).equals(name);
}
@Nullable
@@ -19,11 +18,12 @@ public class KeyBindingWithModifier extends KeyBinding{
super(translationKey, Type.KEYSYM, -1, CATEGORY);
}
public void setModifier(final @Nullable ModifierKey modifier){
public void setModifier(@Nullable final ModifierKey modifier){
this.modifier = modifier;
}
public @Nullable ModifierKey getModifier(){
@Nullable
public ModifierKey getModifier(){
return modifier;
}

View File

@@ -0,0 +1,7 @@
package chylex.bettercontrols.input;
public enum SprintMode{
TAP_TO_START,
TAP_TO_TOGGLE,
HOLD
}

View File

@@ -1,4 +1,5 @@
package chylex.bettercontrols.input;
import chylex.bettercontrols.util.Key;
import net.minecraft.client.options.KeyBinding;
public class ToggleTracker{
@@ -43,7 +44,7 @@ public class ToggleTracker{
public boolean tick(){
final boolean isHoldingReset = isResetKeyPressed();
if (bindingToggle.isPressed()){
if (Key.isPressed(bindingToggle)){
if (!waitForRelease){
if (skipNextToggle){
skipNextToggle = false;
@@ -78,6 +79,11 @@ public class ToggleTracker{
}
protected boolean isResetKeyPressed(){
return bindingReset.isPressed();
return Key.isPressed(bindingReset);
}
public void reset(){
isToggled = false;
waitForRelease = false;
}
}

View File

@@ -5,6 +5,6 @@ import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(ClientPlayerEntity.class)
public interface AccessClientPlayerFields{
@Accessor
@Accessor("field_3935")
void setTicksLeftToDoubleTapSprint(int value);
}

View File

@@ -1,11 +1,10 @@
package chylex.bettercontrols.mixin;
import net.minecraft.client.gui.screen.options.ControlsListWidget.CategoryEntry;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(CategoryEntry.class)
public interface AccessControlsListCategory{
@Accessor
Text getText();
String getName();
}

View File

@@ -0,0 +1,10 @@
package chylex.bettercontrols.mixin;
import net.minecraft.client.render.GameRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(GameRenderer.class)
public interface AccessGameRendererFields{
@Accessor
void setMovementFovMultiplier(float value);
}

View File

@@ -0,0 +1,11 @@
package chylex.bettercontrols.mixin;
import net.minecraft.client.gui.widget.OptionButtonWidget;
import net.minecraft.client.options.Option;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(OptionButtonWidget.class)
public interface AccessOptionButtonWidgetOption{
@Accessor
Option getOption();
}

View File

@@ -0,0 +1,10 @@
package chylex.bettercontrols.mixin;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(PlayerEntity.class)
public interface AccessPlayerFields{
@Accessor("field_7489")
void setTicksLeftToDoubleTapFlight(int value);
}

View File

@@ -0,0 +1,16 @@
package chylex.bettercontrols.mixin;
import net.minecraft.client.options.StickyKeyBinding;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;
import java.util.function.BooleanSupplier;
@Mixin(StickyKeyBinding.class)
public interface AccessStickyKeyBindingStateGetter{
@Accessor
BooleanSupplier getToggleGetter();
@Accessor
@Mutable
void setToggleGetter(final BooleanSupplier toggleGetter);
}

View File

@@ -1,12 +1,12 @@
package chylex.bettercontrols.mixin;
import chylex.bettercontrols.player.PlayerTicker;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.input.KeyboardInput;
import net.minecraft.client.network.ClientPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static chylex.bettercontrols.util.Statics.MINECRAFT;
import static org.spongepowered.asm.mixin.injection.At.Shift.AFTER;
@Mixin(KeyboardInput.class)
@@ -14,7 +14,7 @@ public abstract class HookClientPlayerInputTick{
@Inject(method = "tick(Z)V", at = @At(value = "FIELD", target = "Lnet/minecraft/client/input/KeyboardInput;pressingForward:Z", ordinal = 0, shift = AFTER))
private void afterInputTick(final CallbackInfo info){
final KeyboardInput input = (KeyboardInput)(Object)this;
final ClientPlayerEntity player = MinecraftClient.getInstance().player;
final ClientPlayerEntity player = MINECRAFT.player;
if (player != null){
PlayerTicker.get(player).afterInputAssignsPressingForward(input);

View File

@@ -23,7 +23,7 @@ public abstract class HookControlsListWidget extends ElementListWidget<Entry>{
@Inject(method = "<init>", at = @At("TAIL"))
public void init(final ControlsOptionsScreen parent, final MinecraftClient client, final CallbackInfo ci){
children().removeIf(it -> {
if (it instanceof CategoryEntry && KeyBindingWithModifier.checkCategoryMatches(((AccessControlsListCategory)it).getText())){
if (it instanceof CategoryEntry && KeyBindingWithModifier.checkCategoryMatches(((AccessControlsListCategory)it).getName())){
return true;
}

View File

@@ -2,26 +2,26 @@ package chylex.bettercontrols.mixin;
import chylex.bettercontrols.gui.ScreenPatcher;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.options.AccessibilityOptionsScreen;
import net.minecraft.client.gui.screen.options.AccessibilityScreen;
import net.minecraft.client.gui.screen.options.ControlsOptionsScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static chylex.bettercontrols.util.Statics.MINECRAFT;
@Mixin(value = MinecraftClient.class, priority = 100)
public abstract class HookOpenScreen{
@Inject(method = "openScreen(Lnet/minecraft/client/gui/screen/Screen;)V", at = @At("TAIL"))
private void openScreen(final Screen ignore, final CallbackInfo ci){
final MinecraftClient mc = MinecraftClient.getInstance();
final Screen screen = mc.currentScreen;
final Screen screen = MINECRAFT.currentScreen;
if (screen != null && !Screen.hasAltDown()){
if (screen.getClass() == ControlsOptionsScreen.class){
ScreenPatcher.onControlsScreenOpened((ControlsOptionsScreen)screen);
}
else if (screen.getClass() == AccessibilityOptionsScreen.class){
ScreenPatcher.onAccessibilityScreenOpened((AccessibilityOptionsScreen)screen);
else if (screen.getClass() == AccessibilityScreen.class){
ScreenPatcher.onAccessibilityScreenOpened((AccessibilityScreen)screen);
}
}
}

View File

@@ -0,0 +1,25 @@
package chylex.bettercontrols.mixin;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.Slice;
@Mixin(PlayerEntity.class)
public abstract class HookPlayerFlightSpeed extends LivingEntity{
protected HookPlayerFlightSpeed(final EntityType<? extends LivingEntity> type, final World world){
super(type, world);
}
@Redirect(
method = "travel",
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;isSprinting()Z"),
slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerAbilities;getFlySpeed()F"))
)
private boolean disableVanillaSprintBoost(final PlayerEntity player){
return false;
}
}

View File

@@ -7,28 +7,32 @@ final class FlightHelper{
private FlightHelper(){}
private static final float BASE_FLIGHT_SPEED = 0.05F;
private static final float BASE_FLIGHT_SPEED_SPRINT_MP_INV = 0.5F; // sprinting doubles speed in PlayerEntity.travel
private static final float BASE_VERTICAL_VELOCITY = 3F;
private static BetterControlsConfig cfg(){
return BetterControlsMod.config;
}
static boolean isFlyingCreativeOrSpectator(final ClientPlayerEntity player){
return player.abilities.flying && (player.isCreative() || player.isSpectator());
}
static boolean shouldFlyOnGround(final ClientPlayerEntity player){
return cfg().flyOnGroundInCreative && player.isCreative() && player.abilities.flying;
}
static float getFlightSpeed(final ClientPlayerEntity player){
static float getFlightSpeed(final ClientPlayerEntity player, final boolean boost){
if (player.isCreative()){
if (player.isSprinting()){
return BASE_FLIGHT_SPEED * cfg().flightSpeedMpCreativeSprinting * BASE_FLIGHT_SPEED_SPRINT_MP_INV;
if (boost){
return BASE_FLIGHT_SPEED * cfg().flightSpeedMpCreativeSprinting;
}
else{
return BASE_FLIGHT_SPEED * cfg().flightSpeedMpCreativeDefault;
}
}
else if (player.isSpectator()){
if (player.isSprinting()){
return BASE_FLIGHT_SPEED * cfg().flightSpeedMpSpectatorSprinting * BASE_FLIGHT_SPEED_SPRINT_MP_INV;
if (boost){
return BASE_FLIGHT_SPEED * cfg().flightSpeedMpSpectatorSprinting;
}
else{
return BASE_FLIGHT_SPEED * cfg().flightSpeedMpSpectatorDefault;
@@ -38,4 +42,26 @@ final class FlightHelper{
return 0F;
}
}
static float getExtraVerticalVelocity(final ClientPlayerEntity player, final boolean isSprinting){
if (player.isCreative()){
if (isSprinting){
return BASE_VERTICAL_VELOCITY * cfg().flightVerticalBoostCreativeSprinting;
}
else{
return BASE_VERTICAL_VELOCITY * cfg().flightVerticalBoostCreativeDefault;
}
}
else if (player.isSpectator()){
if (isSprinting){
return BASE_VERTICAL_VELOCITY * cfg().flightVerticalBoostSpectatorSprinting;
}
else{
return BASE_VERTICAL_VELOCITY * cfg().flightVerticalBoostSpectatorDefault;
}
}
else{
return 0F;
}
}
}

View File

@@ -2,15 +2,26 @@ package chylex.bettercontrols.player;
import chylex.bettercontrols.BetterControlsMod;
import chylex.bettercontrols.config.BetterControlsConfig;
import chylex.bettercontrols.gui.BetterControlsScreen;
import chylex.bettercontrols.input.SprintMode;
import chylex.bettercontrols.input.ToggleTracker;
import chylex.bettercontrols.input.ToggleTrackerForStickyKey;
import chylex.bettercontrols.mixin.AccessCameraFields;
import chylex.bettercontrols.mixin.AccessClientPlayerFields;
import net.minecraft.client.MinecraftClient;
import chylex.bettercontrols.mixin.AccessGameRendererFields;
import chylex.bettercontrols.mixin.AccessPlayerFields;
import chylex.bettercontrols.mixin.AccessStickyKeyBindingStateGetter;
import chylex.bettercontrols.util.Key;
import net.minecraft.client.input.Input;
import net.minecraft.client.input.KeyboardInput;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.options.GameOptions;
import java.lang.ref.WeakReference;
import java.util.function.BooleanSupplier;
import static chylex.bettercontrols.util.Statics.KEY_FORWARD;
import static chylex.bettercontrols.util.Statics.KEY_JUMP;
import static chylex.bettercontrols.util.Statics.KEY_SNEAK;
import static chylex.bettercontrols.util.Statics.KEY_SPRINT;
import static chylex.bettercontrols.util.Statics.MINECRAFT;
import static chylex.bettercontrols.util.Statics.OPTIONS;
public final class PlayerTicker{
private static PlayerTicker ticker = new PlayerTicker(null);
@@ -23,10 +34,6 @@ public final class PlayerTicker{
return ticker;
}
private static MinecraftClient mc(){
return MinecraftClient.getInstance();
}
private static BetterControlsConfig cfg(){
return BetterControlsMod.config;
}
@@ -35,47 +42,80 @@ public final class PlayerTicker{
private PlayerTicker(final ClientPlayerEntity player){
this.ref = new WeakReference<>(player);
setup();
}
// Logic
private final ToggleTracker toggleSprint = new ToggleTrackerForStickyKey(cfg().keyToggleSprint, mc().options.keySprint, toggled -> mc().options.sprintToggled = toggled);
private final ToggleTracker toggleSneak = new ToggleTrackerForStickyKey(cfg().keyToggleSneak, mc().options.keySneak, toggled -> mc().options.sneakToggled = toggled);
private final ToggleTracker toggleWalkForward = new ToggleTracker(cfg().keyToggleWalkForward, mc().options.keyForward);
private final ToggleTracker toggleJump = new ToggleTracker(cfg().keyToggleJump, mc().options.keyJump);
private final ToggleTracker toggleSprint = new ToggleTrackerForStickyKey(cfg().keyToggleSprint, KEY_SPRINT, toggled -> OPTIONS.sprintToggled = toggled);
private final ToggleTracker toggleSneak = new ToggleTrackerForStickyKey(cfg().keyToggleSneak, KEY_SNEAK, toggled -> OPTIONS.sneakToggled = toggled);
private final ToggleTracker toggleWalkForward = new ToggleTracker(cfg().keyToggleWalkForward, KEY_FORWARD);
private final ToggleTracker toggleJump = new ToggleTracker(cfg().keyToggleJump, KEY_JUMP);
private boolean waitingForSprintKeyRelease = false;
private boolean stopSprintingAfterReleasingSprintKey = false;
private boolean wasHittingObstacle = false;
private boolean wasSprintingBeforeHittingObstacle = false;
private int temporarySprintTimer = 0;
private boolean wasSneakingBeforeTouchingGround = false;
private boolean holdingSneakWhileTouchingGround = false;
private int temporaryFlyOnGroundTimer = 0;
private void setup(){
final AccessStickyKeyBindingStateGetter sprint = (AccessStickyKeyBindingStateGetter)KEY_SPRINT;
BooleanSupplier getter = sprint.getToggleGetter();
if (getter instanceof SprintPressGetter){
getter = ((SprintPressGetter)getter).getWrapped();
}
sprint.setToggleGetter(new SprintPressGetter(getter, () -> temporarySprintTimer > 0));
}
public void atHead(final ClientPlayerEntity player){
if (FlightHelper.shouldFlyOnGround(player)){
player.setOnGround(false);
player.onGround = false;
}
if (!cfg().doubleTapForwardToSprint){
((AccessClientPlayerFields)player).setTicksLeftToDoubleTapSprint(0);
}
final GameOptions opts = mc().options;
final boolean wasSprintToggled = opts.sprintToggled;
if (!cfg().doubleTapJumpToToggleFlight){
((AccessPlayerFields)player).setTicksLeftToDoubleTapFlight(0);
}
final SprintMode sprintMode = cfg().sprintMode;
final boolean wasSprintToggled = OPTIONS.sprintToggled;
final boolean isSprintToggled = toggleSprint.tick();
if (temporarySprintTimer > 0){
stopSprintingAfterReleasingSprintKey = false;
waitingForSprintKeyRelease = false;
final int nextTemporarySprintTimer = temporarySprintTimer - 1;
temporarySprintTimer = 0;
if (!Key.isPressed(KEY_SPRINT) && Key.isPressed(KEY_FORWARD)){
temporarySprintTimer = nextTemporarySprintTimer;
}
else if (sprintMode == SprintMode.TAP_TO_TOGGLE){
stopSprintingAfterReleasingSprintKey = true;
}
}
if (isSprintToggled){
stopSprintingAfterReleasingSprintKey = false;
waitingForSprintKeyRelease = false;
}
else if (wasSprintToggled){
stopSprintingAfterReleasingSprintKey = true;
waitingForSprintKeyRelease = false;
waitingForSprintKeyRelease = true;
}
else if (cfg().tapSprintKeyAgainToStopSprinting){
if (opts.keySprint.isPressed()){
else if (sprintMode == SprintMode.TAP_TO_TOGGLE){
if (Key.isPressed(KEY_SPRINT)){
if (!waitingForSprintKeyRelease){
waitingForSprintKeyRelease = true;
stopSprintingAfterReleasingSprintKey = player.isSprinting();
@@ -89,9 +129,15 @@ public final class PlayerTicker{
waitingForSprintKeyRelease = false;
}
}
else if (sprintMode == SprintMode.HOLD){
if (Key.isPressed(KEY_SPRINT)){
stopSprintingAfterReleasingSprintKey = true;
}
}
if (stopSprintingAfterReleasingSprintKey && !opts.keySprint.isPressed()){
if (stopSprintingAfterReleasingSprintKey && !Key.isPressed(KEY_SPRINT)){
stopSprintingAfterReleasingSprintKey = false;
waitingForSprintKeyRelease = false;
player.setSprinting(false);
}
@@ -99,30 +145,52 @@ public final class PlayerTicker{
}
public void afterInputAssignsPressingForward(final KeyboardInput input){
if (mc().currentScreen == null){
if (MINECRAFT.currentScreen == null){
input.pressingForward |= toggleWalkForward.tick();
}
}
public void afterInputTick(final ClientPlayerEntity player){
if (mc().currentScreen == null && !player.abilities.flying){
player.input.jumping |= toggleJump.tick();
final Input input = player.input;
if (MINECRAFT.currentScreen == null && !player.abilities.flying){
input.jumping |= toggleJump.tick();
}
final float flightSpeed = FlightHelper.getFlightSpeed(player);
if (FlightHelper.isFlyingCreativeOrSpectator(player)){
final boolean boost = Key.isPressed(KEY_SPRINT);
final float flightSpeed = FlightHelper.getFlightSpeed(player, boost);
final float verticalVelocity = FlightHelper.getExtraVerticalVelocity(player, boost);
if (flightSpeed > 0F){
player.abilities.setFlySpeed(flightSpeed);
if (flightSpeed > 0F){
player.abilities.setFlySpeed(flightSpeed);
}
if (Math.abs(verticalVelocity) > 1E-5F && player == MINECRAFT.getCameraEntity()){
int direction = 0;
if (input.sneaking){
--direction;
}
if (input.jumping){
++direction;
}
if (direction != 0){
player.setVelocity(player.getVelocity().add(0D, flightSpeed * verticalVelocity * direction, 0D));
}
}
}
if (cfg().resumeSprintingAfterHittingObstacle){
if (wasHittingObstacle != player.horizontalCollision){
if (!wasHittingObstacle){
wasSprintingBeforeHittingObstacle = player.isSprinting() || mc().options.keySprint.isPressed();
wasSprintingBeforeHittingObstacle = player.isSprinting() || Key.isPressed(KEY_SPRINT);
}
else if (wasSprintingBeforeHittingObstacle){
wasSprintingBeforeHittingObstacle = false;
player.setSprinting(true);
temporarySprintTimer = 10;
}
// collision also stops when the player lets go of movement keys
@@ -133,12 +201,16 @@ public final class PlayerTicker{
wasHittingObstacle = player.horizontalCollision;
wasSprintingBeforeHittingObstacle = false;
}
if (cfg().disableChangingFovWhileFlying && FlightHelper.isFlyingCreativeOrSpectator(player)){
((AccessGameRendererFields)MINECRAFT.gameRenderer).setMovementFovMultiplier(1F);
}
}
public void afterSuperCall(final ClientPlayerEntity player){
if (FlightHelper.shouldFlyOnGround(player)){
final boolean isSneaking = player.isSneaking();
final boolean isOnGround = player.isOnGround();
final boolean isOnGround = player.onGround;
if (!isSneaking){
wasSneakingBeforeTouchingGround = false;
@@ -165,7 +237,7 @@ public final class PlayerTicker{
}
if (cancelLanding){
player.setOnGround(false);
player.onGround = false;
}
}
}
@@ -174,8 +246,20 @@ public final class PlayerTicker{
holdingSneakWhileTouchingGround = false;
}
if (FlightHelper.isFlyingCreativeOrSpectator(player) && cfg().disableFlightInertia){
final Input input = player.input;
if (input.movementForward == 0F && input.movementSideways == 0F){
player.setVelocity(player.getVelocity().multiply(0.0, 1.0, 0.0));
}
if (!input.jumping && !input.sneaking){
player.setVelocity(player.getVelocity().multiply(1.0, 0.0, 1.0));
}
}
if (player.isCreative()){
if (cfg().keyToggleFlight.wasPressed()){
if (Key.wasPressed(cfg().keyToggleFlight)){
final boolean isFlying = !player.abilities.flying;
player.abilities.flying = isFlying;
@@ -192,7 +276,7 @@ public final class PlayerTicker{
}
else{
--temporaryFlyOnGroundTimer;
player.setOnGround(false);
player.onGround = false;
}
}
}
@@ -201,15 +285,22 @@ public final class PlayerTicker{
}
if (!cfg().sneakingMovesCameraSmoothly){
final AccessCameraFields camera = (AccessCameraFields)mc().gameRenderer.getCamera();
final AccessCameraFields camera = (AccessCameraFields)MINECRAFT.gameRenderer.getCamera();
if (camera.getFocusedEntity() == player){
camera.setCameraY(player.getStandingEyeHeight());
}
}
if (cfg().keyOpenMenu.isPressed()){
mc().openScreen(new BetterControlsScreen(null));
if (Key.wasPressed(cfg().keyResetAllToggles)){
toggleSprint.reset();
toggleSneak.reset();
toggleWalkForward.reset();
toggleJump.reset();
}
if (Key.isPressed(cfg().keyOpenMenu)){
MINECRAFT.openScreen(new BetterControlsScreen(null));
}
}
}

View File

@@ -0,0 +1,21 @@
package chylex.bettercontrols.player;
import java.util.function.BooleanSupplier;
final class SprintPressGetter implements BooleanSupplier{
private final BooleanSupplier wrapped;
private final BooleanSupplier or;
public SprintPressGetter(final BooleanSupplier wrapped, final BooleanSupplier or){
this.wrapped = wrapped;
this.or = or;
}
public BooleanSupplier getWrapped(){
return wrapped;
}
@Override
public boolean getAsBoolean(){
return wrapped.getAsBoolean() || or.getAsBoolean();
}
}

View File

@@ -0,0 +1,45 @@
package chylex.bettercontrols.util;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
public final class Key{
private Key(){}
public static final InputUtil.KeyCode INVALID = InputUtil.UNKNOWN_KEYCODE;
public static boolean isUnbound(final KeyBinding binding){
return binding.isNotBound();
}
public static boolean isPressed(final KeyBinding binding){
return binding.isPressed();
}
public static boolean wasPressed(final KeyBinding binding){
return binding.wasPressed();
}
public static String getBoundKeyText(final KeyBinding binding){
return binding.getLocalizedName();
}
public static void bind(final KeyBinding binding, final InputUtil.KeyCode input){
binding.setKeyCode(input);
}
public static String writeBinding(final KeyBinding binding){
return binding.getName();
}
public static void readBinding(final KeyBinding binding, final String serialized){
bind(binding, InputUtil.fromName(serialized));
}
public static InputUtil.KeyCode inputFromMouse(final int button){
return InputUtil.Type.MOUSE.createFromCode(button);
}
public static InputUtil.KeyCode inputFromKeyboard(final int keyCode, final int scanCode){
return InputUtil.getKeyCode(keyCode, scanCode);
}
}

View File

@@ -0,0 +1,11 @@
package chylex.bettercontrols.util;
public final class LiteralText extends net.minecraft.text.LiteralText{
public static LiteralText text(final String text){
return new LiteralText(text);
}
public LiteralText(final String msg){
super(msg);
}
}

View File

@@ -0,0 +1,16 @@
package chylex.bettercontrols.util;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.GameOptions;
import net.minecraft.client.options.KeyBinding;
public final class Statics{
private Statics(){}
public static final MinecraftClient MINECRAFT = MinecraftClient.getInstance();
public static final GameOptions OPTIONS = MINECRAFT.options;
public static final KeyBinding KEY_SPRINT = OPTIONS.keySprint;
public static final KeyBinding KEY_SNEAK = OPTIONS.keySneak;
public static final KeyBinding KEY_FORWARD = OPTIONS.keyForward;
public static final KeyBinding KEY_JUMP = OPTIONS.keyJump;
}

View File

@@ -4,6 +4,8 @@ import java.util.Map;
@SuppressWarnings("unused")
public interface ModMenuApi{
String getModId();
default ConfigScreenFactory<?> getModConfigScreenFactory(){
return ignore -> null;
}

View File

@@ -5,5 +5,6 @@
"key.bettercontrols.toggle_flight": "Toggle Flight",
"key.bettercontrols.toggle_forward": "Toggle Walk Forwards",
"key.bettercontrols.toggle_jump": "Toggle Jump",
"key.bettercontrols.reset_all_toggles": "Reset All Toggles",
"key.bettercontrols.open_menu": "Open Menu"
}

View File

@@ -8,13 +8,18 @@
"AccessClientPlayerFields",
"AccessControlsListCategory",
"AccessControlsListKeyBinding",
"AccessGameRendererFields",
"AccessKeyBindingFields",
"AccessOptionButtonWidgetOption",
"AccessPlayerFields",
"AccessScreenButtons",
"AccessStickyKeyBindingStateGetter",
"HookClientPlayerInputTick",
"HookClientPlayerTick",
"HookControlsListWidget",
"HookLoadGameOptions",
"HookOpenScreen",
"HookPlayerFlightSpeed",
"HookStickyKeyBindingState"
],
"injectors": {

View File

@@ -28,6 +28,6 @@
"depends": {
"fabricloader": ">=0.7.4",
"minecraft": ">=1.16.2"
"minecraft": "1.15.x"
}
}