Skip to content

Commit

Permalink
[squeaky clean] it's squeaky clean now
Browse files Browse the repository at this point in the history
  • Loading branch information
der-fruhling committed May 20, 2024
1 parent 57f9611 commit e972bba
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.simibubi.create.content.trains.entity.CarriageContraptionEntity;
import dev.architectury.event.events.common.TickEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;

import java.util.*;

Expand All @@ -14,55 +12,62 @@ public class CreateTrainPerspectiveMod {
public static CreateTrainPerspectiveMod INSTANCE;

public CreateTrainPerspectiveMod() {
TickEvent.PLAYER_POST.register(this::onTickPlayer);
TickEvent.PLAYER_POST.register(this::tickEntity);
INSTANCE = this;
}

public void onEntityMount(boolean isMounting, Entity entityMounting, Entity entityBeingMounted) {
public void onEntityMountEvent(boolean isMounting, Entity entityMounting, Entity entityBeingMounted) {
if(
entityMounting instanceof AbstractClientPlayer player &&
Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(entityMounting) instanceof Perspective persp &&
entityBeingMounted instanceof CarriageContraptionEntity contraption
) {
var persp = (Perspective) Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player);
if(isMounting) {
if(persp.getRotationState() == null) {
var state = new RotationState(contraption, false, true);
persp.setRotationState(state);
var carriage = state.getCarriageEntity();
assert carriage != null;
persp.enable(carriage.pitch, carriage.yaw);
} else {
var state = persp.getRotationState();
state.onMounted();
}
onEntityMount(persp, contraption);
} else {
if(persp.getRotationState() != null) {
persp.setRotationState(null);
persp.disable();
}
onEntityDismount(persp);
}
}
}

public void tickStandingPlayer(final CarriageContraptionEntity contraption, final Player player) {
if(player.getVehicle() != null) return;
public void onEntityMount(Perspective persp, CarriageContraptionEntity contraption) {
if(persp.getRotationState() == null) {
var state = new RotationState(contraption, false, true);
persp.setRotationState(state);
var carriage = state.getContraption();
assert carriage != null;
persp.enable(carriage.pitch, carriage.yaw);
} else {
var state = persp.getRotationState();
state.onMounted();
}
}

private void onEntityDismount(Perspective persp) {
if(persp.getRotationState() != null) {
persp.setRotationState(null);
persp.disable();
}
}

public void tickStandingEntity(final CarriageContraptionEntity contraption, final Entity entity) {
if(entity.getVehicle() != null) return;

var persp = (Perspective) Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player);
var persp = (Perspective) Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(entity);
var state = persp.getRotationState();

if (state == null || !Objects.equals(state.getCarriageEntity(), contraption)) {
if (state == null || !Objects.equals(state.getContraption(), contraption)) {
state = new RotationState(contraption, true, false);
persp.setRotationState(state);
var carriage = state.getCarriageEntity();
var carriage = state.getContraption();
assert carriage != null;
persp.enable(carriage.pitch, carriage.yaw);
} else {
state.update();
}
}

private void tickState(Player player, Perspective persp, RotationState state) {
var carriage = state.getCarriageEntity();
private void tickPerspectiveState(Entity player, Perspective persp, RotationState state) {
var carriage = state.getContraption();
if(carriage == null) return;
persp.setLean(carriage.pitch);
persp.setYaw(carriage.yaw);
Expand All @@ -78,19 +83,18 @@ private void tickState(Player player, Perspective persp, RotationState state) {
}
}

public void onTickPlayer(final Player player) {
if(!(player instanceof AbstractClientPlayer)) return;
if(Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player) instanceof Perspective persp
public void tickEntity(final Entity entity) {
if(Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(entity) instanceof Perspective persp
&& persp.getRotationState() != null) {
var state = persp.getRotationState();
assert state != null;

if(state.shouldTickState()) {
tickState(player, persp, state);
tickPerspectiveState(entity, persp, state);
} else {
persp.diminish();

if(persp.diminished()) {
if(persp.isDiminished()) {
persp.setRotationState(null);
persp.disable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ default void diminish() {
setLean(getLean(1.0f) * 0.9f);
}

default boolean diminished() {
default boolean isDiminished() {
return Mth.abs(getLean(1.0f)) < 0.01f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@
import org.jetbrains.annotations.Nullable;

public class RotationState implements RotationStateKeeper {
private CarriageContraptionEntity entity;
private float lastYaw;
private final boolean standingState;
private CarriageContraptionEntity contraption;
private float lastRecordedYaw;
private final boolean isStandingState;
private boolean isMounted;
private boolean shouldTickState = true;
private int ticksSinceLastUpdate = 0;

public RotationState(CarriageContraptionEntity entity, boolean standingState, boolean isMounted) {
this.entity = entity;
lastYaw = entity.yaw;
this.standingState = standingState;
public RotationState(CarriageContraptionEntity contraption, boolean isStandingState, boolean isMounted) {
this.contraption = contraption;
lastRecordedYaw = contraption.yaw;
this.isStandingState = isStandingState;
this.isMounted = isMounted;
}

@Override
public float getYawDelta() {
while (entity.yaw - lastYaw < -180.0f) {
lastYaw -= 360.0f;
while (contraption.yaw - lastRecordedYaw < -180.0f) {
lastRecordedYaw -= 360.0f;
}

while (entity.yaw - lastYaw >= 180.0f) {
lastYaw += 360.0f;
while (contraption.yaw - lastRecordedYaw >= 180.0f) {
lastRecordedYaw += 360.0f;
}

var rotation = entity.yaw - lastYaw;
lastYaw = entity.yaw;
var rotation = contraption.yaw - lastRecordedYaw;
lastRecordedYaw = contraption.yaw;
return rotation;
}

@Override
public @Nullable CarriageContraptionEntity getCarriageEntity() {
return entity;
public @Nullable CarriageContraptionEntity getContraption() {
return contraption;
}

@Override
public void setCarriageEntity(@Nullable CarriageContraptionEntity entity) {
this.entity = entity;
public void setCarriageEntity(@Nullable CarriageContraptionEntity contraption) {
this.contraption = contraption;
}

@Override
public boolean isStanding() {
return standingState;
return isStandingState;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.jetbrains.annotations.Nullable;

public interface RotationStateKeeper {
@Nullable CarriageContraptionEntity getCarriageEntity();
@Nullable CarriageContraptionEntity getContraption();
void setCarriageEntity(@Nullable CarriageContraptionEntity entity);
boolean isStanding();
boolean isMounted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.simibubi.create.content.contraptions.AbstractContraptionEntity;
import com.simibubi.create.content.trains.entity.CarriageContraptionEntity;
import net.derfruhling.minecraft.create.trainperspective.CreateTrainPerspectiveMod;
import net.derfruhling.minecraft.create.trainperspective.Perspective;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -21,8 +22,10 @@ private void onRegisterColliding(
Entity entity,
CallbackInfo ci
) {
if((Object) this instanceof CarriageContraptionEntity carriage && entity instanceof Player player) {
CreateTrainPerspectiveMod.INSTANCE.tickStandingPlayer(carriage, player);
if(!entity.level().isClientSide) return;
if((Object) this instanceof CarriageContraptionEntity carriage &&
Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(entity) instanceof Perspective) {
CreateTrainPerspectiveMod.INSTANCE.tickStandingEntity(carriage, entity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
import org.joml.Quaternionf;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Camera.class)
@Implements({@Interface(iface = Camera3D.class, prefix = "c3d$")})
Expand Down Expand Up @@ -47,11 +45,11 @@ private float modifyRoll(float original) {
}

@Redirect(method = "setup", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Camera;setRotation(FF)V"))
public void modifyRotationsPrimary(Camera instance,
float y,
float x,
@Local(argsOnly = true, ordinal = 0) boolean isThirdPerson,
@Local(argsOnly = true) float f) {
public void modifyRotations(Camera instance,
float y,
float x,
@Local(argsOnly = true, ordinal = 0) boolean isThirdPerson,
@Local(argsOnly = true) float f) {
if(entity instanceof AbstractClientPlayer player && !isThirdPerson) {
var persp = (Perspective) Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player);
ctp$zRot = persp.getLean(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
@Mixin(RaycastHelper.class)
public class CreateRaycastHelperMixin {
@ModifyVariable(method = "getTraceTarget", at = @At("STORE"), index = 4)
private static float modifyXRot(float value, Player player) {
private static float modifyPitch(float pitch, Player player) {
if(Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player) instanceof Perspective persp) {
return MixinUtil.applyDirectionXRotChange(persp, value, player.getYRot(), 1.0f);
} else return value;
return MixinUtil.applyDirectionXRotChange(persp, pitch, player.getYRot(), 1.0f);
} else return pitch;
}

@ModifyVariable(method = "getTraceTarget", at = @At("STORE"), index = 5)
private static float modifyYRot(float value, Player player) {
private static float modifyYaw(float yaw, Player player) {
if(Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player) instanceof Perspective persp) {
return value + MixinUtil.getExtraYRot(persp, player.getXRot(), value, 1.0f);
} else return value;
return yaw + MixinUtil.getExtraYRot(persp, player.getXRot(), yaw, 1.0f);
} else return yaw;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@ public abstract class EntityMixin {
@Shadow private Level level;

@Inject(method = "startRiding(Lnet/minecraft/world/entity/Entity;Z)Z", at = @At(value = "RETURN", ordinal = 4))
public void onStartRiding(Entity entity, boolean bl, CallbackInfoReturnable<Boolean> cir) {
CreateTrainPerspectiveMod.INSTANCE.onEntityMount(true, (Entity)(Object)this, entity);
public void onMount(Entity entity, boolean bl, CallbackInfoReturnable<Boolean> cir) {
CreateTrainPerspectiveMod.INSTANCE.onEntityMountEvent(true, (Entity)(Object)this, entity);
}

@Inject(method = "removeVehicle", at = @At("HEAD"))
public void onRemoveVehicle(CallbackInfo ci) {
public void onDismount(CallbackInfo ci) {
if(vehicle != null) {
CreateTrainPerspectiveMod.INSTANCE.onEntityMount(false, (Entity)(Object)this, vehicle);
CreateTrainPerspectiveMod.INSTANCE.onEntityMountEvent(false, (Entity)(Object)this, vehicle);
}
}

@SuppressWarnings("UnreachableCode")
@ModifyVariable(method = "calculateViewVector", at = @At(value = "LOAD"), index = 1, argsOnly = true)
public float adjustXRot(float xRot, @Local(argsOnly = true, index = 2) float yRot) {
public float modifyPitch(float pitch, @Local(argsOnly = true, index = 2) float yaw) {
if (this.level.isClientSide) {
if (Minecraft.getInstance().getEntityRenderDispatcher().getRenderer((Entity)(Object)this) instanceof Perspective persp && persp.isEnabled()) {
return MixinUtil.applyDirectionXRotChange(persp, xRot, yRot, 1.0f);
} else return xRot;
} else return xRot;
return MixinUtil.applyDirectionXRotChange(persp, pitch, yaw, 1.0f);
} else return pitch;
} else return pitch;
}

@SuppressWarnings("UnreachableCode")
@ModifyVariable(method = "calculateViewVector", at = @At(value = "LOAD"), index = 2, argsOnly = true)
public float adjustYRot(float yRot, @Local(argsOnly = true, index = 1) float xRot) {
public float modifyYaw(float yaw, @Local(argsOnly = true, index = 1) float pitch) {
if (this.level.isClientSide) {
if (Minecraft.getInstance().getEntityRenderDispatcher().getRenderer((Entity)(Object)this) instanceof Perspective persp && persp.isEnabled()) {
return yRot + MixinUtil.getExtraYRot(persp, xRot, yRot, 1.0f);
} else return yRot;
} else return yRot;
return yaw + MixinUtil.getExtraYRot(persp, pitch, yaw, 1.0f);
} else return yaw;
} else return yaw;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GameRendererMixin {
@Shadow @Final private Camera mainCamera;

@Inject(method = "renderLevel", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/PoseStack;mulPose(Lorg/joml/Quaternionf;)V", ordinal = 2, shift = At.Shift.BEFORE))
public void applyZRotation(float f, long l, PoseStack poseStack, CallbackInfo ci) {
public void applyLevelRotations(float f, long l, PoseStack poseStack, CallbackInfo ci) {
poseStack.mulPose(Axis.ZP.rotationDegrees(MixinUtil.asCamera3D(mainCamera).getZRot()));
poseStack.mulPose(Axis.YP.rotationDegrees(MixinUtil.asCamera3D(mainCamera).getExtraYRot()));
}
Expand Down

0 comments on commit e972bba

Please sign in to comment.