FakePlayer module

FakePlayer: Minecraft 26.3 cheat module, full source for every implementation (3 clients: MelaHack (ThunderHack fork), OfflineClient, Combatant).

3 implementationsMISC

Full module source: 3 implementations

Every implementation below is the complete module file, verbatim. Expand a client to read its code.

MelaHack (ThunderHack fork)MISCsrc/main/java/thunder/hack/features/modules/misc/FakePlayer.javaJava · 162 lines · 7.0 KB

Fake player for testing the PvP modules.

SettingsCopyInventoryRecordPlayAutoTotemName
FakePlayer.javaJava · Minecraft 26.2 · 162 lines · 7.0 KB · complete file, verbatimOriginal ↗
1
package thunder.hack.features.modules.misc;
2
3
import com.mojang.authlib.GameProfile;
4
import meteordevelopment.orbit.EventHandler;
5
import net.minecraft.client.player.RemotePlayer;
6
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
7
import net.minecraft.network.protocol.game.ClientboundExplodePacket;
8
import net.minecraft.sounds.SoundEvents;
9
import net.minecraft.sounds.SoundSource;
10
import net.minecraft.world.InteractionHand;
11
import net.minecraft.world.effect.MobEffectInstance;
12
import net.minecraft.world.effect.MobEffects;
13
import net.minecraft.world.entity.Entity;
14
import net.minecraft.world.entity.EntityEvent;
15
import net.minecraft.world.item.ItemStack;
16
import net.minecraft.world.item.Items;
17
import net.minecraft.world.phys.Vec3;
18
import thunder.hack.ThunderHack;
19
import thunder.hack.core.manager.client.ModuleManager;
20
import thunder.hack.events.impl.EventAttack;
21
import thunder.hack.events.impl.EventSync;
22
import thunder.hack.events.impl.PacketEvent;
23
import thunder.hack.events.impl.TotemPopEvent;
24
import thunder.hack.features.modules.Module;
25
import thunder.hack.setting.Setting;
26
import thunder.hack.utility.world.ExplosionUtility;
27
import thunder.hack.utility.player.InventoryUtility;
28
29
import java.util.ArrayList;
30
import java.util.List;
31
import java.util.UUID;
32
33
public class FakePlayer extends Module {
34
    private final Setting<Boolean> copyInventory = new Setting<>("CopyInventory", false);
35
36
    public static RemotePlayer fakePlayer;
37
38
    public FakePlayer() {
39
        super("FakePlayer", Category.MISC);
40
    }
41
42
    private Setting<Boolean> record = new Setting<>("Record", false);
43
    private Setting<Boolean> play = new Setting<>("Play", false);
44
    private Setting<Boolean> autoTotem = new Setting<>("AutoTotem", false);
45
    private Setting<String> name = new Setting<>("Name", "Hell_Raider");
46
47
    private final List<PlayerState> positions = new ArrayList<>();
48
49
    int movementTick, deathTime;
50
51
    @Override
52
    public void onEnable() {
53
        fakePlayer = new RemotePlayer(mc.level, new GameProfile(UUID.fromString("66123666-6666-6666-6666-666666666600"), name.getValue()));
54
        fakePlayer.copyPosition(mc.player);
55
56
        if (copyInventory.getValue()) {
57
            fakePlayer.setItemInHand(InteractionHand.MAIN_HAND, mc.player.getMainHandItem().copy());
58
            fakePlayer.setItemInHand(InteractionHand.OFF_HAND, mc.player.getOffhandItem().copy());
59
60
            fakePlayer.getInventory().setItem(36, mc.player.getInventory().getItem(36).copy());
61
            fakePlayer.getInventory().setItem(37, mc.player.getInventory().getItem(37).copy());
62
            fakePlayer.getInventory().setItem(38, mc.player.getInventory().getItem(38).copy());
63
            fakePlayer.getInventory().setItem(39, mc.player.getInventory().getItem(39).copy());
64
        }
65
66
        mc.level.addEntity(fakePlayer);
67
        fakePlayer.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 9999, 2));
68
        fakePlayer.addEffect(new MobEffectInstance(MobEffects.ABSORPTION, 9999, 4));
69
        fakePlayer.addEffect(new MobEffectInstance(MobEffects.RESISTANCE, 9999, 1));
