AutoWeapon module

AutoWeapon: Minecraft 26.3 cheat module, full source for every implementation (2 clients: OfflineClient, LiquidBounce (Kotlin)).

2 implementationsCOMBAT

Full module source: 2 implementations

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

OfflineClientCOMBATsrc/main/java/com/jellypudding/offlineclient/modules/combat/AutoWeapon.javaJava · 230 lines · 8.3 KB

Switches to your strongest sword or axe when you attack.

SettingsPreferSwap on hoverThresholdAnti breakSwitch backRelease time
AutoWeapon.javaJava · Minecraft 26.2 · 230 lines · 8.3 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.combat;
2
3
import com.jellypudding.offlineclient.event.Subscribe;
4
import com.jellypudding.offlineclient.event.events.AttackEntityEvent;
5
import com.jellypudding.offlineclient.event.events.TickEvent;
6
import com.jellypudding.offlineclient.module.Category;
7
import com.jellypudding.offlineclient.module.ExclusivityGroup;
8
import com.jellypudding.offlineclient.module.Module;
9
import com.jellypudding.offlineclient.setting.BoolSetting;
10
import com.jellypudding.offlineclient.setting.EnumSetting;
11
import com.jellypudding.offlineclient.setting.NumberSetting;
12
import com.jellypudding.offlineclient.util.DamageUtil;
13
import com.jellypudding.offlineclient.util.InventoryUtil.SlotSwap;
14
import com.jellypudding.offlineclient.util.InventoryUtil;
15
import com.jellypudding.offlineclient.util.EntityUtil;
16
import com.jellypudding.offlineclient.util.ItemUtil;
17
import net.minecraft.tags.ItemTags;
18
import net.minecraft.world.entity.LivingEntity;
19
import net.minecraft.world.entity.EquipmentSlot;
20
import net.minecraft.world.entity.ai.attributes.Attributes;
21
import net.minecraft.world.phys.EntityHitResult;
22
import net.minecraft.world.item.ItemStack;
23
24
public final class AutoWeapon extends Module {
25
26
    public enum Prefer { SWORD, AXE, SPEED }
27
28
    private final EnumSetting<Prefer> prefer = new EnumSetting<>("Prefer",
29
        "Which weapon wins when both are close in damage.", Prefer.SWORD)
30
        .describe(Prefer.SWORD, "The sword unless the axe hits much harder.")
31
        .describe(Prefer.AXE, "The axe unless the sword hits much harder.")
32
        .describe(Prefer.SPEED, "Whichever swings fastest. More hits a second.");
33
    private final BoolSetting hover = new BoolSetting("Swap on hover",
34
        "Switches as soon as your crosshair rests on a target. The weapon is in hand before you click.",
35
        false);
36
    private final NumberSetting threshold = new NumberSetting("Threshold",
37
        "How much more damage the other kind must deal after their armour to win.", 2, 0, 10, 0.5)
38
        .under(prefer, Prefer.SWORD, Prefer.AXE);
39
    private final BoolSetting antiBreak = new BoolSetting("Anti break",
40
        "Skip weapons that are about to break.", true);
41
    private final BoolSetting switchBack = new BoolSetting("Switch back",
42
        "Return to the slot you had selected once you stop fighting.", false);
43
    private final NumberSetting releaseTime = new NumberSetting("Release time",
44
        "Ticks without an attack before switching back.", 20, 1, 100, 1, " ticks")
45
        .under(switchBack);
46
47
    private final SlotSwap slots = new SlotSwap();
48
    private int timer;
49
50
    public AutoWeapon() {
51
        super("AutoWeapon", "Switches to your strongest sword or axe when you attack.", Category.COMBAT);
52
        addSettings(prefer, threshold, hover, antiBreak, switchBack, releaseTime);
53
        searchTags("auto sword", "auto axe", "weapon switch");
54
    }
55
56
    @Override
57
    public ExclusivityGroup getExclusivityGroup() {
58
        return ExclusivityGroup.WEAPON_SWAP;
59
    }
60
61
    @Override
62
    protected void onEnable() {
63
        slots.forget();
64
        timer = 0;
65
    }
66
67
    @Override
68
    protected void onDisable() {
69
        if (switchBack.isOn() && inGame()) {
70
            slots.restore();
71
        } else {
72
            slots.forget();
73
        }
74
    }
75
76
    // AutoTool stands aside whilst this is true.
77
    public boolean isHoldingWeapon() {
78
        return isEnabled() && slots.isHolding();
79
    }
80
81
    @Subscribe
82
    private void onAttack(AttackEntityEvent event) {
83
        if (!inGame() || mc.player.isSpectator() || mc.player.isDeadOrDying()) {
84
            return;
85
        }
86
        if (!(event.getTarget() instanceof LivingEntity target)) {
87
            return;
88
        }
89
90
        arm(target);
91
    }
92
93
    private void arm(LivingEntity target) {
94
        int best = bestSlot(target);
95
        if (best != -1 && best != InventoryUtil.selectedSlot()) {
96
            slots.select(best);
97
        }
98
        timer = releaseTime.getInt();
99
    }
100
101
    @Subscribe
102
    private void onTick(TickEvent event) {
103
        if (!inGame()) {
104
            return;
105
        }
106
        if (hover.isOn() && !mc.player.isDeadOrDying()
107
            && mc.hitResult instanceof EntityHitResult hit
108
            && hit.getEntity() instanceof LivingEntity target && target.isAlive()
109
            && !EntityUtil.isFriend(target)) {
110
            arm(target);
111
            return;
112
        }
113
        if (!slots.isHolding()) {
114
            return;
115
        }
116
        if (mc.player.isDeadOrDying()) {
117
            // Respawn hands out a fresh inventory.
118
            slots.forget();
119
            timer = 0;
120
            return;
121
        }
122
        if (!switchBack.isOn()) {
123
            slots.forget();
124
            return;
125
        }
126
        if (timer > 0) {
127
            timer--;
128
            return;
129
        }
130
        slots.restore();
131
    }
132
133
    private int bestSlot(LivingEntity target) {
134
        if (prefer.is(Prefer.SPEED)) {
135
            return fastestWeaponSlot(antiBreak.isOn());
136
        }
137
        return bestWeaponSlot(target, prefer.is(Prefer.SWORD), threshold.getValue(), antiBreak.isOn());
138
    }
139
140
    // The hotbar slot holding the sword or axe that swings most often or minus one.
141
    private static int fastestWeaponSlot(boolean skipBreaking) {
142
        int best = -1;
143
        double bestSpeed = 0;
144
        for (int i = 0; i < InventoryUtil.HOTBAR_SIZE; i++) {
145
            ItemStack stack = mc.player.getInventory().getItem(i);
146
            if (!stack.is(ItemTags.SWORDS) && !stack.is(ItemTags.AXES)
147
                || (skipBreaking && ItemUtil.nearlyBroken(stack))) {
148
                continue;
149
            }
150
            double speed = ItemUtil.attributeValue(stack, Attributes.ATTACK_SPEED, EquipmentSlot.MAINHAND);
151
            if (speed > bestSpeed) {
152
                bestSpeed = speed;
153
                best = i;
154
            }
155
        }
156
        return best;
157
    }
158
159
    public static int bestWeaponSlot(LivingEntity target) {
160
        return bestWeaponSlot(target, true, 2, true);
161
    }
162
163
    // The hotbar slot holding the strongest axe or minus one.
164
    // Only an axe staggers a raised shield however hard the sword hits.
165
    public static int bestAxeSlot(LivingEntity target, boolean skipBreaking) {
166
        int best = -1;
167
        double bestDamage = 0;
168
        for (int i = 0; i < InventoryUtil.HOTBAR_SIZE; i++) {
169
            ItemStack stack = mc.player.getInventory().getItem(i);
170
            if (!stack.is(ItemTags.AXES) || (skipBreaking && ItemUtil.nearlyBroken(stack))) {
171
                continue;
172
            }
173
            double damage = weaponDamage(stack, target);
174
            if (damage > bestDamage) {
175
                bestDamage = damage;
176
                best = i;
177
            }
178
        }
179
        return best;
180
    }
181
182
    // The hotbar slot holding the strongest weapon against this target or minus one.
183
    public static int bestWeaponSlot(LivingEntity target, boolean preferSword,
184
                                     double margin, boolean skipBreaking) {
185
        int swordSlot = -1;
186
        int axeSlot = -1;
187
        double swordDamage = 0;
188
        double axeDamage = 0;
189
190
        for (int i = 0; i < InventoryUtil.HOTBAR_SIZE; i++) {
191
            ItemStack stack = mc.player.getInventory().getItem(i);
192
            if (stack.isEmpty()) {
193
                continue;
194
            }
195
            if (skipBreaking && ItemUtil.nearlyBroken(stack)) {
196
                continue;
197
            }
198
            if (stack.is(ItemTags.SWORDS)) {
199
                double damage = weaponDamage(stack, target);
200
                if (damage > swordDamage) {
201
                    swordDamage = damage;
202
                    swordSlot = i;
203
                }
204
            } else if (stack.is(ItemTags.AXES)) {
205
                double damage = weaponDamage(stack, target);
206
                if (damage > axeDamage) {
207
                    axeDamage = damage;
208
                    axeSlot = i;
209
                }
210
            }
211
        }
212
213
        if (swordSlot == -1) {
214
            return axeSlot;
215
        }
216
        if (axeSlot == -1) {
217
            return swordSlot;
218
        }
219
        if (preferSword) {
220
            return axeDamage - swordDamage > margin ? axeSlot : swordSlot;
221
        }
222
        return swordDamage - axeDamage > margin ? swordSlot : axeSlot;
223
    }
224
225
    // Damage one full strength hit would deal after their armour and your own effects.
226
    private static double weaponDamage(ItemStack stack, LivingEntity target) {
227
        return DamageUtil.attackDamage(mc.player, target, stack);
228
    }
229
}
230
LiquidBounce (Kotlin)COMBATsrc/main/kotlin/net/ccbluex/liquidbounce/features/module/modules/combat/ModuleAutoWeapon.ktKotlin · 218 lines · 8.2 KB

