1
0
mirror of https://github.com/chylex/Better-Controls.git synced 2025-09-15 17:32:13 +02:00

5 Commits

12 changed files with 74 additions and 37 deletions

View File

@@ -1,15 +1,15 @@
modLoader = "javafml"
loaderVersion = "[0,)"
authors = "${author}"
license = "${license}"
issueTrackerURL = "${issuesURL}"
[[mods]]
modId = "${id}"
version = "${version}"
displayName = "${name}"
description = "${description}"
authors = "${author}"
version = "${version}"
logoFile = "icon.png"
[[dependencies.${id}]]

View File

@@ -3,21 +3,21 @@ modId=bettercontrols
modName=Better Controls
modDescription=Adds many powerful key bindings and options to control your movement.\\n\\nThe features complement vanilla mechanics without giving unfair advantages, so server use should be fine.
modAuthor=chylex
modVersion=1.2.3b
modVersion=1.2.4
modLicense=MPL-2.0
modSourcesURL=https://github.com/chylex/Better-Controls
modIssuesURL=https://github.com/chylex/Better-Controls/issues
# Dependencies
minecraftVersion=22w42a
#forgeVersion=41.1.0
fabricVersion=0.14.10
minecraftVersion=1.19.4
forgeVersion=45.0.6
fabricVersion=0.14.17
loomVersion=0.12
mixinVersion=0.8.5
# Constraints
minimumMinecraftVersion=1.19
minimumForgeVersion=41.0.94
minimumMinecraftVersion=1.19.4
minimumForgeVersion=45.0.0
minimumFabricVersion=0.7.4
# Gradle

View File

@@ -24,10 +24,12 @@ final class Json {
return obj.has(key) ? obj.get(key).getAsBoolean() : defaultValue;
}
@SuppressWarnings("SameParameterValue")
static <T extends Enum<T>> void setEnum(final JsonObject obj, final String key, final T value) {
obj.addProperty(key, value.name());
}
@SuppressWarnings("SameParameterValue")
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;

View File

