| 1 | /*
|
| 2 | * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
|
| 3 | *
|
| 4 | * Copyright (c) 2015 - 2026 CCBlueX
|
| 5 | *
|
| 6 | * LiquidBounce is free software: you can redistribute it and/or modify
|
| 7 | * it under the terms of the GNU General Public License as published by
|
| 8 | * the Free Software Foundation, either version 3 of the License, or
|
| 9 | * (at your option) any later version.
|
| 10 | *
|
| 11 | * LiquidBounce is distributed in the hope that it will be useful,
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 | * GNU General Public License for more details.
|
| 15 | *
|
| 16 | * You should have received a copy of the GNU General Public License
|
| 17 | * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
|
| 18 | */
|
| 19 | @file:Suppress("All")
|
| 20 |
|
| 21 | package net.ccbluex.liquidbounce.utils.entity
|
| 22 |
|
| 23 | import net.ccbluex.liquidbounce.event.EventManager.callEvent
|
| 24 | import net.ccbluex.liquidbounce.event.events.PlayerMoveEvent
|
| 25 | import net.ccbluex.liquidbounce.event.events.PlayerSafeWalkEvent
|
| 26 | import net.ccbluex.liquidbounce.injection.mixins.minecraft.entity.MixinEntityFluidInteractionAccessor
|
| 27 | import net.ccbluex.liquidbounce.injection.mixins.minecraft.entity.MixinEntityFluidInteractionTrackerAccessor
|
| 28 | import net.ccbluex.liquidbounce.utils.block.getBlock
|
| 29 | import net.ccbluex.liquidbounce.utils.block.state
|
| 30 | import net.ccbluex.liquidbounce.utils.math.fastCos
|
| 31 | import net.ccbluex.liquidbounce.utils.math.fastSin
|
| 32 | import net.ccbluex.liquidbounce.utils.client.player
|
| 33 | import net.ccbluex.liquidbounce.utils.math.toRadians
|
| 34 | import net.ccbluex.liquidbounce.utils.math.plus
|
| 35 | import net.ccbluex.liquidbounce.utils.math.toBlockPos
|
| 36 | import net.ccbluex.liquidbounce.utils.movement.DirectionalInput
|
| 37 | import net.ccbluex.liquidbounce.utils.movement.getDegreesRelativeToView
|
| 38 | import net.ccbluex.liquidbounce.utils.movement.getDirectionalInputForDegrees
|
| 39 | import net.minecraft.client.player.ClientInput
|
| 40 | import net.minecraft.core.BlockPos
|
| 41 | import net.minecraft.core.Holder
|
| 42 | import net.minecraft.tags.BlockTags
|
| 43 | import net.minecraft.tags.FluidTags
|
| 44 | import net.minecraft.tags.TagKey
|
| 45 | import net.minecraft.util.Mth
|
| 46 | import net.minecraft.world.attribute.EnvironmentAttributes
|
| 47 | import net.minecraft.world.effect.MobEffect
|
| 48 | import net.minecraft.world.effect.MobEffectInstance
|
| 49 | import net.minecraft.world.effect.MobEffects
|
| 50 | import net.minecraft.world.entity.Entity
|
| 51 | import net.minecraft.world.entity.EntityFluidInteraction
|
| 52 | import net.minecraft.world.entity.MoverType
|
| 53 | import net.minecraft.world.entity.ai.attributes.Attribute
|
| 54 | import net.minecraft.world.entity.ai.attributes.Attributes
|
| 55 | import net.minecraft.world.entity.player.Player
|
| 56 | import net.minecraft.world.level.Level
|
| 57 | import net.minecraft.world.level.block.Blocks
|
| 58 | import net.minecraft.world.level.block.LadderBlock
|
| 59 | import net.minecraft.world.level.block.PowderSnowBlock
|
| 60 | import net.minecraft.world.level.block.TrapDoorBlock
|
| 61 | import net.minecraft.world.level.block.state.BlockState
|
| 62 | import net.minecraft.world.level.material.Fluid
|
| 63 | import net.minecraft.world.phys.AABB
|
| 64 | import net.minecraft.world.phys.Vec3
|
| 65 | import net.minecraft.world.phys.shapes.VoxelShape
|
| 66 | import kotlin.math.abs
|
| 67 | import kotlin.math.max
|
| 68 | import kotlin.math.sqrt
|
| 69 |
|
| 70 | private const val STEP_HEIGHT = 0.5
|
| 71 |
|
| 72 | class SimulatedPlayer(
|
| 73 | private val player: Player,
|
| 74 | var input: SimulatedPlayerInput,
|
| 75 | override var pos: Vec3,
|
| 76 | var deltaMovement: Vec3,
|
| 77 | var boundingBox: AABB,
|
| 78 | var yRot: Float,
|
| 79 | var xRot: Float,
|
| 80 | var isSprinting: Boolean,
|
| 81 |
|
| 82 | var fallDistance: Double,
|
| 83 | private var jumpTriggerTime: Int,
|
| 84 | private var jumping: Boolean,
|
| 85 | private var fallFlying: Boolean,
|
| 86 | var onGround: Boolean,
|
| 87 | var horizontalCollision: Boolean,
|
| 88 | private var verticalCollision: Boolean,
|
| 89 |
|
| 90 | private var wasTouchingWater: Boolean,
|
| 91 | private var isSwimming: Boolean,
|
| 92 | private var wasUnderwater: Boolean,
|
| 93 | private val fluidInteraction: EntityFluidInteraction,
|
| 94 | ) : PlayerSimulation {
|
| 95 | private val level: Level get() = player.level()
|
| 96 |
|
| 97 | companion object {
|
| 98 | private fun EntityFluidInteraction.deepCopy(): EntityFluidInteraction {
|
| 99 | val sourceTrackers = (this as MixinEntityFluidInteractionAccessor).trackerByFluid()
|
| 100 | val copy = EntityFluidInteraction(sourceTrackers.keys)
|
| 101 | @Suppress("CAST_NEVER_SUCCEEDS")
|
| 102 | val targetTrackers = (copy as MixinEntityFluidInteractionAccessor).trackerByFluid()
|
| 103 |
|
| 104 | for ((fluid, sourceTracker) in sourceTrackers) {
|
| 105 | val targetTracker = targetTrackers[fluid] ?: continue
|
| 106 |
|
| 107 | val sourceAccessor = sourceTracker as MixinEntityFluidInteractionTrackerAccessor
|
| 108 | val targetAccessor = targetTracker as MixinEntityFluidInteractionTrackerAccessor
|
| 109 |
|
| 110 | targetAccessor.height(sourceAccessor.height())
|
| 111 | targetAccessor.eyesInside(sourceAccessor.eyesInside())
|
| 112 | targetAccessor.accumulatedCurrent(sourceAccessor.accumulatedCurrent())
|
| 113 | targetAccessor.currentCount(sourceAccessor.currentCount())
|
| 114 | }
|
| 115 |
|
| 116 | return copy
|
| 117 | }
|
| 118 |
|
| 119 | @JvmStatic
|
| 120 | fun fromClientPlayer(input: SimulatedPlayerInput): SimulatedPlayer {
|
| 121 | return SimulatedPlayer(
|
| 122 | player,
|
| 123 | input,
|
| 124 | player.position(),
|
| 125 | player.deltaMovement,
|
| 126 | player.boundingBox,
|
| 127 | player.yRot,
|
| 128 | player.xRot,
|
| 129 |
|
| 130 | player.isSprinting,
|
| 131 |
|
| 132 | player.fallDistance,
|
| 133 | player.noJumpDelay,
|
| 134 | player.jumping,
|
| 135 | player.isFallFlying,
|
| 136 | player.onGround(),
|
| 137 | player.horizontalCollision,
|
| 138 | player.verticalCollision,
|
| 139 |
|
| 140 | player.isInWater,
|
| 141 | player.isSwimming,
|
| 142 | player.isUnderWater,
|
| 143 | player.fluidInteraction.deepCopy(),
|
| 144 | )
|
| 145 | }
|
| 146 |
|
| 147 | @JvmStatic
|
| 148 | fun fromOtherPlayer(player: Player, input: SimulatedPlayerInput): SimulatedPlayer {
|
| 149 | return SimulatedPlayer(
|
| 150 | player,
|
| 151 | input,
|
| 152 | player.position(),
|
| 153 | deltaMovement = player.position().subtract(player.lastPos),
|
| 154 | player.boundingBox,
|
| 155 | player.yRot,
|
| 156 | player.xRot,
|
| 157 |
|
| 158 | player.isSprinting,
|
| 159 |
|
| 160 | player.fallDistance,
|
| 161 | player.noJumpDelay,
|
| 162 | player.jumping,
|
| 163 | player.isFallFlying,
|
| 164 | player.onGround(),
|
| 165 | player.horizontalCollision,
|
| 166 | player.verticalCollision,
|
| 167 |
|
| 168 | player.isInWater,
|
| 169 | player.isSwimming,
|
| 170 | player.isUnderWater,
|
| 171 | player.fluidInteraction.deepCopy(),
|
| 172 | )
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private var simulatedTicks: Int = 0
|
| 177 | var clipLedged = false
|
| 178 | private set
|
| 179 |
|
| 180 | override fun tick() {
|
| 181 | clipLedged = false
|
| 182 |
|
| 183 | if (pos.y <= -70) {
|
| 184 | return
|
| 185 | }
|
| 186 |
|
| 187 | this.input.update()
|
| 188 |
|
| 189 | updateFluidInteraction()
|
| 190 | updateIsUnderwater()
|
| 191 | updateSwimming()
|
| 192 |
|
| 193 | if (this.jumpTriggerTime > 0) {
|
| 194 | this.jumpTriggerTime--
|
| 195 | }
|
| 196 |
|
| 197 | this.jumping = this.input.keyPresses.jump
|
| 198 |
|
| 199 | val movement = this.deltaMovement
|
| 200 |
|
| 201 | var motionX = movement.x
|
| 202 | var motionY = movement.y
|
| 203 | var motionZ = movement.z
|
| 204 |
|
| 205 | if (abs(movement.x) < 0.003) {
|
| 206 | motionX = 0.0
|
| 207 | }
|
| 208 | if (abs(movement.y) < 0.003) {
|
| 209 | motionY = 0.0
|
| 210 | }
|
| 211 | if (abs(movement.z) < 0.003) {
|
| 212 | motionZ = 0.0
|
| 213 | }
|
| 214 | if (onGround) {
|
| 215 | this.fallFlying = false
|
| 216 | }
|
| 217 |
|
| 218 | this.deltaMovement = Vec3(motionX, motionY, motionZ)
|
| 219 |
|
| 220 | if (this.jumping) {
|
| 221 | val fluidHeight =
|
| 222 | if (this.isInLava()) this.getFluidHeight(FluidTags.LAVA) else this.getFluidHeight(FluidTags.WATER)
|
| 223 | val inWater = this.isInWater() && fluidHeight > 0.0
|
| 224 |
|
| 225 | val swimHeight = this.getFluidJumpThreshold()
|
| 226 |
|
| 227 | if (inWater && (!this.onGround || fluidHeight > swimHeight)) {
|
| 228 | this.swimUpward(FluidTags.WATER)
|
| 229 | } else if (this.isInLava() && (!this.onGround || fluidHeight > swimHeight)) {
|
| 230 | this.swimUpward(FluidTags.LAVA)
|
| 231 | } else if ((this.onGround || inWater && fluidHeight <= swimHeight) && jumpTriggerTime == 0) {
|
| 232 | this.jumpFromGround()
|
| 233 | jumpTriggerTime = 10
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | val sidewaysSpeed = input.movementSideways * 0.98
|
| 238 | val forwardSpeed = input.movementForward * 0.98
|
| 239 | val upwardsSpeed = 0.0
|
| 240 |
|
| 241 | if (this.hasStatusEffect(MobEffects.SLOW_FALLING) || this.hasStatusEffect(MobEffects.LEVITATION)) {
|
| 242 | this.onLanding()
|
| 243 | }
|
| 244 |
|
| 245 | this.travel(Vec3(sidewaysSpeed, upwardsSpeed, forwardSpeed))
|
| 246 | simulatedTicks++
|
| 247 | }
|
| 248 |
|
| 249 | private fun travel(movementInput: Vec3) {
|
| 250 | if (this.isSwimming && !this.player.isPassenger) {
|
| 251 | val viewY = this.getViewVector().y
|
| 252 | val swimLift = if (viewY < -0.2) 0.085 else 0.06
|
| 253 | if (viewY <= 0.0 || this.input.keyPresses.jump || !this.player.level()
|
| 254 | .getBlockState(BlockPos.containing(this.pos.x, this.pos.y + 1.0 - 0.1, this.pos.z))
|
| 255 | .fluidState.isEmpty
|
| 256 | ) {
|
| 257 | deltaMovement = deltaMovement.add(0.0, (viewY - deltaMovement.y) * swimLift, 0.0)
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | val beforeTravelVelocityY = this.deltaMovement.y
|
| 262 |
|
| 263 | var gravity = 0.08
|
| 264 | val isFalling = deltaMovement.y <= 0.0
|
| 265 | if (deltaMovement.y <= 0.0 && hasStatusEffect(MobEffects.SLOW_FALLING)) {
|
| 266 | gravity = 0.01
|
| 267 | this.onLanding()
|
| 268 | }
|
| 269 |
|
| 270 | if (isInWater() && this.player.isAffectedByFluids) {
|
| 271 | val playerY: Double = this.pos.y
|
| 272 | var movementSpeed = if (isSprinting) 0.9f else 0.8f
|
| 273 | var movementEfficiency = 0.02f
|
| 274 | var waterMovementEfficiency = this.getAttributeValue(Attributes.WATER_MOVEMENT_EFFICIENCY).toFloat()
|
| 275 |
|
| 276 | if (!onGround) {
|
| 277 | waterMovementEfficiency *= 0.5f
|
| 278 | }
|
| 279 | if (waterMovementEfficiency > 0.0f) {
|
| 280 | movementSpeed += (0.54600006f - movementSpeed) * waterMovementEfficiency / 3.0f
|
| 281 | movementEfficiency += (this.getSpeed() - movementEfficiency) * waterMovementEfficiency / 3.0f
|
| 282 | }
|
| 283 | if (hasStatusEffect(MobEffects.DOLPHINS_GRACE)) {
|
| 284 | movementSpeed = 0.96f
|
| 285 | }
|
| 286 | this.updateVelocity(movementEfficiency, movementInput)
|
| 287 | this.move(deltaMovement)
|
| 288 | var movement = deltaMovement
|
| 289 | if (this.horizontalCollision && this.isClimbing()) {
|
| 290 | movement = Vec3(movement.x, 0.2, movement.z)
|
| 291 | }
|
| 292 | deltaMovement = movement.multiply(movementSpeed.toDouble(), 0.8, movementSpeed.toDouble())
|
| 293 | val adjustedMovement = this.player.getFluidFallingAdjustedMovement(gravity, isFalling, deltaMovement)
|
| 294 | deltaMovement = adjustedMovement
|
| 295 | if (this.horizontalCollision && this.doesNotCollide(
|
| 296 | adjustedMovement.x,
|
| 297 | adjustedMovement.y + 0.6 - this.pos.y + playerY,
|
| 298 | adjustedMovement.z
|
| 299 | )
|
| 300 | ) {
|
| 301 | this.deltaMovement = Vec3(adjustedMovement.x, 0.3, adjustedMovement.z)
|
| 302 | }
|
| 303 | } else if (isInLava() && this.player.isAffectedByFluids) {
|
| 304 | val playerY: Double = this.pos.y
|
| 305 | this.updateVelocity(0.02f, movementInput)
|
| 306 | this.move(deltaMovement)
|
| 307 | if (getFluidHeight(FluidTags.LAVA) <= getFluidJumpThreshold()) {
|
| 308 | deltaMovement = deltaMovement.multiply(0.5, 0.8, 0.5)
|
| 309 | deltaMovement = this.player.getFluidFallingAdjustedMovement(gravity, isFalling, deltaMovement)
|
| 310 | } else {
|
| 311 | deltaMovement = deltaMovement.scale(0.5)
|
| 312 | }
|
| 313 | if (!this.player.isNoGravity) {
|
| 314 | deltaMovement = this.deltaMovement.add(0.0, -gravity / 4.0, 0.0)
|
| 315 | }
|
| 316 | if (this.horizontalCollision && this.doesNotCollide(
|
| 317 | deltaMovement.x,
|
| 318 | deltaMovement.y + 0.6 - this.pos.y + playerY,
|
| 319 | deltaMovement.z
|
| 320 | )
|
| 321 | ) {
|
| 322 | deltaMovement = Vec3(deltaMovement.x, 0.3, deltaMovement.z)
|
| 323 | }
|
| 324 | } else if (this.fallFlying) {
|
| 325 | var lift: Double
|
| 326 | var velocity: Vec3 = this.deltaMovement
|
| 327 | if (velocity.y > -0.5) {
|
| 328 | fallDistance = 1.0
|
| 329 | }
|
| 330 | val vec3d3 = this.getViewVector()
|
| 331 | val pitchRadians: Float = this.xRot * (Math.PI.toFloat() / 180)
|
| 332 | val horizontalViewMagnitude = sqrt(vec3d3.x * vec3d3.x + vec3d3.z * vec3d3.z)
|
| 333 | val horizontalVelocity = velocity.horizontalDistance()
|
| 334 | val viewVectorLength = vec3d3.length()
|
| 335 | var liftFactor = pitchRadians.fastCos()
|
| 336 | liftFactor =
|
| 337 | (liftFactor.toDouble() * (liftFactor.toDouble() * 1.0.coerceAtMost(viewVectorLength / 0.4))).toFloat()
|
| 338 | velocity = this.deltaMovement.add(0.0, gravity * (-1.0 + liftFactor.toDouble() * 0.75), 0.0)
|
| 339 | if (velocity.y < 0.0 && horizontalViewMagnitude > 0.0) {
|
| 340 | lift = velocity.y * -0.1 * liftFactor.toDouble()
|
| 341 | velocity = velocity.add(
|
| 342 | vec3d3.x * lift / horizontalViewMagnitude,
|
| 343 | lift,
|
| 344 | vec3d3.z * lift / horizontalViewMagnitude
|
| 345 | )
|
| 346 | }
|
| 347 | if (pitchRadians < 0.0f && horizontalViewMagnitude > 0.0) {
|
| 348 | lift = horizontalVelocity * (-pitchRadians.fastSin()).toDouble() * 0.04
|
| 349 | velocity = velocity.add(
|
| 350 | -vec3d3.x * lift / horizontalViewMagnitude,
|
| 351 | lift * 3.2,
|
| 352 | -vec3d3.z * lift / horizontalViewMagnitude
|
| 353 | )
|
| 354 | }
|
| 355 | if (horizontalViewMagnitude > 0.0) {
|
| 356 | velocity = velocity.add(
|
| 357 | (vec3d3.x / horizontalViewMagnitude * horizontalVelocity - velocity.x) * 0.1,
|
| 358 | 0.0,
|
| 359 | (vec3d3.z / horizontalViewMagnitude * horizontalVelocity - velocity.z) * 0.1
|
| 360 | )
|
| 361 | }
|
| 362 | this.deltaMovement = velocity.multiply(0.99, 0.98, 0.99)
|
| 363 |
|
| 364 | move(this.deltaMovement)
|
| 365 | } else {
|
| 366 | val blockPos = this.getBlockPosBelowThatAffectsMyMovement()
|
| 367 | val p: Float = this.player.level().getBlockState(blockPos).block.friction
|
| 368 | val friction = if (onGround) p * 0.91f else 0.91f
|
| 369 | val movement = this.applyMovementInput(movementInput, p)
|
| 370 | var verticalMovement = movement.y
|
| 371 | if (hasStatusEffect(MobEffects.LEVITATION)) {
|
| 372 | verticalMovement += (0.05 * (getStatusEffect(MobEffects.LEVITATION)!!.amplifier + 1).toDouble() - movement.y) * 0.2
|
| 373 | } else if (this.player.level().isClientSide && !this.player.level().hasChunkAt(blockPos.x, blockPos.z)) {
|
| 374 | verticalMovement = if (this.pos.y > this.player.level().minY.toDouble()) {
|
| 375 | -0.1
|
| 376 | } else {
|
| 377 | 0.0
|
| 378 | }
|
| 379 | } else if (!this.player.isNoGravity) {
|
| 380 | verticalMovement -= gravity
|
| 381 | }
|
| 382 |
|
| 383 | deltaMovement = if (this.player.shouldDiscardFriction()) {
|
| 384 | Vec3(movement.x, verticalMovement, movement.z)
|
| 385 | } else {
|
| 386 | Vec3(
|
| 387 | movement.x * friction.toDouble(),
|
| 388 | verticalMovement * 0.9800000190734863,
|
| 389 | movement.z * friction.toDouble()
|
| 390 | )
|
| 391 | }
|
| 392 | }
|
| 393 |
|
| 394 | if (player.abilities.flying && !this.player.isPassenger) {
|
| 395 | deltaMovement = Vec3(deltaMovement.x, beforeTravelVelocityY * 0.6, deltaMovement.z)
|
| 396 | this.onLanding()
|
| 397 | }
|
| 398 | }
|
| 399 |
|
| 400 | /**
|
| 401 | * @see net.minecraft.world.entity.LivingEntity.handleRelativeFrictionAndCalculateMovement(Vec3, float)
|
| 402 | */
|
| 403 | private fun applyMovementInput(movementInput: Vec3, slipperiness: Float): Vec3 {
|
| 404 | this.updateVelocity(this.getFrictionInfluencedSpeed(slipperiness), movementInput)
|
| 405 | this.deltaMovement = handleOnClimbable(this.deltaMovement)
|
| 406 | this.deltaMovement = applyWebSpeed(this.deltaMovement)
|
| 407 | this.move(this.deltaMovement)
|
| 408 |
|
| 409 |
|
| 410 | var vec3d = this.deltaMovement
|
| 411 | if ((horizontalCollision || this.jumping) && (
|
| 412 | this.isClimbing() || pos.toBlockPos().state
|
| 413 | ?.`is`(Blocks.POWDER_SNOW) == true && PowderSnowBlock.canEntityWalkOnPowderSnow(player)
|
| 414 | )
|
| 415 | ) {
|
| 416 | vec3d = Vec3(vec3d.x, 0.2, vec3d.z)
|
| 417 | }
|
| 418 |
|
| 419 | return vec3d
|
| 420 | }
|
| 421 |
|
| 422 | private fun updateVelocity(speed: Float, movementInput: Vec3) {
|
| 423 | val vec3d = Entity.getInputVector(movementInput, speed, this.yRot)
|
| 424 |
|
| 425 | this.deltaMovement += vec3d
|
| 426 | }
|
| 427 |
|
| 428 | /**
|
| 429 | * @see net.minecraft.world.entity.LivingEntity.getFrictionInfluencedSpeed(float)
|
| 430 | */
|
| 431 | private fun getFrictionInfluencedSpeed(slipperiness: Float): Float {
|
| 432 | return if (this.onGround) {
|
| 433 | getSpeed() * (0.21600002f / (slipperiness * slipperiness * slipperiness))
|
| 434 | } else {
|
| 435 | this.getAirStrafingSpeed()
|
| 436 | }
|
| 437 | }
|
| 438 |
|
| 439 | private fun getAirStrafingSpeed(): Float {
|
| 440 | val speed = 0.02f
|
| 441 |
|
| 442 | if (this.input.sprinting) {
|
| 443 | return (speed + 0.005999999865889549).toFloat()
|
| 444 | }
|
| 445 |
|
| 446 | return speed
|
| 447 | }
|
| 448 |
|
| 449 | private fun getSpeed(): Float = 0.10000000149011612.toFloat()
|
| 450 |
|
| 451 | private fun move(input: Vec3) {
|
| 452 | val event = callEvent(PlayerMoveEvent(MoverType.SELF, input))
|
| 453 | val movement = event.movement
|
| 454 |
|
| 455 | val backedOffMovement = this.maybeBackOffFromEdge(movement)
|
| 456 | val adjustedMovement = this.adjustMovementForCollisions(backedOffMovement)
|
| 457 |
|
| 458 | if (adjustedMovement.lengthSqr() > 1.0E-7) {
|
| 459 | this.pos += adjustedMovement
|
| 460 | this.boundingBox = player.dimensions.makeBoundingBox(this.pos)
|
| 461 | }
|
| 462 |
|
| 463 | val xCollision = !Mth.equal(backedOffMovement.x, adjustedMovement.x)
|
| 464 | val zCollision = !Mth.equal(backedOffMovement.z, adjustedMovement.z)
|
| 465 |
|
| 466 | this.horizontalCollision = xCollision || zCollision
|
| 467 | this.verticalCollision = backedOffMovement.y != adjustedMovement.y
|
| 468 |
|
| 469 | onGround = verticalCollision && backedOffMovement.y < 0.0
|
| 470 |
|
| 471 | if (!isInWater()) {
|
| 472 | updateFluidInteraction()
|
| 473 | }
|
| 474 |
|
| 475 | if (onGround) {
|
| 476 | onLanding()
|
| 477 | } else if (backedOffMovement.y < 0) {
|
| 478 | fallDistance -= backedOffMovement.y.toFloat()
|
| 479 | }
|
| 480 |
|
| 481 | val vec3d2: Vec3 = this.deltaMovement
|
| 482 | if (horizontalCollision || verticalCollision) {
|
| 483 | this.deltaMovement = Vec3(
|
| 484 | if (xCollision) 0.0 else vec3d2.x,
|
| 485 | if (onGround) 0.0 else vec3d2.y,
|
| 486 | if (zCollision) 0.0 else vec3d2.z
|
| 487 | )
|
| 488 | }
|
| 489 | }
|
| 490 |
|
| 491 | private fun adjustMovementForCollisions(movement: Vec3): Vec3 {
|
| 492 | val onGroundOrFalling: Boolean
|
| 493 | val collisionBox: AABB = AABB(-0.3, 0.0, -0.3, 0.3, 1.8, 0.3).move(this.pos)
|
| 494 |
|
| 495 | val entityCollisionList = emptyList<VoxelShape>()
|
| 496 |
|
| 497 | val adjustedMovement = if (movement.lengthSqr() == 0.0) {
|
| 498 | movement
|
| 499 | } else {
|
| 500 | Entity.collideBoundingBox(
|
| 501 | this.player,
|
| 502 | movement,
|
| 503 | collisionBox,
|
| 504 | this.player.level(),
|
| 505 | entityCollisionList
|
| 506 | )
|
| 507 | }
|
| 508 | val collidedX = movement.x != adjustedMovement.x
|
| 509 | val collidedY = movement.y != adjustedMovement.y
|
| 510 | val collidedZ = movement.z != adjustedMovement.z
|
| 511 |
|
| 512 | onGroundOrFalling = onGround || collidedY && movement.y < 0.0
|
| 513 |
|
| 514 | if (this.player.maxUpStep() > 0.0f && onGroundOrFalling && (collidedX || collidedZ)) {
|
| 515 | var steppedMovement = Entity.collideBoundingBox(
|
| 516 | this.player,
|
| 517 | Vec3(movement.x, this.player.maxUpStep().toDouble(), movement.z),
|
| 518 | collisionBox,
|
| 519 | this.player.level(),
|
| 520 | entityCollisionList
|
| 521 | )
|
| 522 | val stepUpMovement = Entity.collideBoundingBox(
|
| 523 | this.player,
|
| 524 | Vec3(0.0, this.player.maxUpStep().toDouble(), 0.0),
|
| 525 | collisionBox.expandTowards(movement.x, 0.0, movement.z),
|
| 526 | this.player.level(),
|
| 527 | entityCollisionList
|
| 528 | )
|
| 529 | val stepDownMovement = Entity.collideBoundingBox(
|
| 530 | this.player,
|
| 531 | Vec3(movement.x, 0.0, movement.z),
|
| 532 | collisionBox.move(stepUpMovement),
|
| 533 | this.player.level(),
|
| 534 | entityCollisionList
|
| 535 | ).add(stepUpMovement)
|
| 536 |
|
| 537 | if (stepUpMovement.y < this.player.maxUpStep()
|
| 538 | .toDouble() && stepDownMovement.horizontalDistanceSqr() > steppedMovement.horizontalDistanceSqr()
|
| 539 | ) {
|
| 540 | steppedMovement = stepDownMovement
|
| 541 | }
|
| 542 |
|
| 543 | if (steppedMovement.horizontalDistanceSqr() > adjustedMovement.horizontalDistanceSqr()) {
|
| 544 | return steppedMovement.add(
|
| 545 | Entity.collideBoundingBox(
|
| 546 | this.player,
|
| 547 | Vec3(0.0, -steppedMovement.y + movement.y, 0.0),
|
| 548 | collisionBox.move(steppedMovement),
|
| 549 | this.player.level(),
|
| 550 | entityCollisionList
|
| 551 | )
|
| 552 | )
|
| 553 | }
|
| 554 | }
|
| 555 | return adjustedMovement
|
| 556 | }
|
| 557 |
|
| 558 | private fun onLanding() {
|
| 559 | this.fallDistance = 0.0
|
| 560 | }
|
| 561 |
|
| 562 | /**
|
| 563 | * @see net.minecraft.world.entity.LivingEntity.jumpFromGround()
|
| 564 | */
|
| 565 | fun jumpFromGround() {
|
| 566 | val jumpPower = this.getJumpPower().toDouble()
|
| 567 | this.deltaMovement = Vec3(this.deltaMovement.x, max(jumpPower, this.deltaMovement.y), this.deltaMovement.z)
|
| 568 |
|
| 569 | if (isSprinting) {
|
| 570 | val yawRadians: Float = this.yRot.toRadians()
|
| 571 |
|
| 572 | this.deltaMovement += Vec3(
|
| 573 | (-yawRadians.fastSin() * 0.2f).toDouble(),
|
| 574 | 0.0,
|
| 575 | (yawRadians.fastCos() * 0.2f).toDouble()
|
| 576 | )
|
| 577 | }
|
| 578 |
|
| 579 | }
|
| 580 |
|
| 581 | fun jump() = jumpFromGround()
|
| 582 |
|
| 583 | /**
|
| 584 | * @see net.minecraft.world.entity.LivingEntity.handleOnClimbable(Vec3)
|
| 585 | */
|
| 586 | private fun handleOnClimbable(motion: Vec3): Vec3 {
|
| 587 | if (!isClimbing()) {
|
| 588 | return motion
|
| 589 | }
|
| 590 |
|
| 591 | onLanding()
|
| 592 | val clampedX = Mth.clamp(motion.x, -0.15000000596046448, 0.15000000596046448)
|
| 593 | val clampedZ = Mth.clamp(motion.z, -0.15000000596046448, 0.15000000596046448)
|
| 594 | var clampedY = max(motion.y, -0.15000000596046448)
|
| 595 | if (clampedY < 0.0 && !pos.toBlockPos().state!!
|
| 596 | .`is`(Blocks.SCAFFOLDING) && player.isSuppressingSlidingDownLadder
|
| 597 | ) {
|
| 598 | clampedY = 0.0
|
| 599 | }
|
| 600 |
|
| 601 | return Vec3(clampedX, clampedY, clampedZ)
|
| 602 | }
|
| 603 |
|
| 604 | private fun applyWebSpeed(motion: Vec3): Vec3 {
|
| 605 | val blockState = level.getBlockState(pos.toBlockPos())
|
| 606 | if (blockState.block != Blocks.COBWEB) {
|
| 607 | return motion
|
| 608 | }
|
| 609 | val multiplier = if (hasStatusEffect(MobEffects.WEAVING)) {
|
| 610 | Vec3(0.5, 0.25, 0.5)
|
| 611 | } else {
|
| 612 | Vec3(0.25, 0.05, 0.25)
|
| 613 | }
|
| 614 | return motion.multiply(multiplier.x, multiplier.y, multiplier.z)
|
| 615 | }
|
| 616 |
|
| 617 | private fun isClimbing(): Boolean {
|
| 618 | val blockPos = pos.toBlockPos()
|
| 619 | val blockState = blockPos.state!!
|
| 620 | return if (blockState.`is`(BlockTags.CLIMBABLE)) {
|
| 621 | true
|
| 622 | } else if (blockState.block is TrapDoorBlock && this.trapdoorUsableAsLadder(blockPos, blockState)) {
|
| 623 | true
|
| 624 | } else {
|
| 625 | false
|
| 626 | }
|
| 627 | }
|
| 628 |
|
| 629 | /**
|
| 630 | * @see net.minecraft.world.entity.LivingEntity.trapdoorUsableAsLadder(BlockPos, BlockState)
|
| 631 | */
|
| 632 | private fun trapdoorUsableAsLadder(pos: BlockPos, state: BlockState): Boolean {
|
| 633 | if (!state.getValue(TrapDoorBlock.OPEN)) {
|
| 634 | return false
|
| 635 | }
|
| 636 | val blockState = this.player.level().getBlockState(pos.below())
|
| 637 | return blockState.`is`(Blocks.LADDER) && blockState.getValue(LadderBlock.FACING) == state.getValue(TrapDoorBlock.FACING)
|
| 638 | }
|
| 639 |
|
| 640 | /**
|
| 641 | * @see net.minecraft.world.entity.player.Player.maybeBackOffFromEdge(Vec3, MoverType)
|
| 642 | */
|
| 643 | private fun maybeBackOffFromEdge(movement: Vec3): Vec3 {
|
| 644 | var adjustedMovement = movement
|
| 645 |
|
| 646 | if (adjustedMovement.y <= 0.0 && this.isAboveGround()) {
|
| 647 | var xMovement = adjustedMovement.x
|
| 648 | var zMovement = adjustedMovement.z
|
| 649 | val step = 0.05
|
| 650 | while (xMovement != 0.0 && level.noCollision(
|
| 651 | player,
|
| 652 | boundingBox.move(xMovement, -STEP_HEIGHT, 0.0)
|
| 653 | )
|
| 654 | ) {
|
| 655 | if (xMovement < step && xMovement >= -step) {
|
| 656 | xMovement = 0.0
|
| 657 | continue
|
| 658 | }
|
| 659 | if (xMovement > 0.0) {
|
| 660 | xMovement -= step
|
| 661 | continue
|
| 662 | }
|
| 663 | xMovement += step
|
| 664 | }
|
| 665 | while (zMovement != 0.0 && level.noCollision(
|
| 666 | player,
|
| 667 | boundingBox.move(0.0, -STEP_HEIGHT, zMovement)
|
| 668 | )
|
| 669 | ) {
|
| 670 | if (zMovement < step && zMovement >= -step) {
|
| 671 | zMovement = 0.0
|
| 672 | continue
|
| 673 | }
|
| 674 | if (zMovement > 0.0) {
|
| 675 | zMovement -= step
|
| 676 | continue
|
| 677 | }
|
| 678 | zMovement += step
|
| 679 | }
|
| 680 | while (xMovement != 0.0 && zMovement != 0.0 && level.noCollision(
|
| 681 | player,
|
| 682 | boundingBox.move(xMovement, -STEP_HEIGHT, zMovement)
|
| 683 | )
|
| 684 | ) {
|
| 685 | xMovement =
|
| 686 | if (xMovement < step && xMovement >= -step) 0.0 else if (xMovement > 0.0) {
|
| 687 | xMovement - step
|
| 688 | } else {
|
| 689 | xMovement + step
|
| 690 | }
|
| 691 | if (zMovement < step && zMovement >= -step) {
|
| 692 | zMovement = 0.0
|
| 693 | continue
|
| 694 | }
|
| 695 | if (zMovement > 0.0) {
|
| 696 | zMovement -= step
|
| 697 | continue
|
| 698 | }
|
| 699 | zMovement += step
|
| 700 | }
|
| 701 |
|
| 702 | if (adjustedMovement.x != xMovement || adjustedMovement.z != zMovement) {
|
| 703 | clipLedged = true
|
| 704 | }
|
| 705 |
|
| 706 | if (this.shouldClipAtLedge()) {
|
| 707 | adjustedMovement = Vec3(xMovement, adjustedMovement.y, zMovement)
|
| 708 | }
|
| 709 | }
|
| 710 | return adjustedMovement
|
| 711 | }
|
| 712 |
|
| 713 | private fun shouldClipAtLedge(): Boolean {
|
| 714 | return !this.input.ignoreClippingAtLedge && (this.input.keyPresses.shift || this.input.forceSafeWalk)
|
| 715 | }
|
| 716 |
|
| 717 | private fun isAboveGround(): Boolean {
|
| 718 | return onGround || this.fallDistance < STEP_HEIGHT && !level.noCollision(
|
| 719 | player,
|
| 720 | boundingBox.move(0.0, this.fallDistance - STEP_HEIGHT, 0.0)
|
| 721 | )
|
| 722 | }
|
| 723 |
|
| 724 | /**
|
| 725 | * Mirrors 26.1 `LivingEntity#getJumpPower()`.
|
| 726 | * @see net.minecraft.world.entity.LivingEntity.getJumpPower
|
| 727 | */
|
| 728 | private fun getJumpPower(): Float =
|
| 729 | this.getAttributeValue(Attributes.JUMP_STRENGTH)
|
| 730 | .toFloat() * this.getJumpVelocityMultiplier() + this.getJumpBoostPower()
|
| 731 |
|
| 732 | /**
|
| 733 | * Mirrors 26.1 `LivingEntity#getJumpBoostPower()`.
|
| 734 | * @see net.minecraft.world.entity.LivingEntity.getJumpBoostPower
|
| 735 | */
|
| 736 | private fun getJumpBoostPower() =
|
| 737 | if (hasStatusEffect(MobEffects.JUMP_BOOST)) {
|
| 738 | 0.1f * (getStatusEffect(MobEffects.JUMP_BOOST)!!.amplifier.toFloat() + 1f)
|
| 739 | } else {
|
| 740 | 0f
|
| 741 | }
|
| 742 |
|
| 743 | /**
|
| 744 | * Mirrors the 26.1 block jump factor lookup used by `LivingEntity#getJumpPower()`.
|
| 745 | * @see net.minecraft.world.entity.Entity.getBlockJumpFactor
|
| 746 | */
|
| 747 | private fun getJumpVelocityMultiplier(): Float {
|
| 748 | val f = pos.toBlockPos().getBlock()?.jumpFactor ?: 0f
|
| 749 | val g = getBlockPosBelowThatAffectsMyMovement().getBlock()?.jumpFactor ?: 0f
|
| 750 |
|
| 751 | return if (f.toDouble() == 1.0) g else f
|
| 752 | }
|
| 753 |
|
| 754 | private fun doesNotCollide(offsetX: Double, offsetY: Double, offsetZ: Double): Boolean {
|
| 755 | return this.doesNotCollide(this.boundingBox.move(offsetX, offsetY, offsetZ))
|
| 756 | }
|
| 757 |
|
| 758 | private fun doesNotCollide(box: AABB): Boolean {
|
| 759 | return this.player.level().noCollision(this.player, box) && !this.player.level().containsAnyLiquid(box)
|
| 760 | }
|
| 761 |
|
| 762 | private fun swimUpward(fluid: TagKey<Fluid>) {
|
| 763 | deltaMovement += Vec3(
|
| 764 | 0.0,
|
| 765 | if (fluid === FluidTags.WATER) 0.03999999910593033 else 0.005999999865889549,
|
| 766 | 0.0
|
| 767 | )
|
| 768 | }
|
| 769 |
|
| 770 | /**
|
| 771 | * Mirrors 26.1 `Entity#getBlockPosBelowThatAffectsMyMovement()`.
|
| 772 | * @see net.minecraft.world.entity.Entity.getBlockPosBelowThatAffectsMyMovement
|
| 773 | */
|
| 774 | private fun getBlockPosBelowThatAffectsMyMovement() =
|
| 775 | BlockPos.containing(this.pos.x, this.boundingBox.minY - 0.5000001, this.pos.z)
|
| 776 |
|
| 777 | /**
|
| 778 | * Mirrors 26.1 `Entity#getFluidJumpThreshold()`.
|
| 779 | * @see net.minecraft.world.entity.Entity.getFluidJumpThreshold
|
| 780 | */
|
| 781 | private fun getFluidJumpThreshold(): Double {
|
| 782 | return if (player.eyeHeight.toDouble() < 0.4) 0.0 else 0.4
|
| 783 | }
|
| 784 |
|
| 785 | /**
|
| 786 | * Mirrors 26.1 `Entity#isEyeInFluid(TagKey)`, restricted to water for the underwater state.
|
| 787 | * @see net.minecraft.world.entity.Entity.isEyeInFluid
|
| 788 | */
|
| 789 | private fun isEyeInWater(): Boolean {
|
| 790 | return this.fluidInteraction.isEyeInFluid(FluidTags.WATER)
|
| 791 | }
|
| 792 |
|
| 793 | private fun isInWater(): Boolean = wasTouchingWater
|
| 794 |
|
| 795 | /**
|
| 796 | * @see Entity.isInLava
|
| 797 | */
|
| 798 | private fun isInLava(): Boolean {
|
| 799 | return this.fluidInteraction.isInFluid(FluidTags.LAVA)
|
| 800 | }
|
| 801 |
|
| 802 | /**
|
| 803 | * Mirrors 26.1 `Player#updateSwimming()`.
|
| 804 | * @see net.minecraft.world.entity.player.Player.updateSwimming
|
| 805 | */
|
| 806 | private fun updateSwimming() {
|
| 807 | isSwimming = if (this.isSwimming) {
|
| 808 | isSprinting && isInWater() && !this.player.isPassenger
|
| 809 | } else {
|
| 810 | isSprinting && this.isSubmergedInWater() &&
|
| 811 | !this.player.isPassenger &&
|
| 812 | this.player.level()
|
| 813 | .getFluidState(this.pos.toBlockPos())
|
| 814 | .`is`(FluidTags.WATER)
|
| 815 | }
|
| 816 | }
|
| 817 |
|
| 818 | /**
|
| 819 | * Mirrors 26.1 `Entity#updateFluidInteraction()`.
|
| 820 | * @see net.minecraft.world.entity.Entity.updateFluidInteraction
|
| 821 | */
|
| 822 | private fun updateFluidInteraction(): Boolean {
|
| 823 | this.fluidInteraction.update(this.player, !this.player.isAffectedByFluids)
|
| 824 |
|
| 825 | val inWater = this.fluidInteraction.isInFluid(FluidTags.WATER)
|
| 826 | val inLava = this.fluidInteraction.isInFluid(FluidTags.LAVA)
|
| 827 |
|
| 828 | if (inWater) {
|
| 829 | onLanding()
|
| 830 | }
|
| 831 |
|
| 832 | this.wasTouchingWater = inWater
|
| 833 | if (this.player.isAffectedByFluids) {
|
| 834 | if (inWater) {
|
| 835 | this.fluidInteraction.applyCurrentTo(FluidTags.WATER, this.player, 0.014)
|
| 836 | }
|
| 837 |
|
| 838 | if (inLava) {
|
| 839 | val lavaFlowScale = if (this.level.environmentAttributes()
|
| 840 | .getDimensionValue(EnvironmentAttributes.FAST_LAVA)
|
| 841 | ) {
|
| 842 | 0.007
|
| 843 | } else {
|
| 844 | 0.0023333333333333335
|
| 845 | }
|
| 846 | this.fluidInteraction.applyCurrentTo(FluidTags.LAVA, this.player, lavaFlowScale)
|
| 847 | }
|
| 848 | }
|
| 849 |
|
| 850 | return inWater || inLava
|
| 851 | }
|
| 852 |
|
| 853 | /**
|
| 854 | * Mirrors 26.1 `Player#updateIsUnderwater()`.
|
| 855 | * @see net.minecraft.world.entity.player.Player.updateIsUnderwater
|
| 856 | */
|
| 857 | private fun updateIsUnderwater(): Boolean {
|
| 858 | this.wasUnderwater = this.isEyeInWater()
|
| 859 | return this.wasUnderwater
|
| 860 | }
|
| 861 |
|
| 862 | private fun isSubmergedInWater(): Boolean {
|
| 863 | return this.wasUnderwater && isInWater()
|
| 864 | }
|
| 865 |
|
| 866 | private fun getFluidHeight(tags: TagKey<Fluid>): Double =
|
| 867 | this.fluidInteraction.getFluidHeight(tags)
|
| 868 |
|
| 869 | /**
|
| 870 | * Mirrors 26.1 `Entity#getViewVector()`.
|
| 871 | * @see net.minecraft.world.entity.Entity.getViewVector
|
| 872 | */
|
| 873 | private fun getViewVector(): Vec3 = calculateViewVector(this.xRot, this.yRot)
|
| 874 |
|
| 875 | /**
|
| 876 | * Mirrors 26.1 `Entity#calculateViewVector(float, float)`.
|
| 877 | * @see net.minecraft.world.entity.Entity.calculateViewVector
|
| 878 | */
|
| 879 | private fun calculateViewVector(xRot: Float, yRot: Float): Vec3 {
|
| 880 | val realXRot = xRot * (Math.PI.toFloat() / 180f)
|
| 881 | val realYRot = -yRot * (Math.PI.toFloat() / 180f)
|
| 882 | val yCos = Mth.cos(realYRot.toDouble())
|
| 883 | val ySin = Mth.sin(realYRot.toDouble())
|
| 884 | val xCos = Mth.cos(realXRot.toDouble())
|
| 885 | val xSin = Mth.sin(realXRot.toDouble())
|
| 886 | return Vec3((ySin * xCos).toDouble(), (-xSin).toDouble(), (yCos * xCos).toDouble())
|
| 887 | }
|
| 888 |
|
| 889 | private fun hasStatusEffect(effect: Holder<MobEffect>): Boolean {
|
| 890 | val instance = player.getEffect(effect) ?: return false
|
| 891 |
|
| 892 | return instance.duration >= this.simulatedTicks
|
| 893 | }
|
| 894 |
|
| 895 | private fun getStatusEffect(effect: Holder<MobEffect>): MobEffectInstance? {
|
| 896 | val instance = player.getEffect(effect) ?: return null
|
| 897 |
|
| 898 | if (instance.duration < this.simulatedTicks) {
|
| 899 | return null
|
| 900 | }
|
| 901 |
|
| 902 | return instance
|
| 903 | }
|
| 904 |
|
| 905 | fun getAttributeValue(attribute: Holder<Attribute>): Double {
|
| 906 | return player.attributes.getValue(attribute)
|
| 907 | }
|
| 908 |
|
| 909 | fun clone(): SimulatedPlayer {
|
| 910 | return SimulatedPlayer(
|
| 911 | player,
|
| 912 | input,
|
| 913 | pos,
|
| 914 | deltaMovement,
|
| 915 | boundingBox,
|
| 916 | yRot,
|
| 917 | xRot,
|
| 918 | isSprinting,
|
| 919 | fallDistance,
|
| 920 | jumpTriggerTime,
|
| 921 | jumping,
|
| 922 | fallFlying,
|
| 923 | onGround,
|
| 924 | horizontalCollision,
|
| 925 | verticalCollision,
|
| 926 | wasTouchingWater,
|
| 927 | isSwimming,
|
| 928 | wasUnderwater,
|
| 929 | fluidInteraction.deepCopy(),
|
| 930 | )
|
| 931 | }
|
| 932 |
|
| 933 | class SimulatedPlayerInput(
|
| 934 | val directionalInput: DirectionalInput,
|
| 935 | jumping: Boolean,
|
| 936 | var sprinting: Boolean,
|
| 937 | sneaking: Boolean,
|
| 938 | var ignoreClippingAtLedge: Boolean = false
|
| 939 | ) : ClientInput() {
|
| 940 | var forceSafeWalk: Boolean = false
|
| 941 |
|
| 942 | init {
|
| 943 | set(
|
| 944 | forward = directionalInput.forwards,
|
| 945 | backward = directionalInput.backwards,
|
| 946 | left = directionalInput.left,
|
| 947 | right = directionalInput.right,
|
| 948 | jump = jumping,
|
| 949 | sneak = sneaking
|
| 950 | )
|
| 951 | }
|
| 952 |
|
| 953 | fun update() {
|
| 954 | if (this.keyPresses.forward != this.keyPresses.backward) {
|
| 955 | this.movementForward = if (this.keyPresses.forward) 1.0f else -1.0f
|
| 956 | } else {
|
| 957 | this.movementForward = 0.0f
|
| 958 | }
|
| 959 |
|
| 960 | movementSideways = if (keyPresses.left == keyPresses.right) {
|
| 961 | 0.0f
|
| 962 | } else if (keyPresses.left) {
|
| 963 | 1.0f
|
| 964 | } else {
|
| 965 | -1.0f
|
| 966 | }
|
| 967 |
|
| 968 | if (keyPresses.shift) {
|
| 969 | movementSideways = (movementSideways.toDouble() * 0.3).toFloat()
|
| 970 | movementForward = (movementForward.toDouble() * 0.3).toFloat()
|
| 971 | }
|
| 972 | }
|
| 973 |
|
| 974 | override fun toString(): String {
|
| 975 | return "SimulatedPlayerInput(forwards={${this.keyPresses.forward}}, backwards={${this.keyPresses.backward}}, left={${this.keyPresses.left}}, right={${this.keyPresses.right}}, jumping={${this.keyPresses.jump}}, sprinting=$sprinting, slowDown=${keyPresses.shift})"
|
| 976 | }
|
| 977 |
|
| 978 | companion object {
|
| 979 | private const val MAX_WALKING_SPEED = 0.121
|
| 980 |
|
| 981 | @JvmStatic
|
| 982 | fun fromClientPlayer(
|
| 983 | directionalInput: DirectionalInput,
|
| 984 | jump: Boolean = player.input.keyPresses.jump,
|
| 985 | sprinting: Boolean = player.isSprinting,
|
| 986 | sneaking: Boolean = player.isShiftKeyDown
|
| 987 | ): SimulatedPlayerInput {
|
| 988 | val input = SimulatedPlayerInput(
|
| 989 | directionalInput,
|
| 990 | jump,
|
| 991 | sprinting,
|
| 992 | sneaking
|
| 993 | )
|
| 994 |
|
| 995 | val safeWalkEvent = PlayerSafeWalkEvent()
|
| 996 |
|
| 997 | callEvent(safeWalkEvent)
|
| 998 |
|
| 999 | if (safeWalkEvent.isSafeWalk) {
|
| 1000 | input.forceSafeWalk = true
|
| 1001 | }
|
| 1002 |
|
| 1003 | return input
|
| 1004 | }
|
| 1005 |
|
| 1006 | /**
|
| 1007 | * Guesses the current input of a server player based on player position and velocity
|
| 1008 | */
|
| 1009 | @JvmStatic
|
| 1010 | fun guessInput(entity: Player): SimulatedPlayerInput {
|
| 1011 | val velocity = entity.position().subtract(entity.lastPos)
|
| 1012 |
|
| 1013 | val horizontalVelocity = velocity.horizontalDistanceSqr()
|
| 1014 |
|
| 1015 | val sprinting = horizontalVelocity >= MAX_WALKING_SPEED * MAX_WALKING_SPEED
|
| 1016 |
|
| 1017 | val input = if (horizontalVelocity > 0.05 * 0.05) {
|
| 1018 | val velocityAngle = getDegreesRelativeToView(velocity, yaw = entity.yRot)
|
| 1019 |
|
| 1020 | val velocityAngle1 = Mth.wrapDegrees(velocityAngle)
|
| 1021 |
|
| 1022 | getDirectionalInputForDegrees(DirectionalInput.NONE, velocityAngle1)
|
| 1023 | } else {
|
| 1024 | DirectionalInput.NONE
|
| 1025 | }
|
| 1026 |
|
| 1027 | val jumping = !entity.onGround()
|
| 1028 |
|
| 1029 | return SimulatedPlayerInput(
|
| 1030 | input,
|
| 1031 | jumping,
|
| 1032 | sprinting,
|
| 1033 | sneaking = entity.isShiftKeyDown
|
| 1034 | )
|
| 1035 | }
|
| 1036 | }
|
| 1037 |
|
| 1038 | }
|
| 1039 | }
|
| 1040 |
|