Skip to content

Commit 1e769f1

Browse files
committed
Freelook
- with code by TheKodeToad
1 parent 7492373 commit 1e769f1

File tree

9 files changed

+189
-4
lines changed

9 files changed

+189
-4
lines changed

src/main/java/io/github/axolotlclient/AxolotlClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static void addBadge(Entity entity, MatrixStack matrices){
156156
entity.getUuid() == MinecraftClient.getInstance().player.getUuid()?
157157
(NickHider.Instance.hideOwnName.get() ? NickHider.Instance.hiddenNameSelf.get(): Team.decorateName(entity.getScoreboardTeam(), entity.getName()).getString()):
158158
(NickHider.Instance.hideOtherNames.get() ? NickHider.Instance.hiddenNameOthers.get(): Team.decorateName(entity.getScoreboardTeam(), entity.getName()).getString())
159-
)/2 + (AxolotlClient.CONFIG.customBadge.get() ? MinecraftClient.getInstance().textRenderer.getWidth(AxolotlClient.CONFIG.badgeText.get()): 10));
159+
)/2 + (AxolotlClient.CONFIG.customBadge.get() ? MinecraftClient.getInstance().textRenderer.getWidth(" "+AxolotlClient.CONFIG.badgeText.get()): 10));
160160

161161
RenderSystem.setShaderColor(1, 1, 1, 1);
162162

