Skip to content

Commit

Permalink
fix the scale and add debug options
Browse files Browse the repository at this point in the history
  • Loading branch information
der-fruhling committed Aug 8, 2024
1 parent 10bf53b commit ae32868
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ModConfig {
public boolean applyToNonPlayerEntities = true;
public List<ResourceLocation> blockedEntities = new ArrayList<>();
public boolean dbgShowStandingTransforms = false;
public boolean dbgShowValueScales = false;

private ModConfig() {
}
Expand Down Expand Up @@ -187,6 +188,16 @@ public static Screen createConfigScreen(Screen parent) {
.setDefaultValue(false)
.build());


debug.add(entryBuilder
.startBooleanToggle(
Component.translatable("option.create_train_perspective.debug.value_scales"),
INSTANCE.dbgShowValueScales)
.setSaveConsumer(value -> INSTANCE.dbgShowValueScales = value)
.setTooltip(Component.translatable("option.create_train_perspective.debug.value_scales.tooltip"))
.setDefaultValue(false)
.build());

advanced.add(debug.build());
general.addEntry(advanced.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.simibubi.create.content.trains.entity.CarriageContraptionEntity;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.Nullable;

public interface Perspective {
Expand All @@ -20,15 +19,15 @@ public interface Perspective {
default float getLean(float f) {
var ref = getReference();
if (ref == null) return 0.0f;
if (f == 1.0f) return ref.pitch * getScale();
return Mth.lerp(f, ref.prevPitch * getPrevScale(), ref.pitch * getScale());
if (f == 1.0f) return ref.pitch * getValueScale();
return Mth.lerp(f, ref.prevPitch * getPrevValueScale(), ref.pitch * getValueScale());
}

default float getYaw(float f) {
var ref = getReference();
if (ref == null) return 0.0f;
if (f == 1.0f) return ref.yaw * getScale();
return Mth.lerp(f, ref.prevYaw * getPrevScale(), ref.yaw * getScale());
if (f == 1.0f) return ref.yaw * getValueScale();
return Mth.lerp(f, ref.prevYaw * getPrevValueScale(), ref.yaw * getValueScale());
}

@Nullable
Expand All @@ -38,11 +37,14 @@ default float getYaw(float f) {

void diminish();

float getPrevScale();
float getPrevValueScale();

float getScale();
// this must not be named getScale()!!!!
// Entity (or something) has a method also called getScale() that conflicts
// with this method in a fuckey way.
float getValueScale();

default boolean isDiminished() {
return Mth.abs(getLean(1.0f)) < 0.01f;
return getValueScale() < 0.00025f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public abstract class EntityMixin {
@Unique
private @Nullable CarriageContraptionEntity ctp$reference = null;
@Unique
private float ctp$scale = 1.0f, ctp$prevScale = 1.0f;
private float ctp$scale = 1.0f;
@Unique
private float ctp$prevScale = 1.0f;
@Unique
private @Nullable RotationState ctp$currentState = null;

Expand Down Expand Up @@ -80,11 +82,15 @@ public float modifyYaw(float yaw, @Local(argsOnly = true, index = 1) float pitch
public void ctp$enable(CarriageContraptionEntity entity) {
ctp$perspectiveActive = true;
ctp$reference = entity;
ctp$prevScale = 1.0f;
ctp$scale = 1.0f;
}

public void ctp$disable() {
ctp$perspectiveActive = false;
ctp$reference = null;
ctp$prevScale = 1.0f;
ctp$scale = 1.0f;
}

public void ctp$setReference(CarriageContraptionEntity entity) {
Expand All @@ -100,14 +106,14 @@ public float modifyYaw(float yaw, @Local(argsOnly = true, index = 1) float pitch
public void ctp$diminish() {
if (ctp$scale <= 0.0f) return;
ctp$prevScale = ctp$scale;
ctp$scale -= 0.99f;
ctp$scale = Mth.lerp(0.1f, ctp$scale, 0.0f);
}

public float ctp$getScale() {
public float ctp$getValueScale() {
return ctp$scale;
}

public float ctp$getPrevScale() {
public float ctp$getPrevValueScale() {
return ctp$prevScale;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.authlib.GameProfile;
import net.derfruhling.minecraft.create.trainperspective.ModConfig;
import net.derfruhling.minecraft.create.trainperspective.Perspective;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Component;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value = LocalPlayer.class)
public class LocalPlayerMixin {
public abstract class LocalPlayerMixin extends AbstractClientPlayer {
@Shadow
@Final
protected Minecraft minecraft;

@Shadow public abstract void displayClientMessage(Component arg, boolean bl);

public LocalPlayerMixin(ClientLevel clientLevel, GameProfile gameProfile) {
super(clientLevel, gameProfile);
}

@WrapOperation(method = "getViewYRot", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/player/LocalPlayer;isPassenger()Z"))
public boolean isPassenger(LocalPlayer localPlayer, Operation<Boolean> original) {
var perspective = (Perspective) localPlayer;
return original.call(localPlayer) || perspective.isEnabled();
}

@Inject(method = "tick", at = @At(value = "TAIL"))
public void tickDebugFeatures(CallbackInfo ci) {
var persp = (Perspective) this;

if(ModConfig.INSTANCE.dbgShowValueScales) {
this.displayClientMessage(Component.literal(persp.getValueScale() + ", " + persp.getPrevValueScale()), true);
}
}
}
6 changes: 4 additions & 2 deletions common/src/main/resources/assets/minecraft/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"category.create_train_perspective.multiplayer": "Multiplayer",
"category.create_train_perspective.advanced": "Advanced",
"category.create_train_perspective.debug": "Debug Features",
"category.create_train_perspective.debug.description": "These are debugging features used for development. You probably don't want to touch these unless you're working on the mod itself.",
"category.create_train_perspective.debug.description": "These are debugging features used for development. You probably don't want to touch these unless you're working on the mod itself, and you should only enable one at a time if you want them to mean anything.",
"option.create_train_perspective.enabled": "Enabled",
"option.create_train_perspective.enabled.tooltip": "Enables the mod. If False, the mod is disabled.",
"option.create_train_perspective.leaning.enabled": "Enable Leaning",
Expand All @@ -23,5 +23,7 @@
"option.create_train_perspective.advanced.blocked_entities": "Blocked entities",
"option.create_train_perspective.advanced.blocked_entities.tooltip": "Entities with an ID in this list will not be considered for this mod's functionality",
"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)"
"option.create_train_perspective.debug.standing_transforms.tooltip": "Shows translations used to transform the player while standing on a train. (X, Y, Z)",
"option.create_train_perspective.debug.value_scales": "Show Value Scales",
"option.create_train_perspective.debug.value_scales.tooltip": "Shows scalars used to scale leaning and yaw after leaving a train. (CURRENT, LAST)"
}

0 comments on commit ae32868

Please sign in to comment.