Skip to content

Commit

Permalink
fix: get roll from external mods instead of matrix in RollComputer
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
  • Loading branch information
Octol1ttle committed May 11, 2024
1 parent 04e9713 commit da13d43
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static void setupCommandRegistration() {
}

private static void setupWorldRender() {
WorldRenderEvents.END.register(context ->
WorldRenderEvents.START.register(context ->
ComputerHost.instance().tick()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package ru.octol1ttle.flightassistant.compatibility.doabarrelroll;

import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;
import nl.enjarai.doabarrelroll.api.RollEntity;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class DaBRInterface {
public static void setRoll(float newRoll) {
if (MinecraftClient.getInstance().player != null) {
((RollEntity) MinecraftClient.getInstance().player).doABarrelRoll$setRoll(newRoll);
}
static float getRoll(PlayerEntity player) {
return ((RollEntity) player).doABarrelRoll$getRoll();
}

static void setRoll(PlayerEntity player, float newRoll) {
((RollEntity) player).doABarrelRoll$setRoll(newRoll);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.octol1ttle.flightassistant.compatibility.doabarrelroll;

import org.jetbrains.annotations.ApiStatus;
import ru.octol1ttle.flightassistant.computers.api.IRollHandler;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.registries.ComputerRegistry;

@ApiStatus.Internal
public class DaBRRollHandler implements IRollHandler {
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);

@Override
public float getRoll() {
return DaBRInterface.getRoll(data.player());
}

@Override
public void setRoll(float newRoll) {
DaBRInterface.setRoll(data.player(), newRoll);
}

@Override
public String getId() {
return "dabr_roll";
}

@Override
public void reset() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public interface IRollController extends IComputer {
ControlInput getControlledRoll();

ControllerPriority getPriority();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.octol1ttle.flightassistant.computers.api;

public interface IRollActuator extends IComputer {
public interface IRollHandler extends IComputer {
float getRoll();
void setRoll(float newRoll);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;
import ru.octol1ttle.flightassistant.FlightAssistant;
import ru.octol1ttle.flightassistant.compatibility.doabarrelroll.DaBRRollActuator;
import ru.octol1ttle.flightassistant.compatibility.doabarrelroll.DaBRRollHandler;
import ru.octol1ttle.flightassistant.computers.api.ControlInput;
import ru.octol1ttle.flightassistant.computers.api.ControllerPriority;
import ru.octol1ttle.flightassistant.computers.api.INormalLawProvider;
import ru.octol1ttle.flightassistant.computers.api.IRollActuator;
import ru.octol1ttle.flightassistant.computers.api.IRollController;
import ru.octol1ttle.flightassistant.computers.api.IRollHandler;
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.impl.TimeComputer;
Expand All @@ -24,33 +24,33 @@ public class RollController implements ITickableComputer, INormalLawProvider {
private final List<IRollController> controllers = new ArrayList<>();
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class);
private final TimeComputer time = ComputerRegistry.resolve(TimeComputer.class);
private @Nullable IRollActuator rollActuator = null;
private @Nullable IRollHandler rollHandler = null;

public RollController() {
CustomComputerRegistrationCallback.EVENT.register(() -> {
if (FabricLoader.getInstance().isModLoaded("do_a_barrel_roll")) {
ComputerRegistry.register(new DaBRRollActuator());
ComputerRegistry.register(new DaBRRollHandler());
}
});
ComputerRegisteredCallback.EVENT.register(computer -> {
if (computer instanceof IRollController controller) {
controllers.add(controller);
controllers.sort(Comparator.comparingInt(ctl -> ctl.getPriority().priority));
}
if (computer instanceof IRollActuator actuator) {
if (rollActuator != null) {
FlightAssistant.LOGGER.warn("Multiple roll actuators found! Discarding actuator %s".formatted(actuator.getClass().getName()));
if (computer instanceof IRollHandler handler) {
if (rollHandler != null) {
FlightAssistant.LOGGER.warn("Multiple roll handlers found! Discarding handler %s".formatted(handler.getClass().getName()));
} else {
rollActuator = actuator;
FlightAssistant.LOGGER.info("Active roll actuator is %s".formatted(rollActuator.getClass().getName()));
rollHandler = handler;
FlightAssistant.LOGGER.info("Active roll handler is %s".formatted(rollHandler.getClass().getName()));
}
}
});
}

@Override
public void tick() {
if (!data.canAutomationsActivate() || rollActuator == null) {
if (!data.canAutomationsActivate() || rollHandler == null) {
return;
}

Expand All @@ -75,7 +75,9 @@ public void tick() {
* @param delta Delta time, in seconds
*/
public void smoothSetRoll(float roll, float delta) {
float difference = roll - data.roll;
assert rollHandler != null;

float difference = roll - rollHandler.getRoll();
if (difference < -180.0f) {
difference += 360.0f;
}
Expand All @@ -87,11 +89,10 @@ public void smoothSetRoll(float roll, float delta) {
if (Math.abs(difference) < 0.05f) {
newRoll = roll;
} else {
newRoll = data.roll + difference * delta;
newRoll = rollHandler.getRoll() + difference * delta;
}

assert rollActuator != null;
rollActuator.setRoll(newRoll);
rollHandler.setRoll(newRoll);
}

@Override
Expand Down

0 comments on commit da13d43

Please sign in to comment.