70
    }
71
72
    @EventHandler
73
    public void onPacketReceive(PacketEvent.Receive e) {
74
        if (e.getPacket() instanceof ClientboundExplodePacket explosion && fakePlayer != null && fakePlayer.hurtTime == 0) {
75
            fakePlayer.handleDamageEvent(mc.level.damageSources().generic());
76
            Vec3 center = explosion.center();
77
            fakePlayer.setHealth(fakePlayer.getHealth() + fakePlayer.getAbsorptionAmount() - ExplosionUtility.getAutoCrystalDamage(new Vec3(center.x, center.y, center.z), fakePlayer, 0, false));
78
            if (fakePlayer.isDeadOrDying()) {
79
                if (fakePlayer.getOffhandItem().is(Items.TOTEM_OF_UNDYING)) {
80
                    fakePlayer.getOffhandItem().shrink(1);
81
                    fakePlayer.setHealth(10f);
82
83
84
                    ThunderHack.EVENT_BUS.post(new TotemPopEvent(fakePlayer, 1));
85
86
                    //      new EntityStatusS2CPacket(fakePlayer, EntityStatuses.USE_TOTEM_OF_UNDYING).apply(mc.player.networkHandler);
87
                }
88
            }
89
        }
90
    }
91
92
    @EventHandler
93
    public void onSync(EventSync e) {
94
        if (record.getValue()) {
95
            positions.add(new PlayerState(mc.player.getX(), mc.player.getY(), mc.player.getZ(), mc.player.getYRot(), mc.player.getXRot()));
96
            return;
97
        }
98
        if (fakePlayer != null) {
99
            if (play.getValue() && !positions.isEmpty()) {
100
                movementTick++;
101
102
                if (movementTick >= positions.size()) {
103
                    movementTick = 0;
104
                    return;
105
                }
106
                PlayerState p = positions.get(movementTick);
107
                fakePlayer.setYRot(p.yaw);
108
                fakePlayer.setXRot(p.pitch);
109
                fakePlayer.setYHeadRot(p.yaw);
110
111
                fakePlayer.setPos(p.x, p.y, p.z);
112
                fakePlayer.setYRot(p.yaw);
113
                fakePlayer.setXRot(p.pitch);
114
                fakePlayer.setYHeadRot(p.yaw);
115
            } else movementTick = 0;
116
117
            if (autoTotem.getValue() && fakePlayer.getOffhandItem().getItem() != Items.TOTEM_OF_UNDYING)
118
                fakePlayer.setItemInHand(InteractionHand.OFF_HAND, new ItemStack(Items.TOTEM_OF_UNDYING));
119
120
            if (fakePlayer.isDeadOrDying()) {
121
                deathTime++;
122
                if (deathTime > 10) disable();
123
            }
124
        }
125
    }
126
127
    @EventHandler
128
    public void onAttack(EventAttack e) {
129
        if (fakePlayer != null && e.getEntity() == fakePlayer && fakePlayer.hurtTime == 0 && !e.isPre()) {
130
            mc.level.playSound(mc.player, fakePlayer.getX(), fakePlayer.getY(), fakePlayer.getZ(), SoundEvents.PLAYER_HURT, SoundSource.PLAYERS, 1f, 1f);
131
132
            if (mc.player.fallDistance > 0 || ModuleManager.criticals.isEnabled())
133
                mc.level.playSound(mc.player, fakePlayer.getX(), fakePlayer.getY(), fakePlayer.getZ(), SoundEvents.PLAYER_ATTACK_CRIT, SoundSource.PLAYERS, 1f, 1f);
134
            fakePlayer.handleDamageEvent(mc.level.damageSources().generic());
135
            if (ModuleManager.aura.getAttackCooldown() >= 0.85)
136
                fakePlayer.setHealth(fakePlayer.getHealth() + fakePlayer.getAbsorptionAmount() - InventoryUtility.getHitDamage(mc.player.getMainHandItem(), fakePlayer));
137
            else fakePlayer.setHealth(fakePlayer.getHealth() + fakePlayer.getAbsorptionAmount() - 1f);
138
            if (fakePlayer.isDeadOrDying()) {
139
                if (fakePlayer.getOffhandItem().is(Items.TOTEM_OF_UNDYING)) {
140
                    fakePlayer.getOffhandItem().shrink(1);
141
                    fakePlayer.setHealth(10f);
142
                    new ClientboundEntityEventPacket(fakePlayer, EntityEvent.PROTECTED_FROM_DEATH).handle(mc.player.connection);
143
                }
144
            }
145
        }
146
    }
