Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Version 2.0
Browse files Browse the repository at this point in the history
*Sort code
*Improved methods
*Fixed FakePlayer (-> FakePlayerUtils)
*Updated icon.png
  • Loading branch information
I-No-oNe committed Jul 18, 2024
1 parent ea5274c commit ef6cbac
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 130 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.21+build.1
loader_version=0.15.11

# Mod Properties
mod_version=1.0-1.21-fabric
mod_version=2.0-1.21-fabric
maven_group=net.i_no_am.freecamera
archives_base_name=free-camera

Expand Down
65 changes: 27 additions & 38 deletions src/main/java/net/i_no_am/freecamera/FreeCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.i_no_am.freecamera.utils.FakePlayer;
import net.minecraft.client.network.ClientPlayerEntity;
import net.i_no_am.freecamera.client.Global;
import net.i_no_am.freecamera.utils.ConfigUtils;
import net.i_no_am.freecamera.utils.FakePlayerUtils;
import net.i_no_am.freecamera.utils.PlayerUtils;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.math.Vec3d;
import org.lwjgl.glfw.GLFW ;
import org.lwjgl.glfw.GLFW;

public class FreeCamera implements ModInitializer, Global {


public static boolean isCameraActive = false;
public FakePlayerUtils fakePlayerUtils;

public class FreeCamera implements ModInitializer {

public static final KeyBinding BIND = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"Toggle Free Camera",
Expand All @@ -20,51 +26,34 @@ public class FreeCamera implements ModInitializer {
"Free Camera"
));

public FakePlayer fakePlayer;
private static boolean isCameraActive = false;

@Override
public void onInitialize() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
final ClientPlayerEntity player = client.player;
if (player == null) return;
final ClientWorld world = player.clientWorld;
if (BIND.wasPressed()) {
toggleCamera();
if (BIND.wasPressed() && PlayerUtils.canUseFreeCam()) {
ConfigUtils.toggleCamera();
if (isCameraActive) {
fakePlayer = new FakePlayer(player, world);
} else {
player.setVelocity(Vec3d.ZERO);
player.getAbilities().flying = false;
if (fakePlayer != null) {
fakePlayer.resetPlayerPosition();
fakePlayer.despawn();
fakePlayerUtils = new FakePlayerUtils();
PlayerUtils.setFlying(true);
}else{
PlayerUtils.setVec3d(Vec3d.ZERO);
PlayerUtils.setFlying(false);
if (fakePlayerUtils != null) {
fakePlayerUtils.despawn();
}
fakePlayer = null;
fakePlayerUtils = null;
}
}

if (isCameraActive) {
player.getAbilities().flying = true;
}
if (!player.isAlive() || player.clientWorld.getDimension() != player.getWorld().getDimension()) {
if (PlayerUtils.isDied() || PlayerUtils.changedDimension()) {
if (isCameraActive) {
toggleCamera();
if (fakePlayer != null) {
fakePlayer.resetPlayerPosition();
fakePlayer.despawn();
ConfigUtils.toggleCamera();
if (fakePlayerUtils != null) {
fakePlayerUtils.despawn();
}
fakePlayer = null;
fakePlayerUtils = null;
}
}
});
}

public static boolean isCameraActive() {
return isCameraActive;
}

private static void toggleCamera() {
isCameraActive = !isCameraActive;
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/net/i_no_am/freecamera/client/Global.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.i_no_am.freecamera.client;

import net.minecraft.client.MinecraftClient;

public interface Global {
MinecraftClient mc = MinecraftClient.getInstance();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package net.i_no_am.freecamera.mixin;

import com.mojang.authlib.GameProfile;
import net.i_no_am.freecamera.FreeCamera;
import net.minecraft.client.MinecraftClient;
import net.i_no_am.freecamera.client.Global;
import net.i_no_am.freecamera.utils.ConfigUtils;
import net.i_no_am.freecamera.utils.FakePlayerUtils;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
Expand All @@ -14,18 +14,17 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(AbstractClientPlayerEntity.class)
public abstract class MixinAbstractClientPlayerEntity extends PlayerEntity {
public abstract class MixinAbstractClientPlayerEntity extends PlayerEntity implements Global {

public MixinAbstractClientPlayerEntity(World world, BlockPos pos, float yaw, GameProfile gameProfile) {
super(world, pos, yaw, gameProfile);
}

@Inject(method = "isSpectator", at = @At("HEAD"), cancellable = true)
private void overrideIsSpectator(CallbackInfoReturnable<Boolean> cir) {
if (MinecraftClient.getInstance() == null) return;
final ClientPlayerEntity player = MinecraftClient.getInstance().player;
if (player == null) return;
if (FreeCamera.isCameraActive() && this.getUuid().equals(player.getUuid())) {
private void onIsSpectator(CallbackInfoReturnable<Boolean> cir) {
if (mc.player == null) return;
if ((Object) this instanceof FakePlayerUtils) return;
if (ConfigUtils.isCameraActive()) {
cir.setReturnValue(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package net.i_no_am.freecamera.mixin;

import net.i_no_am.freecamera.FreeCamera;
import net.i_no_am.freecamera.utils.ConfigUtils;
import net.i_no_am.freecamera.utils.PlayerUtils;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket;

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(ClientConnection.class)
public class MixinClientConnection {
@Inject(at = @At("HEAD"), method = "send(Lnet/minecraft/network/packet/Packet;)V", cancellable = true)
private void sendPacket(Packet<?> packet, CallbackInfo ci) {
if (FreeCamera.isCameraActive() && packet instanceof PlayerMoveC2SPacket) {
@Inject(method = "send(Lnet/minecraft/network/packet/Packet;)V", at = @At("HEAD"), cancellable = true)
private void onSendPacket(Packet<?> packet, CallbackInfo ci) {
if (PlayerUtils.notNull() && ConfigUtils.isCameraActive() && packet instanceof PlayerMoveC2SPacket) {
ci.cancel();
}
}

}
}
37 changes: 37 additions & 0 deletions src/main/java/net/i_no_am/freecamera/mixin/MixinInGameHud.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.i_no_am.freecamera.mixin;

import net.i_no_am.freecamera.client.Global;
import net.i_no_am.freecamera.utils.ConfigUtils;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.render.RenderTickCounter;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;

@Mixin(InGameHud.class)
public class MixinInGameHud implements Global {

@Inject(method = "renderOverlayMessage", at = @At(value = "HEAD"))
private void onRenderOverlayMessage(DrawContext context, RenderTickCounter tickCounter, CallbackInfo ci) {
if (ConfigUtils.isCameraActive()) {

MutableText msgP1 = Text.literal("Free Camera is ");
MutableText msgP2 = Text.literal("ON").setStyle(Style.EMPTY.withColor(TextColor.fromRgb(0x00FF00)));

MutableText fullMsg = msgP1.append(msgP2);

int messageWidth = mc.textRenderer.getWidth(fullMsg);
int x = (mc.getWindow().getScaledWidth() / 2) - (messageWidth / 2);
int y = mc.getWindow().getScaledHeight() - 51;

context.drawText(mc.textRenderer, fullMsg, x, y, 0xFFFFFF, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.i_no_am.freecamera.mixin;

import net.i_no_am.freecamera.FreeCamera;
import net.i_no_am.freecamera.client.Global;
import net.i_no_am.freecamera.utils.ConfigUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.world.World;
Expand All @@ -10,16 +11,14 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerEntity.class)
public abstract class MixinPlayerEntity extends Entity {
public abstract class MixinPlayerEntity extends Entity implements Global {

protected MixinPlayerEntity(World world) {
super(null, world);
super(mc.player.getType(), world);
}

@Inject(method = "isPushedByFluids", at = @At("HEAD"), cancellable = true)
private void isPushedByFluids(CallbackInfoReturnable<Boolean> cir) {

if (FreeCamera.isCameraActive()) {
if (ConfigUtils.isCameraActive()) {
cir.setReturnValue(false);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/i_no_am/freecamera/utils/ConfigUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.i_no_am.freecamera.utils;

import net.i_no_am.freecamera.FreeCamera;

public class ConfigUtils {
public static boolean isCameraActive() {
return FreeCamera.isCameraActive;
}
public static void toggleCamera() {
FreeCamera.isCameraActive = !FreeCamera.isCameraActive;
}
}
69 changes: 0 additions & 69 deletions src/main/java/net/i_no_am/freecamera/utils/FakePlayer.java

This file was deleted.

54 changes: 54 additions & 0 deletions src/main/java/net/i_no_am/freecamera/utils/FakePlayerUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.i_no_am.freecamera.utils;

import com.mojang.authlib.GameProfile;
import net.i_no_am.freecamera.client.Global;
import net.minecraft.client.network.OtherClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;

import java.util.Objects;
import java.util.UUID;

public class FakePlayerUtils extends OtherClientPlayerEntity implements Global {

private final ClientPlayerEntity p = mc.player;
private final ClientWorld w = mc.world;

public FakePlayerUtils() {
super(Objects.requireNonNull(mc.world), new GameProfile(UUID.fromString("66123666-6666-6666-6666-666666666600"), mc.player.getName().getString()));
copyPositionAndRotation(mc.player);
copyInventory();
copyPlayerModel();
copyRotation();
resetCapeMovement();
spawn();
}

private void copyInventory() {
getInventory().clone(p.getInventory());
}

private void copyPlayerModel() {
dataTracker.set(PLAYER_MODEL_PARTS, mc.player.getDataTracker().get(PLAYER_MODEL_PARTS));
}

private void copyRotation() {
headYaw = p.headYaw;
bodyYaw = p.bodyYaw;
}

private void resetCapeMovement() {
capeX = getX();
capeY = getY();
capeZ = getZ();
}

private void spawn() {
w.addEntity(this);
}

public void despawn() {
p.refreshPositionAndAngles(getX(), getY(), getZ(), getYaw(), getPitch());
discard();
}
}
Loading

0 comments on commit ef6cbac

Please sign in to comment.