Skip to content

Commit

Permalink
refactor(dismounting): it was barely readable
Browse files Browse the repository at this point in the history
  • Loading branch information
rvbsm committed Jan 2, 2025
1 parent 5ced922 commit 80b4aab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/main/kotlin/dev/rvbsm/fsit/entity/Dismounting.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
package dev.rvbsm.fsit.entity

import dev.rvbsm.fsit.util.math.toHorizontalDirection
import net.minecraft.entity.Dismounting.canDismountInBlock
import net.minecraft.entity.Dismounting.canPlaceEntityAt
import net.minecraft.entity.Dismounting.getDismountOffsets
import dev.rvbsm.fsit.util.math.plus
import net.minecraft.entity.Entity
import net.minecraft.entity.LivingEntity
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.MathHelper
import net.minecraft.util.math.Vec3d
import net.minecraft.util.shape.VoxelShape

/**
* @see net.minecraft.entity.vehicle.AbstractMinecartEntity.updatePassengerForDismount
* @see net.minecraft.entity.vehicle.BoatEntity.updatePassengerForDismount
*/
/** @see net.minecraft.entity.vehicle.BoatEntity.updatePassengerForDismount */
fun getDismountPosition(vehicle: Entity, passenger: LivingEntity): Vec3d {
val world = vehicle.world
val vehiclePos = vehicle.pos

val dismountOffsets = getDismountOffsets(passenger.yaw.toHorizontalDirection())
for ((xOffset, zOffset) in sequenceOf(intArrayOf(0, 0), *dismountOffsets)) {
val dismountPos = vehiclePos.add(xOffset.toDouble(), 0.0, zOffset.toDouble()).let { dismountPos ->
world.getDismountHeight(BlockPos.ofFloored(dismountPos)).takeIf(::canDismountInBlock)
?.let { dismountHeight -> dismountPos.add(0.0, dismountHeight, 0.0) }
} ?: continue
val dismountSequence = sequence<Vec3d> {
val vehicleDismountHeight = world.getDismountHeight(vehicle.blockPos)
if (vehicleDismountHeight.isFinite() && vehicleDismountHeight < 1) {
yield(vehicle.pos)
}

val dismountOffset = Entity.getPassengerDismountOffset(
vehicle.width.toDouble() * MathHelper.SQUARE_ROOT_OF_TWO,
passenger.width.toDouble(),
passenger.yaw,
)

var dismountBlockPos = BlockPos.ofFloored(vehicle.pos + dismountOffset)
repeat(2) {
val dismountHeight = world.getDismountHeight(dismountBlockPos)
if (dismountHeight.isFinite() && dismountHeight < 1) {
yield(Vec3d.add(dismountBlockPos, 0.5, dismountHeight, 0.5))
}

passenger.poses.find { canPlaceEntityAt(world, dismountPos, passenger, it) }?.let {
passenger.pose = it
return dismountPos
dismountBlockPos = dismountBlockPos.down()
}
}

return vehiclePos
for (dismountPos in dismountSequence) {
val dismountPose = passenger.poses.find { passengerPose ->
val poseBox = passenger.getBoundingBox(passengerPose).offset(dismountPos)
world.getCollisions(passenger, poseBox).all(VoxelShape::isEmpty) && poseBox in world.worldBorder
} ?: continue

passenger.pose = dismountPose
return dismountPos
}

return vehicle.pos
}
1 change: 1 addition & 0 deletions src/main/resources/fsit.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ extendable method net/minecraft/entity/decoration/ArmorStandEntity canClip ()Z

accessible field net/minecraft/entity/Entity vehicle Lnet/minecraft/entity/Entity;
accessible method net/minecraft/entity/Entity addPassenger (Lnet/minecraft/entity/Entity;)V
accessible method net/minecraft/entity/Entity getPassengerDismountOffset (DDF)Lnet/minecraft/util/math/Vec3d;

0 comments on commit 80b4aab

Please sign in to comment.