diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/CreateTrainPerspectiveMod.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/CreateTrainPerspectiveMod.java index 63c38fd..ae59a25 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/CreateTrainPerspectiveMod.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/CreateTrainPerspectiveMod.java @@ -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.*; @@ -14,46 +12,53 @@ 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 { @@ -61,8 +66,8 @@ public void tickStandingPlayer(final CarriageContraptionEntity contraption, fina } } - 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); @@ -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(); } diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/Perspective.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/Perspective.java index b9b2d3f..005ffcb 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/Perspective.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/Perspective.java @@ -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; } } diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/RotationState.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/RotationState.java index a91a102..523e1b0 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/RotationState.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/RotationState.java @@ -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 diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/RotationStateKeeper.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/RotationStateKeeper.java index 8be5110..655ab77 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/RotationStateKeeper.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/RotationStateKeeper.java @@ -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(); diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/AbstractContraptionEntityMixin.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/AbstractContraptionEntityMixin.java index c072623..2ae1f8a 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/AbstractContraptionEntityMixin.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/AbstractContraptionEntityMixin.java @@ -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; @@ -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); } } } diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CameraMixin.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CameraMixin.java index 44871ab..592a57e 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CameraMixin.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CameraMixin.java @@ -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$")}) @@ -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) diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CreateRaycastHelperMixin.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CreateRaycastHelperMixin.java index 86defd0..2ccce5a 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CreateRaycastHelperMixin.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/CreateRaycastHelperMixin.java @@ -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; } } diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/EntityMixin.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/EntityMixin.java index c067f41..fef438c 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/EntityMixin.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/EntityMixin.java @@ -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 cir) { - CreateTrainPerspectiveMod.INSTANCE.onEntityMount(true, (Entity)(Object)this, entity); + public void onMount(Entity entity, boolean bl, CallbackInfoReturnable 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; } } diff --git a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/GameRendererMixin.java b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/GameRendererMixin.java index 18e6ff9..59035c7 100644 --- a/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/GameRendererMixin.java +++ b/common/src/main/java/net/derfruhling/minecraft/create/trainperspective/mixin/GameRendererMixin.java @@ -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())); }