Automatically selects the best weapon in your hotbar.

SettingsPreferredAutoShieldBreakAutoMaceSwitchBack
ModuleAutoWeapon.ktKotlin · Minecraft 26.2 · 218 lines · 8.2 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
3
 *
4
 * Copyright (c) 2015 - 2026 CCBlueX
5
 *
6
 * LiquidBounce is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * LiquidBounce is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
package net.ccbluex.liquidbounce.features.module.modules.combat
20
21
import net.ccbluex.fastutil.enumSetOf
22
import net.ccbluex.liquidbounce.config.types.list.Tagged
23
import net.ccbluex.liquidbounce.event.events.AttackEntityEvent
24
import net.ccbluex.liquidbounce.event.handler
25
import net.ccbluex.liquidbounce.features.module.ClientModule
26
import net.ccbluex.liquidbounce.features.module.ModuleCategories
27
import net.ccbluex.liquidbounce.features.module.modules.combat.ModuleAutoWeapon.autoMace
28
import net.ccbluex.liquidbounce.features.module.modules.combat.ModuleAutoWeapon.autoShieldBreak
29
import net.ccbluex.liquidbounce.features.module.modules.combat.ModuleAutoWeapon.onTarget
30
import net.ccbluex.liquidbounce.features.module.modules.player.autobuff.ModuleAutoBuff
31
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.ItemCategorization
32
import net.ccbluex.liquidbounce.features.module.modules.player.invcleaner.items.WeaponItemFacet
33
import net.ccbluex.liquidbounce.features.module.modules.render.ModuleDebug.debugParameter
34
import net.ccbluex.liquidbounce.utils.client.SilentHotbar
35
import net.ccbluex.liquidbounce.utils.client.isOlderThanOrEqual1_8
36
import net.ccbluex.liquidbounce.utils.entity.hasCooldown
37
import net.ccbluex.liquidbounce.utils.entity.wouldBlockHit
38
import net.ccbluex.liquidbounce.utils.inventory.HotbarItemSlot
39
import net.ccbluex.liquidbounce.utils.inventory.Slots
40
import net.ccbluex.liquidbounce.utils.item.WeaponType
41
import net.ccbluex.liquidbounce.utils.item.attackSpeed
42
import net.ccbluex.liquidbounce.utils.item.isAxe
43
import net.ccbluex.liquidbounce.utils.item.isConsumable
44
import net.ccbluex.liquidbounce.utils.kotlin.matchesAny
45
import net.minecraft.world.InteractionHand
46
import net.minecraft.world.entity.Entity
47
import net.minecraft.world.entity.LivingEntity
48
import net.minecraft.world.item.MaceItem
49
50
/**
51
 * AutoWeapon module
52
 *
53
 * Automatically selects the best weapon in your hotbar
54
 */
