Skip to content

Commit

Permalink
feat: use creative fly instead of custom fly
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurkengewuerz committed Jul 28, 2022
1 parent fc3c9e1 commit 2632d6e
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/main/java/de/mc8051/clientenhancements/client/Flying.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}

0 comments on commit 2632d6e

Please sign in to comment.