AutoReplenish module

AutoReplenish: 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/AutoReplenish.javaJava · 279 lines · 11.0 KB

Refills your hotbar stacks from your inventory.

SettingsRefillsSlotPinned itemsThresholdDelayOffhandUnstackablesSame enchantmentsSearch hotbarExcludedSwap worn tools
AutoReplenish.javaJava · Minecraft 26.2 · 279 lines · 11.0 KB · complete file, verbatimOriginal ↗
1
package com.jellypudding.offlineclient.modules.player;
2
3
import com.jellypudding.offlineclient.event.Subscribe;
4
import com.jellypudding.offlineclient.event.events.TickEvent;
5
import com.jellypudding.offlineclient.module.Category;
6
import com.jellypudding.offlineclient.module.Module;
7
import com.jellypudding.offlineclient.modules.combat.AutoTotem;
8
import com.jellypudding.offlineclient.setting.BoolSetting;
9
import com.jellypudding.offlineclient.setting.EnumSetting;
10
import com.jellypudding.offlineclient.setting.NumberSetting;
11
import com.jellypudding.offlineclient.setting.RegistryListSetting;
12
import com.jellypudding.offlineclient.util.InventoryUtil;
13
import com.jellypudding.offlineclient.util.Modules;
14
import net.minecraft.core.registries.BuiltInRegistries;
15
import net.minecraft.world.item.Item;
16
import net.minecraft.world.item.ItemStack;
17
18
import java.util.List;
19
20
// Tops up hotbar stacks by watching what each slot held last tick.
21
public final class AutoReplenish extends Module {
22
23
    public enum Refills { EVERY_SLOT, HELD_SLOT, ONE_SLOT }
24
25
    private static final int OFFHAND_INDEX = 9;
26
27
    private final EnumSetting<Refills> refills = new EnumSetting<>("Refills",
28
        "Which slots are kept topped up.", Refills.EVERY_SLOT)
29
        .describe(Refills.EVERY_SLOT, "Every hotbar slot that runs low.")
30
        .describe(Refills.HELD_SLOT, "Only the slot you are holding.")
31
        .describe(Refills.ONE_SLOT, "Only the slot picked below.");
32
    private final NumberSetting slot = new NumberSetting("Slot",
33
        "The hotbar slot to keep filled. Ten is your offhand.", 1, 1, 10, 1)
34
        .under(refills, Refills.ONE_SLOT);
35
    private final RegistryListSetting<Item> pinned = new RegistryListSetting<>("Pinned items",
36
        "Items the chosen slot is filled with even whilst it holds something else. Leave it empty to keep whatever the slot had.",
37
        BuiltInRegistries.ITEM, List.of())
38
        .under(refills, Refills.HELD_SLOT, Refills.ONE_SLOT);
39
    private final NumberSetting threshold = new NumberSetting("Threshold",
40
        "Refill a stack once it drops to this many items.", 8, 1, 63, 1).max(64);
41
    private final NumberSetting delay = new NumberSetting("Delay",
42
        "Ticks to wait between refills.", 1, 0, 20, 1, " ticks");
43
    private final BoolSetting offhand = new BoolSetting("Offhand",
44
        "Also refill your offhand.", true);
45
    private final BoolSetting unstackable = new BoolSetting("Unstackables",
46
        "Replace a used up single item like a totem or a pearl.", true);
47
    private final BoolSetting sameEnchants = new BoolSetting("Same enchantments",
48
        "Only replaces a used up single item with one carrying exactly the same enchantments.", true)
49
        .under(unstackable);
50
    private final BoolSetting fromHotbar = new BoolSetting("Search hotbar",
51
        "Pull from other hotbar slots when the inventory has none.", false);
52
    private final RegistryListSetting<Item> excluded = new RegistryListSetting<>("Excluded",
53
        "Items that are never refilled. Click to pick them.", BuiltInRegistries.ITEM, List.of());
54
    private final NumberSetting repairAt = new NumberSetting("Swap worn tools",
55
        "Move a tool into your inventory once it has this many uses left so it can be mended. Zero leaves it in place.",
56
        0, 0, 100, 1, " uses").max(2000);
57
58
    // What each hotbar slot and the offhand held last tick.
59
    private final ItemStack[] previous = new ItemStack[10];
60
    private int timer;
61
    private boolean hadScreen;
62
63
    public AutoReplenish() {
64
        super("AutoReplenish", "Refills your hotbar stacks from your inventory.", Category.PLAYER);
65
        addSettings(refills, slot, pinned, threshold, delay, offhand, unstackable, sameEnchants,
66
            fromHotbar, excluded, repairAt);
67
        searchTags("refill", "restock", "hotbar");
68
    }
69
70
    @Override
71
    protected void onEnable() {
72
        timer = 0;
73
        hadScreen = mc.gui.screen() != null;
74
        if (inGame()) {
75
            snapshot();
76
        } else {
77
            for (int i = 0; i < previous.length; i++) {
78
                previous[i] = ItemStack.EMPTY;
79
            }
80
        }
81
    }
82
83
    @Subscribe
84
    private void onTick(TickEvent event) {
85
        if (!inGame() || mc.player.isSpectator()) {
86
            return;
87
        }
88
        boolean screen = mc.gui.screen() != null;
89
        // Items get moved around whilst a screen is open.
90
        if (hadScreen && !screen) {
91
            snapshot();
92
        }
93
        hadScreen = screen;
94
        if (screen || mc.player.containerMenu.containerId != 0
95
            || !mc.player.containerMenu.getCarried().isEmpty()) {
96
            return;
97
        }
98
        // AutoEat and AutoGap and AutoPotion shuffle stacks between the inventory
99
        // and the hotbar. A slot they emptied looks exactly like one that ran out.
100
        if (feederBusy()) {
101
            snapshot();
102
            return;
103
        }
104
        if (timer > 0) {
105
            timer--;
106
            snapshot();
107
            return;
108
        }
109
110
        boolean totemBusy = Modules.enabled(AutoTotem.class);
111
        boolean moved = false;
112
        int only = trackedSlot();
113
        if (only >= 0) {
114
            if (only != OFFHAND_INDEX || !totemBusy) {
115
                moved = check(only, stackAt(only));
116
            }
117
        } else {
118
            for (int i = 0; i < InventoryUtil.HOTBAR_SIZE && !moved; i++) {
119
                moved = check(i, mc.player.getInventory().getItem(i));
120
            }
121
            if (!moved && offhand.isOn() && !totemBusy) {
122
                moved = check(OFFHAND_INDEX, mc.player.getOffhandItem());
123
            }
124
        }
125
        snapshot();
126
        if (moved) {
127
            timer = delay.getInt();
128
        }
129
    }
130
131
    private static boolean feederBusy() {
132
        return Modules.feeding(null);
133
    }
134
135
    // The one slot the settings pin us to or minus one for the whole hotbar.
136
    private int trackedSlot() {
137
        return switch (refills.getValue()) {
138
            case EVERY_SLOT -> -1;
139
            case HELD_SLOT -> InventoryUtil.selectedSlot();
140
            case ONE_SLOT -> slot.getInt() - 1;
141
        };
142
    }
143
144
    private ItemStack stackAt(int index) {
145
        return index == OFFHAND_INDEX
146
            ? mc.player.getOffhandItem() : mc.player.getInventory().getItem(index);
147
    }
148
149
    private boolean check(int index, ItemStack now) {
150
        if (swapWorn(index, now)) {
151
            return true;
152
        }
153
        ItemStack before = previous[index];
154
        if (excluded.contains(now.getItem()) || excluded.contains(before.getItem())) {
155
            return false;
156
        }
157
        int wanted = threshold.getInt();
158
159
        if (pinned.size() > 0 && index == trackedSlot()) {
160
            return fillPinned(index, now, wanted);
161
        }
162
163
        ItemStack lookFor = null;
164
        boolean topUp = false;
165
        if (!now.isEmpty() && now.isStackable() && now.getCount() <= wanted
166
            && now.getCount() < now.getMaxStackSize()) {
167
            lookFor = now;
168
            topUp = true;
169
        } else if (now.isEmpty() && !before.isEmpty()) {
170
            if (before.isStackable() || unstackable.isOn()) {
171
                lookFor = before;
172
            }
173
        }
174
        if (lookFor == null) {
175
            return false;
176
        }
177
178
        // The offhand is not part of the inventory index space. Passing its own
179
        // index would skip a real backpack slot.
180
        boolean matchEnchants = !topUp && !lookFor.isStackable() && sameEnchants.isOn();
181
        int source = findSource(lookFor, index == OFFHAND_INDEX ? -1 : index, topUp, matchEnchants);
182
        if (source == -1) {
183
            return false;
184
        }
185
        // Only the held slot and the offhand may take from the hotbar.
186
        if (source < InventoryUtil.HOTBAR_SIZE && index < InventoryUtil.HOTBAR_SIZE
187
            && index != InventoryUtil.selectedSlot()) {
188
            return false;
189
        }
190
191
        return move(source, index);
192
    }
193
194
    // A pinned slot takes the first listed item the rest of the inventory still holds.
195
    private boolean fillPinned(int index, ItemStack now, int wanted) {
196
        if (!now.isEmpty() && pinned.contains(now.getItem())
197
            && (now.getCount() > wanted || now.getCount() >= now.getMaxStackSize())) {
198
            return false;
199
        }
200
        for (Item item : pinned.chosen()) {
201
            int source = InventoryUtil.findSlot(stack -> stack.is(item) && !tooWorn(stack),
202
                InventoryUtil.WHOLE_INVENTORY);
203
            if (source == -1 || source == index) {
204
                continue;
205
            }
206
            if (source < InventoryUtil.HOTBAR_SIZE && index < InventoryUtil.HOTBAR_SIZE
207
                && index != InventoryUtil.selectedSlot()) {
208
                continue;
209
            }
210
            return move(source, index);
211
        }
212
        return false;
213
    }
214
215
    // A tool close to snapping is put away before it breaks.
216
    private boolean swapWorn(int index, ItemStack now) {
217
        if (!tooWorn(now)) {
218
            return false;
219
        }
220
        for (int i = InventoryUtil.HOTBAR_SIZE; i < InventoryUtil.WHOLE_INVENTORY; i++) {
221
            ItemStack stack = mc.player.getInventory().getItem(i);
222
            if (stack.isEmpty() || !stack.isDamageableItem()) {
223
                return move(i, index);
224
            }
225
        }
226
        return false;
227
    }
228
229
    private boolean tooWorn(ItemStack stack) {
230
        int limit = repairAt.getInt();
231
        return limit > 0 && !stack.isEmpty() && stack.isDamageableItem()
232
            && stack.getMaxDamage() - stack.getDamageValue() <= limit;
233
    }
234
235
    private boolean move(int source, int index) {
236
        int target = index == OFFHAND_INDEX
237
            ? InventoryUtil.OFFHAND_SLOT : InventoryUtil.networkSlot(index);
238
        return InventoryUtil.swap(InventoryUtil.networkSlot(source), target)
239
            != InventoryUtil.Swap.REFUSED;
240
    }
241
242
    // Only exact matches merge into a partial stack. An emptied slot accepts
243
    // the same item with any data unless the enchantments have to match.
244
    private int findSource(ItemStack lookFor, int excludedIndex, boolean mustMerge,
245
                           boolean matchEnchants) {
246
        int best = -1;
247
        int bestCount = 0;
248
        int lowest = fromHotbar.isOn() ? 0 : 9;
249
        int selected = InventoryUtil.selectedSlot();
250
        for (int i = 35; i >= lowest; i--) {
251
            if (i == excludedIndex || i == selected) {
252
                continue;
253
            }
254
            ItemStack stack = mc.player.getInventory().getItem(i);
255
            if (stack.isEmpty() || !stack.is(lookFor.getItem())) {
256
                continue;
257
            }
258
            if (mustMerge && !ItemStack.isSameItemSameComponents(stack, lookFor)) {
259
                continue;
260
            }
261
            if (matchEnchants && !stack.getEnchantments().equals(lookFor.getEnchantments())) {
262
                continue;
263
            }
264
            if (stack.getCount() > bestCount) {
265
                bestCount = stack.getCount();
266
                best = i;
267
            }
268
        }
269
        return best;
270
    }
271
272
    private void snapshot() {
273
        for (int i = 0; i < InventoryUtil.HOTBAR_SIZE; i++) {
274
            previous[i] = mc.player.getInventory().getItem(i).copy();
275
        }
276
        previous[OFFHAND_INDEX] = mc.player.getOffhandItem().copy();
277
    }
278
}
279