@@ -8,6 +8,7 @@ import chylex.bettercontrols.gui.elements.TextWidget;
import chylex.bettercontrols.input.KeyBindingWithModifier;
import chylex.bettercontrols.input.ModifierKey;
import chylex.bettercontrols.input.SprintMode;
import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.vertex.PoseStack;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
@@ -88,7 +89,7 @@ public class BetterControlsScreen extends OptionsSubScreen {
private int generateFlightOptions(int y, final List<GuiEventListener> elements) {
final BetterControlsConfig cfg = BetterControlsCommon.getConfig();
final List<Option<Float>> flightSpeedOptions = Arrays.asList(
final ImmutableList<Option<Float>> flightSpeedOptions = ImmutableList.of(
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")),
@@ -103,7 +104,7 @@ public class BetterControlsScreen extends OptionsSubScreen {
new Option<>(Float.valueOf(8.00F), text("8x"))
);
final List<Option<Float>> flightVerticalBoostOptions = Arrays.asList(
final ImmutableList<Option<Float>> flightVerticalBoostOptions = ImmutableList.of(
new Option<>(Float.valueOf(0.00F), text("None")),
new Option<>(Float.valueOf(0.25F), text("+25%")),
new Option<>(Float.valueOf(0.50F), text("+50%")),
@@ -219,6 +220,7 @@ public class BetterControlsScreen extends OptionsSubScreen {
private KeyBindingWidget editingKeyBinding;
private final List<KeyBindingWidget> allKeyBindings = new ArrayList<>();
@SuppressWarnings("DataFlowIssue")
public BetterControlsScreen(@Nullable final Screen parentScreen) {
super(parentScreen, Minecraft.getInstance().options, TITLE);
}
@@ -242,7 +244,9 @@ public class BetterControlsScreen extends OptionsSubScreen {
elements.add(new TextWidget(0, y, ROW_WIDTH, ROW_HEIGHT, text("Miscellaneous"), CENTER));
y = generateMiscellaneousOptions(y + ROW_HEIGHT, elements) + TITLE_MARGIN_TOP;
addRenderableWidget(new Button(width / 2 - 99, height - 29, 200, 20, CommonComponents.GUI_DONE, btn -> minecraft.setScreen(lastScreen)));
//noinspection DataFlowIssue
addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, btn -> minecraft.setScreen(lastScreen)).pos(width / 2 - 99, height - 29).size(200, 20).build());
addWidget(optionsWidget = new OptionListWidget(21, height - 32, width, height, elements, y - TITLE_MARGIN_TOP + BOTTOM_PADDING));
}

View File

@@ -4,7 +4,7 @@ import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
import net.minecraft.client.gui.components.Widget;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import org.jetbrains.annotations.NotNull;
@@ -35,14 +35,14 @@ public final class OptionListWidget extends ContainerObjectSelectionList<Entry>
return new Offset(widget.getX(), widget.getY());
}
else if (element instanceof final AbstractWidget widget) {
return new Offset(widget.x, widget.y);
return new Offset(widget.getX(), widget.getY());
}
else {
return new Offset(0, 0);
}
}
public interface OptionWidget extends GuiEventListener, Widget {
public interface OptionWidget extends GuiEventListener, Renderable {
int getX();
int getY();
void setX(int x);
@@ -82,13 +82,15 @@ public final class OptionListWidget extends ContainerObjectSelectionList<Entry>
this.offsets = elements.stream().collect(Collectors.toMap(Function.identity(), OptionListWidget::getElementOffset));
}
@NotNull
@Override
public @NotNull List<? extends GuiEventListener> children() {
public List<? extends GuiEventListener> children() {
return Collections.unmodifiableList(elements);
}
@NotNull
@Override
public @NotNull List<? extends NarratableEntry> narratables() {
public List<? extends NarratableEntry> narratables() {
return Collections.unmodifiableList(narratables);
}
@@ -98,16 +100,16 @@ public final class OptionListWidget extends ContainerObjectSelectionList<Entry>
final Offset offset = offsets.get(element);
if (element instanceof final AbstractWidget widget) {
widget.x = x + offset.x;
widget.y = y + offset.y;
widget.setX(x + offset.x);
widget.setY(y + offset.y);
}
else if (element instanceof final OptionWidget widget) {
widget.setX(x + offset.x);
widget.setY(y + offset.y);
}
if (element instanceof final Widget widget) {
widget.render(matrices, mouseX, mouseY, tickDelta);
if (element instanceof final Renderable renderable) {
renderable.render(matrices, mouseX, mouseY, tickDelta);
}
}
}

View File

@@ -1,19 +1,19 @@
package chylex.bettercontrols.gui.elements;
import com.google.common.collect.ImmutableList;
import net.minecraft.client.gui.components.AbstractSliderButton;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.function.Consumer;
public final class DiscreteValueSliderWidget<T> extends AbstractSliderButton {
private final Component narration;
private final List<Option<T>> options;
private final ImmutableList<Option<T>> options;
private final Consumer<T> onChanged;
private T selectedValue;
public DiscreteValueSliderWidget(final int x, final int y, final int width, final int height, final Component narration, final List<Option<T>> options, final T selectedValue, final Consumer<T> onChanged) {
public DiscreteValueSliderWidget(final int x, final int y, final int width, final int height, final Component narration, final ImmutableList<Option<T>> options, final T selectedValue, final Consumer<T> onChanged) {
super(x, y, width, height, Option.find(options, selectedValue).text(), options.indexOf(Option.find(options, selectedValue)) / (options.size() - 1.0));
this.narration = narration;
this.options = options;
@@ -21,7 +21,7 @@ public final class DiscreteValueSliderWidget<T> extends AbstractSliderButton {
this.onChanged = onChanged;
}
public DiscreteValueSliderWidget(final int x, final int y, final int width, final Component narration, final List<Option<T>> options, final T selectedValue, final Consumer<T> onChanged) {
public DiscreteValueSliderWidget(final int x, final int y, final int width, final Component narration, final ImmutableList<Option<T>> options, final T selectedValue, final Consumer<T> onChanged) {
this(x, y, width, 20, narration, options, selectedValue, onChanged);
}
@@ -44,8 +44,9 @@ public final class DiscreteValueSliderWidget<T> extends AbstractSliderButton {
}
}
@NotNull
@Override
protected @NotNull MutableComponent createNarrationMessage() {
protected MutableComponent createNarrationMessage() {
return Component.translatable("gui.narrate.slider", narration.plainCopy().append(" ").append(getMessage()));
}
}

View File

@@ -22,7 +22,7 @@ public final class KeyBindingWidget extends Button {
private boolean isEditing;
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, Component.empty(), btn -> {});
super(x, y, width, height, Component.empty(), btn -> {}, DEFAULT_NARRATION);
this.binding = binding;
this.bindingName = bindingName;
this.onEditingStarted = onEditingStarted;
@@ -38,8 +38,9 @@ public final class KeyBindingWidget extends Button {
button.active = !binding.isUnbound();
}
@NotNull
@Override
protected @NotNull MutableComponent createNarrationMessage() {
protected MutableComponent createNarrationMessage() {
return binding.isUnbound() ? Component.translatable("narrator.controls.unbound", bindingName) : Component.translatable("narrator.controls.bound", bindingName, super.createNarrationMessage());
}

View File

@@ -57,6 +57,14 @@ public final class TextWidget extends GuiComponent implements OptionWidget {
this.y = y;
}
@Override
public void setFocused(final boolean focused) {}
@Override
public boolean isFocused() {
return false;
}
@Override
public void render(final @NotNull PoseStack matrices, final int mouseX, final int mouseY, final float delta) {
final Font textRenderer = Minecraft.getInstance().font;

View File

@@ -9,7 +9,9 @@ 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.client.gui.screens.controls.ControlsScreen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
@@ -29,25 +31,31 @@ public abstract class HookControlsScreen extends OptionsSubScreen {
int leftBottomY = 0;
int rightBottomY = 0;
AbstractWidget doneButton = null;
for (final GuiEventListener child : children()) {
if (!(child instanceof AbstractWidget widget)) {
continue;
}
final int bottomY = widget.y + widget.getHeight();
if (widget instanceof Button button && button.getMessage() == CommonComponents.GUI_DONE) {
doneButton = widget;
continue;
}
if (widget.x + widget.getWidth() < center) {
final int bottomY = widget.getY() + widget.getHeight();
if (widget.getX() + widget.getWidth() < center) {
leftBottomY = Math.max(leftBottomY, bottomY);
}
if (widget.x >= center) {
if (widget.getX() >= center) {
rightBottomY = Math.max(rightBottomY, bottomY);
}
}
final int x, y;
if (leftBottomY < rightBottomY) {
if (leftBottomY <= rightBottomY) {
x = center - 155;
y = leftBottomY + 4;
}
@@ -56,6 +64,11 @@ public abstract class HookControlsScreen extends OptionsSubScreen {
y = rightBottomY + 4;
}
addRenderableWidget(new Button(x, y, 150, 20, BetterControlsScreen.TITLE.plainCopy().append("..."), btn -> Minecraft.getInstance().setScreen(new BetterControlsScreen(screen))));
final MutableComponent buttonTitle = BetterControlsScreen.TITLE.plainCopy().append("...");
addRenderableWidget(Button.builder(buttonTitle, btn -> Minecraft.getInstance().setScreen(new BetterControlsScreen(screen))).pos(x, y).size(150, 20).build());
if (doneButton != null) {
doneButton.setY(y + 24);
}
}
}

View File

@@ -16,9 +16,12 @@ public abstract class HookPlayerFlightSpeed extends LivingEntity {
}
@Redirect(
method = "travel",
method = "getFlyingSpeed",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;isSprinting()Z"),
slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Abilities;getFlyingSpeed()F"))
slice = @Slice(
from = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/player/Abilities;flying:Z"),
to = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Abilities;getFlyingSpeed()F")
)
)
private boolean disableVanillaSprintBoost(final Player player) {
return false;

View File

@@ -7,16 +7,17 @@ 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.CallbackInfoReturnable;
import java.util.function.Consumer;
@Mixin(OptionInstance.class)
public abstract class HookToggleOptionButtons {
@Inject(method = "createButton", at = @At("RETURN"))
private void disableToggleOptions(final Options options, final int x, final int y, final int width, final CallbackInfoReturnable<AbstractWidget> ci) {
@Inject(method = "createButton(Lnet/minecraft/client/Options;IIILjava/util/function/Consumer;)Lnet/minecraft/client/gui/components/AbstractWidget;", at = @At("RETURN"))
private <T> void disableToggleOptions(final Options options, final int x, final int y, final int width, final Consumer<T> callback, final CallbackInfoReturnable<AbstractWidget> cir) {
@SuppressWarnings("ConstantConditions")
final OptionInstance<?> me = (OptionInstance<?>)(Object)this;
if (me == options.toggleCrouch() || me == options.toggleSprint()) {
ci.getReturnValue().active = false;
cir.getReturnValue().active = false;
}
}
}

View File

@@ -149,6 +149,7 @@ public final class PlayerTicker {
public void afterInputAssignsPressingForward(final Input input) {
if (MINECRAFT.screen == null) {
//noinspection NonShortCircuitBooleanExpression
input.up |= toggleWalkForward.tick();
}
}
@@ -157,6 +158,7 @@ public final class PlayerTicker {
final Input input = player.input;
if (MINECRAFT.screen == null && !player.getAbilities().flying) {
//noinspection NonShortCircuitBooleanExpression
input.jumping |= toggleJump.tick();
}