ChestSwap module

ChestSwap: Minecraft 26.3 cheat module, full source for every implementation (1 client: OfflineClient).

1 implementationPLAYER

Clients with this module

Full module source: 1 implementation

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

OfflineClientPLAYERsrc/main/java/com/jellypudding/offlineclient/modules/player/ChestSwap.javaJava · 187 lines · 6.7 KB

Swaps an elytra and a chestplate on one key.

SettingsChestplateCount protectionClose inventoryStay on
ChestSwap.javaJava · Minecraft 26.2 · 187 lines · 6.7 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.player;
2
3
import com.jellypudding.offlineclient.module.Category;
4
import com.jellypudding.offlineclient.module.Module;
5
import com.jellypudding.offlineclient.setting.BoolSetting;
6
import com.jellypudding.offlineclient.setting.EnumSetting;
7
import com.jellypudding.offlineclient.util.InventoryUtil;
8
import com.jellypudding.offlineclient.util.ItemUtil;
9
import net.minecraft.core.component.DataComponents;
10
import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
11
import net.minecraft.world.entity.EquipmentSlot;
12
import net.minecraft.world.entity.ai.attributes.Attributes;
13
import net.minecraft.world.item.ItemStack;
14
import net.minecraft.world.item.Items;
15
import net.minecraft.world.item.enchantment.Enchantments;
16
import org.lwjgl.glfw.GLFW;
17
18
// Swaps the chest slot between an elytra and a chestplate on one key.
19
// AutoArmor never takes an elytra off.
20
public final class ChestSwap extends Module {
21
22
    public enum Choice { STRONGEST, FIRST_FOUND, DIAMOND, NETHERITE, PREFER_DIAMOND, PREFER_NETHERITE }
23
24
    // Container slot the chest piece sits in on the survival inventory.
25
    private static final int CHEST_SLOT = 6;
26
27
    private final EnumSetting<Choice> chestplate = new EnumSetting<>("Chestplate",
28
        "Which chestplate goes on.", Choice.STRONGEST)
29
        .describe(Choice.STRONGEST, "The chestplate with the most armour.")
30
        .describe(Choice.FIRST_FOUND, "The first chestplate in your inventory.")
31
        .describe(Choice.DIAMOND, "A diamond chestplate and nothing else.")
32
        .describe(Choice.NETHERITE, "A netherite chestplate and nothing else.")
33
        .describe(Choice.PREFER_DIAMOND, "Diamond when you have one and otherwise netherite.")
34
        .describe(Choice.PREFER_NETHERITE, "Netherite when you have one and otherwise diamond.");
35
    private final BoolSetting countProtection = new BoolSetting("Count protection",
36
        "A well enchanted piece can beat a higher tier plain one.", true)
37
        .under(chestplate, Choice.STRONGEST);
38
    private final BoolSetting closeInventory = new BoolSetting("Close inventory",
39
        "Tells the server the inventory was closed after the swap as the vanilla screen does.",
40
        true);
41
    private final BoolSetting stayOn = new BoolSetting("Stay on",
42
        "The module stays on after the swap and swaps back when turned off.", false);
43
44
    public ChestSwap() {
45
        super("ChestSwap", "Swaps an elytra and a chestplate on one key.",
46
            Category.PLAYER, GLFW.GLFW_KEY_G);
47
        addSettings(chestplate, countProtection, closeInventory, stayOn);
48
        searchTags("elytra swap", "chestplate", "wings");
49
    }
50
51
    // The bind does the swap on its own unless the module is asked to stay on.
52
    @Override
53
    public boolean isTogglable() {
54
        return stayOn.isOn();
55
    }
56
57
    @Override
58
    public boolean savesEnabledState() {
59
        return false;
60
    }
61
62
    @Override
63
    public void onKeybind() {
64
        if (stayOn.isOn()) {
65
            toggle();
66
        } else {
67
            swap();
68
        }
69
    }
70
71
    @Override
72
    protected void onEnable() {
73
        swap();
74
    }
75
76
    @Override
77
    protected void onDisable() {
78
        swap();
79
    }
80
81
    public void swap() {
82
        if (!inGame() || mc.player.isSpectator() || mc.gameMode == null) {
83
            return;
84
        }
85
        // Container clicks need the survival inventory with a clear cursor.
86
        if (mc.player.containerMenu.containerId != 0
87
            || !mc.player.containerMenu.getCarried().isEmpty()) {
88
            return;
89
        }
90
91
        ItemStack worn = mc.player.getItemBySlot(EquipmentSlot.CHEST);
92
        if (isElytra(worn)) {
93
            equipChestplate();
94
        } else if (isChestplate(worn)) {
95
            equipElytra();
96
        } else if (!equipChestplate()) {
97
            equipElytra();
98
        }
99
    }
100
101
    private boolean equipElytra() {
102
        int slot = InventoryUtil.findSlot(this::isElytra, InventoryUtil.WHOLE_INVENTORY);
103
        if (slot == -1) {
104
            return false;
105
        }
106
        wear(slot);
107
        return true;
108
    }
109
110
    private boolean equipChestplate() {
111
        int found = -1;
112
        double bestScore = 0;
113
        for (int i = 0; i < InventoryUtil.WHOLE_INVENTORY; i++) {
114
            ItemStack stack = mc.player.getInventory().getItem(i);
115
            if (!isChestplate(stack)) {
116
                continue;
117
            }
118
            double score = score(stack);
119
            if (score <= 0) {
120
                continue;
121
            }
122
            if (found == -1 || score > bestScore) {
123
                found = i;
124
                bestScore = score;
125
            }
126
            if (settles(score)) {
127
                break;
128
            }
129
        }
130
        if (found == -1) {
131
            return false;
132
        }
133
        wear(found);
134
        return true;
135
    }
136
137
    // True when no later piece can beat this one.
138
    private boolean settles(double score) {
139
        return switch (chestplate.getValue()) {
140
            case STRONGEST -> false;
141
            case FIRST_FOUND -> true;
142
            default -> score >= 2;
143
        };
144
    }
145
146
    private void wear(int inventorySlot) {
147
        InventoryUtil.swap(InventoryUtil.networkSlot(inventorySlot), CHEST_SLOT);
148
        if (closeInventory.isOn()) {
149
            mc.player.connection.send(new ServerboundContainerClosePacket(0));
150
        }
151
    }
152
153
    private boolean isElytra(ItemStack stack) {
154
        return !stack.isEmpty() && stack.has(DataComponents.GLIDER);
155
    }
156
157
    private boolean isChestplate(ItemStack stack) {
158
        return !stack.isEmpty() && !isElytra(stack)
159
            && ItemUtil.equipSlot(stack) == EquipmentSlot.CHEST;
160
    }
161
162
    // Zero for a piece the choice refuses. The tier choices score a wanted tier
163
    // two and a fallback tier one. The first wanted piece wins outright.
164
    private double score(ItemStack stack) {
165
        boolean diamond = stack.is(Items.DIAMOND_CHESTPLATE);
166
        boolean netherite = stack.is(Items.NETHERITE_CHESTPLATE);
167
        return switch (chestplate.getValue()) {
168
            case STRONGEST -> strength(stack);
169
            case FIRST_FOUND -> 1;
170
            case DIAMOND -> diamond ? 2 : 0;
171
            case NETHERITE -> netherite ? 2 : 0;
172
            case PREFER_DIAMOND -> diamond ? 2 : netherite ? 1 : 0;
173
            case PREFER_NETHERITE -> netherite ? 2 : diamond ? 1 : 0;
174
        };
175
    }
176
177
    // Armour points weigh the most. Then Protection. Then toughness.
178
    private double strength(ItemStack stack) {
179
        double armor = ItemUtil.attributeValue(stack, Attributes.ARMOR, EquipmentSlot.CHEST);
180
        double toughness = ItemUtil.attributeValue(stack, Attributes.ARMOR_TOUGHNESS,
181
            EquipmentSlot.CHEST);
182
        double protection = countProtection.isOn()
183
            ? ItemUtil.enchantLevel(Enchantments.PROTECTION, stack) : 0;
184
        return armor * 5 + protection * 3 + toughness;
185
    }
186
}
187