Skip to content

Commit

Permalink
Add jump height check for sitting on blocks.
Browse files Browse the repository at this point in the history
This prevents scaling up buildings as you now need to
be able to first get to it prior to sitting on it.

Fixes in part #3
  • Loading branch information
Ampflower committed Jul 8, 2023
1 parent e33b4d6 commit a6b64f8
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 8 deletions.
88 changes: 88 additions & 0 deletions src/main/java/gay/ampflower/polysit/JumpHeightUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* Copyright 2023 Ampflower
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package gay.ampflower.polysit;

import gay.ampflower.polysit.mixin.AccessorLivingEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;

/**
* Unnecessarily accurate and fast jump height utilities.
* <p>
* Effectively the difference operator from {@code travel() { y += v; v = (v -
* 0.08) * 0.98 }} eventually turned back into a curve with a well-defined
* derivative.
* </p>
* <p>
* The Maxima script for solving the equations, if it is ever needed again.
*
* <pre>
* load("solve_rec")$
*
* vy_expl: solve_rec(vy[t+1] = (vy[t] - b) * decay, vy[t], vy[0] = v0);
* vy(t) := ev(rhs(vy_expl))$
*
* y_expl: y[t] = sum(vy(i), i, 0, t), simpsum=true;
* y(t) := ev(rhs(y_expl))$
*
* &#x2f;* We have to manually factor decay^t out *&#x2f;
* tzero: expand(solve(expand(vy(t)/decay^t) = 0, t));
*
* print("Call 'y(t), tzero, b=0.08, decay=0.98;' to get the expression for ymax")$
* y(t), tzero, b=0.08, decay=0.98;
* </pre>
* </p>
*
* @author Moxie Amethyst
* @author Ampflower
* @since 0.5
**/
public final class JumpHeightUtil {

public static double maxJumpHeight(Entity entity) {
if (entity instanceof LivingEntity livingEntity) {
return maxJumpHeight(livingEntity);
}
return 1.D;
}

public static double maxJumpHeight(LivingEntity entity) {
return maxJumpHeight(((AccessorLivingEntity) entity).invokeGetJumpVelocity());
}

/**
* Gets the maximum height for a jump using a given velocity.
*
* @param velocity The Y velocity to find the maximum relative Y of.
* @return The maximum Y of the velocity.
*/
public static double maxJumpHeight(double velocity) {
return heightAtTime(velocity, zeroTangent(velocity));
}

/**
* Gets the Y position of a given entity when given velocity and ticks.
*
* @param velocity The Y velocity to define the curve with.
* @param ticks The time in ticks, as the X coordinate.
* @return The height from the velocity after given time in ticks.
*/
public static double heightAtTime(double velocity, double ticks) {
return -50 * (velocity + 3.92) * (Math.pow(0.98, ticks) - 1D) - 3.92 * ticks;
}

/**
* Gets the point where the graph is at a zero-slope.
*
* @param velocity The Y velocity to define the curve with.
* @return The time in ticks where the graph will have a 0-point slope, defining
* the maximum of the curve.
*/
public static double zeroTangent(double velocity) {
return 49.4983 * Math.log(Math.fma(0.25768759333570745, velocity, 1.010135365875973));
}
}
54 changes: 46 additions & 8 deletions src/main/java/gay/ampflower/polysit/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3i;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand Down Expand Up @@ -60,12 +61,22 @@ public static void bootstrap() {
*/
public static void main() {
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
if (!world.isClient && hand == Hand.MAIN_HAND && (player.isOnGround() || player.hasVehicle())
if (!world.isClient && hand == Hand.MAIN_HAND
&& (player.isOnGround() || player.hasVehicle() || player.isCreative())
&& player.getStackInHand(hand).isEmpty() && hitResult.getSide() != Direction.DOWN) {
var pos = hitResult.getBlockPos();
// TODO: Make this check the entire collision area where the player fits.
// This would accounts for Pehkui and other mods that modify the hit box.
if (!world.testBlockState(pos.up(), BlockState::isAir))
return ActionResult.PASS;
var block = world.getBlockState(pos);

final var block = world.getBlockState(pos);
final var topHeight = getTopHeight(world, block, pos, player);
final var relative = pos.getY() + topHeight - getEffectiveEntityY(player);

if (relative > JumpHeightUtil.maxJumpHeight(player)) {
return ActionResult.PASS;
}

try {
return sit(world, block, pos, player, false);
Expand Down Expand Up @@ -97,16 +108,16 @@ public static void main() {
var pos = entity.getBlockPos();
var world = entity.getWorld();
var state = world.getBlockState(pos);
var shape = state.getCollisionShape(world, pos, ShapeContext.of(entity));
var topHeight = getTopHeight(world, state, pos, entity);

// Skip if it's not solid or taller than 1 block.
if (state.isAir() || shape.isEmpty() || shape.getMax(Direction.Axis.Y) > 1D) {
// Skip if it's not solid or taller than jump height.
if (topHeight < 0.D || topHeight > JumpHeightUtil.maxJumpHeight(entity)) {
pos = pos.down();
state = world.getBlockState(pos);
shape = state.getCollisionShape(world, pos, ShapeContext.of(entity));
topHeight = getTopHeight(world, state, pos, entity);
}

if (state.isAir() || shape.isEmpty()) {
if (topHeight < 0.D) {
source.sendError(Text.of("It appears you're trying to sit on air."));
return 0;
}
Expand All @@ -117,7 +128,7 @@ public static void main() {
}

double x = entity.getX();
double y = pos.getY() + Math.min(assertFinite(shape.getMax(Direction.Axis.Y), 's'), 1D) - 0.2D;
double y = pos.getY() + assertFinite(topHeight, 's') - 0.2D;
double z = entity.getZ();

sit(world, entity, x, y, z);
Expand All @@ -133,6 +144,33 @@ public static void main() {
});
}

public static double getEffectiveEntityY(Entity entity) {
if (!entity.hasVehicle()) {
return entity.getY();
}

final var pos = entity.getBlockPos();
final var world = entity.getWorld();
final var block = world.getBlockState(pos);
final var height = getTopHeight(world, block, pos, entity);

return Math.max(pos.getY() + height, entity.getY());
}

public static double getTopHeight(BlockView world, BlockState state, BlockPos pos, Entity entity) {
if (state.isAir()) {
return -1.D;
}

if (state.getBlock() instanceof StairsBlock && state.get(StairsBlock.HALF) == BlockHalf.BOTTOM) {
return 0.5D;
}

final var shape = state.getCollisionShape(world, pos, ShapeContext.of(entity));

return shape.getMax(Direction.Axis.Y);
}

public static ActionResult sit(@NotNull final World world, @NotNull final BlockState state,
@NotNull final BlockPos pos, @NotNull final Entity entity, final boolean command) {
if (state.getBlock() instanceof StairsBlock && state.get(StairsBlock.HALF) == BlockHalf.BOTTOM) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Copyright 2023 Ampflower
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package gay.ampflower.polysit.mixin;

import net.minecraft.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

/**
* @author Ampflower
* @since 0.5
**/
@Mixin(LivingEntity.class)
public interface AccessorLivingEntity {
@Invoker
float invokeGetJumpVelocity();
}
1 change: 1 addition & 0 deletions src/main/resources/polysit.mixin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "gay.ampflower.polysit.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"AccessorLivingEntity",
"MixinEntityTypeBootstrap",
"MixinLivingEntity",
"MixinServerPlayerEntity"
Expand Down

0 comments on commit a6b64f8

Please sign in to comment.