Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FakePlayer Rewrite #4283

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.then(literal("add")
.executes(context -> {
FakePlayer fakePlayer = Modules.get().get(FakePlayer.class);
FakePlayerManager.add(fakePlayer.name.get(), fakePlayer.health.get(), fakePlayer.copyInv.get());
FakePlayerManager.add(fakePlayer.name.get(), fakePlayer.health.get(), fakePlayer.copyInv.get(), fakePlayer.allowDamage.get());
return SINGLE_SUCCESS;
})
.then(argument("name", StringArgumentType.word())
.executes(context -> {
FakePlayer fakePlayer = Modules.get().get(FakePlayer.class);
FakePlayerManager.add(StringArgumentType.getString(context, "name"), fakePlayer.health.get(), fakePlayer.copyInv.get());
FakePlayerManager.add(StringArgumentType.getString(context, "name"), fakePlayer.health.get(), fakePlayer.copyInv.get(), fakePlayer.allowDamage.get());
return SINGLE_SUCCESS;
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Blink() {
@Override
public void onActivate() {
if (renderOriginal.get()) {
model = new FakePlayerEntity(mc.player, mc.player.getGameProfile().getName(), 20, true);
model = new FakePlayerEntity(mc.player, mc.player.getGameProfile().getName(), 20, true, false);
model.doNotPush = true;
model.hideWhenInsideCamera = true;
model.spawn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package meteordevelopment.meteorclient.systems.modules.player;

import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.widgets.WWidget;
import meteordevelopment.meteorclient.gui.widgets.containers.WTable;
Expand All @@ -15,6 +16,10 @@
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.utils.entity.fakeplayer.FakePlayerEntity;
import meteordevelopment.meteorclient.utils.entity.fakeplayer.FakePlayerManager;
import meteordevelopment.meteorclient.utils.player.PlayerPosition;
import meteordevelopment.orbit.EventHandler;

import java.util.ArrayList;

public class FakePlayer extends Module {
private final SettingGroup sgGeneral = settings.getDefaultGroup();
Expand Down Expand Up @@ -42,10 +47,45 @@ public class FakePlayer extends Module {
.build()
);

public final Setting<Boolean> allowDamage = sgGeneral.add(new BoolSetting.Builder()
.name("allow-damage")
.description("Allows the fake player spawned to take damage and pop totems")
.defaultValue(true)
.build()
);

public final Setting<Boolean> loop = sgGeneral.add(new BoolSetting.Builder()
.name("loop-recording")
.description("Automatically replays the current recording everytime it ends")
.defaultValue(true)
.build()
);

public boolean recording = false;
public boolean playing = false;

public ArrayList<PlayerPosition> playerPositionsList = new ArrayList<>();


public FakePlayer() {
super(Categories.Player, "fake-player", "Spawns a client-side fake player for testing usages. No need to be active.");
}

@EventHandler
public void onTick(TickEvent.Post event) {
if (recording) {
playerPositionsList.add(new PlayerPosition(mc.player.getPos(), mc.player.getYaw(), mc.player.getPitch()));
} if (playing) {
if (!playerPositionsList.isEmpty()) {
if (loop.get()) playerPositionsList.add(playerPositionsList.get(0));
PlayerPosition playerPosition = playerPositionsList.remove(0);
FakePlayerManager.forEach(entity -> {entity.updateTrackedPositionAndAngles(playerPosition.pos().x, playerPosition.pos().y, playerPosition.pos().z, playerPosition.yaw(), playerPosition.pitch(),1);
entity.updateTrackedHeadRotation(playerPosition.yaw(), 2);
});
} else playing = false;
}
}

@Override
public WWidget getWidget(GuiTheme theme) {
WTable table = theme.table();
Expand All @@ -68,11 +108,35 @@ private void fillTable(GuiTheme theme, WTable table) {

WButton spawn = table.add(theme.button("Spawn")).expandCellX().right().widget();
spawn.action = () -> {
FakePlayerManager.add(name.get(), health.get(), copyInv.get());
if (!this.isActive()) return;
FakePlayerManager.add(name.get(), health.get(), copyInv.get(), allowDamage.get());
table.clear();
fillTable(theme, table);
};

WButton record = table.add(theme.button("Record")).right().widget();
record.action = () -> {
if (!recording) {
playerPositionsList.clear();
this.info("Recording started.");
} else this.info("Recording stopped.");
recording = !recording;
};
WButton play = table.add(theme.button("Play")).right().widget();
play.action = () -> {
if (!playing) {
if (playerPositionsList.isEmpty()) {
this.info("Cannot play an empty recording.");
return;
} else this.info("Recording now playing.");
} else this.info("Recording stopped playing.");
playing = !playing;
if (recording && playing) {
playing = false;
this.info("Cannot play while recording.");
}
};

WButton clear = table.add(theme.button("Clear All")).right().widget();
clear.action = () -> {
FakePlayerManager.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private class GhostPlayer extends FakePlayerEntity {
private double timer, scale = 1;

public GhostPlayer(PlayerEntity player) {
super(player, "ghost", 20, false);
super(player, "ghost", 20, false, false);

uuid = player.getUuid();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,37 @@
package meteordevelopment.meteorclient.utils.entity.fakeplayer;

import com.mojang.authlib.GameProfile;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.events.packets.PacketEvent;
import meteordevelopment.meteorclient.events.world.TickEvent;
import meteordevelopment.meteorclient.utils.player.DamageUtils;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.client.network.OtherClientPlayerEntity;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.ExplosionS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

import static meteordevelopment.meteorclient.MeteorClient.mc;

public class FakePlayerEntity extends OtherClientPlayerEntity {
public boolean doNotPush, hideWhenInsideCamera;
public boolean doNotPush, hideWhenInsideCamera, Pop, allowDamage, swapToTotem;
public float chosenHealth;

public FakePlayerEntity(PlayerEntity player, String name, float health, boolean copyInv) {
public int ticks;

public FakePlayerEntity(PlayerEntity player, String name, float health, boolean copyInv, boolean allowDamage) {
super(mc.world, new GameProfile(UUID.randomUUID(), name));

copyPositionAndRotation(player);
Expand All @@ -29,6 +47,9 @@ public FakePlayerEntity(PlayerEntity player, String name, float health, boolean
prevHeadYaw = headYaw;
bodyYaw = player.bodyYaw;
prevBodyYaw = bodyYaw;
this.allowDamage = allowDamage;
chosenHealth = health;
ticks = 0;

Byte playerModel = player.getDataTracker().get(PlayerEntity.PLAYER_MODEL_PARTS);
dataTracker.set(PlayerEntity.PLAYER_MODEL_PARTS, playerModel);
Expand All @@ -40,24 +61,107 @@ public FakePlayerEntity(PlayerEntity player, String name, float health, boolean
capeY = getY();
capeZ = getZ();

if (health <= 20) {
setHealth(health);
this.setHealth(health);

if (copyInv) getInventory().clone(player.getInventory());
}

// Removes health from FakePlayer and checks if Fake Player pops a totem.

private void removeHealth(float Damage) {
float health = this.getHealth() - Damage;

if (health < .5) {
health = chosenHealth;
Pop = true;
}

ticks = 0;
this.setHealth(health);
this.animateDamage(this.getYaw());
}

// Gets Explosion Damage
@EventHandler
private void onPacket(PacketEvent.Receive event) {
Packet<?> eventPacket = event.packet;
if (eventPacket instanceof ExplosionS2CPacket packet) {
if (!allowDamage) return;
if (ticks < 10) return;
float crystalDamage = (float) DamageUtils.crystalDamage(this, new Vec3d(packet.getX(), packet.getY(), packet.getZ()));
removeHealth(crystalDamage);
}
}

@EventHandler
public void onTick(TickEvent.Post event) {
ticks++;

// Gets Sword Damage
if ((mc.options.attackKey.isPressed() && mc.targetedEntity == this && mc.player.handSwinging)) {
if (ticks < 10) return;
if (!allowDamage) return;

if (canCrit()) {
mc.particleManager.addEmitter(this, ParticleTypes.CRIT);
mc.world.playSound(mc.player, this.getBlockPos(), SoundEvent.of(SoundEvents.ENTITY_PLAYER_ATTACK_CRIT.getId()), SoundCategory.PLAYERS, 1.0F, 1.0F);
} else {
mc.world.playSound(mc.player, this.getBlockPos(), SoundEvent.of(SoundEvents.ENTITY_PLAYER_ATTACK_STRONG.getId()), SoundCategory.PLAYERS, 1.0F, 1.0F);
}

removeHealth((float) DamageUtils.getSwordDamage(this, canCrit()));
ticks = 0;
}
this.tick();

// Sets FakePlayer Offhand Item to a Totem if Damage Option is enabled
if (allowDamage) {
if (swapToTotem && ticks < 3) return;
swapToTotem = false;
this.setStackInHand(Hand.OFF_HAND, Items.TOTEM_OF_UNDYING.getDefaultStack());
} else {
setHealth(health);
setAbsorptionAmount(health - 20);
this.setStackInHand(Hand.OFF_HAND, Items.AIR.getDefaultStack());
return;
}

if (copyInv) getInventory().clone(player.getInventory());
if (!Pop) return;

afterPop(this);

// Plays a Totem Pop sound and adds Totem Pop effects
mc.world.playSound(mc.player, this.getBlockPos(), SoundEvent.of(SoundEvents.ITEM_TOTEM_USE.getId()), SoundCategory.PLAYERS, 1.0F, 1.0F);
mc.particleManager.addEmitter(this, ParticleTypes.TOTEM_OF_UNDYING, 30);
Pop = false;
}

// What happens after Fake Player pops a totem.
private void afterPop(PlayerEntity player) {
if (player.getMainHandStack().getItem() == Items.TOTEM_OF_UNDYING) {
player.setStackInHand(Hand.MAIN_HAND, Items.AIR.getDefaultStack());
return;
}

if (player.getMainHandStack().getItem() == Items.TOTEM_OF_UNDYING && player.getOffHandStack().getItem() != Items.TOTEM_OF_UNDYING)
return;
player.setStackInHand(Hand.OFF_HAND, Items.AIR.getDefaultStack());
swapToTotem = true;
}

// Checks if Player can Crit
private boolean canCrit() {
return mc.player.getAttackCooldownProgress(0.5f) > 0.9f && mc.player.fallDistance > 0.0F && !mc.player.isOnGround() && !mc.player.isClimbing() && !mc.player.isSubmergedInWater() && !mc.player.hasStatusEffect(StatusEffects.BLINDNESS) && !mc.player.isSprinting();
}

public void spawn() {
unsetRemoved();
mc.world.addEntity(this);
MeteorClient.EVENT_BUS.subscribe(this);
}

public void despawn() {
mc.world.removeEntity(getId(), RemovalReason.DISCARDED);
setRemoved(RemovalReason.DISCARDED);
MeteorClient.EVENT_BUS.unsubscribe(this);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public static FakePlayerEntity get(String name) {
return null;
}

public static void add(String name, float health, boolean copyInv) {
public static void add(String name, float health, boolean copyInv, boolean allowDamage) {
if (!Utils.canUpdate()) return;

FakePlayerEntity fakePlayer = new FakePlayerEntity(mc.player, name, health, copyInv);
FakePlayerEntity fakePlayer = new FakePlayerEntity(mc.player, name, health, copyInv, allowDamage);
fakePlayer.spawn();
ENTITIES.add(fakePlayer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.utils.player;

import net.minecraft.util.math.Vec3d;

public record PlayerPosition(Vec3d pos, float yaw, float pitch) {
}