147
148
    @Override
149
    public void onDisable() {
150
        if (fakePlayer == null) return;
151
        fakePlayer.discard();
152
        fakePlayer.setRemoved(Entity.RemovalReason.KILLED);
153
        fakePlayer.onClientRemoval();
154
        fakePlayer = null;
155
        positions.clear();
156
        deathTime = 0;
157
    }
158
159
    private record PlayerState(double x, double y, double z, float yaw, float pitch) {
160
    }
161
}
162
OfflineClientMISCsrc/main/java/com/jellypudding/offlineclient/modules/misc/FakePlayer.javaJava · 194 lines · 7.2 KB

Spawns a copy of you that only you can see. Practise your combat on it.

SettingsNameCopiesCopy gearHealth
FakePlayer.javaJava · Minecraft 26.2 · 194 lines · 7.2 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.misc;
2
3
import com.jellypudding.offlineclient.OfflineClient;
4
import com.jellypudding.offlineclient.event.Subscribe;
5
import com.jellypudding.offlineclient.event.events.ClientTickEvent;
6
import com.jellypudding.offlineclient.module.Category;
7
import com.jellypudding.offlineclient.module.Module;
8
import com.jellypudding.offlineclient.setting.BoolSetting;
9
import com.jellypudding.offlineclient.setting.NumberSetting;
10
import com.jellypudding.offlineclient.setting.TextSetting;
11
import com.jellypudding.offlineclient.util.ChatUtil;
12
import com.mojang.authlib.GameProfile;
13
import net.minecraft.client.multiplayer.ClientLevel;
14
import net.minecraft.client.multiplayer.PlayerInfo;
15
import net.minecraft.client.player.LocalPlayer;
16
import net.minecraft.client.player.RemotePlayer;
17
import net.minecraft.client.resources.DefaultPlayerSkin;
18
import net.minecraft.world.entity.Entity;
19
import net.minecraft.world.entity.player.Inventory;
20
import net.minecraft.world.entity.player.PlayerSkin;
21
import net.minecraft.world.phys.Vec3;
22
23
import java.util.ArrayList;
24
import java.util.List;
25
import java.util.UUID;
26
import java.util.concurrent.ThreadLocalRandom;
27
import java.util.function.Supplier;
28
29
// A client side copy of the player. The server never knows it exists.
30
public final class FakePlayer extends Module {
31
32
    // How far apart several copies stand.
33
    private static final double RING = 1.5;
34
35
    private final TextSetting name = new TextSetting("Name",
36
        "The name over the copy. Blank uses your own name and skin.", "");
37
    private final NumberSetting copies = new NumberSetting("Copies",
38
        "How many are spawned in a ring round you.", 1, 1, 5, 1).min(1).max(10);
39
    private final BoolSetting copyGear = new BoolSetting("Copy gear",
40
        "Give the copy your armour and held items.", true);
41
    private final NumberSetting health = new NumberSetting("Health",
42
        "Health points the copy starts with.",
43
        20, 1, 40, 1).min(1);
44
45
    private final List<Body> bodies = new ArrayList<>();
46
47
    public FakePlayer() {
48
        super("FakePlayer", "Spawns a copy of you that only you can see. Practise your combat on it.", Category.MISC);
49
        addSettings(name, copies, copyGear, health);
50
        searchTags("dummy", "bot", "target");
51
    }
52
53
    @Override
54
    public boolean savesEnabledState() {
55
        return false;
56
    }
57
58
    @Override
59
    protected void onEnable() {
60
        if (!inGame()) {
61
            ChatUtil.error("Join a world before spawning a fake player.");
62
            setEnabled(false);
63
            return;
64
        }
65
        int count = copies.getInt();
66
        for (int i = 0; i < count; i++) {
67
            Body body = new Body(mc.level, mc.player, name.getValue().trim(), health.getFloat(),
68
                copyGear.isOn(), false);
69
            if (count > 1) {
70
                Vec3 offset = Vec3.directionFromRotation(0, mc.player.getYRot() + 360f * i / count)
71
                    .scale(RING);
72
                body.setPos(mc.player.getX() + offset.x, mc.player.getY(), mc.player.getZ() + offset.z);
73
                body.setOldPosAndRot();
74
            }
75
            mc.level.addEntity(body);
76
            bodies.add(body);
77
        }
78
    }
79
80
    @Override
81
    protected void onDisable() {
82
        for (Body body : bodies) {
83
            if (mc.level != null && body.level() == mc.level) {
84
                mc.level.removeEntity(body.getId(), Entity.RemovalReason.DISCARDED);
85
            }
86
        }
87
        bodies.clear();
88
    }
89
90
    @Subscribe
91
    private void onClientTick(ClientTickEvent event) {
92
        if (bodies.isEmpty()) {
93
            return;
94
        }
95
        bodies.removeIf(body -> mc.level == null || body.level() != mc.level || body.isRemoved());
96
        if (bodies.isEmpty()) {
97
            setEnabled(false);
98
        }
99
    }
100
101
    // The level refuses two entities with the same UUID. Every body gets a fresh one.
102
    public static final class Body extends RemotePlayer {
103
104
        // Far above any entity id the server hands out.
105
        private static final int ID_BASE = Integer.MAX_VALUE - 100_000;
106
107
        // Read live. A skin that finishes downloading later still shows up.
108
        private final Supplier<PlayerSkin> skin;
109
        private final UUID infoOwner;
110
111
        // A ghost is only a picture. Nothing hits it and it pushes nobody.
112
        private final boolean ghost;
113
114
        // A copy under the player's own name and skin.
115
        public Body(ClientLevel level, LocalPlayer source, float startHealth, boolean copyGear,
116
                    boolean ghost) {
117
            this(level, source, "", startHealth, copyGear, ghost);
118
        }
119
120
        public Body(ClientLevel level, LocalPlayer source, String name, float startHealth,
121
                    boolean copyGear, boolean ghost) {
122
            super(level, new GameProfile(UUID.randomUUID(),
123
                name.isEmpty() ? source.getGameProfile().name() : name));
124
            this.ghost = ghost;
125
            setId(ID_BASE + ThreadLocalRandom.current().nextInt(100_000));
126
127
            PlayerInfo other = name.isEmpty() || OfflineClient.MC.getConnection() == null ? null
128
                : OfflineClient.MC.getConnection().getPlayerInfoIgnoreCase(name);
129
            if (name.isEmpty() || name.equalsIgnoreCase(source.getGameProfile().name())) {
130
                skin = source::getSkin;
131
                infoOwner = source.getUUID();
132
            } else if (other != null) {
133
                skin = other::getSkin;
134
                infoOwner = other.getProfile().id();
135
            } else {
136
                PlayerSkin fallback = DefaultPlayerSkin.get(getUUID());
137
                skin = () -> fallback;
138
                infoOwner = null;
139
            }
140
141
            copyPosition(source);
142
            yRotO = getYRot();
143
            xRotO = getXRot();
144
            yHeadRot = source.yHeadRot;
145
            yHeadRotO = yHeadRot;
146
            yBodyRot = source.yBodyRot;
147
            yBodyRotO = yBodyRot;
148
            setPose(source.getPose());
149
            getAttributes().assignAllValues(source.getAttributes());
150
151
            setHealth(Math.min(startHealth, getMaxHealth()));
152
            if (startHealth > getMaxHealth()) {
153
                setAbsorptionAmount(startHealth - getMaxHealth());
154
            }
155
            if (copyGear) {
156
                Inventory theirs = source.getInventory();
157
                Inventory ours = getInventory();
158
                for (int slot = 0; slot < ours.getContainerSize(); slot++) {
159
                    ours.setItem(slot, theirs.getItem(slot).copy());
160
                }
161
                ours.setSelectedSlot(theirs.getSelectedSlot());
162
            }
163
        }
164
165
        // Read by EntityRendererMixin. A ghost vanishes whilst the camera stands inside it.
166
        public boolean hidesAroundCamera() {
167
            return ghost;
168
        }
169
170
        @Override
171
        public boolean isPushable() {
172
            return !ghost && super.isPushable();
173
        }
174
175
        @Override
176
        public boolean isPickable() {
177
            return !ghost && super.isPickable();
178
        }
179
180
        @Override
181
        public PlayerSkin getSkin() {
182
            return skin.get();
183
        }
184
185
        @Override
186
        protected PlayerInfo getPlayerInfo() {
187
            if (infoOwner == null || OfflineClient.MC.getConnection() == null) {
188
                return null;
189
            }
190
            return OfflineClient.MC.getConnection().getPlayerInfo(infoOwner);
191
        }
192
    }
193
}
194
CombatantMISCsrc/main/java/combatant/client/features/module/modules/misc/FakePlayer.javaJava · 348 lines · 13.8 KB
SettingsfakePlayerCopyInventoryfakePlayerAutoTotem
FakePlayer.javaJava · Minecraft 26.2 · 348 lines · 13.8 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of the Combatant Client distribution.
3
 * Copyright (c) 2026 pivosos2007.
