Skip to content

Commit

Permalink
Add step and sneak height options
Browse files Browse the repository at this point in the history
I'm not entirely sure whether adding a configurable step height is really something worth pursuing with this mod.
  • Loading branch information
SecretOnline committed Jun 26, 2024
1 parent 82b9c10 commit 0d037a2
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ The versioning scheme is listed in the README.

Minecraft 1.x.x

### Added

- Options for changing the step height.
- For users with Mod Menu installed, this appears in the mod's settings screen.
- For users without Mod Menu installed, you can change the heights in your `options.txt`.

## v1.1.0 - 2024-06-14

### Updated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,16 @@
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 RegistryEntry<EntityAttribute> 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) {
Expand All @@ -49,25 +27,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(AccessibleStepOptions.VANILLA_STEP_HEIGHT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,42 @@

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.entity.attribute.EntityAttributes;
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;
// It'd probably be more accurate to use 16 increments as that maps to a pixel;
// the smallest imcrement in vanilla. However, that leads to options
// values that have up to 4 decimal places which just doesn't look nice.
// An alternative could be to format the step heights as mixed fractions
// (e.g. 1¼ or even having 16 as the denominator in all cases), but that strays
// further from Minecraft's formatting style.
private static final double STEP_HEIGHT_INCREMENTS_PER_BLOCK = 20;

public static final double VANILLA_STEP_HEIGHT = EntityAttributes.GENERIC_STEP_HEIGHT.value().getDefaultValue();
private static final double MOD_DEFAULT_STEP_HEIGHT = 1.25;
private static final double MOD_DEFAULT_SNEAK_HEIGHT = VANILLA_STEP_HEIGHT;

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<StepMode> stepModeOption = new SimpleOption<StepMode>(
"options.accessiblestep.option",
"options.accessiblestep.mode",
AccessibleStepOptions::getStepModeTooltip,
SimpleOption.enumValueText(),
new SimpleOption.PotentialValuesBasedCallbacks<StepMode>(
Expand Down Expand Up @@ -44,7 +66,74 @@ private static void onStepModeChange(StepMode value) {
}
}

private static final SimpleOption<Double> stepHeightOption = new SimpleOption<Double>(
"options.accessiblestep.height",
SimpleOption.emptyTooltip(),
AccessibleStepOptions::getStepHeightText,
DoubleSliderCallbacks.INSTANCE.withModifier(
AccessibleStepOptions::toStepHeight,
AccessibleStepOptions::fromStepHeight),
Codec.doubleRange(0.0, 10.0),
MOD_DEFAULT_STEP_HEIGHT,
(value) -> {
});

private static final SimpleOption<Double> sneakHeightOption = new SimpleOption<Double>(
"options.accessiblestep.sneakheight",
(Double value) -> Tooltip.of(SNEAK_HEIGHT_TOOLTIP),
AccessibleStepOptions::getSneakHeightText,
DoubleSliderCallbacks.INSTANCE.withModifier(
AccessibleStepOptions::toStepHeight,
AccessibleStepOptions::fromStepHeight),
Codec.doubleRange(0.0, 10.0),
MOD_DEFAULT_SNEAK_HEIGHT,
(value) -> {
});

private static double toStepHeight(double rangeValue) {
double reducedRange = (rangeValue * (MAX_STEP_HEIGHT - MIN_STEP_HEIGHT)) +
MIN_STEP_HEIGHT;
// Limit to 0.05 block increments so the slider doesn't go crazy
return Math.floor(reducedRange * STEP_HEIGHT_INCREMENTS_PER_BLOCK) /
STEP_HEIGHT_INCREMENTS_PER_BLOCK;
}

private static double fromStepHeight(double stepHeight) {
// No increments checking required here
return (stepHeight - MIN_STEP_HEIGHT) / (MAX_STEP_HEIGHT - MIN_STEP_HEIGHT);
}

private static Text getStepHeightText(Text optionText, Double value) {
Object displayValue = value;
if (value == MOD_DEFAULT_STEP_HEIGHT) {
displayValue = Text.translatable("options.accessiblestep.default.mod");
} else if (value == VANILLA_STEP_HEIGHT) {
displayValue = Text.translatable("options.accessiblestep.default.vanilla");
}

return Text.translatable("options.generic_value", new Object[] { optionText, displayValue });
}

private static Text getSneakHeightText(Text optionText, Double value) {
Object displayValue = value;
if (value == MOD_DEFAULT_SNEAK_HEIGHT) {
displayValue = Text.translatable("options.accessiblestep.default.mod");
} else if (value == VANILLA_STEP_HEIGHT) {
displayValue = Text.translatable("options.accessiblestep.default.vanilla");
}

return Text.translatable("options.generic_value", new Object[] { optionText, displayValue });
}

public static SimpleOption<StepMode> getStepModeOption() {
return stepModeOption;
}

public static SimpleOption<Double> getStepHeightOption() {
return stepHeightOption;
}

public static SimpleOption<Double> getSneakHeightOption() {
return sneakHeightOption;
}
}
17 changes: 11 additions & 6 deletions src/client/resources/assets/accessible-step/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"options.accessiblestep.title": "Accessible Step",
"options.accessiblestep.option": "Step Assistance",
"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.title": "Accessible Step",
"options.accessiblestep.mode": "Step Assistance",
"options.accessiblestep.height": "Step Height",
"options.accessiblestep.sneakheight": "Sneak Height",
"options.accessiblestep.step": "Step",
"options.accessiblestep.default.mod": "Default",
"options.accessiblestep.default.vanilla": "Vanilla",
"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.sneakheight.tooltip": "Values greater than the configured Step Height will be ignored"
}

0 comments on commit 0d037a2

Please sign in to comment.