Skip to content

Commit

Permalink
make mod activate with other entities too (it broken now)
Browse files Browse the repository at this point in the history
  • Loading branch information
der-fruhling committed Jun 10, 2024
1 parent 4855679 commit 0edb2a7
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ private Conditional() {}

public static boolean shouldApplyPerspectiveTo(Entity player) {
if(ModConfig.INSTANCE.enabled) {
return (ModConfig.INSTANCE.applyToOthers && player instanceof RemotePlayer)
return ModConfig.INSTANCE.applyToNonPlayerEntities ||
(ModConfig.INSTANCE.applyToOthers && player instanceof RemotePlayer)
|| player instanceof LocalPlayer;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private ModConfig() {}
public boolean rollEnabled = true;
public float rollMagnitude = 1.0f;
public boolean applyToOthers = true;
public boolean applyToNonPlayerEntities = false;
public boolean dbgShowStandingTransforms = false;

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
Expand Down Expand Up @@ -156,6 +157,15 @@ public static Screen createConfigScreen(Screen parent) {
.setSaveConsumer(value -> INSTANCE.rollMagnitude = value)
.setDefaultValue(1.0f)
.build());

advanced.add(entryBuilder
.startBooleanToggle(
Component.translatable("option.create_train_perspective.multiplayer.apply_to_entities"),
INSTANCE.applyToNonPlayerEntities)
.setTooltip(Component.translatable("option.create_train_perspective.multiplayer.apply_to_entities.tooltip"))
.setSaveConsumer(value -> INSTANCE.applyToNonPlayerEntities = value)
.setDefaultValue(true)
.build());

var debug = entryBuilder.startSubCategory(Component.translatable("category.create_train_perspective.debug"));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package net.derfruhling.minecraft.create.trainperspective.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import net.derfruhling.minecraft.create.trainperspective.Conditional;
import net.derfruhling.minecraft.create.trainperspective.CreateTrainPerspectiveMod;
import net.derfruhling.minecraft.create.trainperspective.MixinUtil;
import net.derfruhling.minecraft.create.trainperspective.Perspective;
import net.derfruhling.minecraft.create.trainperspective.*;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Entity.class)
@Implements({@Interface(iface = Perspective.class, prefix = "ctp$")})
@Environment(EnvType.CLIENT)
public abstract class EntityMixin {
@Shadow @Nullable private Entity vehicle;
@Shadow private Level level;

@Unique private boolean ctp$perspectiveActive = false;
@Unique private float ctp$lean = 0.0f, ctp$yaw = 0.0f, ctp$oldLean = 0.0f, ctp$oldYaw = 0.0f;
@Unique private @Nullable RotationState ctp$currentState = null;

@Inject(method = "startRiding(Lnet/minecraft/world/entity/Entity;Z)Z", at = @At(value = "RETURN", ordinal = 4))
public void onMount(Entity entity, boolean bl, CallbackInfoReturnable<Boolean> cir) {
var self = (Entity)(Object)this;
Expand Down Expand Up @@ -64,4 +66,62 @@ public float modifyYaw(float yaw, @Local(argsOnly = true, index = 1) float pitch
} else return yaw;
} else return yaw;
}

public void ctp$enable(float initialLean, float initialYaw) {
ctp$perspectiveActive = true;
ctp$lean = initialLean;
ctp$yaw = initialYaw;
ctp$oldLean = initialLean;
ctp$oldYaw = initialYaw;
}

public void ctp$disable() {
ctp$perspectiveActive = false;
ctp$lean = 0.0f;
ctp$yaw = 0.0f;
ctp$oldLean = 0.0f;
ctp$oldYaw = 0.0f;
}

public boolean ctp$isEnabled() {
return ctp$perspectiveActive;
}

public void ctp$setLean(float lean) {
ctp$oldLean = ctp$lean;
ctp$lean = lean;
}

public void ctp$setYaw(float yaw) {
// some configurations flip between 0 and 360 constantly
// adjust accordingly
ctp$oldYaw = ctp$yaw;
ctp$yaw = yaw;

while (ctp$yaw - ctp$oldYaw < -180.0f) {
ctp$oldYaw -= 360.0f;
}

while (ctp$yaw - ctp$oldYaw >= 180.0f) {
ctp$oldYaw += 360.0f;
}
}

public float ctp$getLean(float f) {
if(f == 1.0f) return ctp$lean;
return Mth.lerp(f, ctp$oldLean, ctp$lean);
}

public float ctp$getYaw(float f) {
if(f == 1.0f) return ctp$yaw;
return Mth.lerp(f, ctp$oldYaw, ctp$yaw);
}

public @Nullable RotationState ctp$getRotationState() {
return ctp$currentState;
}

public void ctp$setRotationState(@Nullable RotationState state) {
ctp$currentState = state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.derfruhling.minecraft.create.trainperspective.mixin;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.derfruhling.minecraft.create.trainperspective.Conditional;
import net.derfruhling.minecraft.create.trainperspective.Perspective;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.client.renderer.entity.player.PlayerRenderer;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(LivingEntityRenderer.class)
@Environment(EnvType.CLIENT)
public class LivingEntityRendererMixin {
@Inject(method = "setupRotations", at = @At("HEAD"))
protected void setupRotations(LivingEntity livingEntity, PoseStack poseStack, float f, float g, float h, CallbackInfo ci) {
if(Conditional.shouldApplyPerspectiveTo(livingEntity)) {
Perspective persp = (Perspective) livingEntity;
float height = 0;

if(livingEntity.getVehicle() != null) {
height = livingEntity.getEyeHeight();
}

var lean = persp.getLean(f);
var yaw = persp.getYaw(f);
poseStack.rotateAround(Axis.ZP.rotationDegrees(Mth.cos(Mth.DEG_TO_RAD * yaw) * lean), 0, height, 0);
poseStack.rotateAround(Axis.XP.rotationDegrees(Mth.sin(Mth.DEG_TO_RAD * yaw) * -lean), 0, height, 0);
}
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions common/src/main/resources/assets/minecraft/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"option.create_train_perspective.advanced.lean_magnitude.tooltip": "The magnitude to which the player will lean. Higher = more intense.",
"option.create_train_perspective.advanced.roll_magnitude": "Rolling Magnitude",
"option.create_train_perspective.advanced.roll_magnitude.tooltip": "The magnitude to which the player will roll. Higher = more intense.",
"option.create_train_perspective.multiplayer.apply_to_entities": "Apply mod to non-player entities",
"option.create_train_perspective.multiplayer.apply_to_entities.tooltip": "Applies the mod to non-player entities as well (may be broken with some entities)",
"option.create_train_perspective.debug.standing_transforms": "Show Standing Translations",
"option.create_train_perspective.debug.standing_transforms.tooltip": "Shows translations used to transform the player while standing on a train. (X, Y, Z)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
"package": "net.derfruhling.minecraft.create.trainperspective.mixin",
"compatibilityLevel": "JAVA_8",
"client": [
"AbstractClientPlayerMixin",
"AbstractContraptionEntityMixin",
"CameraMixin",
"ClientLevelMixin",
"CreateRaycastHelperMixin",
"EntityMixin",
"GameRendererMixin",
"LocalPlayerMixin",
"PlayerRendererMixin"
"LivingEntityRendererMixin"
],
"overwrites": {
"requireAnnotations": true,
Expand Down

0 comments on commit 0edb2a7

Please sign in to comment.