55
object ModuleAutoWeapon : ClientModule("AutoWeapon", ModuleCategories.COMBAT) {
56
57
    /**
58
     * The weapon type to prefer, which on 1.8 and 1.9+ versions is usually a sword,
59
     * due to the attack speed.
60
     *
61
     * If this [Set] is empty, do not prefer any weapon type.
62
     * This is useful if you only want to make use of either [autoShieldBreak] or [autoMace].
63
     */
64
    private val preferredWeapon by multiEnumChoice("Preferred", WeaponType.SWORD)
65
66
    private val autoShieldBreak by boolean("AutoShieldBreak", true)
67
    private val autoMace by boolean("AutoMace", true)
68
69
    private val switchBack by int("SwitchBack", 20, 1..300, "ticks")
70
71
    private val changeOnActions by multiEnumChoice<ChangeOnAction>(
72
        "ChangeOn",
73
        enumSetOf(ChangeOnAction.ON_ATTACK)
74
    )
75
76
    @Suppress("unused")
77
    private enum class ChangeOnAction(
78
        override val tag: String
79
    ): Tagged {
80
        ON_ATTACK("OnAttack"),
81
        ON_TARGET("OnTarget")
82
    }
83
84
    /**
85
     * Prioritize Auto Buff or consuming an item over Auto Weapon
86
     */
87
    private val isBusy: Boolean
88
        get() = SilentHotbar.isSlotModifiedBy(ModuleAutoBuff) || player.isUsingItem && player.usingItemHand ==
89
            InteractionHand.MAIN_HAND && player.useItem.isConsumable
90
91
    /**
92
     * Check if the attack will break the shield
93
     */
94
    val willShieldBreak: Boolean
95
        get() {
96
            if (isOlderThanOrEqual1_8) {
97
                return false
98
            }
99
100
            // If we have an axe in our main hand, we will break the shield
101
            if (player.mainHandItem.isAxe) {
102
                return true
103
            }
104
105
            // If we are not going to switch to an axe, we will not break the shield
106
            return determineWeaponSlot(null, enforceShield = true)?.itemStack?.isAxe == true
107
        }
108
109
    /**
110
     * Check if the attack will mace smash
111
     */
112
    val willMaceSmash: Boolean
113
        get() {
114
            if (!canMaceSmash) {
115
                return false
116
            }
117
118
            if (player.mainHandItem.item is MaceItem) {
119
                return true
120
            }
121
122
            return determineWeaponSlot(null)?.itemStack?.item is MaceItem
123
        }
124
125
    // https://minecraft.wiki/w/Mace#Falling
126
    private val canMaceSmash
127
        get() = (!isOlderThanOrEqual1_8 && MaceItem.canSmashAttack(player)) || ModuleMaceKill.enabled
128
129
    @Suppress("unused")
130
    private val attackHandler = handler<AttackEntityEvent> { event ->
131
        val entity = event.entity as? LivingEntity ?: return@handler
132
        val weaponSlot = determineWeaponSlot(entity)?.inventorySlot ?: return@handler
133
134
        if (isBusy || ChangeOnAction.ON_ATTACK !in changeOnActions) {
135
            return@handler
136
        }
137
138
        SilentHotbar.selectSlotSilently(
139
            this,
140
            weaponSlot,
141
            switchBack
142
        )
143
144
        // [ClientPlayerInteractionManager.attackEntity] will sync the selected slot,
145
        // so we can do that here already. This is legitimate, but unfortunately, the server seems
146
        // to not care about the sync when it occurs in the same tick as the attack.
147
        interaction.ensureHasSentCarriedItem()
148
    }
149
150
    /**
151
     * Prepare AutoWeapon for given [entity] if [onTarget] is enabled
152
     */
153
    fun onTarget(entity: Entity?) {
154
        if (!running || entity !is LivingEntity || isBusy || ChangeOnAction.ON_TARGET !in changeOnActions) {
155
            return
156
        }
157
158
        determineWeaponSlot(entity)?.let { slot ->
159
            SilentHotbar.selectSlotSilently(
160
                this,
161
                slot.inventorySlot,
162
                switchBack
163
            )
164
        }
165
    }
166
167
    override fun onDisabled() {
168
        SilentHotbar.resetSlot(this)
169
    }
170
171
    private fun determineWeaponSlot(target: LivingEntity?, enforceShield: Boolean = false): HotbarItemSlot? {
172
        val itemCategorization = ItemCategorization(Slots.Hotbar)
173
        val requiresShield = autoShieldBreak && (enforceShield || target?.wouldBlockHit == true)
174
        val requiresMace = autoMace && canMaceSmash
175
176
        val bestSlot = Slots.Hotbar
177
            .flatMap { slot -> itemCategorization.getItemFacets(slot).filterIsInstance<WeaponItemFacet>() }
178
            .filter { itemFacet ->
179
                val itemStack = itemFacet.itemStack
180
                when {
181
                    // A mace's smash attack cannot be blocked by a shield
182
                    requiresMace -> WeaponType.MACE.test(itemStack)
183
                    // An axe will stun the target if it is blocking with a shield
184
                    requiresShield -> WeaponType.AXE.test(itemStack)
185
                    // Fall back to a preferred weapon when no special case applies
186
                    else -> preferredWeapon.matchesAny(itemStack)
187
                }
188
            }
189
            .maxOrNull()
190
191
        return bestSlot?.itemSlot as HotbarItemSlot?
192
    }
193
194
    /**
195
     * Get the attack speed of the determined weapon, or
196
     * return [original] if no weapon is selected
197
     * or if [ChangeOnAction] does not contain [ChangeOnAction.ON_ATTACK].
198
     *
199
     * When we switch our item on the same tick as we attack,
200
     * the cooldown progress is not updated.
201
     */
202
    fun getAttackSpeed(original: Double): Double {
203
        debugParameter("Original Attack Speed") { original }
204
205
        if (!running || ChangeOnAction.ON_ATTACK !in changeOnActions || !player.hasCooldown) {
206
            return original
207
        }
208
209
        val itemStack = determineWeaponSlot(null)?.itemStack ?: return original
210
        val itemAttackSpeed = itemStack.attackSpeed
211
        debugParameter("Item") { itemStack.itemName.string }
212
        debugParameter("Attack Speed") { itemAttackSpeed }
213
214
        return itemAttackSpeed
215
    }
216
217
}
218