|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.hacks; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import net.minecraft.client.gui.screen.ingame.MerchantScreen; |
| 13 | +import net.minecraft.client.network.ClientPlayerEntity; |
| 14 | +import net.minecraft.item.ItemStack; |
| 15 | +import net.minecraft.item.Items; |
| 16 | +import net.minecraft.network.packet.c2s.play.SelectMerchantTradeC2SPacket; |
| 17 | +import net.minecraft.screen.slot.SlotActionType; |
| 18 | +import net.minecraft.village.TradeOffer; |
| 19 | +import net.minecraft.village.TradeOfferList; |
| 20 | +import net.wurstclient.Category; |
| 21 | +import net.wurstclient.SearchTags; |
| 22 | +import net.wurstclient.WurstClient; |
| 23 | +import net.wurstclient.hack.Hack; |
| 24 | +import net.wurstclient.events.UpdateListener; |
| 25 | +import net.wurstclient.settings.ItemListSetting; |
| 26 | +import net.wurstclient.util.ChatUtils; |
| 27 | +import net.wurstclient.util.InventoryUtils; |
| 28 | + |
| 29 | +@SearchTags({"autotrader", "auto trader", "villager trader", "trade"}) |
| 30 | +public final class AutoTraderHack extends Hack implements UpdateListener |
| 31 | +{ |
| 32 | + private final ItemListSetting items = new ItemListSetting("Items", |
| 33 | + "Items to sell to the villager.", "minecraft:paper"); |
| 34 | + |
| 35 | + public AutoTraderHack() |
| 36 | + { |
| 37 | + super("AutoTrader"); |
| 38 | + setCategory(Category.OTHER); |
| 39 | + addSetting(items); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void onEnable() |
| 44 | + { |
| 45 | + WurstClient.INSTANCE.getEventManager().add(UpdateListener.class, this); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected void onDisable() |
| 50 | + { |
| 51 | + WurstClient.INSTANCE.getEventManager().remove(UpdateListener.class, |
| 52 | + this); |
| 53 | + } |
| 54 | + |
| 55 | + public void triggerFromGui() |
| 56 | + { |
| 57 | + if(!isEnabled()) |
| 58 | + { |
| 59 | + ChatUtils.warning("AutoTrader needs to be enabled."); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + ChatUtils.message("AutoTrader triggered from GUI."); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onUpdate() |
| 68 | + { |
| 69 | + if(MC.player == null || MC.currentScreen == null) |
| 70 | + return; |
| 71 | + |
| 72 | + if(!(MC.currentScreen instanceof MerchantScreen tradeScreen)) |
| 73 | + return; |
| 74 | + |
| 75 | + TradeOfferList recipes = tradeScreen.getScreenHandler().getRecipes(); |
| 76 | + if(recipes == null || recipes.isEmpty()) |
| 77 | + return; |
| 78 | + |
| 79 | + List<String> wanted = items.getItemNames(); |
| 80 | + if(wanted.isEmpty()) |
| 81 | + return; |
| 82 | + |
| 83 | + ClientPlayerEntity player = MC.player; |
| 84 | + |
| 85 | + // do one purchase per update tick to avoid spamming |
| 86 | + if(MC.itemUseCooldown > 0) |
| 87 | + return; |
| 88 | + |
| 89 | + for(int i = 0; i < recipes.size(); i++) |
| 90 | + { |
| 91 | + TradeOffer offer = recipes.get(i); |
| 92 | + if(offer == null) |
| 93 | + continue; |
| 94 | + |
| 95 | + ItemStack sell = offer.getSellItem(); |
| 96 | + // we want trades where the villager gives emeralds (we sell items) |
| 97 | + if(sell == null || sell.isEmpty() |
| 98 | + || sell.getItem() != Items.EMERALD) |
| 99 | + continue; |
| 100 | + |
| 101 | + ItemStack firstBuy = offer.getDisplayedFirstBuyItem(); |
| 102 | + if(firstBuy == null || firstBuy.isEmpty()) |
| 103 | + continue; |
| 104 | + |
| 105 | + String req = net.minecraft.registry.Registries.ITEM |
| 106 | + .getId(firstBuy.getItem()).toString(); |
| 107 | + boolean matches = |
| 108 | + wanted.stream().anyMatch(s -> s.equalsIgnoreCase(req)); |
| 109 | + if(!matches) |
| 110 | + continue; |
| 111 | + |
| 112 | + // check inventory for required amount |
| 113 | + if(InventoryUtils.count(firstBuy.getItem()) < firstBuy.getCount()) |
| 114 | + continue; |
| 115 | + |
| 116 | + // select trade |
| 117 | + tradeScreen.getScreenHandler().setRecipeIndex(i); |
| 118 | + tradeScreen.getScreenHandler().switchTo(i); |
| 119 | + MC.getNetworkHandler() |
| 120 | + .sendPacket(new SelectMerchantTradeC2SPacket(i)); |
| 121 | + |
| 122 | + // click the result slot to perform the trade. Use HandledScreen |
| 123 | + // mouse clicks so the client's cursor stack is updated and we can |
| 124 | + // move the result into the inventory if it ends up on the cursor. |
| 125 | + var handler = tradeScreen.getScreenHandler(); |
| 126 | + if(handler.slots.size() > 2) |
| 127 | + { |
| 128 | + var outputSlot = handler.slots.get(2); |
| 129 | + // pickup result |
| 130 | + tradeScreen.onMouseClick(outputSlot, outputSlot.id, 0, |
| 131 | + SlotActionType.PICKUP); |
| 132 | + |
| 133 | + // if result ended up on the cursor, quick-move it into the |
| 134 | + // inventory to avoid stalling. |
| 135 | + if(!handler.getCursorStack().isEmpty()) |
| 136 | + tradeScreen.onMouseClick(outputSlot, outputSlot.id, 0, |
| 137 | + SlotActionType.QUICK_MOVE); |
| 138 | + } |
| 139 | + |
| 140 | + // set a small cooldown |
| 141 | + MC.itemUseCooldown = 4; |
| 142 | + return; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments