| 1 | package com.jellypudding.offlineclient.modules.player;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.ClientTickEvent;
|
| 5 | import com.jellypudding.offlineclient.event.events.PacketSendEvent;
|
| 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.EnumSetting;
|
| 10 | import com.jellypudding.offlineclient.setting.KeybindSetting;
|
| 11 | import com.jellypudding.offlineclient.setting.NumberSetting;
|
| 12 | import com.jellypudding.offlineclient.setting.RegistryListSetting;
|
| 13 | import com.jellypudding.offlineclient.util.InventoryUtil;
|
| 14 | import com.jellypudding.offlineclient.util.ItemUtil;
|
| 15 | import com.jellypudding.offlineclient.util.Modules;
|
| 16 | import net.minecraft.client.gui.screens.Screen;
|
| 17 | import net.minecraft.client.gui.screens.inventory.InventoryScreen;
|
| 18 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 19 | import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
|
| 20 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 21 | import net.minecraft.world.inventory.ContainerInput;
|
| 22 | import net.minecraft.world.inventory.Slot;
|
| 23 | import net.minecraft.world.item.Item;
|
| 24 | import net.minecraft.world.item.ItemStack;
|
| 25 |
|
| 26 | import java.util.ArrayList;
|
| 27 | import java.util.List;
|
| 28 |
|
| 29 | public final class InventoryTweaks extends Module {
|
| 30 |
|
| 31 | public enum SortWhen {
|
| 32 | ALWAYS,
|
| 33 | ON_KEY
|
| 34 | }
|
| 35 |
|
| 36 | public enum DumpFilter {
|
| 37 | WHITELIST,
|
| 38 | BLACKLIST,
|
| 39 | EVERYTHING
|
| 40 | }
|
| 41 |
|
| 42 | // Inventory indices nine and up are the main grid.
|
| 43 | private static final int GRID_START = 9;
|
| 44 | private static final int GRID_END = 36;
|
| 45 |
|
| 46 | private final BoolSetting sort = new BoolSetting("Sort",
|
| 47 | "Keep the inventory in a stable order.", true);
|
| 48 | private final BoolSetting sortHotbar = new BoolSetting("Sort hotbar",
|
| 49 | "Include the hotbar in the sorting.", false)
|
| 50 | .under(sort);
|
| 51 | private final BoolSetting sortContainer = new BoolSetting("Sort containers",
|
| 52 | "Also sort the chest or barrel you have open.", false)
|
| 53 | .under(sort);
|
| 54 | private final EnumSetting<SortWhen> sortWhen = new EnumSetting<>("Sort when",
|
| 55 | "When the sorting runs.", SortWhen.ALWAYS)
|
| 56 | .describe(SortWhen.ALWAYS, "Sorts whenever a screen is open.")
|
| 57 | .describe(SortWhen.ON_KEY, "Sorts only whilst the sort key is held.")
|
| 58 | .under(sort);
|
| 59 | private final KeybindSetting sortKey = new KeybindSetting("Sort key",
|
| 60 | "The key that runs the sorting.", KeybindSetting.UNBOUND)
|
| 61 | .under(sortWhen, SortWhen.ON_KEY);
|
| 62 | private final BoolSetting skipCreative = new BoolSetting("Skip creative",
|
| 63 | "The sorting does nothing in creative mode.", true)
|
| 64 | .under(sort);
|
| 65 | private final BoolSetting merge = new BoolSetting("Merge",
|
| 66 | "Push split stacks of the same item back together.", true);
|
| 67 | private final BoolSetting dump = new BoolSetting("Dump junk",
|
| 68 | "Move junk into a container the moment you open one.", false);
|
| 69 | private final EnumSetting<DumpFilter> dumpFilter = new EnumSetting<>("Dump filter",
|
| 70 | "What the junk list means.", DumpFilter.WHITELIST)
|
| 71 | .describe(DumpFilter.WHITELIST, "Dumps only the listed items.")
|
| 72 | .describe(DumpFilter.BLACKLIST, "Dumps everything except the listed items.")
|
| 73 | .describe(DumpFilter.EVERYTHING, "Dumps the whole inventory.")
|
| 74 | .under(dump);
|
| 75 | private final RegistryListSetting<Item> junk = new RegistryListSetting<>("Junk",
|
| 76 | "Items the dump filter applies to. Click to pick them.", BuiltInRegistries.ITEM,
|
| 77 | ItemUtil.JUNK)
|
| 78 | .under(dump, () -> dump.isOn() && !dumpFilter.is(DumpFilter.EVERYTHING));
|
| 79 | private final NumberSetting delay = new NumberSetting("Delay",
|
| 80 | "Ticks between clicks.", 2, 0, 20, 1, " ticks").min(0);
|
| 81 | private final BoolSetting dragMove = new BoolSetting("Drag move",
|
| 82 | "Holding shift and dragging the left button moves every stack the cursor passes.", true);
|
| 83 | private final BoolSetting craftingCarry = new BoolSetting("Crafting carry",
|
| 84 | "Keeps items in your own crafting grid as four extra slots.", true);
|
| 85 | private final BoolSetting bundleScrolling = new BoolSetting("Bundle scrolling",
|
| 86 | "Scrolling over a bundle can reach every item inside it.", true);
|
| 87 | private final BoolSetting frameInput = new BoolSetting("Frame input",
|
| 88 | "Reads your keys and mining every drawn frame rather than every tick.", false);
|
| 89 |
|
| 90 | private int timer;
|
| 91 | // The last slot a dump click was sent for and what it held.
|
| 92 | private int dumpSlot = -1;
|
| 93 | private int dumpCount = -1;
|
| 94 | // True whilst a close packet for your own inventory has been held back.
|
| 95 | private boolean heldClose;
|
| 96 |
|
| 97 | public InventoryTweaks() {
|
| 98 | super("InventoryTweaks", "Sorts and merges your inventory whilst it is open.",
|
| 99 | Category.PLAYER);
|
| 100 | addSettings(sort, sortHotbar, sortContainer, sortWhen, sortKey, skipCreative, merge,
|
| 101 | dump, dumpFilter, junk, delay, dragMove, craftingCarry, bundleScrolling, frameInput);
|
| 102 | searchTags("sort", "tidy", "stack", "dump", "xcarry", "bundle");
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | protected void onEnable() {
|
| 107 | timer = 0;
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | protected void onDisable() {
|
| 112 | releaseClose();
|
| 113 | }
|
| 114 |
|
| 115 | public boolean dragsStacks() {
|
| 116 | return isEnabled() && dragMove.isOn();
|
| 117 | }
|
| 118 |
|
| 119 | public boolean uncapsBundles() {
|
| 120 | return isEnabled() && bundleScrolling.isOn();
|
| 121 | }
|
| 122 |
|
| 123 | public boolean frameInput() {
|
| 124 | return isEnabled() && frameInput.isOn();
|
| 125 | }
|
| 126 |
|
| 127 | // Your own inventory never really closes. The crafting grid keeps its items.
|
| 128 | @Subscribe
|
| 129 | private void onPacketSend(PacketSendEvent event) {
|
| 130 | if (!craftingCarry.isOn()) {
|
| 131 | return;
|
| 132 | }
|
| 133 | if (event.getPacket() instanceof ServerboundContainerClosePacket packet
|
| 134 | && packet.getContainerId() == 0) {
|
| 135 | heldClose = true;
|
| 136 | event.cancel();
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | // The server is told the grid is closed once the setting stops holding it.
|
| 141 | private void releaseClose() {
|
| 142 | if (!heldClose) {
|
| 143 | return;
|
| 144 | }
|
| 145 | heldClose = false;
|
| 146 | if (mc.getConnection() != null) {
|
| 147 | mc.getConnection().send(new ServerboundContainerClosePacket(0));
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | @Subscribe
|
| 152 | private void onClientTick(ClientTickEvent event) {
|
| 153 | if (!inGame() || mc.player.isSpectator()) {
|
| 154 | return;
|
| 155 | }
|
| 156 | if (!craftingCarry.isOn()) {
|
| 157 | releaseClose();
|
| 158 | }
|
| 159 | Screen screen = mc.gui.screen();
|
| 160 | boolean own = screen instanceof InventoryScreen;
|
| 161 | boolean container = !own && InventoryUtil.isStorage(screen);
|
| 162 | if (!own && !container) {
|
| 163 | dumpSlot = -1;
|
| 164 | dumpCount = -1;
|
| 165 | return;
|
| 166 | }
|
| 167 | AbstractContainerMenu menu = mc.player.containerMenu;
|
| 168 | if (!menu.getCarried().isEmpty()) {
|
| 169 | return;
|
| 170 | }
|
| 171 | if (timer > 0) {
|
| 172 | timer--;
|
| 173 | return;
|
| 174 | }
|
| 175 |
|
| 176 | List<Integer> mine = playerSlots(menu, sortHotbar.isOn());
|
| 177 | if (mine.isEmpty()) {
|
| 178 | return;
|
| 179 | }
|
| 180 |
|
| 181 | if (container && dump.isOn() && !stealerBusy() && dumpJunk(menu)) {
|
| 182 | timer = delay.getInt();
|
| 183 | return;
|
| 184 | }
|
| 185 | if (merge.isOn() && mergeOne(menu, mine)) {
|
| 186 | timer = delay.getInt();
|
| 187 | return;
|
| 188 | }
|
| 189 | if (!sorts()) {
|
| 190 | return;
|
| 191 | }
|
| 192 | if (sortStep(menu, mine)) {
|
| 193 | timer = delay.getInt();
|
| 194 | return;
|
| 195 | }
|
| 196 | if (container && sortContainer.isOn() && sortStep(menu, containerSlots(menu))) {
|
| 197 | timer = delay.getInt();
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | private boolean sorts() {
|
| 202 | if (!sort.isOn() || (skipCreative.isOn() && mc.player.isCreative())) {
|
| 203 | return false;
|
| 204 | }
|
| 205 | return sortWhen.is(SortWhen.ALWAYS) || sortKey.isHeld();
|
| 206 | }
|
| 207 |
|
| 208 | private boolean stealerBusy() {
|
| 209 | return Modules.enabled(ChestStealer.class);
|
| 210 | }
|
| 211 |
|
| 212 | private List<Integer> playerSlots(AbstractContainerMenu menu, boolean withHotbar) {
|
| 213 | List<Integer> found = new ArrayList<>();
|
| 214 | for (int i = 0; i < menu.slots.size(); i++) {
|
| 215 | Slot slot = menu.slots.get(i);
|
| 216 | if (slot.container != mc.player.getInventory()) {
|
| 217 | continue;
|
| 218 | }
|
| 219 | int index = slot.getContainerSlot();
|
| 220 | if (index >= GRID_START && index < GRID_END) {
|
| 221 | found.add(i);
|
| 222 | } else if (withHotbar && index < GRID_START) {
|
| 223 | found.add(i);
|
| 224 | }
|
| 225 | }
|
| 226 | return found;
|
| 227 | }
|
| 228 |
|
| 229 | private List<Integer> containerSlots(AbstractContainerMenu menu) {
|
| 230 | List<Integer> found = new ArrayList<>();
|
| 231 | for (int i = 0; i < menu.slots.size(); i++) {
|
| 232 | Slot slot = menu.slots.get(i);
|
| 233 | if (slot.container != mc.player.getInventory() && !slot.isFake()) {
|
| 234 | found.add(i);
|
| 235 | }
|
| 236 | }
|
| 237 | return found;
|
| 238 | }
|
| 239 |
|
| 240 | // True when the dump filter wants this stack moved out.
|
| 241 | public boolean dumps(ItemStack stack) {
|
| 242 | if (stack.isEmpty()) {
|
| 243 | return false;
|
| 244 | }
|
| 245 | return switch (dumpFilter.getValue()) {
|
| 246 | case WHITELIST -> junk.contains(stack.getItem());
|
| 247 | case BLACKLIST -> !junk.contains(stack.getItem());
|
| 248 | case EVERYTHING -> true;
|
| 249 | };
|
| 250 | }
|
| 251 |
|
| 252 | private boolean dumpJunk(AbstractContainerMenu menu) {
|
| 253 | for (int i = 0; i < menu.slots.size(); i++) {
|
| 254 | Slot slot = menu.slots.get(i);
|
| 255 | if (slot.container != mc.player.getInventory()) {
|
| 256 | continue;
|
| 257 | }
|
| 258 | int index = slot.getContainerSlot();
|
| 259 | if (index < GRID_START || index >= GRID_END) {
|
| 260 | continue;
|
| 261 | }
|
| 262 | ItemStack stack = slot.getItem();
|
| 263 | if (!dumps(stack)) {
|
| 264 | continue;
|
| 265 | }
|
| 266 | // The same stack coming back means the container had no room for it.
|
| 267 | if (i == dumpSlot && stack.getCount() == dumpCount) {
|
| 268 | return false;
|
| 269 | }
|
| 270 | dumpSlot = i;
|
| 271 | dumpCount = stack.getCount();
|
| 272 | click(menu, i, 0, ContainerInput.QUICK_MOVE);
|
| 273 | return true;
|
| 274 | }
|
| 275 | return false;
|
| 276 | }
|
| 277 |
|
| 278 | private boolean mergeOne(AbstractContainerMenu menu, List<Integer> region) {
|
| 279 | for (int a = 0; a < region.size(); a++) {
|
| 280 | ItemStack first = menu.getSlot(region.get(a)).getItem();
|
| 281 | if (first.isEmpty() || !first.isStackable()
|
| 282 | || first.getCount() >= first.getMaxStackSize()) {
|
| 283 | continue;
|
| 284 | }
|
| 285 | for (int b = a + 1; b < region.size(); b++) {
|
| 286 | ItemStack second = menu.getSlot(region.get(b)).getItem();
|
| 287 | if (second.isEmpty() || second.getCount() >= second.getMaxStackSize()) {
|
| 288 | continue;
|
| 289 | }
|
| 290 | if (!ItemStack.isSameItemSameComponents(first, second)) {
|
| 291 | continue;
|
| 292 | }
|
| 293 | click(menu, region.get(b), 0, ContainerInput.PICKUP);
|
| 294 | click(menu, region.get(a), 0, ContainerInput.PICKUP);
|
| 295 | if (!menu.getCarried().isEmpty()) {
|
| 296 | click(menu, region.get(b), 0, ContainerInput.PICKUP);
|
| 297 | }
|
| 298 | return true;
|
| 299 | }
|
| 300 | }
|
| 301 | return false;
|
| 302 | }
|
| 303 |
|
| 304 | // One swap per call. Repeated calls settle the whole region.
|
| 305 | private boolean sortStep(AbstractContainerMenu menu, List<Integer> region) {
|
| 306 | for (int i = 0; i < region.size(); i++) {
|
| 307 | int best = i;
|
| 308 | for (int j = i + 1; j < region.size(); j++) {
|
| 309 | if (compare(menu.getSlot(region.get(j)).getItem(),
|
| 310 | menu.getSlot(region.get(best)).getItem()) < 0) {
|
| 311 | best = j;
|
| 312 | }
|
| 313 | }
|
| 314 | if (best == i) {
|
| 315 | continue;
|
| 316 | }
|
| 317 | swap(menu, region.get(i), region.get(best));
|
| 318 | return true;
|
| 319 | }
|
| 320 | return false;
|
| 321 | }
|
| 322 |
|
| 323 | // Trades two slots with three pickup clicks.
|
| 324 | private void swap(AbstractContainerMenu menu, int from, int to) {
|
| 325 | click(menu, from, 0, ContainerInput.PICKUP);
|
| 326 | click(menu, to, 0, ContainerInput.PICKUP);
|
| 327 | click(menu, from, 0, ContainerInput.PICKUP);
|
| 328 | // Never leave a stack stuck to the cursor.
|
| 329 | if (!menu.getCarried().isEmpty()) {
|
| 330 | click(menu, to, 0, ContainerInput.PICKUP);
|
| 331 | }
|
| 332 | }
|
| 333 |
|
| 334 | private void click(AbstractContainerMenu menu, int slot, int button, ContainerInput kind) {
|
| 335 | mc.gameMode.handleContainerInput(menu.containerId, slot, button, kind, mc.player);
|
| 336 | }
|
| 337 |
|
| 338 | // Empty slots sink to the end and everything else goes by id then size.
|
| 339 | private static int compare(ItemStack a, ItemStack b) {
|
| 340 | if (a.isEmpty() || b.isEmpty()) {
|
| 341 | return Boolean.compare(a.isEmpty(), b.isEmpty());
|
| 342 | }
|
| 343 | int byId = BuiltInRegistries.ITEM.getKey(a.getItem())
|
| 344 | .compareTo(BuiltInRegistries.ITEM.getKey(b.getItem()));
|
| 345 | if (byId != 0) {
|
| 346 | return byId;
|
| 347 | }
|
| 348 | return Integer.compare(b.getCount(), a.getCount());
|
| 349 | }
|
| 350 | }
|
| 351 |
|