Skip to content

Commit

Permalink
Merge pull request #169 from Octol1ttle/fix-thrust-particles
Browse files Browse the repository at this point in the history
Fix thrust particles not appearing when mods are changing the thrust output via event
  • Loading branch information
enjarai authored Oct 2, 2024
2 parents 6ea1d39 + 4212c4b commit 8ba5a95
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ public class DoABarrelRollClient {
public static final Smoother YAW_SMOOTHER = new Smoother();
public static final Smoother ROLL_SMOOTHER = new Smoother();
public static final RollGroup FALL_FLYING_GROUP = RollGroup.of(DoABarrelRoll.id("fall_flying"));
public static double throttle = 0;

public static void init() {
FALL_FLYING_GROUP.trueIf(DoABarrelRollClient::isFallFlying);

// Keyboard modifiers
RollEvents.EARLY_CAMERA_MODIFIERS.register(context -> context
.useModifier(RotationModifiers::manageThrottle, ModConfig.INSTANCE::getEnableThrust)
.useModifier(RotationModifiers.buttonControls(1800)),
2000, FALL_FLYING_GROUP);

Expand Down Expand Up @@ -57,7 +55,6 @@ public static void clearValues() {
PITCH_SMOOTHER.clear();
YAW_SMOOTHER.clear();
ROLL_SMOOTHER.clear();
throttle = 0;
}

public static boolean isFallFlying() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package nl.enjarai.doabarrelroll.flight;

import java.util.HashMap;
import java.util.Map;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Smoother;
import nl.enjarai.doabarrelroll.DoABarrelRoll;
import nl.enjarai.doabarrelroll.DoABarrelRollClient;
import nl.enjarai.doabarrelroll.ModKeybindings;
import nl.enjarai.doabarrelroll.api.event.RollContext;
import nl.enjarai.doabarrelroll.api.rotation.RotationInstant;
import nl.enjarai.doabarrelroll.config.ModConfig;
import nl.enjarai.doabarrelroll.config.Sensitivity;
import nl.enjarai.doabarrelroll.math.MagicNumbers;

import java.util.HashMap;
import java.util.Map;

public class RotationModifiers {
public static final double ROLL_REORIENT_CUTOFF = Math.sqrt(10.0 / 3.0);

public static RollContext.ConfiguresRotation buttonControls(double power) {
return (rotationInstant, context) -> {
var delta = power * context.getRenderDelta();
var pitch = 0;
var yaw = 0;
var roll = 0;
var pitch = 0.0;
var yaw = 0.0;
var roll = 0.0;

if (ModKeybindings.PITCH_UP.isPressed()) {
pitch -= delta;
Expand Down Expand Up @@ -92,35 +89,19 @@ public static RotationInstant reorient(RotationInstant rotationInstant, RollCont
return rotationInstant.add(0, 0, -rollDelta * strength * delta);
}

public static RotationInstant manageThrottle(RotationInstant rotationInstant, RollContext context) {
var delta = context.getRenderDelta();

if (ModKeybindings.THRUST_FORWARD.isPressed()) {
DoABarrelRollClient.throttle += 0.1 * delta;
} else if (ModKeybindings.THRUST_BACKWARD.isPressed()) {
DoABarrelRollClient.throttle -= 0.1 * delta;
} else {
DoABarrelRollClient.throttle -= DoABarrelRollClient.throttle * 0.95 * delta;
}

DoABarrelRollClient.throttle = MathHelper.clamp(DoABarrelRollClient.throttle, 0, ModConfig.INSTANCE.getMaxThrust());

return rotationInstant;
}

public static RollContext.ConfiguresRotation fixNaN(String name) {
return (rotationInstant, context) -> {
if (Double.isNaN(rotationInstant.pitch())) {
rotationInstant = RotationInstant.of(0, rotationInstant.yaw(), rotationInstant.roll());
DoABarrelRoll.LOGGER.warn("NaN found in pitch for " + name + ", setting to 0 as fallback");
DoABarrelRoll.LOGGER.warn("NaN found in pitch for {}, setting to 0 as fallback", name);
}
if (Double.isNaN(rotationInstant.yaw())) {
rotationInstant = RotationInstant.of(rotationInstant.pitch(), 0, rotationInstant.roll());
DoABarrelRoll.LOGGER.warn("NaN found in yaw for " + name + ", setting to 0 as fallback");
DoABarrelRoll.LOGGER.warn("NaN found in yaw for {}, setting to 0 as fallback", name);
}
if (Double.isNaN(rotationInstant.roll())) {
rotationInstant = RotationInstant.of(rotationInstant.pitch(), rotationInstant.yaw(), 0);
DoABarrelRoll.LOGGER.warn("NaN found in roll for " + name + ", setting to 0 as fallback");
DoABarrelRoll.LOGGER.warn("NaN found in roll for {}, setting to 0 as fallback", name);
}
return rotationInstant;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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.ModKeybindings;
import nl.enjarai.doabarrelroll.api.event.ThrustEvents;
import nl.enjarai.doabarrelroll.config.ModConfig;
Expand All @@ -35,14 +34,17 @@ public LivingEntityMixin(EntityType<?> type, World world) {
)
)
private Vec3d doABarrelRoll$wrapElytraVelocity(Vec3d original) {
if (!(((LivingEntity) (Object) this) instanceof ClientPlayerEntity) || !ModConfig.INSTANCE.getEnableThrust()) return original;
if (!((Object) this instanceof ClientPlayerEntity) || !ModConfig.INSTANCE.getEnableThrust()) return original;

Vec3d rotation = getRotationVector();
Vec3d velocity = getVelocity();

double throttleSign = ModKeybindings.THRUST_FORWARD.isPressed() ? 1 : ModKeybindings.THRUST_BACKWARD.isPressed() ? -1 : 0;
throttleSign = ThrustEvents.modifyThrustInput(throttleSign);

if (ModConfig.INSTANCE.getThrustParticles()) {
int particleDensity = (int) MathHelper.clamp(DoABarrelRollClient.throttle * 10, 0, 10);
if (DoABarrelRollClient.throttle > 0.1 && getWorld().getTime() % (11 - particleDensity) == 0) {
int particleDensity = (int) MathHelper.clamp(throttleSign * 10, 0, 10);
if (throttleSign > 0.1 && getWorld().getTime() % (11 - particleDensity) == 0) {
var pPos = getPos().add(velocity.multiply(0.5).negate());
getWorld().addParticle(
ParticleTypes.CAMPFIRE_SIGNAL_SMOKE,
Expand All @@ -52,8 +54,6 @@ public LivingEntityMixin(EntityType<?> type, World world) {
}
}

double throttleSign = ModKeybindings.THRUST_FORWARD.isPressed() ? 1 : ModKeybindings.THRUST_BACKWARD.isPressed() ? -1 : 0;
throttleSign = ThrustEvents.modifyThrustInput(throttleSign);
double maxSpeed = ModConfig.INSTANCE.getMaxThrust();
double speedIncrease = Math.max(maxSpeed - velocity.length(), 0) / maxSpeed * throttleSign;
double acceleration = ModConfig.INSTANCE.getThrustAcceleration() * speedIncrease;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public abstract class CameraMixin implements RollCamera {
method = "setRotation",
at = @At(
value = "INVOKE",
target = "Lorg/joml/Quaternionf;rotationYXZ(FFF)Lorg/joml/Quaternionf;"
target = "Lorg/joml/Quaternionf;rotationYXZ(FFF)Lorg/joml/Quaternionf;",
remap = false
),
index = 2
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.isxander.controlify.api.event.ControlifyEvents;
import dev.isxander.controlify.bindings.BindContext;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import nl.enjarai.doabarrelroll.DoABarrelRoll;
import nl.enjarai.doabarrelroll.DoABarrelRollClient;
import nl.enjarai.doabarrelroll.ModKeybindings;
Expand Down Expand Up @@ -67,15 +66,6 @@ public static double getThrustModifier() {
return forward - backward;
}

public static RotationInstant manageThrottle(RotationInstant rotationInstant, RollContext context) {
var delta = context.getRenderDelta();

DoABarrelRollClient.throttle += getThrustModifier() * delta;
DoABarrelRollClient.throttle = MathHelper.clamp(DoABarrelRollClient.throttle, 0, ModConfig.INSTANCE.getMaxThrust());

return rotationInstant;
}

@Override
public void onControlifyPreInit(ControlifyApi controlifyApi) {
var bindings = ControlifyBindApi.get();
Expand Down Expand Up @@ -138,9 +128,6 @@ public void onControlifyPreInit(ControlifyApi controlifyApi) {
.addKeyCorrelation(ModKeybindings.THRUST_BACKWARD)
);

RollEvents.EARLY_CAMERA_MODIFIERS.register(context -> context
.useModifier(ControlifyCompat::manageThrottle, ModConfig.INSTANCE::getEnableThrust),
8, DoABarrelRollClient::isFallFlying);
RollEvents.LATE_CAMERA_MODIFIERS.register(context -> context
.useModifier(this::applyToRotation),
5, DoABarrelRollClient::isFallFlying);
Expand Down

0 comments on commit 8ba5a95

Please sign in to comment.