From 2632d6e6b84654a806bd14eccebece0e6902b641 Mon Sep 17 00:00:00 2001 From: Gurkengewuerz Date: Thu, 28 Jul 2022 14:22:06 +0200 Subject: [PATCH] feat: use creative fly instead of custom fly --- .../clientenhancements/client/Flying.java | 63 +++++++++---------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/src/main/java/de/mc8051/clientenhancements/client/Flying.java b/src/main/java/de/mc8051/clientenhancements/client/Flying.java index 9fc358d..bbd5700 100644 --- a/src/main/java/de/mc8051/clientenhancements/client/Flying.java +++ b/src/main/java/de/mc8051/clientenhancements/client/Flying.java @@ -1,49 +1,46 @@ package de.mc8051.clientenhancements.client; import net.minecraft.client.MinecraftClient; -import net.minecraft.entity.Entity; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.entity.player.PlayerAbilities; import net.minecraft.util.math.Vec3d; public class Flying { int toggle = 0; - int MAX_SPEED = 3; double FALL_SPEED = -0.04; - double ACCELERATION = 0.1; + + boolean switchedState = false; public void tick(MinecraftClient client) { - if (client.player != null && ClientEnhancementsClient.getKeyBindingController().getState(KeyBindingController.FLY)) { - boolean jumpPressed = client.options.jumpKey.isPressed(); - boolean forwardPressed = client.options.forwardKey.isPressed(); - boolean leftPressed = client.options.leftKey.isPressed(); - boolean rightPressed = client.options.rightKey.isPressed(); - boolean backPressed = client.options.backKey.isPressed(); - - Entity entity = client.player; - if (client.player.hasVehicle()) return; - - final boolean anyMovement = forwardPressed || leftPressed || rightPressed || backPressed; - Vec3d velocity = entity.getVelocity(); - Vec3d newVelocity = new Vec3d(velocity.x, -FALL_SPEED, velocity.z); - if (forwardPressed) newVelocity = client.player.getRotationVector().multiply(ACCELERATION); - if (leftPressed) { - newVelocity = client.player.getRotationVector().multiply(ACCELERATION).rotateY((float) (Math.PI / 2)); - newVelocity = new Vec3d(newVelocity.x, 0, newVelocity.z); - } - if (rightPressed) { - newVelocity = client.player.getRotationVector().multiply(ACCELERATION).rotateY((float) -(Math.PI / 2)); - newVelocity = new Vec3d(newVelocity.x, 0, newVelocity.z); + if (client.player != null) { + ClientPlayerEntity player = client.player; + boolean inFlyMode = player.isCreative() || player.isSpectator(); + + if (ClientEnhancementsClient.getKeyBindingController().getState(KeyBindingController.FLY)) { + if (!switchedState) switchedState = true; + player.getAbilities().allowFlying = true; + + if (player.hasVehicle()) return; + if (inFlyMode) return; + + final Vec3d velocity = player.getVelocity(); + if (toggle == 0) { + player.setVelocity(new Vec3d( + velocity.x, FALL_SPEED - velocity.y, velocity.z + )); + } + + if (toggle == 0 || velocity.y < FALL_SPEED) toggle = 40; + toggle--; + } else if (switchedState) { + switchedState = false; + PlayerAbilities abilities = player.getAbilities(); + + abilities.flying = inFlyMode && !player.isOnGround(); + abilities.allowFlying = inFlyMode; } - if (backPressed) newVelocity = client.player.getRotationVector().negate().multiply(ACCELERATION); - newVelocity = new Vec3d(newVelocity.x, (toggle == 0 && newVelocity.y > FALL_SPEED) ? FALL_SPEED : (anyMovement ? newVelocity.y : 0), newVelocity.z); - entity.setVelocity(newVelocity); - - if(anyMovement) { - if (ACCELERATION < MAX_SPEED) ACCELERATION += 0.1; - } else if (ACCELERATION > 0.2) ACCELERATION -= 0.2; - if(toggle == 0 || newVelocity.y <= FALL_SPEED) toggle = 40; - toggle--; } } }