src/main/java/io/github/axolotlclient/config/options/EnumOption.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public OptionType getType() {
3939

4040
@Override
4141
public void setValueFromJsonElement(JsonElement element) {
42-
for(int i=0; i<values.length-1;i++){
42+
for(int i=0; i<values.length;i++){
4343
String v = values[i];
4444
if(Objects.equals(v, element.getAsString())){
4545
this.i = i;
@@ -55,7 +55,7 @@ public void setDefaults() {
5555
return;
5656
}
5757

58-
for(int i=0;i< values.length-1; i++){
58+
for(int i=0;i< values.length; i++){
5959
String v = values[i];
6060
if(Objects.equals(v, def)){
6161
this.i=i;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.github.axolotlclient.mixin;
2+
3+
import io.github.axolotlclient.modules.freelook.Freelook;
4+
import net.minecraft.client.MinecraftClient;
5+
import net.minecraft.client.render.Camera;
6+
import net.minecraft.entity.Entity;
7+
import net.minecraft.world.BlockView;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.Shadow;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.ModifyArg;
13+
import org.spongepowered.asm.mixin.injection.ModifyArgs;
14+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
15+
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;
16+
17+
@Mixin(Camera.class)
18+
public abstract class MixinCamera {
19+
20+
@Shadow private float pitch;
21+
22+
@Shadow private float yaw;
23+
24+
@Shadow protected abstract double clipToSpace(double desiredCameraDistance);
25+
26+
@Inject(method = "update", at = @At(value = "INVOKE", target = "net/minecraft/client/render/Camera.moveBy(DDD)V", ordinal = 0))
27+
private void perspectiveUpdatePitchYaw(BlockView area, Entity focusedEntity, boolean thirdPerson, boolean inverseView, float tickDelta, CallbackInfo ci) {
28+
this.pitch = Freelook.INSTANCE.pitch(pitch) * (inverseView && Freelook.INSTANCE.enabled.get() && Freelook.INSTANCE.active ? -1 : 1);
29+
this.yaw = Freelook.INSTANCE.yaw(yaw) + (inverseView && Freelook.INSTANCE.enabled.get() && Freelook.INSTANCE.active ? 180 : 0);
30+
// System.out.println("Pitch: "+pitch+" Yaw: "+ yaw);
31+
}
32+
33+
@ModifyArgs(method = "update", at = @At(value = "INVOKE", target = "net/minecraft/client/render/Camera.setRotation(FF)V", ordinal = 0))
34+
private void perspectiveFixRotation(Args args) {
35+
args.set(0, Freelook.INSTANCE.yaw(args.get(0)));
36+
args.set(1, Freelook.INSTANCE.pitch(args.get(1)));
37+
}
38+
39+
@ModifyArg(method = "update", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/Camera;moveBy(DDD)V", ordinal = 0), index = 0)
40+
private double correctDistance(double x){
41+
if(Freelook.INSTANCE.enabled.get() && Freelook.INSTANCE.active && MinecraftClient.getInstance().options.getPerspective().isFrontView()){
42+
return -clipToSpace(4);
43+
}
44+
return x;
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.axolotlclient.mixin;
2+
3+
import io.github.axolotlclient.modules.freelook.Freelook;
4+
import net.minecraft.entity.Entity;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
9+
10+
@Mixin(Entity.class)
11+
public abstract class MixinEntity {
12+
13+
@Inject(method = "changeLookDirection", at = @At("HEAD"), cancellable = true)
14+
public void interceptMovement(double cursorDeltaX, double cursorDeltaY, CallbackInfo callback) {
15+
if(Freelook.INSTANCE.consumeRotation(cursorDeltaX, cursorDeltaY)) {
16+
callback.cancel();
17+
}
18+
}
19+
}

src/main/java/io/github/axolotlclient/mixin/MixinGameRenderer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import net.minecraft.entity.Entity;
1111
import net.minecraft.entity.LivingEntity;
1212
import net.minecraft.util.math.MathHelper;
13+
import net.minecraft.util.math.Matrix3f;
1314
import org.spongepowered.asm.mixin.Final;
1415
import org.spongepowered.asm.mixin.Mixin;
1516
import org.spongepowered.asm.mixin.Shadow;
1617
import org.spongepowered.asm.mixin.injection.At;
1718
import org.spongepowered.asm.mixin.injection.Inject;
19+
import org.spongepowered.asm.mixin.injection.Redirect;
1820
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1921
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2022

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package io.github.axolotlclient.modules.freelook;
2+
3+
import com.mojang.blaze3d.platform.InputUtil;
4+
import io.github.axolotlclient.AxolotlClient;
5+
import io.github.axolotlclient.config.options.BooleanOption;
6+
import io.github.axolotlclient.config.options.EnumOption;
7+
import io.github.axolotlclient.config.options.OptionCategory;
8+
import io.github.axolotlclient.modules.AbstractModule;
9+
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
10+
import net.minecraft.client.MinecraftClient;
11+
import net.minecraft.client.option.KeyBind;
12+
import net.minecraft.client.option.Perspective;
13+
import net.minecraft.entity.Entity;
14+
import net.minecraft.util.Identifier;
15+
16+
public class Freelook extends AbstractModule {
17+
18+
public static final Identifier ID = new Identifier("freelook");
19+
public static final Freelook INSTANCE = new Freelook();
20+
private static final KeyBind KEY = new KeyBind("key.freelook", InputUtil.KEY_V_CODE, "category.axolotlclient");
21+
22+
private final MinecraftClient client = MinecraftClient.getInstance();
23+
24+
private float yaw, pitch;
25+
public boolean active;
26+
27+
private final OptionCategory category = new OptionCategory(ID, ID.getPath());
28+
public final BooleanOption enabled = new BooleanOption("enabled", false);
29+
private final EnumOption perspective = new EnumOption("perspective", Perspective.values(), Perspective.THIRD_PERSON_BACK);
30+
private final BooleanOption invert = new BooleanOption("invert", false);
31+
32+
private Perspective previousPerspective;
33+
34+
@Override
35+
public void init() {
36+
KeyBindingHelper.registerKeyBinding(KEY);
37+
category.add(enabled, perspective, invert);
38+
AxolotlClient.CONFIG.addCategory(category);
39+
}
40+
41+
@Override
42+
public void tick() {
43+
44+
if(!enabled.get()) return;
45+
46+
if(KEY.isPressed()) {
47+
if(!active) {
48+
start();
49+
}
50+
} else if(active) stop();
51+
}
52+
53+
private void start() {
54+
active = true;
55+
56+
previousPerspective = client.options.getPerspective();
57+
setPerspective(Perspective.valueOf(perspective.get()));
58+
59+
Entity camera = client.getCameraEntity();
60+
61+
if(camera == null) camera = client.player;
62+
if(camera == null) return;
63+
64+
yaw = camera.getYaw();
65+
pitch = camera.getPitch();
66+
}
67+
68+
private void stop() {
69+
active = false;
70+
client.worldRenderer.scheduleTerrainUpdate();
71+
setPerspective(previousPerspective);
72+
}
73+
74+
public boolean consumeRotation(double dx, double dy) {
75+
if(!active || !enabled.get()) return false;
76+
77+
if(!invert.get()) dy = -dy;
78+
79+
if(MinecraftClient.getInstance().options.getPerspective().isFrontView()||
80+
MinecraftClient.getInstance().options.getPerspective().isFirstPerson()) dy*=-1;
81+
82+
yaw += dx * 0.15F;
83+
pitch += dy * 0.15F;
84+
85+
if(pitch > 90) {
86+
pitch = 90;
87+
} else if(pitch < -90) {
88+
pitch = -90;
89+
}
90+
91+
client.worldRenderer.scheduleTerrainUpdate();
92+
return true;
93+
}
94+
95+
public float yaw(float defaultValue) {
96+
if(!active || !enabled.get()) return defaultValue;
97+
98+
return yaw;
99+
}
100+
101+
public float pitch(float defaultValue) {
102+
if(!active || !enabled.get()) return defaultValue;
103+
104+
return pitch;
105+
}
106+
107+
private void setPerspective(Perspective perspective){
108+
MinecraftClient.getInstance().options.setPerspective(perspective);
109+
110+
}
111+
112+
}

src/main/java/io/github/axolotlclient/modules/hud/HudManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,17 @@ public AbstractHudEntry get(Identifier identifier) {
116116
}
117117

118118
public void render(MatrixStack matrices) {
119+
client.getProfiler().push("Hud Modules");
119120
if (!(client.currentScreen instanceof HudEditScreen) && !client.options.debugEnabled) {
120121
for (AbstractHudEntry hud : getEntries()) {
121122
if (hud.isEnabled()) {
123+
client.getProfiler().push(hud.getName());
122124
hud.renderHud(matrices);
125+
client.getProfiler().pop();
123126
}
124127
}
125128
}
129+
client.getProfiler().pop();
126130
}
127131

128132
public Optional<AbstractHudEntry> getEntryXY(int x, int y) {

src/main/java/io/github/axolotlclient/modules/hud/gui/AbstractHudEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public abstract class AbstractHudEntry extends DrawUtil {
3131
public int height;
3232

3333
protected BooleanOption enabled = new BooleanOption("enabled",false);
34-
public DoubleOption scale = new DoubleOption("scale", 1, 0.1F, 2);
34+
public DoubleOption scale = new DoubleOption("scale", 1, 0.1F, 5);
3535
protected final ColorOption textColor = new ColorOption("textColor", Color.WHITE);
3636
protected EnumOption textAlignment = new EnumOption("textAlignment", new String[]{"center", "left", "right"}, "center");
3737
protected final BooleanOption chroma = new BooleanOption("chroma", false);

src/main/resources/axolotlclient.mixins.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"AccessorShaderEffect",
1313
"MixinAddServerScreen",
1414
"MixinBossBarHud",
15+
"MixinCamera",
1516
"MixinChatHud",
1617
"MixinClientBrandRetriever",
1718
"MixinClientPlayerEntity",
@@ -20,6 +21,7 @@
2021
"MixinCrashReport",
2122
"MixinDebugHud",
2223
"MixinDownloadingTerrainScreen",
24+
"MixinEntity",
2325
"MixinEntityRenderer",
2426
"MixinGameMenuScreen",
2527
"MixinGameRenderer",

0 commit comments

Comments
 (0)