From 05f79590316ad11f9fb01e4a0c86a857eb331106 Mon Sep 17 00:00:00 2001 From: Jacob Herd <53945697+ToxicAven@users.noreply.github.com> Date: Thu, 25 Mar 2021 14:30:51 -0400 Subject: [PATCH] impr: Separate BoatFly from EntitySpeed (#2155) Co-authored-by: lv <~@l1v.in> --- .../mixin/client/render/MixinModelBoat.java | 6 +- .../client/module/modules/movement/BoatFly.kt | 79 ++++++++++++++++++ .../module/modules/movement/EntitySpeed.kt | 80 ++++--------------- .../org/kamiblue/client/util/EntityUtils.kt | 23 ++++++ 4 files changed, 121 insertions(+), 67 deletions(-) create mode 100644 src/main/kotlin/org/kamiblue/client/module/modules/movement/BoatFly.kt diff --git a/src/main/java/org/kamiblue/client/mixin/client/render/MixinModelBoat.java b/src/main/java/org/kamiblue/client/mixin/client/render/MixinModelBoat.java index e00f44c7..7d00571f 100644 --- a/src/main/java/org/kamiblue/client/mixin/client/render/MixinModelBoat.java +++ b/src/main/java/org/kamiblue/client/mixin/client/render/MixinModelBoat.java @@ -3,7 +3,7 @@ import net.minecraft.client.model.ModelBoat; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.entity.Entity; -import org.kamiblue.client.module.modules.movement.EntitySpeed; +import org.kamiblue.client.module.modules.movement.BoatFly; import org.kamiblue.client.util.Wrapper; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -18,8 +18,8 @@ public class MixinModelBoat { @Inject(method = "render", at = @At("HEAD")) public void render(Entity entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scale, CallbackInfo info) { - if (Wrapper.getPlayer().getRidingEntity() == entityIn && EntitySpeed.INSTANCE.isEnabled()) { - GlStateManager.color(1.0f, 1.0f, 1.0f, EntitySpeed.getOpacity()); + if (Wrapper.getPlayer().getRidingEntity() == entityIn && BoatFly.INSTANCE.isEnabled()) { + GlStateManager.color(1.0f, 1.0f, 1.0f, BoatFly.INSTANCE.getOpacity()); GlStateManager.enableBlend(); } } diff --git a/src/main/kotlin/org/kamiblue/client/module/modules/movement/BoatFly.kt b/src/main/kotlin/org/kamiblue/client/module/modules/movement/BoatFly.kt new file mode 100644 index 00000000..6a2cdec6 --- /dev/null +++ b/src/main/kotlin/org/kamiblue/client/module/modules/movement/BoatFly.kt @@ -0,0 +1,79 @@ +package org.kamiblue.client.module.modules.movement + +import net.minecraft.entity.Entity +import net.minecraft.entity.item.EntityBoat +import net.minecraft.network.play.client.CPacketInput +import net.minecraft.network.play.client.CPacketPlayer +import net.minecraft.network.play.client.CPacketVehicleMove +import net.minecraft.network.play.server.SPacketMoveVehicle +import net.minecraft.util.EnumHand +import net.minecraft.world.chunk.EmptyChunk +import org.kamiblue.client.event.SafeClientEvent +import org.kamiblue.client.event.events.PacketEvent +import org.kamiblue.client.event.events.PlayerTravelEvent +import org.kamiblue.client.module.Category +import org.kamiblue.client.module.Module +import org.kamiblue.client.util.EntityUtils.steerEntity +import org.kamiblue.client.util.MovementUtils +import org.kamiblue.client.util.MovementUtils.calcMoveYaw +import org.kamiblue.client.util.threads.safeListener +import kotlin.math.cos +import kotlin.math.sin + +internal object BoatFly : Module( + name = "BoatFly", + category = Category.MOVEMENT, + description = "Fly using boats" +) { + private const val flight = true + + private val speed by setting("Speed", 1.0f, 0.1f..25.0f, 0.1f) + private val antiStuck by setting("Anti Stuck", true) + private val glideSpeed by setting("Glide Speed", 0.1f, 0.0f..1.0f, 0.01f) + private val upSpeed by setting("Up Speed", 1.0f, 0.0f..5.0f, 0.1f) + val opacity by setting("Boat Opacity", 1.0f, 0.0f..1.0f, 0.01f) + private val forceInteract by setting("Force Interact", false) + private val interactTickDelay by setting("Interact Delay", 2, 1..20, 1, { forceInteract }, description = "Force interact packet delay, in ticks.") + + init { + safeListener { + val ridingEntity = player.ridingEntity + + if (!forceInteract || ridingEntity !is EntityBoat) return@safeListener + + if (it.packet is CPacketPlayer.Rotation || it.packet is CPacketInput) { + it.cancel() + } + + if (it.packet is CPacketVehicleMove) { + if (player.ticksExisted % interactTickDelay == 0) { + playerController.interactWithEntity(player, ridingEntity, EnumHand.MAIN_HAND) + } + } + } + + safeListener { + if (!forceInteract || player.ridingEntity !is EntityBoat || it.packet !is SPacketMoveVehicle) return@safeListener + it.cancel() + } + + safeListener { + player.ridingEntity?.let { entity -> + if (entity is EntityBoat && entity.controllingPassenger == player) { + steerEntity(entity, speed, antiStuck) + + // Make sure the boat doesn't turn etc (params: isLeftDown, isRightDown, isForwardDown, isBackDown) + entity.rotationYaw = player.rotationYaw + entity.updateInputs(false, false, false, false) + + if (flight) fly(entity) + } + } + } + } + + private fun fly(entity: Entity) { + if (!entity.isInWater) entity.motionY = -glideSpeed.toDouble() + if (mc.gameSettings.keyBindJump.isKeyDown) entity.motionY += upSpeed / 2.0 + } +} diff --git a/src/main/kotlin/org/kamiblue/client/module/modules/movement/EntitySpeed.kt b/src/main/kotlin/org/kamiblue/client/module/modules/movement/EntitySpeed.kt index 9ba13167..32dd2562 100644 --- a/src/main/kotlin/org/kamiblue/client/module/modules/movement/EntitySpeed.kt +++ b/src/main/kotlin/org/kamiblue/client/module/modules/movement/EntitySpeed.kt @@ -1,7 +1,6 @@ package org.kamiblue.client.module.modules.movement import net.minecraft.entity.Entity -import net.minecraft.entity.item.EntityBoat import net.minecraft.entity.passive.AbstractHorse import net.minecraft.entity.passive.EntityHorse import net.minecraft.entity.passive.EntityPig @@ -16,6 +15,7 @@ import org.kamiblue.client.event.events.PacketEvent import org.kamiblue.client.event.events.PlayerTravelEvent import org.kamiblue.client.module.Category import org.kamiblue.client.module.Module +import org.kamiblue.client.util.EntityUtils.steerEntity import org.kamiblue.client.util.MovementUtils import org.kamiblue.client.util.MovementUtils.calcMoveYaw import org.kamiblue.client.util.threads.safeListener @@ -27,78 +27,30 @@ internal object EntitySpeed : Module( category = Category.MOVEMENT, description = "Abuse client-sided movement to shape sound barrier breaking rideables" ) { - private val speed = setting("Speed", 1.0f, 0.1f..25.0f, 0.1f) - private val antiStuck = setting("Anti Stuck", true) - private val flight = setting("Flight", false) - private val glideSpeed = setting("Glide Speed", 0.1f, 0.0f..1.0f, 0.01f, { flight.value }) - private val upSpeed = setting("Up Speed", 1.0f, 0.0f..5.0f, 0.1f, { flight.value }) - private val opacity = setting("Boat Opacity", 1.0f, 0.0f..1.0f, 0.01f) - private val forceInteract = setting("Force Interact", false) - private val interactTickDelay = setting("Interact Delay", 2, 1..20, 1, { forceInteract.value }, description = "Force interact packet delay, in ticks.") + private val speed by setting("Speed", 1.0f, 0.1f..25.0f, 0.1f) + private val antiStuck by setting("Anti Stuck", true) + private val flight by setting("Flight", false) + private val glideSpeed by setting("Glide Speed", 0.1f, 0.0f..1.0f, 0.01f, { flight }) + private val upSpeed by setting("Up Speed", 1.0f, 0.0f..5.0f, 0.1f, { flight }) init { - safeListener { - val ridingEntity = player.ridingEntity - - if (!forceInteract.value || ridingEntity !is EntityBoat) return@safeListener - - if (it.packet is CPacketPlayer.Rotation || it.packet is CPacketInput) { - it.cancel() - } - - if (it.packet is CPacketVehicleMove) { - if (player.ticksExisted % interactTickDelay.value == 0) { - playerController.interactWithEntity(player, ridingEntity, EnumHand.MAIN_HAND) - } - } - } + safeListener { + player.ridingEntity?.let { entity -> + if (entity is EntityPig || entity is AbstractHorse && entity.controllingPassenger == player) { + steerEntity(entity, speed, antiStuck) - safeListener { - if (!forceInteract.value || player.ridingEntity !is EntityBoat || it.packet !is SPacketMoveVehicle) return@safeListener - it.cancel() - } + if (entity is EntityHorse) { + entity.rotationYaw = player.rotationYaw + } - safeListener { - player.ridingEntity?.let { - if (it is EntityPig || it is AbstractHorse || it is EntityBoat && it.controllingPassenger == player) { - steerEntity(it) - if (flight.value) fly(it) + if (flight) fly(entity) } } } } - private fun SafeClientEvent.steerEntity(entity: Entity) { - val yawRad = calcMoveYaw() - - val motionX = -sin(yawRad) * speed.value - val motionZ = cos(yawRad) * speed.value - - if (MovementUtils.isInputting && !isBorderingChunk(entity, motionX, motionZ)) { - entity.motionX = motionX - entity.motionZ = motionZ - } else { - entity.motionX = 0.0 - entity.motionZ = 0.0 - } - - if (entity is EntityHorse || entity is EntityBoat) { - entity.rotationYaw = player.rotationYaw - - // Make sure the boat doesn't turn etc (params: isLeftDown, isRightDown, isForwardDown, isBackDown) - if (entity is EntityBoat) entity.updateInputs(false, false, false, false) - } - } - private fun fly(entity: Entity) { - if (!entity.isInWater) entity.motionY = -glideSpeed.value.toDouble() - if (mc.gameSettings.keyBindJump.isKeyDown) entity.motionY += upSpeed.value / 2.0 + if (!entity.isInWater) entity.motionY = -glideSpeed.toDouble() + if (mc.gameSettings.keyBindJump.isKeyDown) entity.motionY += upSpeed / 2.0 } - - private fun SafeClientEvent.isBorderingChunk(entity: Entity, motionX: Double, motionZ: Double): Boolean { - return antiStuck.value && world.getChunk((entity.posX + motionX).toInt() shr 4, (entity.posZ + motionZ).toInt() shr 4) is EmptyChunk - } - - @JvmStatic - fun getOpacity(): Float = opacity.value } diff --git a/src/main/kotlin/org/kamiblue/client/util/EntityUtils.kt b/src/main/kotlin/org/kamiblue/client/util/EntityUtils.kt index 4749b164..ee08e5d6 100644 --- a/src/main/kotlin/org/kamiblue/client/util/EntityUtils.kt +++ b/src/main/kotlin/org/kamiblue/client/util/EntityUtils.kt @@ -14,12 +14,16 @@ import net.minecraft.entity.passive.* import net.minecraft.entity.player.EntityPlayer import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Vec3d +import net.minecraft.world.chunk.EmptyChunk import org.kamiblue.client.event.SafeClientEvent import org.kamiblue.client.manager.managers.FriendManager +import org.kamiblue.client.util.MovementUtils.calcMoveYaw import org.kamiblue.client.util.items.id import org.kamiblue.client.util.math.VectorUtils.toBlockPos import org.kamiblue.commons.extension.ceilToInt import org.kamiblue.commons.extension.floorToInt +import kotlin.math.cos +import kotlin.math.sin object EntityUtils { private val mc = Minecraft.getMinecraft() @@ -170,4 +174,23 @@ object EntityUtils { .minByOrNull { player.getDistance(it) } ?.positionVector ?.toBlockPos() + + fun SafeClientEvent.steerEntity(entity: Entity, speed: Float, antiStuck: Boolean) { + val yawRad = calcMoveYaw() + + val motionX = -sin(yawRad) * speed + val motionZ = cos(yawRad) * speed + + if (MovementUtils.isInputting && !isBorderingChunk(entity, motionX, motionZ, antiStuck)) { + entity.motionX = motionX + entity.motionZ = motionZ + } else { + entity.motionX = 0.0 + entity.motionZ = 0.0 + } + } + + private fun SafeClientEvent.isBorderingChunk(entity: Entity, motionX: Double, motionZ: Double, antiStuck: Boolean): Boolean { + return antiStuck && world.getChunk((entity.posX + motionX).toInt() shr 4, (entity.posZ + motionZ).toInt() shr 4) is EmptyChunk + } } \ No newline at end of file