From 2c20501924f532bdddb630806deb5700ebfafaec Mon Sep 17 00:00:00 2001 From: enjarai Date: Sun, 2 Oct 2022 12:33:44 +0200 Subject: [PATCH] Speed and bugfix --- CHANGELOG.md | 3 +- gradle.properties | 2 +- .../doabarrelroll/DoABarrelRollClient.java | 40 +++++++------- .../nl/enjarai/doabarrelroll/ElytraMath.java | 3 +- .../doabarrelroll/config/ModConfig.java | 6 +++ .../mixin/ClientPlayerEntityMixin.java | 2 +- .../mixin/GameRendererMixin.java | 2 +- .../doabarrelroll/mixin/InGameHudMixin.java | 4 +- .../mixin/LivingEntityMixin.java | 54 +++++++++++++++++++ .../doabarrelroll/mixin/MouseMixin.java | 2 +- .../mixin/PlayerEntityRendererMixin.java | 4 +- .../assets/do-a-barrel-roll/lang/en_us.json | 7 +++ .../resources/do-a-barrel-roll.mixins.json | 1 + 13 files changed, 101 insertions(+), 29 deletions(-) create mode 100644 src/main/java/nl/enjarai/doabarrelroll/mixin/LivingEntityMixin.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 00a2cbcb..4fdaa69e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,2 @@ -- Significant improvements to the banking physics +- Hold W to go faster +- Hopefully fixed the world disappearing sometimes diff --git a/gradle.properties b/gradle.properties index 1cb8986d..a759fc0c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ minecraft_version=1.19 yarn_mappings=1.19+build.4 loader_version=0.14.8 -mod_version = 1.3.1 +mod_version = 1.3.2 maven_group = nl.enjarai archives_base_name = do-a-barrel-roll diff --git a/src/main/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java b/src/main/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java index 47649ba9..16eb8d6a 100644 --- a/src/main/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java +++ b/src/main/java/nl/enjarai/doabarrelroll/DoABarrelRollClient.java @@ -27,6 +27,7 @@ public class DoABarrelRollClient implements ClientModInitializer { public static double landingLerp = 1; public static Vec3d left; public static Vec2f mouseTurnVec = Vec2f.ZERO; + public static double throttle = 0; @Override @@ -111,7 +112,7 @@ public static void onWorldRender(MinecraftClient client, float tickDelta, long l if (client.player != null && landingLerp < 1) { // calculate the camera angle and apply it - double angle = -Math.acos(left.dotProduct(ElytraMath.getAssumedLeft(client.player.getYaw()))) * ElytraMath.TODEG; + double angle = -Math.acos(MathHelper.clamp(left.dotProduct(ElytraMath.getAssumedLeft(client.player.getYaw())), -1, 1)) * ElytraMath.TODEG; if (left.getY() < 0) angle *= -1; matrix.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion((float) angle)); @@ -131,6 +132,7 @@ private static void clearValues() { rollSmoother.clear(); mouseTurnVec = Vec2f.ZERO; lastLookUpdate = GlfwUtil.getTime(); + throttle = 0; } /** @@ -164,6 +166,7 @@ public static void changeElytraLook(double pitch, double yaw, double roll, Sensi var rotDelta = new RotationInstant(pitch, yaw, roll, delta); ElytraMath.changeElytraLookDirectly(player, rotDelta + .useModifier(DoABarrelRollClient::manageThrottle) .useModifier(DoABarrelRollClient::strafeButtons) .applySensitivity(sensitivity) .applyConfig(ModConfig.INSTANCE) @@ -209,24 +212,23 @@ public static RotationInstant banking(RotationInstant rotationInstant) { return rotationInstant.addAbsolute(dX * delta, dY * delta, currentRoll); } -// public static RotationInstant yawBanking(RotationInstant rotationInstant) { -// if (!ModConfig.INSTANCE.enableBanking) return rotationInstant; -// -// var client = MinecraftClient.getInstance(); -// var player = client.player; -// if (player == null) return rotationInstant; -// -// var delta = rotationInstant.getRenderDelta(); -// var currentRoll = ElytraMath.getRoll(player.getYaw(), left) * ElytraMath.TORAD; -// var yawMod = Math.sin(currentRoll) * 10 * ModConfig.INSTANCE.bankingStrength * delta; -// -// // check if we accidentally got NaN, for some reason this happens sometimes -// if (Double.isNaN(yawMod)) { -// yawMod = 0; -// } -// -// return rotationInstant.add(0, yawMod, 0); -// } + public static RotationInstant manageThrottle(RotationInstant rotationInstant) { + var client = MinecraftClient.getInstance(); + + var delta = rotationInstant.getRenderDelta(); + + if (client.options.forwardKey.isPressed()) { + throttle += 0.1 * delta; + } else if (client.options.backKey.isPressed()) { + throttle -= 0.1 * delta; + } else { + throttle -= throttle * 0.95 * delta; + } + + throttle = MathHelper.clamp(throttle, 0, ModConfig.INSTANCE.maxThrust); + + return rotationInstant; + } public static boolean isFallFlying() { var player = MinecraftClient.getInstance().player; diff --git a/src/main/java/nl/enjarai/doabarrelroll/ElytraMath.java b/src/main/java/nl/enjarai/doabarrelroll/ElytraMath.java index d53a5714..0da7b487 100644 --- a/src/main/java/nl/enjarai/doabarrelroll/ElytraMath.java +++ b/src/main/java/nl/enjarai/doabarrelroll/ElytraMath.java @@ -1,6 +1,7 @@ package nl.enjarai.doabarrelroll; import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; import nl.enjarai.doabarrelroll.config.RotationInstant; @@ -79,7 +80,7 @@ public static Vec3d rotateAxisAngle(Vec3d v, Vec3d axis, double angle) { } public static double getRoll(float yaw, Vec3d left) { - double angle = -Math.acos(left.dotProduct(getAssumedLeft(yaw))) * TODEG; + double angle = -Math.acos(MathHelper.clamp(left.dotProduct(getAssumedLeft(yaw)), -1, 1)) * TODEG; if (left.getY() < 0) angle *= -1; return angle; } diff --git a/src/main/java/nl/enjarai/doabarrelroll/config/ModConfig.java b/src/main/java/nl/enjarai/doabarrelroll/config/ModConfig.java index 18afecb1..c722d0e3 100644 --- a/src/main/java/nl/enjarai/doabarrelroll/config/ModConfig.java +++ b/src/main/java/nl/enjarai/doabarrelroll/config/ModConfig.java @@ -35,6 +35,12 @@ public static void init() { public float bankingStrength = 20; + @ConfigEntry.Gui.Tooltip(count = 4) + public boolean enableThrust = false; + + @ConfigEntry.Gui.Tooltip + public float maxThrust = 1; + @ConfigEntry.Gui.CollapsibleObject public Sensitivity desktopSensitivity = new Sensitivity(); diff --git a/src/main/java/nl/enjarai/doabarrelroll/mixin/ClientPlayerEntityMixin.java b/src/main/java/nl/enjarai/doabarrelroll/mixin/ClientPlayerEntityMixin.java index a6e93d73..8db37354 100644 --- a/src/main/java/nl/enjarai/doabarrelroll/mixin/ClientPlayerEntityMixin.java +++ b/src/main/java/nl/enjarai/doabarrelroll/mixin/ClientPlayerEntityMixin.java @@ -28,7 +28,7 @@ public abstract class ClientPlayerEntityMixin extends AbstractClientPlayerEntity method = "(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/world/ClientWorld;Lnet/minecraft/client/network/ClientPlayNetworkHandler;Lnet/minecraft/stat/StatHandler;Lnet/minecraft/client/recipebook/ClientRecipeBook;ZZ)V", at = @At("RETURN") ) - public void init(MinecraftClient client, ClientWorld world, ClientPlayNetworkHandler networkHandler, StatHandler stats, ClientRecipeBook recipeBook, boolean lastSneaking, boolean lastSprinting, CallbackInfo ci) { + public void doABarrelRoll$init(MinecraftClient client, ClientWorld world, ClientPlayNetworkHandler networkHandler, StatHandler stats, ClientRecipeBook recipeBook, boolean lastSneaking, boolean lastSprinting, CallbackInfo ci) { DoABarrelRollClient.left = ElytraMath.getAssumedLeft(getYaw()); } } \ No newline at end of file diff --git a/src/main/java/nl/enjarai/doabarrelroll/mixin/GameRendererMixin.java b/src/main/java/nl/enjarai/doabarrelroll/mixin/GameRendererMixin.java index c72a1cdf..5f1ee601 100644 --- a/src/main/java/nl/enjarai/doabarrelroll/mixin/GameRendererMixin.java +++ b/src/main/java/nl/enjarai/doabarrelroll/mixin/GameRendererMixin.java @@ -17,7 +17,7 @@ public abstract class GameRendererMixin { @Final @Shadow private MinecraftClient client; @Inject(at = @At("HEAD"), method = "renderWorld") - public void renderWorld(float tickDelta, long limitTime, MatrixStack matrix, CallbackInfo ci) { + public void doABarrelRoll$renderWorld(float tickDelta, long limitTime, MatrixStack matrix, CallbackInfo ci) { DoABarrelRollClient.onWorldRender(client, tickDelta, limitTime, matrix); } } diff --git a/src/main/java/nl/enjarai/doabarrelroll/mixin/InGameHudMixin.java b/src/main/java/nl/enjarai/doabarrelroll/mixin/InGameHudMixin.java index b1e033a8..524aadf9 100644 --- a/src/main/java/nl/enjarai/doabarrelroll/mixin/InGameHudMixin.java +++ b/src/main/java/nl/enjarai/doabarrelroll/mixin/InGameHudMixin.java @@ -20,7 +20,7 @@ public abstract class InGameHudMixin extends DrawableHelper { method = "renderCrosshair(Lnet/minecraft/client/util/math/MatrixStack;)V", at = @At(value = "HEAD") ) - private void renderCrosshairHead(MatrixStack matrices, CallbackInfo ci) { + private void doABarrelRoll$renderCrosshairHead(MatrixStack matrices, CallbackInfo ci) { matrices.push(); DoABarrelRollClient.onRenderCrosshair(matrices, scaledWidth, scaledHeight); } @@ -29,7 +29,7 @@ private void renderCrosshairHead(MatrixStack matrices, CallbackInfo ci) { method = "renderCrosshair(Lnet/minecraft/client/util/math/MatrixStack;)V", at = @At(value = "RETURN") ) - private void renderCrosshairReturn(MatrixStack matrices, CallbackInfo ci) { + private void doABarrelRoll$renderCrosshairReturn(MatrixStack matrices, CallbackInfo ci) { matrices.pop(); } } diff --git a/src/main/java/nl/enjarai/doabarrelroll/mixin/LivingEntityMixin.java b/src/main/java/nl/enjarai/doabarrelroll/mixin/LivingEntityMixin.java new file mode 100644 index 00000000..b24c2fc4 --- /dev/null +++ b/src/main/java/nl/enjarai/doabarrelroll/mixin/LivingEntityMixin.java @@ -0,0 +1,54 @@ +package nl.enjarai.doabarrelroll.mixin; + +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.particle.ParticleTypes; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; +import nl.enjarai.doabarrelroll.DoABarrelRollClient; +import nl.enjarai.doabarrelroll.config.ModConfig; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; + +@Mixin(LivingEntity.class) +public abstract class LivingEntityMixin extends Entity { + + public LivingEntityMixin(EntityType type, World world) { + super(type, world); + } + + @ModifyArg( + method = "travel(Lnet/minecraft/util/math/Vec3d;)V", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/entity/LivingEntity;setVelocity(Lnet/minecraft/util/math/Vec3d;)V", + ordinal = 6 + ) + ) + private Vec3d doABarrelRoll$wrapElytraVelocity(Vec3d original) { + if (!(((LivingEntity) (Object) this) instanceof ClientPlayerEntity) || !ModConfig.INSTANCE.enableThrust) return original; + + Vec3d rotation = getRotationVector(); + Vec3d velocity = getVelocity(); + + int particleDensity = (int) MathHelper.clamp(DoABarrelRollClient.throttle * 10, 0, 10); + if (DoABarrelRollClient.throttle > 0.1 && world.getTime() % (11 - particleDensity) == 0) { + var pPos = getPos().add(velocity.multiply(0.5).negate()); + world.addParticle( + ParticleTypes.CAMPFIRE_SIGNAL_SMOKE, + pPos.getX(), pPos.getY(), pPos.getZ(), + random.nextGaussian() * 0.05, -velocity.y * 0.5, random.nextGaussian() * 0.05 + ); + } + + return original.add( + (rotation.x * 0.1 + (rotation.x * 1.5 - velocity.x) * 0.5) * DoABarrelRollClient.throttle, + (rotation.y * 0.1 + (rotation.y * 1.5 - velocity.y) * 0.5) * DoABarrelRollClient.throttle, + (rotation.z * 0.1 + (rotation.z * 1.5 - velocity.z) * 0.5) * DoABarrelRollClient.throttle + ); + } +} diff --git a/src/main/java/nl/enjarai/doabarrelroll/mixin/MouseMixin.java b/src/main/java/nl/enjarai/doabarrelroll/mixin/MouseMixin.java index 12a1e237..c4e3a692 100644 --- a/src/main/java/nl/enjarai/doabarrelroll/mixin/MouseMixin.java +++ b/src/main/java/nl/enjarai/doabarrelroll/mixin/MouseMixin.java @@ -18,7 +18,7 @@ public abstract class MouseMixin { target = "Lnet/minecraft/client/network/ClientPlayerEntity;changeLookDirection(DD)V" ) ) - private boolean changeLookDirection(ClientPlayerEntity player, double cursorDeltaX, double cursorDeltaY) { + private boolean doABarrelRoll$changeLookDirection(ClientPlayerEntity player, double cursorDeltaX, double cursorDeltaY) { return DoABarrelRollClient.updateMouse(player, cursorDeltaX, cursorDeltaY); } } diff --git a/src/main/java/nl/enjarai/doabarrelroll/mixin/PlayerEntityRendererMixin.java b/src/main/java/nl/enjarai/doabarrelroll/mixin/PlayerEntityRendererMixin.java index 1c891f6c..0e4c0bdc 100644 --- a/src/main/java/nl/enjarai/doabarrelroll/mixin/PlayerEntityRendererMixin.java +++ b/src/main/java/nl/enjarai/doabarrelroll/mixin/PlayerEntityRendererMixin.java @@ -23,7 +23,7 @@ public abstract class PlayerEntityRendererMixin { method = "setupTransforms(Lnet/minecraft/client/network/AbstractClientPlayerEntity;Lnet/minecraft/client/util/math/MatrixStack;FFF)V", at = @At("HEAD") ) - private void capturePlayer(AbstractClientPlayerEntity abstractClientPlayerEntity, MatrixStack matrixStack, float f, float g, float h, CallbackInfo ci) { + private void doABarrelRoll$capturePlayer(AbstractClientPlayerEntity abstractClientPlayerEntity, MatrixStack matrixStack, float f, float g, float h, CallbackInfo ci) { player = abstractClientPlayerEntity; } @@ -36,7 +36,7 @@ private void capturePlayer(AbstractClientPlayerEntity abstractClientPlayerEntity ), index = 0 ) - private Quaternion modifyRoll(Quaternion original) { + private Quaternion doABarrelRoll$modifyRoll(Quaternion original) { if (!(player instanceof ClientPlayerEntity)) return original; var roll = ElytraMath.getRoll(player.getYaw(), DoABarrelRollClient.left); diff --git a/src/main/resources/assets/do-a-barrel-roll/lang/en_us.json b/src/main/resources/assets/do-a-barrel-roll/lang/en_us.json index 093782c4..3fa37c1a 100644 --- a/src/main/resources/assets/do-a-barrel-roll/lang/en_us.json +++ b/src/main/resources/assets/do-a-barrel-roll/lang/en_us.json @@ -11,6 +11,13 @@ "text.autoconfig.do-a-barrel-roll.option.enableBanking": "Enable banking", "text.autoconfig.do-a-barrel-roll.option.enableBanking.@Tooltip": "Banking slightly yaws the camera when rolling, creating a more realistic, plane-like feeling.", "text.autoconfig.do-a-barrel-roll.option.bankingStrength": "Banking strength", + "text.autoconfig.do-a-barrel-roll.option.enableThrust": "Enable thrusting", + "text.autoconfig.do-a-barrel-roll.option.enableThrust.@Tooltip[0]": "Enables you to use the forward and back keys (W and S by default)", + "text.autoconfig.do-a-barrel-roll.option.enableThrust.@Tooltip[1]": "to infinitely accelerate when using an elytra.", + "text.autoconfig.do-a-barrel-roll.option.enableThrust.@Tooltip[2]": "This is a very cheaty feature, and can be used to fly very fast.", + "text.autoconfig.do-a-barrel-roll.option.enableThrust.@Tooltip[3]": "ยง4Use this on servers at your own risk, or just don't.", + "text.autoconfig.do-a-barrel-roll.option.maxThrust": "Max thrusting speed", + "text.autoconfig.do-a-barrel-roll.option.maxThrust.@Tooltip": "Be careful with this one, for some reason things tend to get weird above 3-4.", "text.autoconfig.do-a-barrel-roll.option.desktopSensitivity": "Mouse and keyboard sensitivity", "text.autoconfig.do-a-barrel-roll.option.desktopSensitivity.pitch": "Pitch", "text.autoconfig.do-a-barrel-roll.option.desktopSensitivity.yaw": "Yaw", diff --git a/src/main/resources/do-a-barrel-roll.mixins.json b/src/main/resources/do-a-barrel-roll.mixins.json index a5906fde..c61d8e37 100644 --- a/src/main/resources/do-a-barrel-roll.mixins.json +++ b/src/main/resources/do-a-barrel-roll.mixins.json @@ -14,5 +14,6 @@ "defaultRequire": 1 }, "mixins": [ + "LivingEntityMixin" ] }