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 22, 2024
1 parent 485550f commit 0a3af47
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 +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);
}
}
}
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,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<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 +55,54 @@ 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),
1.25,
(value) -> {
});

private static final SimpleOption<Double> sneakHeightOption = new SimpleOption<Double>(
"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<StepMode> getStepModeOption() {
return stepModeOption;
}

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

public static SimpleOption<Double> getSneakHeightOption() {
return sneakHeightOption;
}
}
7 changes: 5 additions & 2 deletions src/client/resources/assets/accessible-step/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 0a3af47

Please sign in to comment.