4
 *
5
 * Licensed under the GNU General Public License v3.0.
6
 */
7
8
package combatant.client.features.module.modules.misc;
9
10
import com.mojang.authlib.GameProfile;
11
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
12
import net.minecraft.client.Minecraft;
13
import net.minecraft.core.Holder;
14
import net.minecraft.core.Registry;
15
import net.minecraft.core.registries.Registries;
16
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
17
import net.minecraft.resources.ResourceKey;
18
import net.minecraft.sounds.SoundEvents;
19
import net.minecraft.sounds.SoundSource;
20
import net.minecraft.world.InteractionHand;
21
import net.minecraft.world.InteractionResult;
22
import net.minecraft.world.damagesource.CombatRules;
23
import net.minecraft.world.damagesource.DamageSource;
24
import net.minecraft.world.effect.MobEffectInstance;
25
import net.minecraft.world.effect.MobEffects;
26
import net.minecraft.world.entity.EntityEvent;
27
import net.minecraft.world.entity.EquipmentSlot;
28
import net.minecraft.world.entity.EquipmentSlotGroup;
29
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
30
import net.minecraft.world.entity.ai.attributes.Attributes;
31
import net.minecraft.world.entity.player.Player;
32
import net.minecraft.world.item.ItemStack;
33
import net.minecraft.world.item.Items;
34
import net.minecraft.world.item.MaceItem;
35
import net.minecraft.world.item.enchantment.Enchantment;
36
import net.minecraft.world.item.enchantment.EnchantmentHelper;
37
import net.minecraft.world.item.enchantment.Enchantments;
38
import net.minecraft.world.phys.Vec3;
39
import combatant.client.config.values.BooleanValue;
40
import combatant.client.features.module.Module;
41
import combatant.client.features.module.ModuleCategory;
42
import combatant.client.features.module.ModuleInfo;
43
44
import java.util.UUID;
45
46
//todo Description
47
@ModuleInfo(
48
        id = "fakeplayer",
49
        displayName = "FakePlayer",
50
        category = ModuleCategory.MISC
51
)
52
public class FakePlayer extends Module {
53
54
    private static final String SETTING_COPY_INVENTORY = "copy_inventory";
55
    private static final String SETTING_AUTO_TOTEM = "auto_totem";
56
    private static final float SMASH_THRESHOLD = 1.5f;
57
    private static final float SMASH_BASE_DAMAGE_PER_BLOCK = 1.0f;
58
    private final Minecraft mc = Minecraft.getInstance();
59
    private final BooleanValue copyInventory =
60
            bool("fakePlayerCopyInventory", SETTING_COPY_INVENTORY, false);
61
    private final BooleanValue autoTotem =
62
            bool("fakePlayerAutoTotem", SETTING_AUTO_TOTEM, true);
63
    private static int nextFakeEntityId = -1_000_000;
64
    private Player fakePlayer;
65
    private int deathTime;
66
67
    public FakePlayer() {
68
        AttackEntityCallback.EVENT.register((player, world, hand, target, hitResult) -> {
69
            if (!isEnabled()) return InteractionResult.PASS;
70
            if (mc.player == null || mc.level == null) return InteractionResult.PASS;
71
            if (hand != InteractionHand.MAIN_HAND) return InteractionResult.PASS;
72
            if (fakePlayer == null || target != fakePlayer) return InteractionResult.PASS;
73
            if (!(player instanceof Player attacker)) return InteractionResult.PASS;
74
            if (fakePlayer.hurtTime > 0) return InteractionResult.PASS;
75
76
            handleAttack(attacker, fakePlayer);
77
            return InteractionResult.PASS;
78
        });
79
    }
80
81
    /**
82
     * Extracts the MAINHAND attack-damage modifier amount from an ItemStack.
83
     * <p>
84
     * Compatibility fallback for client-side attack callbacks that sometimes observe
85
     * EntityAttributes.ATTACK_DAMAGE without the weapon modifiers applied.
86
     */
87
    private static float getMainHandAttackDamageBonus(ItemStack stack) {
88
        if (stack == null || stack.isEmpty()) return 0.0f;
89
90
        final double[] add = {0.0};
91
        final double[] mulBase = {0.0};
92
        final double[] mulTotal = {1.0};
93
94
        stack.forEachModifier(EquipmentSlotGroup.MAINHAND, (attribute, modifier, display) -> {
95
            if (!attribute.equals(Attributes.ATTACK_DAMAGE)) return;
96
            AttributeModifier.Operation op = modifier.operation();
97
            double amount = modifier.amount();
98
            if (op == AttributeModifier.Operation.ADD_VALUE) {
99
                add[0] += amount;
100
            } else if (op == AttributeModifier.Operation.ADD_MULTIPLIED_BASE) {
101
                mulBase[0] += amount;
102
            } else if (op == AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL) {
103
                mulTotal[0] *= (1.0 + amount);
104
            }
105
        });
106
107
        // For vanilla weapons this is effectively just the ADD_VALUE sum.
108
        double base = 0.0;
109
        double value = (base + add[0] + base * mulBase[0]) * mulTotal[0];
110
        return (float) value;
111
    }
112
113
    @Override
114
    public void onEnable() {
115
        if (mc.level == null || mc.player == null) return;
116
117
        despawnFake();
118
119
        String baseName = mc.player.getGameProfile().name();
120
        String fakeName = baseName + "_fake";
121
        fakePlayer = new net.minecraft.client.player.RemotePlayer(
122
                mc.level,
123
                new GameProfile(UUID.fromString("66123666-6666-6666-6666-666666666600"), fakeName)
124
        );
125
126
        Vec3 pos = mc.player.position().add(1.0, 0.0, 0.0);
127
        fakePlayer.snapTo(pos.x, pos.y, pos.z, mc.player.getYRot(), mc.player.getXRot());
128
        fakePlayer.setYHeadRot(mc.player.getYRot());
129
130
        if (copyInventory.get()) {
131
            fakePlayer.setItemInHand(InteractionHand.MAIN_HAND, mc.player.getMainHandItem().copy());
132
            fakePlayer.setItemInHand(InteractionHand.OFF_HAND, mc.player.getOffhandItem().copy());
133
134
            // OtherClientPlayerEntity primarily renders/uses equipment slots, not the backing inventory indices.
135
            // Copy both to keep UI and damage/armor calculations consistent.
136
            ItemStack head = mc.player.getItemBySlot(EquipmentSlot.HEAD).copy();
137
            ItemStack chest = mc.player.getItemBySlot(EquipmentSlot.CHEST).copy();
138
            ItemStack legs = mc.player.getItemBySlot(EquipmentSlot.LEGS).copy();
139
            ItemStack feet = mc.player.getItemBySlot(EquipmentSlot.FEET).copy();
140
141
            fakePlayer.setItemSlot(EquipmentSlot.HEAD, head);
142
            fakePlayer.setItemSlot(EquipmentSlot.CHEST, chest);
143
            fakePlayer.setItemSlot(EquipmentSlot.LEGS, legs);
144
            fakePlayer.setItemSlot(EquipmentSlot.FEET, feet);
145
146
            // Inventory armor indices (vanilla): 36=FEET, 37=LEGS, 38=CHEST, 39=HEAD
147
            fakePlayer.getInventory().setItem(36, feet.copy());
148
            fakePlayer.getInventory().setItem(37, legs.copy());
149
            fakePlayer.getInventory().setItem(38, chest.copy());
150
            fakePlayer.getInventory().setItem(39, head.copy());
151
152
            fakePlayer.setItemSlot(EquipmentSlot.MAINHAND, fakePlayer.getMainHandItem());
153
            fakePlayer.setItemSlot(EquipmentSlot.OFFHAND, fakePlayer.getOffhandItem());
154
        }
155
156
        fakePlayer.setHealth(fakePlayer.getMaxHealth());
157
        fakePlayer.setAbsorptionAmount(0f);
158
        fakePlayer.setId(allocateFakeEntityId());
159
160
        mc.level.addEntity(fakePlayer);
161
        deathTime = 0;
162
    }
163
164
    @Override
165
    public void onDisable() {
166
        despawnFake();
167
    }
168
169
    @Override
170
    public void onTick() {
171
        if (!isEnabled() || mc.level == null || mc.player == null) return;
172
        if (fakePlayer == null) return;
173
174
        if (autoTotem.get() && fakePlayer.getOffhandItem().getItem() != Items.TOTEM_OF_UNDYING) {
175
            fakePlayer.setItemInHand(InteractionHand.OFF_HAND, new ItemStack(Items.TOTEM_OF_UNDYING));
176
        }
177
178
        if (fakePlayer.isDeadOrDying()) {
179
            deathTime++;
180
            if (deathTime > 10) {
181
                setEnabled(false);
182
            }
183
        } else {
184
            deathTime = 0;
185
        }
186
    }
187
188
    private int allocateFakeEntityId() {
189
        int id = nextFakeEntityId--;
190
        while (mc.level != null && mc.level.getEntity(id) != null) {
191
            id = nextFakeEntityId--;
192
        }
193
        if (nextFakeEntityId > -1) {
194
            nextFakeEntityId = -1_000_000;
195
        }
196
        return id;
197
    }
198
199
    private void despawnFake() {
200
        if (fakePlayer == null) return;
201
        fakePlayer.discard();
202
        fakePlayer = null;
203
        deathTime = 0;
204
    }
205
206
    private void handleAttack(Player attacker, Player target) {
207
        if (mc.level == null) return;
208
209
210
        ItemStack weapon = attacker.getMainHandItem();
211
        float baseDamage = (float) attacker.getAttributeValue(Attributes.ATTACK_DAMAGE);
212
213
        float handBase = (float) attacker.getAttributeBaseValue(Attributes.ATTACK_DAMAGE); // обычно 1.0
214
        float weaponBonus = getMainHandAttackDamageBonus(weapon);
215
216
// Если значение атрибута совпадает с базой (без оружия), значит weapon-модификаторы не учлись — добавляем вручную.
217
        if (weaponBonus > 0.0f && Math.abs(baseDamage - handBase) < 0.0001f) {
218
            baseDamage += weaponBonus;
219
        }
220
221
        float cooldown = attacker.getAttackStrengthScale(0.5f);
222
        float cooldownScale = 0.2f + cooldown * cooldown * 0.8f;
223
        float damage = baseDamage * cooldownScale;
224
225
        float effectBonus = 0f;
226
        if (attacker.hasEffect(MobEffects.STRENGTH)) {
227
            MobEffectInstance inst = attacker.getEffect(MobEffects.STRENGTH);
228
            if (inst != null) effectBonus += 3f * (inst.getAmplifier() + 1);
229
        }
230
        if (attacker.hasEffect(MobEffects.WEAKNESS)) {
231
            MobEffectInstance inst = attacker.getEffect(MobEffects.WEAKNESS);
232
            if (inst != null) effectBonus -= 4f * (inst.getAmplifier() + 1);
233
        }
234
        damage += effectBonus * cooldownScale;
235
236
        boolean smash = isMaceSmash(attacker, weapon);
237
        DamageSource source = smash
238
                ? mc.level.damageSources().mace(attacker)
239
                : mc.level.damageSources().playerAttack(attacker);
240
241
        damage += getSharpnessBonus(weapon) * cooldownScale;
242
        if (smash) {
243
            float smashDamage = getSmashDamage(attacker, weapon) * cooldownScale;
244
            damage += smashDamage;
245
        }
246
247
        boolean critical = isCritical(attacker, cooldown);
248
        if (critical) {
249
            damage *= 1.5f;
250
        }
251
252
        if (damage <= 0f) return;
253
254
        float armor = target.getArmorValue();
255
        float toughness = (float) target.getAttributeValue(Attributes.ARMOR_TOUGHNESS);
256
        float finalDamage = CombatRules.getDamageAfterAbsorb(target, damage, source, armor, toughness);
257
258
        applyDamage(target, source, finalDamage);
259
260
        if (critical) {
261
            mc.level.playSound(mc.player, target.getX(), target.getY(), target.getZ(),
262
                    SoundEvents.PLAYER_ATTACK_CRIT, SoundSource.PLAYERS, 1f, 1f);
263
        }
264
        mc.level.playSound(mc.player, target.getX(), target.getY(), target.getZ(),
265
                SoundEvents.PLAYER_HURT, SoundSource.PLAYERS, 1f, 1f);
266
267
        int fireAspect = getEnchantmentLevel(Enchantments.FIRE_ASPECT, weapon);
268
        if (fireAspect > 0) {
269
            target.igniteForSeconds(4 * fireAspect);
270
        }
271
    }
272
273
    private void applyDamage(Player target, DamageSource source, float damage) {
274
        if (damage <= 0f) return;
275
        target.handleDamageEvent(source);
276
277
        float absorption = target.getAbsorptionAmount();
278
        float left = damage;
279
        if (absorption > 0f) {
280
            float absorbed = Math.min(absorption, left);
281
            absorption -= absorbed;
282
            left -= absorbed;
283
            target.setAbsorptionAmount(absorption);
284
        }
285
286
        if (left > 0f) {
287
            target.setHealth(target.getHealth() - left);
288
        }
289
290
        if (target.isDeadOrDying() && autoTotem.get()
291
                && target.getOffhandItem().getItem() == Items.TOTEM_OF_UNDYING) {
292
            target.setHealth(10f);
293
            if (mc.player != null && mc.player.connection != null) {
294
                new ClientboundEntityEventPacket(target, EntityEvent.PROTECTED_FROM_DEATH)
295
                        .handle(mc.player.connection);
296
            }
297
        }
298
    }
299
300
    private boolean isCritical(Player attacker, float cooldown) {
301
        if (cooldown < 0.9f) return false;
302
        if (attacker.onGround()) return false;
303
        if (attacker.onClimbable()) return false;
304
        if (attacker.isInWater()) return false;
305
        if (attacker.isPassenger()) return false;
306
        return attacker.fallDistance > 0.0f;
307
    }
308
309
    private boolean isMaceSmash(Player attacker, ItemStack weapon) {
310
        if (!(weapon.getItem() instanceof MaceItem)) return false;
311
        if (!MaceItem.canSmashAttack(attacker)) return false;
312
        return attacker.fallDistance > SMASH_THRESHOLD;
313
    }
314
315
    private float getSmashDamage(Player attacker, ItemStack weapon) {
316
        float perBlock = getSmashDamagePerBlock(weapon);
317
        float fall = Math.max(0f, (float) (attacker.fallDistance - SMASH_THRESHOLD));
318
        return perBlock * fall;
319
    }
320
321
    private float getSmashDamagePerBlock(ItemStack weapon) {
322
        int density = getEnchantmentLevel(Enchantments.DENSITY, weapon);
323
        return SMASH_BASE_DAMAGE_PER_BLOCK + density * 0.5f;
324
    }
325
326
    private float getSharpnessBonus(ItemStack weapon) {
327
        int sharp = getEnchantmentLevel(Enchantments.SHARPNESS, weapon);
328
        if (sharp <= 0) return 0f;
329
        return 0.5f * sharp + 0.5f;
330
    }
331
332
    private int getEnchantmentLevel(ResourceKey<Enchantment> key, ItemStack stack) {
333
        Holder<Enchantment> entry = entryOf(key);
334
        if (entry == null) return 0;
335
        return EnchantmentHelper.getItemEnchantmentLevel(entry, stack);
336
    }
337
338
    private Holder<Enchantment> entryOf(ResourceKey<Enchantment> key) {
339
        if (key == null || mc.level == null) return null;
340
        Registry<Enchantment> registry =
341
                mc.level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT);
342
        Enchantment value = registry.getValue(key);
343
        if (value == null) return null;
344
        return registry.wrapAsHolder(value);
345
    }
346
347
}
348