mirror of
https://github.com/chylex/Better-Controls.git
synced 2025-04-22 00:15:46 +02:00
Remove some utility classes & unify with Fabric codebase
This commit is contained in:
parent
25c7565a49
commit
fd391e81a0
src/main
java/chylex/bettercontrols
config
gui
input
player
util
resources
@ -45,7 +45,7 @@ public final class BetterControlsConfig {
|
||||
}
|
||||
|
||||
public KeyBindingWithModifier[] getAllKeyBindings() {
|
||||
return new KeyBindingWithModifier[]{
|
||||
return new KeyBindingWithModifier[] {
|
||||
keyToggleSprint,
|
||||
keyToggleSneak,
|
||||
keyToggleFlight,
|
||||
|
@ -1,8 +1,8 @@
|
||||
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 com.mojang.blaze3d.platform.InputConstants;
|
||||
|
||||
final class Json {
|
||||
private Json() {}
|
||||
@ -52,7 +52,7 @@ final class Json {
|
||||
private static final String MOD_SUFFIX = ".Mod";
|
||||
|
||||
static void writeKeyBinding(final JsonObject obj, final String key, final KeyBindingWithModifier keyBinding) {
|
||||
obj.addProperty(key + KEY_SUFFIX, Key.writeBinding(keyBinding));
|
||||
obj.addProperty(key + KEY_SUFFIX, keyBinding.saveString());
|
||||
|
||||
if (keyBinding.getModifier() != null) {
|
||||
obj.addProperty(key + MOD_SUFFIX, Integer.valueOf(keyBinding.getModifier().id));
|
||||
@ -61,7 +61,11 @@ final class Json {
|
||||
|
||||
static void readKeyBinding(final JsonObject obj, final String key, final KeyBindingWithModifier keyBinding) {
|
||||
if (obj.has(key + KEY_SUFFIX)) {
|
||||
Key.readBinding(keyBinding, obj.get(key + KEY_SUFFIX).getAsString());
|
||||
try {
|
||||
keyBinding.setKey(InputConstants.getKey(obj.get(key + KEY_SUFFIX).getAsString()));
|
||||
} catch (final IllegalArgumentException e) {
|
||||
e.printStackTrace(); // let's not crash if the config file has garbage, okay?
|
||||
}
|
||||
}
|
||||
|
||||
if (obj.has(key + MOD_SUFFIX)) {
|
||||
|
@ -10,8 +10,7 @@ import chylex.bettercontrols.gui.elements.TextWidget;
|
||||
import chylex.bettercontrols.input.KeyBindingWithModifier;
|
||||
import chylex.bettercontrols.input.ModifierKey;
|
||||
import chylex.bettercontrols.input.SprintMode;
|
||||
import chylex.bettercontrols.util.Key;
|
||||
import chylex.bettercontrols.util.LiteralText;
|
||||
import com.mojang.blaze3d.platform.InputConstants;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.client.gui.components.Button;
|
||||
@ -19,6 +18,8 @@ import net.minecraft.client.gui.components.events.GuiEventListener;
|
||||
import net.minecraft.client.gui.screens.OptionsSubScreen;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.network.chat.CommonComponents;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.TextComponent;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
@ -30,11 +31,14 @@ 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 OptionsSubScreen {
|
||||
public static final LiteralText TITLE = text("Better Controls");
|
||||
private static TextComponent text(final String text) {
|
||||
return new TextComponent(text);
|
||||
}
|
||||
|
||||
public static final Component TITLE = text("Better Controls");
|
||||
|
||||
private static final int BOTTOM_PADDING = 3;
|
||||
private static final int TEXT_PADDING_RIGHT = 4;
|
||||
@ -203,7 +207,7 @@ public class BetterControlsScreen extends OptionsSubScreen {
|
||||
new Option<>(ModifierKey.ALT, text("Alt"))
|
||||
);
|
||||
|
||||
private void generateKeyBindingWithModifierOption(final int y, final List<GuiEventListener> elements, final LiteralText text, final KeyBindingWithModifier binding) {
|
||||
private void generateKeyBindingWithModifierOption(final int y, final List<GuiEventListener> elements, final Component 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, text, binding, this::startEditingKeyBinding);
|
||||
bindingButton.linkButtonToBoundState(modifierButton);
|
||||
@ -214,7 +218,7 @@ public class BetterControlsScreen extends OptionsSubScreen {
|
||||
allKeyBindings.add(bindingButton);
|
||||
}
|
||||
|
||||
private static void generateLeftSideText(final int y, final List<GuiEventListener> elements, final LiteralText text) {
|
||||
private static void generateLeftSideText(final int y, final List<GuiEventListener> elements, final Component text) {
|
||||
elements.add(new TextWidget(col2(0), y, COL2_W - TEXT_PADDING_RIGHT, text));
|
||||
}
|
||||
|
||||
@ -275,7 +279,7 @@ public class BetterControlsScreen extends OptionsSubScreen {
|
||||
@Override
|
||||
public boolean mouseClicked(final double mouseX, final double mouseY, final int button) {
|
||||
if (editingKeyBinding != null) {
|
||||
editingKeyBinding.bindAndStopEditing(Key.inputFromMouse(button));
|
||||
editingKeyBinding.bindAndStopEditing(InputConstants.Type.MOUSE.getOrCreate(button));
|
||||
onKeyBindingEditingFinished();
|
||||
return true;
|
||||
}
|
||||
@ -288,10 +292,10 @@ public class BetterControlsScreen extends OptionsSubScreen {
|
||||
public boolean keyPressed(final int keyCode, final int scanCode, final int modifiers) {
|
||||
if (editingKeyBinding != null) {
|
||||
if (keyCode == GLFW.GLFW_KEY_ESCAPE) {
|
||||
editingKeyBinding.bindAndStopEditing(Key.INVALID);
|
||||
editingKeyBinding.bindAndStopEditing(InputConstants.UNKNOWN);
|
||||
}
|
||||
else {
|
||||
editingKeyBinding.bindAndStopEditing(Key.inputFromKeyboard(keyCode, scanCode));
|
||||
editingKeyBinding.bindAndStopEditing(InputConstants.getKey(keyCode, scanCode));
|
||||
}
|
||||
|
||||
onKeyBindingEditingFinished();
|
||||
|
@ -1,6 +1,4 @@
|
||||
package chylex.bettercontrols.gui.elements;
|
||||
import chylex.bettercontrols.util.Key;
|
||||
import chylex.bettercontrols.util.LiteralText;
|
||||
import com.mojang.blaze3d.platform.InputConstants;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
@ -24,7 +22,7 @@ public final class KeyBindingWidget extends Button {
|
||||
private final Consumer<KeyBindingWidget> onEditingStarted;
|
||||
private boolean isEditing;
|
||||
|
||||
public KeyBindingWidget(final int x, final int y, final int width, final int height, final LiteralText bindingName, final KeyMapping binding, final Consumer<KeyBindingWidget> onEditingStarted) {
|
||||
public KeyBindingWidget(final int x, final int y, final int width, final int height, final Component bindingName, final KeyMapping binding, final Consumer<KeyBindingWidget> onEditingStarted) {
|
||||
super(x, y, width, height, TextComponent.EMPTY, btn -> {});
|
||||
this.binding = binding;
|
||||
this.bindingName = bindingName;
|
||||
@ -32,18 +30,18 @@ public final class KeyBindingWidget extends Button {
|
||||
updateKeyBindingText();
|
||||
}
|
||||
|
||||
public KeyBindingWidget(final int x, final int y, final int width, final LiteralText bindingName, final KeyMapping binding, final Consumer<KeyBindingWidget> onEditingStarted) {
|
||||
public KeyBindingWidget(final int x, final int y, final int width, final Component bindingName, final KeyMapping binding, final Consumer<KeyBindingWidget> onEditingStarted) {
|
||||
this(x, y, width, 20, bindingName, binding, onEditingStarted);
|
||||
}
|
||||
|
||||
public void linkButtonToBoundState(final AbstractButton button) {
|
||||
linkedButtons.add(button);
|
||||
button.active = !Key.isUnbound(binding);
|
||||
button.active = !binding.isUnbound();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MutableComponent createNarrationMessage() {
|
||||
return Key.isUnbound(binding) ? new TranslatableComponent("narrator.controls.unbound", bindingName) : new TranslatableComponent("narrator.controls.bound", bindingName, super.createNarrationMessage());
|
||||
return binding.isUnbound() ? new TranslatableComponent("narrator.controls.unbound", bindingName) : new TranslatableComponent("narrator.controls.bound", bindingName, super.createNarrationMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -54,11 +52,11 @@ public final class KeyBindingWidget extends Button {
|
||||
}
|
||||
|
||||
public void bindAndStopEditing(final InputConstants.Key key) {
|
||||
Key.bind(binding, key);
|
||||
binding.setKey(key);
|
||||
stopEditing();
|
||||
|
||||
for (final AbstractButton button : linkedButtons) {
|
||||
button.active = !Key.isUnbound(binding);
|
||||
button.active = !binding.isUnbound();
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,7 +68,7 @@ public final class KeyBindingWidget extends Button {
|
||||
public void updateKeyBindingText() {
|
||||
boolean hasConflict = false;
|
||||
|
||||
if (!Key.isUnbound(binding)) {
|
||||
if (!binding.isUnbound()) {
|
||||
for (final KeyMapping other : OPTIONS.keyMappings) {
|
||||
if (binding != other && binding.equals(other)) {
|
||||
hasConflict = true;
|
||||
@ -80,13 +78,13 @@ public final class KeyBindingWidget extends Button {
|
||||
}
|
||||
|
||||
if (isEditing) {
|
||||
setMessage((new TextComponent("> ")).append(Key.getBoundKeyText(binding).copy().withStyle(ChatFormatting.YELLOW)).append(" <").withStyle(ChatFormatting.YELLOW));
|
||||
setMessage((new TextComponent("> ")).append(binding.getTranslatedKeyMessage().copy().withStyle(ChatFormatting.YELLOW)).append(" <").withStyle(ChatFormatting.YELLOW));
|
||||
}
|
||||
else if (hasConflict) {
|
||||
setMessage(Key.getBoundKeyText(binding).copy().withStyle(ChatFormatting.RED));
|
||||
setMessage(binding.getTranslatedKeyMessage().copy().withStyle(ChatFormatting.RED));
|
||||
}
|
||||
else {
|
||||
setMessage(Key.isUnbound(binding) ? new TextComponent("(No Binding)") : Key.getBoundKeyText(binding));
|
||||
setMessage(binding.isUnbound() ? new TextComponent("(No Binding)") : binding.getTranslatedKeyMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package chylex.bettercontrols.gui.elements;
|
||||
import chylex.bettercontrols.util.LiteralText;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class Option<T> {
|
||||
private final T value;
|
||||
private final LiteralText text;
|
||||
private final Component text;
|
||||
|
||||
public Option(final T value, final LiteralText text) {
|
||||
public Option(final T value, final Component text) {
|
||||
this.value = value;
|
||||
this.text = text;
|
||||
}
|
||||
@ -16,7 +16,7 @@ public final class Option<T> {
|
||||
return value;
|
||||
}
|
||||
|
||||
public LiteralText getText() {
|
||||
public Component getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
package chylex.bettercontrols.gui.elements;
|
||||
import chylex.bettercontrols.gui.OptionListWidget.OptionWidget;
|
||||
import chylex.bettercontrols.util.LiteralText;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.GuiComponent;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.util.FormattedCharSequence;
|
||||
import java.util.List;
|
||||
import static chylex.bettercontrols.util.Statics.MINECRAFT;
|
||||
@ -12,14 +12,14 @@ public final class TextWidget extends GuiComponent implements OptionWidget {
|
||||
public static final int LEFT = 0;
|
||||
public static final int CENTER = 1;
|
||||
|
||||
private final LiteralText text;
|
||||
private final Component 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 LiteralText text, final int align) {
|
||||
public TextWidget(final int x, final int y, final int width, final int height, final Component text, final int align) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
@ -28,11 +28,11 @@ public final class TextWidget extends GuiComponent implements OptionWidget {
|
||||
this.align = align;
|
||||
}
|
||||
|
||||
public TextWidget(final int x, final int y, final int width, final LiteralText text, final int align) {
|
||||
public TextWidget(final int x, final int y, final int width, final Component text, final int align) {
|
||||
this(x, y, width, 20, text, align);
|
||||
}
|
||||
|
||||
public TextWidget(final int x, final int y, final int width, final LiteralText text) {
|
||||
public TextWidget(final int x, final int y, final int width, final Component text) {
|
||||
this(x, y, width, 20, text, LEFT);
|
||||
}
|
||||
|
||||
|
@ -32,11 +32,11 @@ public enum ModifierKey {
|
||||
public abstract boolean isPressed();
|
||||
|
||||
public static ModifierKey getById(final int id) {
|
||||
switch (id) {
|
||||
case 0: return CONTROL;
|
||||
case 1: return SHIFT;
|
||||
case 2: return ALT;
|
||||
default: return null;
|
||||
}
|
||||
return switch (id) {
|
||||
case 0 -> CONTROL;
|
||||
case 1 -> SHIFT;
|
||||
case 2 -> ALT;
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
package chylex.bettercontrols.input;
|
||||
import chylex.bettercontrols.util.Key;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
|
||||
public class ToggleTracker {
|
||||
@ -44,7 +43,7 @@ public class ToggleTracker {
|
||||
public boolean tick() {
|
||||
final boolean isHoldingReset = isResetKeyPressed();
|
||||
|
||||
if (Key.isPressed(bindingToggle)) {
|
||||
if (bindingToggle.isDown()) {
|
||||
if (!waitForRelease) {
|
||||
if (skipNextToggle) {
|
||||
skipNextToggle = false;
|
||||
@ -79,7 +78,7 @@ public class ToggleTracker {
|
||||
}
|
||||
|
||||
protected boolean isResetKeyPressed() {
|
||||
return Key.isPressed(bindingReset);
|
||||
return bindingReset.isDown();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
|
@ -9,7 +9,6 @@ import chylex.bettercontrols.mixin.AccessCameraFields;
|
||||
import chylex.bettercontrols.mixin.AccessClientPlayerFields;
|
||||
import chylex.bettercontrols.mixin.AccessPlayerFields;
|
||||
import chylex.bettercontrols.mixin.AccessStickyKeyBindingStateGetter;
|
||||
import chylex.bettercontrols.util.Key;
|
||||
import net.minecraft.client.Camera;
|
||||
import net.minecraft.client.player.Input;
|
||||
import net.minecraft.client.player.LocalPlayer;
|
||||
@ -97,7 +96,7 @@ public final class PlayerTicker {
|
||||
final int nextTemporarySprintTimer = temporarySprintTimer - 1;
|
||||
temporarySprintTimer = 0;
|
||||
|
||||
if (!Key.isPressed(KEY_SPRINT) && Key.isPressed(KEY_FORWARD)) {
|
||||
if (!KEY_SPRINT.isDown() && KEY_FORWARD.isDown()) {
|
||||
temporarySprintTimer = nextTemporarySprintTimer;
|
||||
}
|
||||
else if (sprintMode == SprintMode.TAP_TO_TOGGLE) {
|
||||
@ -114,7 +113,7 @@ public final class PlayerTicker {
|
||||
waitingForSprintKeyRelease = true;
|
||||
}
|
||||
else if (sprintMode == SprintMode.TAP_TO_TOGGLE) {
|
||||
if (Key.isPressed(KEY_SPRINT)) {
|
||||
if (KEY_SPRINT.isDown()) {
|
||||
if (!waitingForSprintKeyRelease) {
|
||||
waitingForSprintKeyRelease = true;
|
||||
stopSprintingAfterReleasingSprintKey = player.isSprinting();
|
||||
@ -129,12 +128,12 @@ public final class PlayerTicker {
|
||||
}
|
||||
}
|
||||
else if (sprintMode == SprintMode.HOLD) {
|
||||
if (Key.isPressed(KEY_SPRINT)) {
|
||||
if (KEY_SPRINT.isDown()) {
|
||||
stopSprintingAfterReleasingSprintKey = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (stopSprintingAfterReleasingSprintKey && !Key.isPressed(KEY_SPRINT)) {
|
||||
if (stopSprintingAfterReleasingSprintKey && !KEY_SPRINT.isDown()) {
|
||||
stopSprintingAfterReleasingSprintKey = false;
|
||||
waitingForSprintKeyRelease = false;
|
||||
player.setSprinting(false);
|
||||
@ -157,7 +156,7 @@ public final class PlayerTicker {
|
||||
}
|
||||
|
||||
if (FlightHelper.isFlyingCreativeOrSpectator(player)) {
|
||||
final boolean boost = Key.isPressed(KEY_SPRINT);
|
||||
final boolean boost = KEY_SPRINT.isDown();
|
||||
final float flightSpeed = FlightHelper.getFlightSpeed(player, boost);
|
||||
final float verticalVelocity = FlightHelper.getExtraVerticalVelocity(player, boost);
|
||||
|
||||
@ -185,7 +184,7 @@ public final class PlayerTicker {
|
||||
if (cfg().resumeSprintingAfterHittingObstacle) {
|
||||
if (wasHittingObstacle != player.horizontalCollision) {
|
||||
if (!wasHittingObstacle) {
|
||||
wasSprintingBeforeHittingObstacle = player.isSprinting() || Key.isPressed(KEY_SPRINT);
|
||||
wasSprintingBeforeHittingObstacle = player.isSprinting() || KEY_SPRINT.isDown();
|
||||
}
|
||||
else if (wasSprintingBeforeHittingObstacle) {
|
||||
wasSprintingBeforeHittingObstacle = false;
|
||||
@ -254,7 +253,7 @@ public final class PlayerTicker {
|
||||
}
|
||||
|
||||
if (player.isCreative()) {
|
||||
if (Key.wasPressed(cfg().keyToggleFlight)) {
|
||||
if (cfg().keyToggleFlight.consumeClick()) {
|
||||
final boolean isFlying = !player.getAbilities().flying;
|
||||
|
||||
player.getAbilities().flying = isFlying;
|
||||
@ -287,14 +286,14 @@ public final class PlayerTicker {
|
||||
}
|
||||
}
|
||||
|
||||
if (Key.wasPressed(cfg().keyResetAllToggles)) {
|
||||
if (cfg().keyResetAllToggles.consumeClick()) {
|
||||
toggleSprint.reset();
|
||||
toggleSneak.reset();
|
||||
toggleWalkForward.reset();
|
||||
toggleJump.reset();
|
||||
}
|
||||
|
||||
if (Key.isPressed(cfg().keyOpenMenu)) {
|
||||
if (cfg().keyOpenMenu.isDown()) {
|
||||
MINECRAFT.setScreen(new BetterControlsScreen(null));
|
||||
}
|
||||
}
|
||||
|
@ -1,46 +0,0 @@
|
||||
package chylex.bettercontrols.util;
|
||||
import com.mojang.blaze3d.platform.InputConstants;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.network.chat.Component;
|
||||
|
||||
public final class Key {
|
||||
private Key() {}
|
||||
|
||||
public static final InputConstants.Key INVALID = InputConstants.UNKNOWN;
|
||||
|
||||
public static boolean isUnbound(final KeyMapping binding) {
|
||||
return binding.isUnbound();
|
||||
}
|
||||
|
||||
public static boolean isPressed(final KeyMapping binding) {
|
||||
return binding.isDown();
|
||||
}
|
||||
|
||||
public static boolean wasPressed(final KeyMapping binding) {
|
||||
return binding.consumeClick();
|
||||
}
|
||||
|
||||
public static Component getBoundKeyText(final KeyMapping binding) {
|
||||
return binding.getTranslatedKeyMessage();
|
||||
}
|
||||
|
||||
public static void bind(final KeyMapping binding, final InputConstants.Key input) {
|
||||
binding.setKey(input);
|
||||
}
|
||||
|
||||
public static String writeBinding(final KeyMapping binding) {
|
||||
return binding.saveString();
|
||||
}
|
||||
|
||||
public static void readBinding(final KeyMapping binding, final String serialized) {
|
||||
bind(binding, InputConstants.getKey(serialized));
|
||||
}
|
||||
|
||||
public static InputConstants.Key inputFromMouse(final int button) {
|
||||
return InputConstants.Type.MOUSE.getOrCreate(button);
|
||||
}
|
||||
|
||||
public static InputConstants.Key inputFromKeyboard(final int keyCode, final int scanCode) {
|
||||
return InputConstants.getKey(keyCode, scanCode);
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package chylex.bettercontrols.util;
|
||||
import net.minecraft.network.chat.TextComponent;
|
||||
|
||||
public final class LiteralText extends TextComponent {
|
||||
public static LiteralText text(final String text) {
|
||||
return new LiteralText(text);
|
||||
}
|
||||
|
||||
public LiteralText(final String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
"required": true,
|
||||
"minVersion": "0.8",
|
||||
"package": "chylex.bettercontrols.mixin",
|
||||
"compatibilityLevel": "JAVA_8",
|
||||
"compatibilityLevel": "JAVA_16",
|
||||
"client": [
|
||||
"AccessCameraFields",
|
||||
"AccessClientPlayerFields",
|
||||
|
Loading…
Reference in New Issue
Block a user