From 0a3af470b9990bb4b6ff4ee92350282848417496 Mon Sep 17 00:00:00 2001 From: secret_online Date: Sun, 16 Jun 2024 16:28:38 +1200 Subject: [PATCH] Add step and sneak height options I'm not entirely sure whether adding a configurable step height is really something worth pursuing with this mod. --- .../accessiblestep/AccessibleStepEndTick.java | 45 +++----------- .../mixin/client/GameOptionsMixin.java | 2 + .../modmenu/AccessibleStepOptionsScreen.java | 6 +- .../options/AccessibleStepOptions.java | 60 ++++++++++++++++++- .../assets/accessible-step/lang/en_us.json | 7 ++- 5 files changed, 79 insertions(+), 41 deletions(-) diff --git a/src/client/java/co/secretonline/accessiblestep/AccessibleStepEndTick.java b/src/client/java/co/secretonline/accessiblestep/AccessibleStepEndTick.java index 215506d..e093bd2 100644 --- a/src/client/java/co/secretonline/accessiblestep/AccessibleStepEndTick.java +++ b/src/client/java/co/secretonline/accessiblestep/AccessibleStepEndTick.java @@ -6,38 +6,17 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.entity.attribute.EntityAttribute; import net.minecraft.entity.attribute.EntityAttributeInstance; -import net.minecraft.entity.attribute.EntityAttributeModifier; -import net.minecraft.entity.attribute.EntityAttributeModifier.Operation; import net.minecraft.registry.Registries; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.util.Identifier; public class AccessibleStepEndTick implements EndTick { private static final Identifier STEP_HEIGHT_ATTRIBUTE_ID = Identifier.ofVanilla("generic.step_height"); - private static final Identifier STEP_HEIGHT_ATTRIBUTE_MODIFIER_ID = AccessibleStep.id("generic.step_height"); + private static final double DEFAULT_STEP_HEIGHT = 0.6; private static final RegistryEntry STEP_HEIGHT_ATTRIBUTE = Registries.ATTRIBUTE .getEntry(STEP_HEIGHT_ATTRIBUTE_ID).get(); - /** - * Default step height is 0.6 blocks. - * From testing with other step mods, modified step heights are usually around - * 1.25 blocks. - */ - private static final float STEP_HEIGHT_MODIFIER_AMOUNT = 0.65f; - - /** - * Modifier to add an amount to the step height. - * - * This mod uses modifiers to avoid conflicts where the server might update the - * base value to be different from the normal value. This mod will still affect - * the step height, but hopefully not as much. - */ - private static final EntityAttributeModifier STEP_HEIGHT_MODIFIER = new EntityAttributeModifier( - STEP_HEIGHT_ATTRIBUTE_MODIFIER_ID, - STEP_HEIGHT_MODIFIER_AMOUNT, - Operation.ADD_VALUE); - @Override public void onEndTick(MinecraftClient client) { if (client.player == null) { @@ -49,25 +28,17 @@ public void onEndTick(MinecraftClient client) { EntityAttributeInstance stepHeightAttribute = client.player.getAttributeInstance(STEP_HEIGHT_ATTRIBUTE); if (stepMode.equals(StepMode.STEP)) { + double stepHeight = AccessibleStepOptions.getStepHeightOption().getValue(); + if (client.player.isSneaking()) { - removeModifier(stepHeightAttribute); + double sneakHeight = AccessibleStepOptions.getSneakHeightOption().getValue(); + double heightToSet = Math.min(stepHeight, sneakHeight); + stepHeightAttribute.setBaseValue(heightToSet); } else { - addModifier(stepHeightAttribute); + stepHeightAttribute.setBaseValue(stepHeight); } } else { - removeModifier(stepHeightAttribute); - } - } - - private static void addModifier(EntityAttributeInstance attributeInstance) { - if (!attributeInstance.hasModifier(STEP_HEIGHT_ATTRIBUTE_MODIFIER_ID)) { - attributeInstance.addTemporaryModifier(STEP_HEIGHT_MODIFIER); - } - } - - private static void removeModifier(EntityAttributeInstance attributeInstance) { - if (attributeInstance.hasModifier(STEP_HEIGHT_ATTRIBUTE_MODIFIER_ID)) { - attributeInstance.removeModifier(STEP_HEIGHT_MODIFIER); + stepHeightAttribute.setBaseValue(DEFAULT_STEP_HEIGHT); } } } diff --git a/src/client/java/co/secretonline/accessiblestep/mixin/client/GameOptionsMixin.java b/src/client/java/co/secretonline/accessiblestep/mixin/client/GameOptionsMixin.java index 03c651f..14a80d2 100644 --- a/src/client/java/co/secretonline/accessiblestep/mixin/client/GameOptionsMixin.java +++ b/src/client/java/co/secretonline/accessiblestep/mixin/client/GameOptionsMixin.java @@ -13,5 +13,7 @@ public class GameOptionsMixin { @Inject(at = @At("HEAD"), method = "accept(Lnet/minecraft/client/option/GameOptions$Visitor;)V") private void injectStepOption(GameOptions.Visitor visitor, CallbackInfo info) { visitor.accept(AccessibleStepOptions.STEP_MODE_OPTIONS_KEY, AccessibleStepOptions.getStepModeOption()); + visitor.accept(AccessibleStepOptions.STEP_HEIGHT_OPTIONS_KEY, AccessibleStepOptions.getStepHeightOption()); + visitor.accept(AccessibleStepOptions.SNEAK_HEIGHT_OPTIONS_KEY, AccessibleStepOptions.getSneakHeightOption()); } } diff --git a/src/client/java/co/secretonline/accessiblestep/modmenu/AccessibleStepOptionsScreen.java b/src/client/java/co/secretonline/accessiblestep/modmenu/AccessibleStepOptionsScreen.java index 8ee3f2f..0a65452 100644 --- a/src/client/java/co/secretonline/accessiblestep/modmenu/AccessibleStepOptionsScreen.java +++ b/src/client/java/co/secretonline/accessiblestep/modmenu/AccessibleStepOptionsScreen.java @@ -9,7 +9,11 @@ public class AccessibleStepOptionsScreen extends GameOptionsScreen { private static SimpleOption[] getOptions(GameOptions gameOptions) { - return new SimpleOption[] { AccessibleStepOptions.getStepModeOption() }; + return new SimpleOption[] { + AccessibleStepOptions.getStepModeOption(), + AccessibleStepOptions.getStepHeightOption(), + AccessibleStepOptions.getSneakHeightOption() + }; } public AccessibleStepOptionsScreen(Screen parent, GameOptions gameOptions) { diff --git a/src/client/java/co/secretonline/accessiblestep/options/AccessibleStepOptions.java b/src/client/java/co/secretonline/accessiblestep/options/AccessibleStepOptions.java index 69358bb..584c34b 100644 --- a/src/client/java/co/secretonline/accessiblestep/options/AccessibleStepOptions.java +++ b/src/client/java/co/secretonline/accessiblestep/options/AccessibleStepOptions.java @@ -2,20 +2,31 @@ import java.util.Arrays; +import com.mojang.serialization.Codec; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.option.SimpleOption; +import net.minecraft.client.option.SimpleOption.DoubleSliderCallbacks; import net.minecraft.text.Text; public class AccessibleStepOptions { public static final String STEP_MODE_OPTIONS_KEY = "accessibleStep"; + public static final String STEP_HEIGHT_OPTIONS_KEY = "accessibleStepHeight"; + public static final String SNEAK_HEIGHT_OPTIONS_KEY = "accessibleSneakHeight"; + + private static final double MIN_STEP_HEIGHT = 0.0; + private static final double MAX_STEP_HEIGHT = 10.0; + private static final int STEP_HEIGHT_INCREMENTS_PER_BLOCK = 20; private static final Text STEP_MODE_OFF_TOOLTIP = Text.translatable("options.accessiblestep.off.tooltip"); private static final Text STEP_MODE_STEP_TOOLTIP = Text.translatable("options.accessiblestep.step.tooltip"); private static final Text STEP_MODE_AUTO_JUMP_TOOLTIP = Text.translatable("options.accessiblestep.autojump.tooltip"); + private static final Text SNEAK_HEIGHT_TOOLTIP = Text.translatable("options.accessiblestep.sneakheight.tooltip"); + private static final SimpleOption stepModeOption = new SimpleOption( - "options.accessiblestep.option", + "options.accessiblestep.mode", AccessibleStepOptions::getStepModeTooltip, SimpleOption.enumValueText(), new SimpleOption.PotentialValuesBasedCallbacks( @@ -44,7 +55,54 @@ private static void onStepModeChange(StepMode value) { } } + private static final SimpleOption stepHeightOption = new SimpleOption( + "options.accessiblestep.height", + SimpleOption.emptyTooltip(), + AccessibleStepOptions::getStepHeightText, + DoubleSliderCallbacks.INSTANCE.withModifier( + AccessibleStepOptions::toStepHeight, + AccessibleStepOptions::fromStepHeight), + Codec.doubleRange(0.0, 10.0), + 1.25, + (value) -> { + }); + + private static final SimpleOption sneakHeightOption = new SimpleOption( + "options.accessiblestep.sneakheight", + (Double value) -> Tooltip.of(SNEAK_HEIGHT_TOOLTIP), + AccessibleStepOptions::getStepHeightText, + DoubleSliderCallbacks.INSTANCE.withModifier( + AccessibleStepOptions::toStepHeight, + AccessibleStepOptions::fromStepHeight), + Codec.doubleRange(0.0, 10.0), + 0.6, + (value) -> { + }); + + private static double toStepHeight(double rangeValue) { + double reducedRange = (rangeValue * (MAX_STEP_HEIGHT - MIN_STEP_HEIGHT)) + + MIN_STEP_HEIGHT; + return Math.floor(reducedRange * (double) STEP_HEIGHT_INCREMENTS_PER_BLOCK) / + (double) STEP_HEIGHT_INCREMENTS_PER_BLOCK; + } + + private static double fromStepHeight(double stepHeight) { + return (stepHeight - MIN_STEP_HEIGHT) / (MAX_STEP_HEIGHT - MIN_STEP_HEIGHT); + } + + private static Text getStepHeightText(Text optionText, Double value) { + return Text.translatable("options.generic_value", new Object[] { optionText, value }); + } + public static SimpleOption getStepModeOption() { return stepModeOption; } + + public static SimpleOption getStepHeightOption() { + return stepHeightOption; + } + + public static SimpleOption getSneakHeightOption() { + return sneakHeightOption; + } } diff --git a/src/client/resources/assets/accessible-step/lang/en_us.json b/src/client/resources/assets/accessible-step/lang/en_us.json index 96f5f61..77800ae 100644 --- a/src/client/resources/assets/accessible-step/lang/en_us.json +++ b/src/client/resources/assets/accessible-step/lang/en_us.json @@ -1,8 +1,11 @@ { "options.accessiblestep.title": "Accessible Step", - "options.accessiblestep.option": "Step Assistance", + "options.accessiblestep.mode": "Step Assistance", + "options.accessiblestep.height": "Step Height", + "options.accessiblestep.sneakheight": "Sneak Height", "options.accessiblestep.step": "Step", "options.accessiblestep.off.tooltip": "No assistance", "options.accessiblestep.autojump.tooltip": "Jump when approaching a full block", - "options.accessiblestep.step.tooltip": "Step up smoothly when approaching a full block" + "options.accessiblestep.step.tooltip": "Step up smoothly when approaching a full block", + "options.accessiblestep.sneakheight.tooltip": "Values greater than the configured step height will be ignored" }